Example usage for android.graphics Color alpha

List of usage examples for android.graphics Color alpha

Introduction

In this page you can find the example usage for android.graphics Color alpha.

Prototype

@IntRange(from = 0, to = 255)
public static int alpha(int color) 

Source Link

Document

Return the alpha component of a color int.

Usage

From source file:Main.java

/**
 * Blend {@code color1} and {@code color2} using the given ratio.
 *
 * @param ratio of which to blend. 0.0 will return {@code color1}, 0.5 will give an even blend,
 *              1.0 will return {@code color2}.
 *///  w  ww .j  a  v  a  2  s . c o m
private static int blendColors(int color1, int color2, float ratio) {
    final float inverseRatio = 1f - ratio;
    float a = (Color.alpha(color1) * inverseRatio) + (Color.alpha(color2) * ratio);
    float r = (Color.red(color1) * inverseRatio) + (Color.red(color2) * ratio);
    float g = (Color.green(color1) * inverseRatio) + (Color.green(color2) * ratio);
    float b = (Color.blue(color1) * inverseRatio) + (Color.blue(color2) * ratio);
    return Color.argb((int) a, (int) r, (int) g, (int) b);
}

From source file:Main.java

/**
 * Blend between two ARGB colors using the given ratio.
 *
 * <p>A blend ratio of 0.0 will result in {@code color1}, 0.5 will give an even blend,
 * 1.0 will result in {@code color2}.</p>
 *
 * @param color1 the first ARGB color/*www.j a v a2 s .c om*/
 * @param color2 the second ARGB color
 * @param ratio  the blend ratio of {@code color1} to {@code color2}
 */
public static int blendARGB(int color1, int color2, float ratio) {
    final float inverseRatio = 1 - ratio;
    float a = Color.alpha(color1) * inverseRatio + Color.alpha(color2) * ratio;
    float r = Color.red(color1) * inverseRatio + Color.red(color2) * ratio;
    float g = Color.green(color1) * inverseRatio + Color.green(color2) * ratio;
    float b = Color.blue(color1) * inverseRatio + Color.blue(color2) * ratio;
    return Color.argb((int) a, (int) r, (int) g, (int) b);
}

From source file:Main.java

/**
 * Returns the contrast ratio between {@code foreground} and {@code background}.
 * {@code background} must be opaque./*from   w w w.j  a  va 2s  .co m*/
 * <p>
 * Formula defined
 * <a href="http://www.w3.org/TR/2008/REC-WCAG20-20081211/#contrast-ratiodef">here</a>.
 */
public static double calculateContrast(int foreground, int background) {
    if (Color.alpha(background) != 255) {
        throw new IllegalArgumentException(
                "background can not be translucent: #" + Integer.toHexString(background));
    }
    if (Color.alpha(foreground) < 255) {
        // If the foreground is translucent, composite the foreground over the background
        foreground = compositeColors(foreground, background);
    }

    final double luminance1 = calculateLuminance(foreground) + 0.05;
    final double luminance2 = calculateLuminance(background) + 0.05;

    // Now return the lighter luminance divided by the darker luminance
    return Math.max(luminance1, luminance2) / Math.min(luminance1, luminance2);
}

From source file:Main.java

public static int scaleColor(int color, float factor, boolean scaleAlpha) {
    return Color.argb(scaleAlpha ? (Math.round(Color.alpha(color) * factor)) : Color.alpha(color),
            Math.round(Color.red(color) * factor), Math.round(Color.green(color) * factor),
            Math.round(Color.blue(color) * factor));
}

From source file:Main.java

public static int getColor(int col_1, int col_2, float p) {
    int c0;//from   w  w w . j  ava 2s  .c  o m
    int c1;
    // if (p <= 0.5f)
    {
        // p *= 2;
        c0 = col_1;
        c1 = col_2;
    }
    //      else
    //      {
    //         p = (p - 0.5f) * 2;
    //         c0 = col_right;
    //         c1 = col_left;
    //      }
    int a = ave(Color.alpha(c0), Color.alpha(c1), p);
    int r = ave(Color.red(c0), Color.red(c1), p);
    int g = ave(Color.green(c0), Color.green(c1), p);
    int b = ave(Color.blue(c0), Color.blue(c1), p);
    return Color.argb(a, r, g, b);
}

From source file:Main.java

/**
 * Lightens a color by a given factor./*from   ww w.  j  av a2  s .  com*/
 *
 * @param color
 *            The color to lighten
 * @param factor
 *            The factor to lighten the color. 0 will make the color unchanged. 1 will make the
 *            color white.
 * @return lighter version of the specified color.
 */
public static int lighten(int color, float factor) {
    int red = (int) ((Color.red(color) * (1 - factor) / 255 + factor) * 255);
    int green = (int) ((Color.green(color) * (1 - factor) / 255 + factor) * 255);
    int blue = (int) ((Color.blue(color) * (1 - factor) / 255 + factor) * 255);
    return Color.argb(Color.alpha(color), red, green, blue);
}

From source file:Main.java

public static String colorHexToHtmlRgba(String colorHex) {
    int c = Color.parseColor(colorHex);
    float alpha = (float) (Color.alpha(c) / 255.0); //html uses normalized values
    int r = Color.red(c);
    int g = Color.green(c);
    int b = Color.blue(c);
    String colorStr = "rgba(" + r + "," + g + "," + b + "," + alpha + ")";

    return colorStr;
}

From source file:Main.java

/**
 * Darken a color by percent.//from   ww  w.  j  a v  a2  s .  c  o m
 * 
 * @param color
 * @param percent 0.0 - 1.0
 * @return A new, darker color.
 */
public static int darkenByPercent(int color, float percent) {
    // TODO We may try an HSV approach...
    // float[] hsv = new float[3];
    // Color.colorToHSV(color, hsv);
    // hsv[2] *= percent;
    // return Color.HSVToColor(hsv);
    float r = Color.red(color) * percent;
    float g = Color.green(color) * percent;
    float b = Color.blue(color) * percent;
    int ir = Math.min(255, (int) r);
    int ig = Math.min(255, (int) g);
    int ib = Math.min(255, (int) b);
    int ia = Color.alpha(color);
    return (Color.argb(ia, ir, ig, ib));
}

From source file:Main.java

public static int lighten(int color, float fraction) {
    int red = Color.red(color);
    int green = Color.green(color);
    int blue = Color.blue(color);
    red = lightenColor(red, fraction);//from   w w w.  j a  va2 s  . c  o  m
    green = lightenColor(green, fraction);
    blue = lightenColor(blue, fraction);
    int alpha = Color.alpha(color);
    return Color.argb(alpha, red, green, blue);
}

From source file:Main.java

public static int darken(int color, float fraction) {
    int red = Color.red(color);
    int green = Color.green(color);
    int blue = Color.blue(color);
    red = darkenColor(red, fraction);/*w  w  w  . jav  a2  s . c  om*/
    green = darkenColor(green, fraction);
    blue = darkenColor(blue, fraction);
    int alpha = Color.alpha(color);

    return Color.argb(alpha, red, green, blue);
}