converting a hex integer to color in xamarin android

The difference in your examples is whether the alpha (transparency) value of color is obeyed.

Per the Android documentation on android.graphics.Color, an integer color value consists of a byte each for alpha, red, green, and blue components, i.e.: 0xAARRGGBB (written out in big endian notation). The red RR, green GG, and blue BB components are somewhat easy to understand – the value of each component corresponds to the intensity of that primary color.

The alpha component AA is essentially the transparency of that pixel when the image in question is placed on top of another image. If pixel x of image X overlays pixel y of image Y, then x‘s alpha component determines how much of the final rendered pixel z comes from x and how much from y. If the alpha value is high, more of z‘s color will come from x; if the alpha value is low, more of z‘s color will come from y. The highest value 0xFF indicates x is completely opaque – z and x will be the same color, regardless of y. The lowest value 0x00 indicates x is completely transparent – z and y will be the same color, regardless of x.

In the first example, you extract the red, green, and blue components from the integer with e.g. GetRedComponent. Notice that you don’t extract the alpha value. Then, you call Rgb, which is documented in the Android docs as always setting the alpha component to 0xFF – completely opaque. So, no matter what value the color parameter is, it will never have any transparency.

In the second example, you provide color directly to the constructor, which appears to take alpha channel into account.

So it seems you’re input color has an alpha value of something other than 0xFF. In your comments on the question, you say some bars “disappear” – it’s likely that your bars are technically there, they’re just colored with 0x00 for full transparency and thus can’t be seen.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top