23

Paint.setColor is expecting an integer. But what I have is a Color object. I don't see a color.getIntValue() in Java? So how do I do that? What I want is something like

public Something myMethod(Color rgb){
    myPaint.setColor(rgb.getIntValue());
    ...
}

Correction: android.graphics.Color; I thought having android as one of the tags would be enough. But apparently not.

Cote Mounyo
  • 13,817
  • 23
  • 66
  • 87
  • This looks like the same problem: http://stackoverflow.com/questions/6926644/android-color-to-int-conversion – TravJenkins Aug 02 '13 at 17:02
  • Where does your color object come from? I am confused why you are passing a Color object instead of the int representing the color in the first place. The Color class in android defines static methods for creating and converting color ints. From my understanding it should not be initialized – kalelien Aug 02 '13 at 18:52

9 Answers9

54

First of all, android.graphics.Color is a class thats composed of only static methods. How and why did you create a new android.graphics.Color object? (This is completely useless and the object itself stores no data)

But anyways... I'm going to assume your using some object that actually stores data...

A integer is composed of 4 bytes (in java). Looking at the function getRGB() from the standard java Color object we can see java maps each color to one byte of the integer in the order ARGB (Alpha-Red-Green-Blue). We can replicate this behavior with a custom method as follows:

public int getIntFromColor(int Red, int Green, int Blue){
    Red = (Red << 16) & 0x00FF0000; //Shift red 16-bits and mask out other stuff
    Green = (Green << 8) & 0x0000FF00; //Shift Green 8-bits and mask out other stuff
    Blue = Blue & 0x000000FF; //Mask out anything not blue.

    return 0xFF000000 | Red | Green | Blue; //0xFF000000 for 100% Alpha. Bitwise OR everything together.
}

This assumes you can somehow retrieve the individual red, green and blue colour components and that all the values you passed in for the colours are 0-255.

If your RGB values are in form of a float percentage between 0 and 1 consider the following method:

public int getIntFromColor(float Red, float Green, float Blue){
    int R = Math.round(255 * Red);
    int G = Math.round(255 * Green);
    int B = Math.round(255 * Blue);

    R = (R << 16) & 0x00FF0000;
    G = (G << 8) & 0x0000FF00;
    B = B & 0x000000FF;

    return 0xFF000000 | R | G | B;
}

As others have stated, if you're using a standard java object, just use getRGB();

If you decide to use the android color class properly you can also do:

int RGB = android.graphics.Color.argb(255, Red, Green, Blue); //Where Red, Green, Blue are the RGB components. The number 255 is for 100% Alpha

or

int RGB = android.graphics.Color.rgb(Red, Green, Blue); //Where Red, Green, Blue are the RGB components.

as others have stated... (Second function assumes 100% alpha)

Both methods basically do the same thing as the first method created above.

initramfs
  • 8,275
  • 2
  • 36
  • 58
27

If you are developing for Android, Color's method for this is rgb(int, int, int)

So you would do something like

myPaint.setColor(Color.rgb(int, int, int)); 

For retrieving the individual color values you can use the methods for doing so:

Color.red(int color) 
Color.blue(int color) 
Color.green(int color) 

Refer to this document for more info

hBrent
  • 1,696
  • 1
  • 17
  • 38
superdiazepam
  • 457
  • 5
  • 13
12

Color has a getRGB() method that returns the color as an int.

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
7

You want to use intvalue = Color.parseColor("#" + colorobject);

JoppeD
  • 95
  • 1
  • 7
4
int color = (A & 0xff) << 24 | (R & 0xff) << 16 | (G & 0xff) << 8 | (B & 0xff);
Amol Jindal
  • 106
  • 4
1

You may declare a value in color.xml, and thus you can get integer value by calling the code below.

context.getColor(int resId);
TangQisen
  • 9
  • 1
0
int color =  Color.rgb(red, green, blue);

where red, green blue are int values between 0 and 255.

toto_tata
  • 14,526
  • 27
  • 108
  • 198
-2

Use getRGB(), it helps ( no complicated programs )

Returns an array of integer pixels in the default RGB color model (TYPE_INT_ARGB) and default sRGB color space, from a portion of the image data.

GitaarLAB
  • 14,536
  • 11
  • 60
  • 80
meow
  • 1
-3

Try this one:

Color color = new Color (10,10,10)


myPaint.setColor(color.getRGB());
Dhanuka
  • 2,826
  • 5
  • 27
  • 38
lexpfb
  • 175
  • 3
  • 12