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

/**
 * Creates an approximated cubic gradient using a multi-stop linear gradient. See
 * <a href="https://plus.google.com/+RomanNurik/posts/2QvHVFWrHZf">this post</a> for more
 * details./*  ww  w  .  ja  va 2s  .  c o m*/
 */
public static Drawable makeCubicGradientScrimDrawable(int baseColor, int numStops, int gravity) {

    // Generate a cache key by hashing together the inputs, based on the method described in the Effective Java book
    int cacheKeyHash = baseColor;
    cacheKeyHash = 31 * cacheKeyHash + numStops;
    cacheKeyHash = 31 * cacheKeyHash + gravity;

    Drawable cachedGradient = cubicGradientScrimCache.get(cacheKeyHash);
    if (cachedGradient != null) {
        return cachedGradient;
    }

    numStops = Math.max(numStops, 2);

    PaintDrawable paintDrawable = new PaintDrawable();
    paintDrawable.setShape(new RectShape());

    final int[] stopColors = new int[numStops];

    int red = Color.red(baseColor);
    int green = Color.green(baseColor);
    int blue = Color.blue(baseColor);
    int alpha = Color.alpha(baseColor);

    for (int i = 0; i < numStops; i++) {
        float x = i * 1f / (numStops - 1);
        float opacity = constrain(0, 1, (float) Math.pow(x, 3));
        stopColors[i] = Color.argb((int) (alpha * opacity), red, green, blue);
    }

    final float x0, x1, y0, y1;
    switch (gravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
    case Gravity.LEFT:
        x0 = 1;
        x1 = 0;
        break;
    case Gravity.RIGHT:
        x0 = 0;
        x1 = 1;
        break;
    default:
        x0 = 0;
        x1 = 0;
        break;
    }
    switch (gravity & Gravity.VERTICAL_GRAVITY_MASK) {
    case Gravity.TOP:
        y0 = 1;
        y1 = 0;
        break;
    case Gravity.BOTTOM:
        y0 = 0;
        y1 = 1;
        break;
    default:
        y0 = 0;
        y1 = 0;
        break;
    }

    paintDrawable.setShaderFactory(new ShapeDrawable.ShaderFactory() {
        @Override
        public Shader resize(int width, int height) {
            return new LinearGradient(width * x0, height * y0, width * x1, height * y1, stopColors, null,
                    Shader.TileMode.CLAMP);
        }
    });

    cubicGradientScrimCache.put(cacheKeyHash, paintDrawable);
    return paintDrawable;
}

From source file:babbq.com.searchplace.util.ColorUtils.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. java  2s .  c  o m*/
public static @ColorInt int blendColors(@ColorInt int color1, @ColorInt int color2,
        @FloatRange(from = 0f, to = 1f) 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:com.yoloo.android.util.ScrimUtil.java

/**
 * Creates an approximated cubic gradient using a multi-stop linear gradient. See
 * <a href="https://plus.google.com/+RomanNurik/posts/2QvHVFWrHZf">this post</a> for more
 * details./*from  w  w  w. j  ava  2  s.  c o  m*/
 */
public static Drawable makeCubicGradientScrimDrawable(int baseColor, int numStops, int gravity) {

    // Generate a cache key by hashing together the inputs,
    // based on the method described in the Effective Java book
    int cacheKeyHash = baseColor;
    cacheKeyHash = 31 * cacheKeyHash + numStops;
    cacheKeyHash = 31 * cacheKeyHash + gravity;

    Drawable cachedGradient = cubicGradientScrimCache.get(cacheKeyHash);
    if (cachedGradient != null) {
        return cachedGradient;
    }

    numStops = Math.max(numStops, 2);

    PaintDrawable paintDrawable = new PaintDrawable();
    paintDrawable.setShape(new RectShape());

    final int[] stopColors = new int[numStops];

    final int red = Color.red(baseColor);
    final int green = Color.green(baseColor);
    final int blue = Color.blue(baseColor);
    final int alpha = Color.alpha(baseColor);

    for (int i = 0; i < numStops; i++) {
        float x = i * 1f / (numStops - 1);
        float opacity = MathUtils.constrain(0, 1, (float) Math.pow(x, 3));
        stopColors[i] = Color.argb((int) (alpha * opacity), red, green, blue);
    }

    final float x0, x1, y0, y1;
    switch (gravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
    case Gravity.START:
        x0 = 1;
        x1 = 0;
        break;
    case Gravity.END:
        x0 = 0;
        x1 = 1;
        break;
    default:
        x0 = 0;
        x1 = 0;
        break;
    }
    switch (gravity & Gravity.VERTICAL_GRAVITY_MASK) {
    case Gravity.TOP:
        y0 = 1;
        y1 = 0;
        break;
    case Gravity.BOTTOM:
        y0 = 0;
        y1 = 1;
        break;
    default:
        y0 = 0;
        y1 = 0;
        break;
    }

    paintDrawable.setShaderFactory(new ShapeDrawable.ShaderFactory() {
        @Override
        public Shader resize(int width, int height) {
            return new LinearGradient(width * x0, height * y0, width * x1, height * y1, stopColors, null,
                    Shader.TileMode.CLAMP);
        }
    });

    cubicGradientScrimCache.put(cacheKeyHash, paintDrawable);
    return paintDrawable;
}

From source file:app.witness.com.myapplication.plaint.ColorUtils.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}.
 *///from  w  ww.  j  a  v a2s .  c  o  m
