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

public static Bitmap handleImagePixelsRelief(Bitmap bm) {
    Bitmap bmp = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.ARGB_8888);
    int width = bm.getWidth();
    int height = bm.getHeight();
    int color = 0, colorBefore = 0;
    int a, r, g, b;
    int r1, g1, b1;

    int[] oldPx = new int[width * height];
    int[] newPx = new int[width * height];

    bm.getPixels(oldPx, 0, bm.getWidth(), 0, 0, width, height);
    for (int i = 1; i < width * height; i++) {
        colorBefore = oldPx[i - 1];/*  ww  w  .ja va 2  s  . c o  m*/
        a = Color.alpha(colorBefore);
        r = Color.red(colorBefore);
        g = Color.green(colorBefore);
        b = Color.blue(colorBefore);

        color = oldPx[i];
        r1 = Color.red(color);
        g1 = Color.green(color);
        b1 = Color.blue(color);

        r = (r - r1 + 127);
        g = (g - g1 + 127);
        b = (b - b1 + 127);
        if (r > 255) {
            r = 255;
        }
        if (g > 255) {
            g = 255;
        }
        if (b > 255) {
            b = 255;
        }
        newPx[i] = Color.argb(a, r, g, b);
    }
    bmp.setPixels(newPx, 0, width, 0, 0, width, height);
    return bmp;
}

From source file:Main.java

/**
 * This method calculates a lighter color provided a factor of increase in lightness.
 *
 * @param color A color value/*from w  w w  .  ja  va 2  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

/**
 * Composite two potentially translucent colors over each other and returns the result.
 *///from   w  ww .j a  va 2 s  .c o 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

/**
 * 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 . j a va  2  s .  c o m
 */
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:Main.java

public static Bitmap olderImage(Bitmap bm) {

    int width = bm.getWidth();
    int height = bm.getHeight();
    int color;//from  w w w  .  java2 s.  c  o m
    int r, g, b, a;

    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

    int[] oldPx = new int[width * height];
    int[] newPx = new int[width * height];

    bm.getPixels(oldPx, 0, width, 0, 0, width, height);

    for (int i = 0; i < oldPx.length; i++) {
        color = oldPx[i];
        r = Color.red(color);
        g = Color.green(color);
        b = Color.blue(color);
        a = Color.alpha(color);

        r = (int) (0.393 * r + 0.769 * g + 0.189 * b);
        g = (int) (0.349 * r + 0.686 * g + 0.168 * b);
        b = (int) (0.272 * r + 0.534 * g + 0.131 * b);

        if (r > 255) {
            r = 255;
        } else if (r < 0) {
            r = 0;
        }

        if (g > 255) {
            g = 255;
        } else if (g < 0) {
            g = 0;
        }

        if (b > 255) {
            b = 255;
        } else if (b < 0) {
            b = 0;
        }

        newPx[i] = Color.argb(a, r, g, b);
    }

    bitmap.setPixels(newPx, 0, width, 0, 0, width, height);
    return bitmap;
}

From source file:Main.java

public static Bitmap emboss(Bitmap bmp) {
    int width = bmp.getWidth();
    int height = bmp.getHeight();
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);

    int pixR = 0;
    int pixG = 0;
    int pixB = 0;

    int pixColor = 0;

    int newR = 0;
    int newG = 0;
    int newB = 0;

    int[] pixels = new int[width * height];
    bmp.getPixels(pixels, 0, width, 0, 0, width, height);
    int pos = 0;/*ww  w.  j av  a2s .c om*/
    for (int i = 1, length = height - 1; i < length; i++) {
        for (int k = 1, len = width - 1; k < len; k++) {
            pos = i * width + k;
            pixColor = pixels[pos];

            pixR = Color.red(pixColor);
            pixG = Color.green(pixColor);
            pixB = Color.blue(pixColor);

            pixColor = pixels[pos + 1];
            newR = Color.red(pixColor) - pixR + 127;
            newG = Color.green(pixColor) - pixG + 127;
            newB = Color.blue(pixColor) - pixB + 127;

            newR = Math.min(255, Math.max(0, newR));
            newG = Math.min(255, Math.max(0, newG));
            newB = Math.min(255, Math.max(0, newB));

            pixels[pos] = Color.argb(255, newR, newG, newB);
        }
    }

    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return bitmap;
}

From source file:Main.java

public static int getColor(int col_1, int col_2, float p) {
    int c0;//from  ww w  . java 2  s  .c  om
    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

public static int withAlpha(final int color, final int alpha) {
    return Color.argb(alpha, Color.red(color), Color.green(color), Color.blue(color));
}

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}.
 *///from w  w w .  j a v  a2  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

public static Bitmap reliefImage(Bitmap bm) {

    int width = bm.getWidth();
    int height = bm.getHeight();
    int color;// ww  w.j a  v a2  s.co m
    int r, g, b, a;

    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

    int[] oldPx = new int[width * height];
    int[] newPx = new int[width * height];

    bm.getPixels(oldPx, 0, width, 0, 0, width, height);

    for (int i = 1; i < oldPx.length; i++) {
        color = oldPx[i - 1];
        r = Color.red(color);
        g = Color.green(color);
        b = Color.blue(color);
        a = Color.alpha(color);

        int nextColor = oldPx[i];
        int r1 = Color.red(nextColor);
        int g1 = Color.green(nextColor);
        int b1 = Color.blue(nextColor);

        r = r1 - r + 127;
        g = g1 - g + 127;
        b = b1 - b + 127;

        if (r > 255) {
            r = 255;
        } else if (r < 0) {
            r = 0;
        }

        if (g > 255) {
            g = 255;
        } else if (g < 0) {
            g = 0;
        }

        if (b > 255) {
            b = 255;
        } else if (b < 0) {
            b = 0;
        }

        newPx[i] = Color.argb(a, r, g, b);
    }

    bitmap.setPixels(newPx, 0, width, 0, 0, width, height);
    return bitmap;
}