1

According to Oracle's site, the class Color has a constructor that accepts a single int value which represents an RGB value. http://download.oracle.com/javase/1.4.2/docs/api/java/awt/Color.html#Color(int)

An RGB color is actually three different numbers ranging from 0-255. So combining them together to make one int would look like this:

White 255,255,255
White 255255255 

Right? So I pass this to the constructor and get a vibrant teal color. What am I doing wrong? What haven't I understood?

3 Answers3

5

From Convert RGB values to Integer

int rgb = red;
rgb = (rgb << 8) + green;
rgb = (rgb << 8) + blue;

To pull values out:

int red = (rgb >> 16) & 0xFF;
int green = (rgb >> 8) & 0xFF;
int blue = rgb & 0xFF;
Community
  • 1
  • 1
Reverend Gonzo
  • 39,701
  • 6
  • 59
  • 77
2

Javadoc from the other constructor:

Creates an sRGB color with the specified combined RGBA value consisting of the alpha component in bits 24-31, the red component in bits 16-23, the green component in bits 8-15, and the blue component in bits 0-7. If the hasalpha argument is false, alpha is defaulted to 255.

So, you just need to construct the int using bit operations.

Vladimir Ivanov
  • 42,730
  • 18
  • 77
  • 103
0
Color col=new Color(255,255,255);
Label l1=new Label("I got this color");

//setting the color in label background

l1.setBackground(col);
EdChum
  • 376,765
  • 198
  • 813
  • 562