public static @CheckResult @ColorInt int blendColors(@ColorInt int color1, @ColorInt int color2,
        @FloatRange(from = 0f, to = 1f) 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:com.jaredrummler.materialspinner.MaterialSpinner.java

/**
 * Darkens a color by a given factor.// w ww .j  av  a 2 s.c om
 *
 * @param color
 *     the color to darken
 * @param factor
 *     The factor to darken the color.
 * @return darker version of specified color.
 */
@ColorInt
private static int darker(@ColorInt int color, @FloatRange(from = 0.0, to = 1.0) float factor) {
    return Color.argb(Color.alpha(color), Math.max((int) (Color.red(color) * factor), 0),
            Math.max((int) (Color.green(color) * factor), 0), Math.max((int) (Color.blue(color) * factor), 0));
}

From source file:jahirfiquitiva.iconshowcase.utilities.color.ColorUtils.java

/**
 * Changes opacity of {@code color} to the specified {@code factor}
 *
 * @param factor which will change the opacity of the color
 *//*from w w  w  .j  a  va  2  s . c om*/
public static int adjustAlpha(@ColorInt int color, @FloatRange(from = 0.0, to = 1.0) float factor) {
    float a = Color.alpha(color) * factor;
    float r = Color.red(color);
    float g = Color.green(color);
    float b = Color.blue(color);
    return Color.argb((int) a, (int) r, (int) g, (int) b);
}

From source file:org.adw.library.widgets.discreteseekbar.internal.drawable.AlmostRippleDrawable.java

private static int getModulatedAlphaColor(int alphaValue, int originalColor) {
    int alpha = Color.alpha(originalColor);
    int scale = alphaValue + (alphaValue >> 7);
    alpha = alpha * scale >> 8;/*from w w w. j  av  a 2  s . c o m*/
    return Color.argb(alpha, Color.red(originalColor), Color.green(originalColor), Color.blue(originalColor));
}

From source file:Main.java

private static boolean isTransparent(int color, int alphaThreshold) {
    return Color.alpha(color) <= alphaThreshold;
}

From source file:org.chromium.chrome.browser.widget.ClipDrawableProgressBar.java

/**
 * Constructor for inflating from XML./*www  . j a v  a 2s. com*/
 */
public ClipDrawableProgressBar(Context context, AttributeSet attrs) {
    super(context, attrs);
    mDesiredVisibility = getVisibility();

    assert attrs != null;
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ClipDrawableProgressBar, 0, 0);

    int foregroundColor = a.getColor(R.styleable.ClipDrawableProgressBar_progressBarColor, Color.TRANSPARENT);
    mBackgroundColor = a.getColor(R.styleable.ClipDrawableProgressBar_backgroundColor, Color.TRANSPARENT);
    assert foregroundColor != Color.TRANSPARENT;
    assert Color.alpha(
            foregroundColor) == 255 : "Currently ClipDrawableProgressBar only supports opaque progress bar color.";

    a.recycle();

    mForegroundDrawable = new ColorDrawable(foregroundColor);
    setImageDrawable(new ClipDrawable(mForegroundDrawable, Gravity.START, ClipDrawable.HORIZONTAL));
    setBackgroundColor(mBackgroundColor);
}

From source file:com.pranavpandey.smallapp.theme.DynamicTheme.java

/**
 * Calculate accent based on a given color for dynamic theme generation.
 * Still in beta so, sometimes may be inaccurate colors.
 *
 * @param color whose accent to be calculated.
 *
 * @return Accent based on given color.//from  ww  w  .ja  v a 2  s .  c o  m
 */
public static @ColorInt int getAccentColor(@ColorInt int color) {
    int finalColor;
    int a = Color.alpha(color);
    int r = Color.red(color);
    int g = Color.green(color);
    int b = Color.blue(color);
    double Y = ((r * 299) + (g * 587) + (b * 114)) / 1000;

    int rc = b ^ 0x55;
    int gc = g & 0xFA;
    int bc = r ^ 0x55;

    finalColor = Color.argb(a, rc, gc, bc);
    int r1 = Color.red(finalColor);
    int g1 = Color.green(finalColor);
    int b1 = Color.blue(finalColor);
    double YC = ((r1 * 299) + (g1 * 587) + (b1 * 114)) / 1000;

    int CD = (Math.max(r, r1) - Math.min(r, r1)) + (Math.max(g, g1) - Math.min(g, g1))
            + (Math.max(b, b1) - Math.min(b, b1));
    if ((Y - YC <= 50) && CD <= 200) {
        rc = b ^ 0xFA;
        gc = g & 0x55;
        bc = r ^ 0x55;
    }

    finalColor = Color.argb(a, rc, gc, bc);
    return finalColor;
}