Example usage for android.graphics Color argb

List of usage examples for android.graphics Color argb

Introduction

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

Prototype

@ColorInt
public static int argb(float alpha, float red, float green, float blue) 

Source Link

Document

Return a color-int from alpha, red, green, blue float components in the range \([0..1]\).

Usage

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//from ww  w.j  av a  2 s . co m
 * @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

/**
 * Darken a color by percent.//from w w w .ja v  a  2 s.  co  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);/*w  w  w.  j a v a2  s.  co  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);/*from  w w w.  j  av  a2 s .  c o  m*/
    green = darkenColor(green, fraction);
    blue = darkenColor(blue, fraction);
    int alpha = Color.alpha(color);

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

From source file:Main.java

/**
 * Lighten a color by percent./*  w ww .ja  v  a  2  s.c  om*/
 * 
 * @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

/**
 * This method calculates the suitable contrasting color that is viewable.
 *
 * @param color A color value.//from www .ja va  2  s .  c o  m
 * @return  The calculated contrasting color that is viewable.
 */
public static int calculateContrastingColor(final int color) {
    final boolean isLightColor = isLightColor(color);
    final int alpha = isLightColor ? OPAQUE_ALPHA : TRANSPARENT_ALPHA;
    final int rgbColor = isLightColor ? COLOR_PARTIALLY_BLACK : COLOR_FULLY_WHITE;
    return Color.argb(alpha, rgbColor, rgbColor, rgbColor);
}

From source file:Main.java

/**
 * Returns a color from a color-hex string and an alpha value.
 * //from w  w  w. ja  va  2  s. c  o  m
 * @param hex
 * @param alpha
 * @return A color.
 */
public static int colorFromHexString(String hex, float alpha) {
    if (hex == null || hex.length() == 0) {
        throw new IllegalArgumentException("Hex color was null or empty");
    }
    int color = 0;
    if (hex.charAt(0) != '#') {
        color = Color.parseColor('#' + hex);
    } else {
        color = Color.parseColor(hex);
    }
    return Color.argb((int) (alpha * 255), Color.red(color), Color.green(color), Color.blue(color));
}

From source file:com.grottworkshop.gwsmaterialviewpagerreg.Utils.java

public static int colorWithAlpha(int color, float percent) {
    int r = Color.red(color);
    int g = Color.green(color);
    int b = Color.blue(color);
    int alpha = Math.round(percent * 255);

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

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 a 2 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./*ww w .  j  a  va2s  .  c  om*/
 * @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);
}