Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What’s the use of 0xff -colors in android?

Tags:

android

colors

Sometimes in android projects there are color formats like '0xff4ca665'.

What is the purpose of using them?

like image 214
Mishel Avatar asked Jan 06 '23 14:01

Mishel


1 Answers

Some programmers come from have more experience with overt 24 bit colors. And sometimes it matters in Android, the alpha isn't masked in some more direct manipulations in renderscript. And as you want '0xff4ca665' telling the system you want '0x4ca665' and hoping something else masks it correctly later on seems like a sketchy bet on something that literally cannot even waste a clock cycle.

Somethings do not take resID links to colors or give you an option like View.setBackgroundColor(int) where the int for the color is encoded in a 32 bit integer like that.

Specifically this is how alpha masking in 24 bit colors work. They are formatted as a 32 bit integer as 0bAAAAAAAARRRRRRRRGGGGGGGGBBBBBBBB So each color of RGB gets 8 bits and there's 8 bit masking for the alpha done with whatever PorterDuff blending is later used to render the result. Android stores colors purely as integers, so using these is a good idea, if that's what it ends up as.

When you do it in hex like that you get two digits for each thing 0xAARRGGBB and the AA is set to ff meaning 255 or fully opaque. No need to trust the colors to not being fully opaque. There's a lot of times when if you leave those as 00 you end up with a blank image, or well fully transparent one. Knowing which times it matters isn't worth it, if we just overtly include the alpha.

Android also has a few other schemes that matter which store colors in shorts. Namely 4444 which stores alpha and each color in 4 bits. And 565 which stores the red in 5 bits, green in 6 bits and blue in 5 bits. Both of which end up using 16 bits in total rather than 32. But, a great many encoding schemes use 8888 which is pretty standard so it's very common to see people referencing the colors like that, especially in hex.

like image 132
Tatarize Avatar answered Feb 02 '23 22:02

Tatarize