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

/**
 * Lighten a color by percent./*w  w w  . j  a  v  a2  s .  com*/
 * 
 * @param color
 * @param percent 0.0 - 1.0
 * @return A new, lighter color.
 */
public static int lightterByPercent(int color, float percent) {
    // TODO We may try an HSV approach...
    // float[] hsv = new float[3];
    // Color.colorToHSV(color, hsv);
    // hsv[2] *= (1 + percent);
    // return Color.HSVToColor(hsv);
    float r = Color.red(color) * (1 + percent);
    float g = Color.green(color) * (1 + percent);
    float b = Color.blue(color) * (1 + 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

/**
 * Calculates the minimum alpha value which can be applied to {@code foreground} so that would
 * have a contrast value of at least {@code minContrastRatio} when compared to
 * {@code background}.//  w w  w  .  j  a  va 2 s.c om
 *
 * @param foreground       the foreground color.
 * @param background       the background color. Should be opaque.
 * @param minContrastRatio the minimum contrast ratio.
 * @return the alpha value in the range 0-255, or -1 if no value could be calculated.
 */
public static int calculateMinimumAlpha(int foreground, int background, float minContrastRatio) {
    if (Color.alpha(background) != 255) {
        throw new IllegalArgumentException("background can not be translucent");
    }

    // First lets check that a fully opaque foreground has sufficient contrast
    int testForeground = setAlphaComponent(foreground, 255);
    double testRatio = calculateContrast(testForeground, background);
    if (testRatio < minContrastRatio) {
        // Fully opaque foreground does not have sufficient contrast, return error
        return -1;
    }

    // Binary search to find a value with the minimum value which provides sufficient contrast
    int numIterations = 0;
    int minAlpha = 0;
    int maxAlpha = 255;

    while (numIterations <= MIN_ALPHA_SEARCH_MAX_ITERATIONS
            && (maxAlpha - minAlpha) > MIN_ALPHA_SEARCH_PRECISION) {
        final int testAlpha = (minAlpha + maxAlpha) / 2;

        testForeground = setAlphaComponent(foreground, testAlpha);
        testRatio = calculateContrast(testForeground, background);

        if (testRatio < minContrastRatio) {
            minAlpha = testAlpha;
        } else {
            maxAlpha = testAlpha;
        }

        numIterations++;
    }

    // Conservatively return the max of the range of possible alphas, which is known to pass.
    return maxAlpha;
}

From source file:com.shopgun.android.utils.ColorUtils.java

/**
 * Returns the complimentary color. (Alpha channel remains intact)
 *
 * @param color An ARGB color to return the compliment of
 * @return An ARGB of compliment color/*from   w w  w.  ja v a  2  s .  com*/
 */
@ColorInt
public static int getCompliment(@ColorInt int color) {
    // get existing colors
    int alpha = Color.alpha(color);
    int red = Color.red(color);
    int blue = Color.blue(color);
    int green = Color.green(color);

    // find compliments
    red = (~red) & 0xff;
    blue = (~blue) & 0xff;
    green = (~green) & 0xff;

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

From source file:Main.java

/**
 * Creates a copy of the bitmap by replacing the color of every pixel 
 * by tintColor while keeping the alpha value.
 * @param bitmap The original bitmap.//from   ww  w .j  a  v  a2 s.c o m
 * @param tintColor The color to apply to every pixel.
 * @return A copy of the given bitmap with the tint color applied.
 */
public static Bitmap applyColor(Bitmap bitmap, int tintColor) {
    int r = Color.red(tintColor);
    int g = Color.green(tintColor);
    int b = Color.blue(tintColor);

    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    int[] pixels = new int[width * height];
    bitmap.getPixels(pixels, 0, width, 0, 0, width, height);

    for (int i = 0; i < pixels.length; i++) {
        int color = pixels[i];
        int alpha = Color.alpha(color);
        pixels[i] = Color.argb(alpha, r, g, b);
    }
    return Bitmap.createBitmap(pixels, width, height, Bitmap.Config.ARGB_8888);
}

From source file:Main.java

/**
 * Creates a copy of the bitmap by replacing the color of every pixel 
 * by accentColor while keeping the alpha value.
 * @param bitmap The original bitmap./*from  w ww.j  a v a2  s .  c  o m*/
 * @param accentColor The color to apply to every pixel.
 * @return A copy of the given bitmap with the accent color applied.
 */
public static Bitmap applyColor(Bitmap bitmap, int accentColor) {
    int r = Color.red(accentColor);
    int g = Color.green(accentColor);
    int b = Color.blue(accentColor);

    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    int[] pixels = new int[width * height];
    bitmap.getPixels(pixels, 0, width, 0, 0, width, height);

    for (int i = 0; i < pixels.length; i++) {
        int color = pixels[i];
        int alpha = Color.alpha(color);
        pixels[i] = Color.argb(alpha, r, g, b);
    }
    return Bitmap.createBitmap(pixels, width, height, Bitmap.Config.ARGB_8888);
}

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./*w w w  .  j a  v a  2 s  .  co  m*/
 */
public static Drawable makeCubicGradientScrimDrawable(int baseColor, int numStops, int gravity) {
    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.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);
        }
    });

    return paintDrawable;
}

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.//from  ww  w.j  a v  a2s. 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 = MathUtil.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) {
            LinearGradient linearGradient = new LinearGradient(width * x0, height * y0, width * x1, height * y1,
                    stopColors, null, Shader.TileMode.CLAMP);
            return linearGradient;
        }
    });

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

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./*from   w ww. j  av a  2s .  co 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 = Math.max(0, Math.min(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:Main.java

/**
 * Apply the given tint color to the transformation map.
 * @param transformationMap A bitmap representing the transformation map.
 * @param tintColor Tint color to be applied.
 * @return A bitmap with the with the tint color set.
 *//* w  ww  . j a va 2 s. c  om*/
