Hex colors in Android are sometimes eight digits. How? What is the difference between #FFFFFF and #FFFFFF00?

AndroidAndroid LayoutColorsHexAndroid Canvas

Android Problem Overview


I sometimes have seen in examples where the coloring in Android is done as #FF191919. I mean an eight-digit hex number. But it should only be a six-digit number. How are they related?

If I want to convert a six-digit number to a eight-digit number, how can I do it? I mean convert #424242 to a eight-digit number coloring? What are the the details?

Android Solutions


Solution 1 - Android

The extra two digits are used to define the colors' transparency, or alpha channel.

Android uses the ARGB format (or AARRGGBB as you use in your example).

For more (Android-specific) information, take a look at the Color documentation.

Solution 2 - Android

The first two characters represent the alpha (transparency) value, where FF is fully visible. This is known as ARGB.

Solution 3 - Android

The eight-digit hexadecimal value is an ARGB color. It is the same as the usual RGB, but it provides an extra alpha channel.

#RRGGBB in RGB is the same as #00RRGGBB in ARGB. Also take a look at Color.argb.

Solution 4 - Android

An eight-digit Android hexadecimal value is called an ARGB. ARGB values are typically expressed using eight hexadecimal digits, with each pair of the hexadecimal digits representing the values of the alpha, red, green and blue channel, respectively. For example, 80FFFF00 represents 50.2% opaque (non-premultiplied) yellow.

The 80 hexadecimal 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 hexadecimal); to continue to decipher the 80FFFF00 value, the first FF 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 six digits, RRGGBB, and this is why it was chosen to put the alpha in the top bits. Depending on the context, a 0x or a number sign, #, is put before the hexadecimal digits.

Solution 5 - Android

The eight-digit color is defined with an alpha level.

Let’s extract all. We define the hexadecimal color as six value pairs of RGB two digits per pair.

  • The first two digits for red.
  • The second two digits for green.
  • The third two digits for blue.

Now if you want to set the alpha level of that then it is defined with the eight digits as ARGB.

So now the first two digit values define the alpha and the rest are for the RGB.

Solution 6 - Android

Eight-digit hex notation works the same as the six-digit notation, in that you provide a six-digit hexadecimal value, prefixed with a hash (#) symbol.

The difference is, eight-digit notation, as the name suggests, adds two more digits. These two digits represent the alpha channel of the color.

The alpha channel is represented by the last two digits.

This last pair of digits are interpreted as a hexadecimal number (just like the other digits). A value of 00 represents a fully transparent color, and a value of FF represents a fully opaque color.

So for a fully opaque color do this : Color(0xFF<your-6digit-code>)

For example, if you have a 6 digit code: E64526 Now convert it to an 8 digit code with: Color(0xFFE64526)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVinodView Question on Stackoverflow
Solution 1 - AndroidVegerView Answer on Stackoverflow
Solution 2 - AndroidshanethehatView Answer on Stackoverflow
Solution 3 - AndroidAndyView Answer on Stackoverflow
Solution 4 - AndroidTerry MillerView Answer on Stackoverflow
Solution 5 - AndroidPratikView Answer on Stackoverflow
Solution 6 - AndroidSharukh RahmanView Answer on Stackoverflow