The Color Constructor

The Color Constructor

When you construct a Color object in Java, you can supply the arguments in three ways. The most familiar way is to provide three int values each in the range 0-255, with each value corresponding to the Red, Green, or Blue component. For example Color (255,0,0) creates a pure red color.

However, you can also provide three float values, each in the range 0.0 to 1.0, with each value corresponding to the red, green, or blue component. The final method can save space if memory is critical, but it’s a little less obvious. You can supply a single int value by multiplying the red value (expressed as 0-255) by 65536, the green value by 256, and then adding those results to the blue value. For example, you could express the color with the values Red=125, Green=45, Blue=77 as the integer 8203597 (8192000+11520+77).


Share the Post: