Android Color Management

Android uses hexadecimal ARGB values, which are formatted as #AARRGGBB. That first pair of letters, the AA, represent the alpha channel.

Color ints

Color ints are the most common representation of colors on Android and have been used since API level 1.

A color int always defines a color in the sRGB color space using 4 components packed in a single 32 bit integer value:


Component Name Size Range
A Alpha 8 bits [0...255]
R Red 8 bits [0...255]
G Green 8 bits [0...255]
B Blue 8 bits [0...255]

The components in this table are listed in encoding order, which is why color ints are called ARGB colors.

The four components of a color int are encoded in the following way:

int color = (A & 0xff) << 24 | (R & 0xff) << 16 | (G & 0xff) << 8 | (B & 0xff);

The four ARGB components can be individually extracted from a color int using the following expressions:

int A = (color >> 24) & 0xff; // or color >>> 24 int R = (color >> 16) & 0xff; int G = (color >> 8) & 0xff; int B = (color ) & 0xff;

ARGB

ARGB values are typically expressed using 8 hexadecimal digits, with each pair of the hexadecimal digits representing the values of the Alpha, Red, Green, and Blue channels, respectively and which are formatted as #AARRGGBB. That first pair of letters, the AA, represent the alpha channel.

The ARGB channels are arranged in memory in such a manner that a single 32-bit unsigned integer has the alpha sample in the highest 8-bits, followed by the red sample, green sample, and finally the blue sample in the lowest 8 bits:


Colors on Android:What Developers and Designers Need to Know

So, if you have 8 characters, it's ARGB, with the first two characters specifying the alpha channel. If you remove the leading two characters it's only RGB (solid colors, no alpha/transparency).

For example, 80FFFF00 represents 50.2% opaque (non-premultiplied) yellow:

The 80 hex value, which is 128 in decimal, represents a 50.2% alpha value because 128 is approximately 50.2% of the maximum value of 255 (FF hex).

  1. The first FF in the 80FFFF00 value represents the maximum value red can have.
  2. The second FF is like the previous but for green.
  3. The final 00 represents the minimum value blue can have (effectively – no blue).

Consequently, red + green yields yellow. In cases where the alpha is not used, this can be shortened to 6 digits, RRGGBB. This is why it was chosen to put the alpha in the top bits.

RGB to ARGB conversion in Android

If you have an RGB code for a color, how can you use it in setPixel() function? ; because setPixel() function wants ARGB for color.

int color = Color.argb(255, 118, 118, 188);

The first value (255) represents the Alpha channel, or in plain language: the transparency. The value is one byte, so acceptable values range from 0 to 255.

In the above code, a value of 255 means your colour will be completely opaque (solid). A value of 128 will give you 50% transparency. A value of 0 will make your object completely invisible, regardless of your colour value, but the object still exists, like a sheet of perfectly clear glass, if you allow the analogy. This can be useful for e.g secret/hidden/invisible buttons, creating easter eggs, or specific cases of UI tuning.




NEXT.....What Colour is Chucknorris?