Example usage for android.graphics Color rgb

List of usage examples for android.graphics Color rgb

Introduction

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

Prototype

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

Source Link

Document

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

Usage

From source file:Main.java

public static Bitmap binarize(Bitmap bmp) {
    int threshold = Math.round(getOtsuThreshold(bmp));
    int width = bmp.getWidth();
    int height = bmp.getHeight();
    int[] pixels = new int[width * height];
    bmp.getPixels(pixels, 0, width, 0, 0, width, height);

    for (int i = 0; i < width * height; i++) {
        int p = 0;
        if (Color.red(pixels[i]) > threshold)
            p = 255;/*www  .  ja  va2 s.c o m*/
        pixels[i] = Color.rgb(p, p, p);
    }

    return Bitmap.createBitmap(pixels, width, height, bmp.getConfig());
}

From source file:Main.java

/**
 * Calculate the value for different color.
 *
 * @param baseColor/*from   w w  w .j a  va  2 s . c o  m*/
 *         Value of base color.
 * @param emptyColor
 *         Value of empty color
 * @param level
 *         Level.
 * @return The value for the level of the base color.
 */
public static int calculateLevelColor(int baseColor, int emptyColor, int level) {
    if (level == 0) {
        return emptyColor;
    }
    return Color.rgb(calculateR(Color.red(baseColor), level), calculateG(Color.green(baseColor), level),
            calculateB(Color.blue(baseColor), level));
}

From source file:Main.java

public static Bitmap toThreshold(Bitmap bmpOriginal) {
    int imageWidth = bmpOriginal.getWidth();
    int imageHeight = bmpOriginal.getHeight();
    Bitmap bmpThreshold = Bitmap.createBitmap(imageWidth, imageHeight, Bitmap.Config.RGB_565);
    int[] buffer = new int[imageWidth];

    int grayVal;/*from www.j  a v  a  2s.co m*/
    for (int row = 0; row < imageHeight; row++) {
        for (int col = 0; col < imageWidth; col++) {
            grayVal = rgbToGray(bmpOriginal.getPixel(col, row));
            if (grayVal > 125)
                buffer[col] = Color.rgb(255, 255, 255);
            else
                buffer[col] = Color.rgb(0, 0, 0);
        }
        bmpThreshold.setPixels(buffer, 0, imageWidth, 0, row, imageWidth, 1);
    }
    return bmpThreshold;
}

From source file:Main.java

/**
 * get random color rgb value//  w w w  .ja  va2  s.c om
 * @return rgb color int
 */
public static int getRandomColor() {
    Random random = new Random();
    /*
     * Better limiting value.
     * if value is too small, it will appear lighter
     * if value is too large, it will appear darker
     */
    //limit within 50-199
    int red = random.nextInt(150) + 50;
    int green = random.nextInt(150) + 50;
    int blue = random.nextInt(150) + 50;
    //create a color int as per rgb and return
    return Color.rgb(red, green, blue);
}

From source file:Main.java

private static int hslToRgb(float hsl[]) {
    float h = hsl[0] / 360.f;
    float s = hsl[1];
    float l = hsl[2];

    int r;//from ww  w .  j a va2  s  . co m
    int g;
    int b;

    if (s == 0) {
        r = g = b = (int) (l * 255f);
    } else {
        float t1 = l < 0.5 ? l * (1 + s) : l + s - l * s;
        float t2 = 2 * l - t1;
        r = (int) (hueToMagnitude(t1, t2, h + 1 / 3f) * 255f);
        g = (int) (hueToMagnitude(t1, t2, h) * 255f);
        b = (int) (hueToMagnitude(t1, t2, h - 1 / 3f) * 255f);
    }
    return Color.rgb(r, g, b);
}

From source file:Main.java

public static int rgb565to24(short v) {
    int i = uShortToInt(v);
    byte r = (byte) (i & 0xF800 >> 11);
    byte g = (byte) (i & 0x07E0 >> 5);
    byte b = (byte) (i & 0x001F);
    return Color.rgb(r, g, b);

}

From source file:Main.java

public static Bitmap drawTextToBitmap(Bitmap bitmap, String gText) {
    android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();
    // set default bitmap config if none
    if (bitmapConfig == null) {
        bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
    }//from   ww w .j a  v a 2  s.c  o  m
    bitmap = bitmap.copy(bitmapConfig, true);
    Canvas canvas = new Canvas(bitmap);
    // new antialised Paint
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    // text color - #3D3D3D
    paint.setColor(Color.rgb(61, 61, 61));
    // text size in pixels
    paint.setTextSize((int) (21)); //* scale));
    // text shadow
    paint.setShadowLayer(2f, 1f, 1f, Color.WHITE);
    int x = bitmap.getWidth() - 150;//bounds.width()) - 150;
    int y = bitmap.getHeight() - 27;//bounds.height()) - 30;
    canvas.drawRect(x, y, x + 150, y + 27, paint);
    canvas.drawText(gText, x, y + 20, paint);
    return bitmap;
}

From source file:Main.java

public static int createRandomColor() {
    int r = 50 + mRandom.nextInt(150);
    int g = 50 + mRandom.nextInt(150);
    int b = 50 + mRandom.nextInt(150);
    return Color.rgb(r, g, b);
}

From source file:Main.java

public static Bitmap createBitmapFromBytes(byte[] data, int width, int height) {

    int[] colors = new int[width * height];
    Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            int index = (y * width + x) * 3;
            int r = (((byte) data[index + 0]) & 0x00FF) & getMask(8);
            int g = (((byte) data[index + 1]) & 0x00FF) & getMask(8);
            int b = (((byte) data[index + 2]) & 0x00FF) & getMask(8);
            colors[x + y * width] = Color.rgb(r, g, b);
        }//from   w ww  .  java  2  s .  co m
    }
    data = null;

    bitmap.setPixels(colors, 0, width, 0, 0, width, height);
    colors = null;

    return bitmap;
}

From source file:Main.java

/**
 * Generates an array of random colors given the number of colors to create.
 * @param numOfColors, the number of colors to create.
 * @return an array of the random colors.
 *//*ww  w. ja  va  2 s.  c om*/
public static int[] getRandomColors(int numOfColors) {
    Random random = new Random();
    int colors[] = new int[numOfColors];
    for (int i = 0; i < numOfColors; i++) {
        int r = random.nextInt(256);
        int g = random.nextInt(256);
        int b = random.nextInt(256);

        if ((r + g + b) > 450) {
            r = 110;
            b = 110;
            g = 110;
        }
        colors[i] = Color.rgb(r, g, b);
    }
    return colors;
}