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 int XYZToColor(double x, double y, double z) {
    double r = (x * 3.2406 + y * -1.5372 + z * -0.4986) / 100;
    double g = (x * -0.9689 + y * 1.8758 + z * 0.0415) / 100;
    double b = (x * 0.0557 + y * -0.2040 + z * 1.0570) / 100;

    r = r > 0.0031308 ? 1.055 * Math.pow(r, 1 / 2.4) - 0.055 : 12.92 * r;
    g = g > 0.0031308 ? 1.055 * Math.pow(g, 1 / 2.4) - 0.055 : 12.92 * g;
    b = b > 0.0031308 ? 1.055 * Math.pow(b, 1 / 2.4) - 0.055 : 12.92 * b;

    return Color.rgb(constrain((int) Math.round(r * 255), 0, 255), constrain((int) Math.round(g * 255), 0, 255),
            constrain((int) Math.round(b * 255), 0, 255));
}

From source file:Main.java

/**
 * Get the color that lies in between two colors
 *
 * @param startColor The first color//from  w  w  w .  j av  a 2s. c  o  m
 * @param endColor   The last color
 * @param n          Number of steps between the two colors
 * @param i          The index at which the color is to be calculated
 * @return The newly generated color
 */
public static int getGradientColor(int startColor, int endColor, int n, int i) {
    return Color.rgb(interpolate(Color.red(startColor), Color.red(endColor), n, i),
            interpolate(Color.green(startColor), Color.green(endColor), n, i),
            interpolate(Color.blue(startColor), Color.blue(endColor), n, i));
}

From source file:Main.java

public static Bitmap drawTextToBitmap(Context mContext, int resourceId, String mText) {
    try {//  w  w w .  j a  v  a2s .  c o m
        Resources resources = mContext.getResources();
        float scale = resources.getDisplayMetrics().density;
        Bitmap bitmap = BitmapFactory.decodeResource(resources, resourceId);

        android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();
        // set default bitmap config if none
        if (bitmapConfig == null) {
            bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
        }
        // resource bitmaps are imutable,
        // so we need to convert it to mutable one
        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(77, 77, 77));
        // text size in pixels
        paint.setTextSize((int) (13 * scale));
        // text shadow
        paint.setShadowLayer(1f, 0f, 1f, Color.DKGRAY);

        // draw text to the Canvas center
        Rect bounds = new Rect();
        paint.getTextBounds(mText, 0, mText.length(), bounds);
        int x = (int) ((bitmap.getWidth() - bounds.width()) / 4);
        int y = (int) ((bitmap.getHeight() + bounds.height()) / 4);

        canvas.drawText(mText, x * scale, y * scale, paint);

        return bitmap;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;

}

From source file:Main.java

/**
 * calculates a color for a given name.//from  w w  w .  j a  va 2 s. c  o  m
 *
 * @param name the name String
 * @return the calculated color
 */
public static int calculateColor(String name) {
    int hash = name.hashCode();
    int r = (hash & 0xFF0000) >> 16;
    int g = (hash & 0x00FF00) >> 8;
    int b = hash & 0x0000FF;

    //pastelize color
    r = (r + 127) / 2;
    g = (g + 127) / 2;
    b = (b + 127) / 2;

    return Color.rgb(r, g, b);
}

From source file:com.game.prayansh.ultimatetictactoe.ui.ThemeManager.java

public static void setMarvel(Context context) {
    int seed = (int) (Math.random() * 10);
    theme = new Theme(0, R.drawable.minimal_background, (seed > 5) ? R.drawable.cap : R.drawable.iron_man,
            (seed <= 5) ? R.drawable.cap : R.drawable.iron_man,
            ContextCompat.getColor(context, R.color.mt_black), R.drawable.blank_white,
            R.drawable.blank_white_highlight, Color.rgb(255, 255, 255), R.drawable.btn_light);
}

From source file:Main.java

