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 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:
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:
The four ARGB components can be individually extracted from a color int using the following expressions:
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:
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).
- The first FF in the 80FFFF00 value represents the maximum value red can have.
- The second FF is like the previous but for green.
- 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.
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?