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

/**
 * Composite two potentially translucent colors over each other and returns the result.
 *//*from   ww w.ja  v a  2 s.co  m*/
public static int compositeColors(int foreground, int background) {
    int bgAlpha = Color.alpha(background);
    int fgAlpha = Color.alpha(foreground);
    int a = compositeAlpha(fgAlpha, bgAlpha);

    int r = compositeComponent(Color.red(foreground), fgAlpha, Color.red(background), bgAlpha, a);
    int g = compositeComponent(Color.green(foreground), fgAlpha, Color.green(background), bgAlpha, a);
    int b = compositeComponent(Color.blue(foreground), fgAlpha, Color.blue(background), bgAlpha, a);

    return Color.argb(a, r, g, b);
}

From source file:Main.java

/**
 * Method returning color between start and end color proportional to given values.
 *
 * @param colorStart start color//from ww w.  java2s.co  m
 * @param colorEnd   end color
 * @param fullValue  total value
 * @param partValue  part of fullValue. When partValue equals 0, returning color is colorStart,
 *                   when partValue is fullValue returning color is endColor. Otherwise returning
 *                   color is from between those, relative to partValue/fullValue ratio.
 * @return color from between start and end color relative to partValue/fullValue ratio.
 */
public static int getProportionalColor(int colorStart, int colorEnd, float fullValue, float partValue) {
    float progress = Math.min(Math.max(partValue, 0f), fullValue) / fullValue;
    return Color.argb(Math.round(Color.alpha(colorStart) * (1 - progress) + Color.alpha(colorEnd) * progress),
            Math.round(Color.red(colorStart) * (1 - progress) + Color.red(colorEnd) * progress),
            Math.round(Color.green(colorStart) * (1 - progress) + Color.green(colorEnd) * progress),
            Math.round(Color.blue(colorStart) * (1 - progress) + Color.blue(colorEnd) * progress));
}

From source file:Main.java

/**
 * Adjust the alpha of a color./*from ww w  . jav a2 s.c  o m*/
 * @param color the color [0x00000000, 0xffffffff]
 * @param factor the factor for the alpha [0,1]
 * @return the adjusted color
 */
public static int adjustAlpha(int color, float factor) {
    int alpha = Math.round(Color.alpha(color) * factor);
    int red = Color.red(color);
    int green = Color.green(color);
    int blue = Color.blue(color);
    return Color.argb(alpha, red, green, blue);
}

From source file:Main.java

/** Calculate the resulting alpha from the original color and the relative alpha. */
public static int calculateAlpha(int alpha, int originalColor) {
    int originalAlpha = Color.alpha(originalColor);
    return originalAlpha * alpha / 255;
}

From source file:Main.java

/** Modify the colors translucency by alpha [0..1] with respect to the original color alpha. */
public static int applyColorAlpha(int color, float alpha) {
    final int originalAlpha = Color.alpha(color);
    // Return the color, multiplying the original alpha by the disabled value
    return (color & 0x00ffffff) | (Math.round(originalAlpha * alpha) << 24);
}

From source file:ooo.oxo.moments.util.ColorMixer.java

public static int mix(@ColorInt int background, @ColorInt int foreground, float ratio) {
    int alpha = Color.alpha(foreground);
    alpha = (int) Math.floor((float) alpha * Math.max(0f, Math.min(1f, ratio)));
    foreground = ColorUtils.setAlphaComponent(foreground, alpha);
    return ColorUtils.compositeColors(foreground, background);
}

From source file:Main.java

/**
 * Returns the contrast ratio between {@code foreground} and {@code background}.
 * {@code background} must be opaque./*from   w ww .  j a v a  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");
    }
    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

/**
 * This method calculates a lighter color provided a factor of increase in lightness.
 *
 * @param color A color value/* w w  w.j  a  v a2 s.c om*/
 * @param factor Factor of increase in lightness, range can be between 0 - 1.0
 * @return  The calculated darker color
 */
public static int calculateLighterColor(final int color, final float factor) {
    final int a = Color.alpha(color);
    final int r = Color.red(color);
    final int g = Color.green(color);
    final int b = Color.blue(color);

    final int lightnessLevel = Math.round(RGB_TOTAL_COLORS * factor);

    return Color.argb(a, Math.min(r + lightnessLevel, 255), Math.min(g + lightnessLevel, 255),
            Math.min(b + lightnessLevel, 255));
}

From source file:Main.java

/**
 * This method calculates a darker color provided a factor of reduction in lightness.
 *
 * @param color The original color value
 * @param factor Factor of lightness reduction, range can be between 0 - 1.0
 * @return  The calculated darker color/*from   w  w w. ja  v a 2s.  com*/
 */
public static int calculateDarkerColor(final int color, final float factor) {
    final int a = Color.alpha(color);
    final int r = Color.red(color);
    final int g = Color.green(color);
    final int b = Color.blue(color);

    final int lightnessLevel = Math.round(RGB_TOTAL_COLORS * factor);

    return Color.argb(a, Math.max(r - lightnessLevel, 0), Math.max(g - lightnessLevel, 0),
            Math.max(b - lightnessLevel, 0));
}

From source file:me.xingrz.gankmeizhi.util.ColorMixer.java

/**
 * ??//from   w  w  w .j a v  a2  s.c  om
 *
 * @param background 
 * @param foreground ?
 * @param ratio      ??
 * @return ??
 */
public static int mix(@ColorInt int background, @ColorInt int foreground, float ratio) {
    int alpha = Color.alpha(foreground);
    alpha = (int) Math.floor((float) alpha * ratio);
    foreground = ColorUtils.setAlphaComponent(foreground, alpha);
    return ColorUtils.compositeColors(foreground, background);
}