public static Bitmap effectChangeContrast(Bitmap bitmap, double effectLevel) {
    if (bitmap == null) {
        return bitmap;
    }/*from   w  w w .j a v a 2 s  .c o  m*/

    Bitmap dst = bitmap.copy(Bitmap.Config.ARGB_8888, true);

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

    for (int YY = 0; YY < width; ++YY) {
        for (int XX = 0; XX < height; ++XX) {
            int bitmapColor = pixels[(YY + XX * width)];
            int[] color = { Color.red(bitmapColor), Color.green(bitmapColor), Color.blue(bitmapColor) };

            if (color[0] > 200 && color[1] > 200 && color[3] > 200) {
                color[0] = 255;
                color[1] = 255;
                color[2] = 255;
            }

            for (int i = 0; i < color.length; i++) {
                int tmpColor = color[i];
                tmpColor = (int) ((tmpColor - 128) * effectLevel + 128);
                if (tmpColor < 0) {
                    tmpColor = 0;
                } else if (tmpColor > 255) {
                    tmpColor = 255;
                }
                color[i] = tmpColor;
            }

            pixels[(YY + XX * width)] = Color.rgb(color[0], color[1], color[2]);
        }
    }

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

From source file:Main.java

/**
 * Blend two colors.//  w w w .j a v  a  2  s .  co m
 * 
 * @param color1
 *            First color to blend.
 * @param color2
 *            Second color to blend.
 * @param ratio
 *            Blend ratio. 0.5 will give even blend, 1.0 will return color1,
 *            0.0 will return color2 and so on.
 * @return Blended color.
 */
public static int blend(final int color1, final int color2, final double ratio) {
    final float r = (float) ratio;
    final float ir = (float) 1.0 - r;

    final float rgb1[] = new float[3];
    final float rgb2[] = new float[3];

    rgb1[0] = Color.red(color1);
    rgb1[1] = Color.green(color1);
    rgb1[2] = Color.blue(color1);
    rgb2[0] = Color.red(color2);
    rgb2[1] = Color.green(color2);
    rgb2[2] = Color.blue(color2);

    final int color = Color.rgb(Math.round(rgb1[0] * r + rgb2[0] * ir), Math.round(rgb1[1] * r + rgb2[1] * ir),
            Math.round(rgb1[2] * r + rgb2[2] * ir));

    return color;
}

From source file:Main.java

public static int HSLtoRGB(float[] hsl) {
    final float h = hsl[0];
    final float s = hsl[1];
    final float l = hsl[2];

    final float c = (1f - Math.abs(2 * l - 1f)) * s;
    final float m = l - 0.5f * c;
    final float x = c * (1f - Math.abs((h / 60f % 2f) - 1f));

    final int hueSegment = (int) h / 60;

    int r = 0, g = 0, b = 0;

    switch (hueSegment) {
    case 0://from w  ww .  ja v  a 2  s.  c om
        r = Math.round(255 * (c + m));
        g = Math.round(255 * (x + m));
        b = Math.round(255 * m);
        break;
    case 1:
        r = Math.round(255 * (x + m));
        g = Math.round(255 * (c + m));
        b = Math.round(255 * m);
        break;
    case 2:
        r = Math.round(255 * m);
        g = Math.round(255 * (c + m));
        b = Math.round(255 * (x + m));
        break;
    case 3:
        r = Math.round(255 * m);
        g = Math.round(255 * (x + m));
        b = Math.round(255 * (c + m));
        break;
    case 4:
        r = Math.round(255 * (x + m));
        g = Math.round(255 * m);
        b = Math.round(255 * (c + m));
        break;
    case 5:
    case 6:
        r = Math.round(255 * (c + m));
        g = Math.round(255 * m);
        b = Math.round(255 * (x + m));
        break;
    }

    r = Math.max(0, Math.min(255, r));
    g = Math.max(0, Math.min(255, g));
    b = Math.max(0, Math.min(255, b));

    return Color.rgb(r, g, b);
}

From source file:Main.java

/**
 * Gets the RGB pixel at the given position in a YUV420SPNV21 byte array
 *
 * @param yuv byte array//from  www  .j  a  v  a  2  s .co m
 * @param x   {@link Integer}
 * @param y   {@link Integer}
 * @return {@link Integer}
 */
public static int getColorAtPoint(byte[] yuv, int x, int y) {
    int i = (FRAME_WIDTH * FRAME_HEIGHT) + FRAME_WIDTH * (y >> 1) + (x & 0xFFFFFFFE);
    int j = 0xFF & yuv[x + y * FRAME_WIDTH];
    int k = 0xFF & yuv[(i + 1)];
    int m = 0xFF & yuv[i];
    int n = k - 128;
    int i1 = m - 128;
    int i2 = (int) (j + 1.402f * i1);
    int i3 = (int) (j - 0.344f * n - 0.714f * i1);
    int i4 = (int) (j + 1.772f * n);
    i2 = (i2 < 0) ? 0 : i2;
    i2 = (i2 > 255) ? 255 : i2;
    i3 = (i3 < 0) ? 0 : i3;
    i3 = (i3 > 255) ? 255 : i3;
    i4 = (i4 < 0) ? 0 : i4;
    i4 = (i4 > 255) ? 255 : i4;
    return Color.rgb(i2, i3, i4);
}

From source file:com.game.prayansh.ultimatetictactoe.ui.ThemeManager.java

public static void setMinimal(Context context) {
    theme = new Theme(0, R.drawable.minimal_background, R.drawable.cross_minimal, R.drawable.circle_minimal,
            ContextCompat.getColor(context, R.color.mt_black), R.drawable.blank_white,
            R.drawable.blank_white_highlight, Color.rgb(255, 255, 255), R.drawable.btn_light);
}