public static Bitmap processTintTransformationMap(Bitmap transformationMap, int tintColor) {
    // tint color
    int[] t = new int[] { Color.red(tintColor), Color.green(tintColor), Color.blue(tintColor) };

    int width = transformationMap.getWidth();
    int height = transformationMap.getHeight();
    int[] pixels = new int[width * height];
    transformationMap.getPixels(pixels, 0, width, 0, 0, width, height);

    float[] transformation = new float[2];

    for (int i = 0; i < pixels.length; i++) {
        int color = pixels[i];
        int alpha = Color.alpha(color);
        transformation[0] = Color.red(color) / 255f;
        transformation[1] = Color.green(color) / 255f;
        pixels[i] = applyTransformation(t, alpha, transformation);
    }
    return Bitmap.createBitmap(pixels, width, height, Bitmap.Config.ARGB_8888);
}

From source file:Main.java

public static int getDominantColor(Bitmap source, boolean applyThreshold) {
    if (source == null)
        return Color.argb(255, 255, 255, 255);

    // Keep track of how many times a hue in a given bin appears in the image.
    // Hue values range [0 .. 360), so dividing by 10, we get 36 bins.
    int[] colorBins = new int[36];

    // The bin with the most colors. Initialize to -1 to prevent accidentally
    // thinking the first bin holds the dominant color.
    int maxBin = -1;

    // Keep track of sum hue/saturation/value per hue bin, which we'll use to
    // compute an average to for the dominant color.
    float[] sumHue = new float[36];
    float[] sumSat = new float[36];
    float[] sumVal = new float[36];
    float[] hsv = new float[3];

    int height = source.getHeight();
    int width = source.getWidth();
    int[] pixels = new int[width * height];
    source.getPixels(pixels, 0, width, 0, 0, width, height);
    for (int row = 0; row < height; row++) {
        for (int col = 0; col < width; col++) {
            int c = pixels[col + row * width];
            // Ignore pixels with a certain transparency.
            if (Color.alpha(c) < 128)
                continue;

            Color.colorToHSV(c, hsv);

            // If a threshold is applied, ignore arbitrarily chosen values for "white" and "black".
            if (applyThreshold && (hsv[1] <= 0.35f || hsv[2] <= 0.35f))
                continue;

            // We compute the dominant color by putting colors in bins based on their hue.
            int bin = (int) Math.floor(hsv[0] / 10.0f);

            // Update the sum hue/saturation/value for this bin.
            sumHue[bin] = sumHue[bin] + hsv[0];
            sumSat[bin] = sumSat[bin] + hsv[1];
            sumVal[bin] = sumVal[bin] + hsv[2];

            // Increment the number of colors in this bin.
            colorBins[bin]++;/*from ww  w .  j a v  a2  s . c o  m*/

            // Keep track of the bin that holds the most colors.
            if (maxBin < 0 || colorBins[bin] > colorBins[maxBin])
                maxBin = bin;
        }
    }

    // maxBin may never get updated if the image holds only transparent and/or black/white pixels.
    if (maxBin < 0)
        return Color.argb(255, 255, 255, 255);

    // Return a color with the average hue/saturation/value of the bin with the most colors.
    hsv[0] = sumHue[maxBin] / colorBins[maxBin];
    hsv[1] = sumSat[maxBin] / colorBins[maxBin];
    hsv[2] = sumVal[maxBin] / colorBins[maxBin];
    return Color.HSVToColor(hsv);
}