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 int alpha(int color, int alpha) {
    return Color.argb(alpha, Color.red(color), Color.green(color), Color.blue(color));
}

From source file:Main.java

public static Bitmap doInvert(Bitmap src) {
    Bitmap bmOut = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig());
    int A, R, G, B;
    int pixelColor;
    int w = src.getWidth();
    int h = src.getHeight();
    for (int y = 0; y < h; y++) {
        for (int x = 0; x < w; x++) {
            pixelColor = src.getPixel(x, y);
            A = Color.alpha(pixelColor);
            R = 255 - Color.red(pixelColor);
            G = 255 - Color.green(pixelColor);
            B = 255 - Color.blue(pixelColor);
            bmOut.setPixel(x, y, Color.argb(A, R, G, B));
        }//from  www  .j  ava  2  s. c  o m
    }
    return bmOut;
}

From source file:Main.java

public static Bitmap doColorFilter(Bitmap src, double red, double green, double blue) {
    int w = src.getWidth();
    int h = src.getHeight();

    Bitmap bmOut = Bitmap.createBitmap(w, h, src.getConfig());
    int A, R, G, B, pixel;
    for (int x = 0; x < w; x++) {
        for (int y = 0; y < h; y++) {
            pixel = src.getPixel(x, y);//from   w  w w .  j av  a  2s. co m
            A = Color.alpha(pixel);
            R = (int) (Color.red(pixel) * red);
            G = (int) (Color.green(pixel) * green);
            B = (int) (Color.blue(pixel) * blue);
            bmOut.setPixel(x, y, Color.argb(A, R, G, B));
        }
    }
    return bmOut;
}

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   www  .ja  v a2  s. co  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);
}

From source file:Main.java

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

From source file:Main.java

public static int mixColors(int fromColor, int toColor, float ratio) {
    int fromRed = Color.red(fromColor);
    int toRed = Color.red(toColor);
    int diffRed = toRed - fromRed;

    int fromGreen = Color.green(fromColor);
    int toGreen = Color.green(toColor);
    int diffGreen = toGreen - fromGreen;

    int fromBlue = Color.blue(fromColor);
    int toBlue = Color.blue(toColor);
    int diffBlue = toBlue - fromBlue;

    int fromAlpha = Color.alpha(fromColor);
    int toAlpha = Color.alpha(toColor);
    int diffAlpha = toAlpha - fromAlpha;

    return Color.argb((int) (fromAlpha + diffAlpha * ratio), (int) (fromRed + diffRed * ratio),
            (int) (fromGreen + diffGreen * ratio), (int) (fromBlue + diffBlue * ratio));
}

From source file:Main.java

public static int setColorOpaque(int color) {
    return Color.argb(255, Color.red(color), Color.green(color), Color.blue(color));
}

From source file:Main.java

public static int getRandomColor() {
    Random rand = new Random();
    return Color.argb(100, rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
}

From source file:Main.java

public static int getMiddleColor(int prevColor, int curColor, float factor) {
    if (prevColor == curColor) {
        return curColor;
    }//  w w w. j  a  v a2 s  .c o  m
    ;

    if (factor == 0f) {
        return prevColor;
    } else if (factor == 1f) {
        return curColor;
    }

    int a = getMiddleValue(Color.alpha(prevColor), Color.alpha(curColor), factor);
    int r = getMiddleValue(Color.red(prevColor), Color.red(curColor), factor);
    int g = getMiddleValue(Color.green(prevColor), Color.green(curColor), factor);
    int b = getMiddleValue(Color.blue(prevColor), Color.blue(curColor), factor);

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

From source file:Main.java

public static int getRandomColor() {
    return Color.argb(100, random.nextInt(256), random.nextInt(256), random.nextInt(256));
}