# Can we start using HEX color code values instead of RGB?

I’m doing some color migration between addons, and from the very start, I’ve noticed that entering 3 color values is quite inefficient.

Is there a way to default to HEX values? I’m sure specific addons can use it some how, but I’m also curious why developers don’t default to HEX codes instead of having to enter 3 values (sometimes 4 for transparency).

because RGB works off a 256 bit? system, which is way easier to store than HEX. and in the end, whatever you were using for HEX would be translating it into RGB anyway, so all you’re really doing is bloating up the code for the reduction of one tiny parameter.

also because RGB is infinitely easier to understand than HEX codes.

You can just use converter functions:

``````
function HexToColor( hex )
return Color( bit.band( bit.rshift( hex, 16 ), 0xFF ), bit.band( bit.rshift( hex, 8 ), 0xFF ), bit.band( bit.rshift( hex, 0 ), 0xFF ) )
end

function ColorToHex( color )
return bit.lshift( color.r, 16 ) + bit.lshift( color.g, 8 ) + bit.lshift( color.b, 0 )
end

``````

You can also override SetColor to accept both hexes and colours.

256-bit colors would be a pretty cool thing to have, but I’m afraid most screens right now are 24-bit You’re thinking of 256 values per channel, or 8 bits. (a channel being red or green, for example.)

A hexadecimal representation (which is still RGB, actually), as lintz said, would need to be converted to three numeric values (for red, green and blue) for lower systems to be able to work with it. In other words, “#RRGGBB” would have to be parsed, split into three hexadecimal numbers (RR, GG and BB), and then convert to three actual numeric values.

If you supply red, green and blue separately, no strings have to be parsed and the operation is a lot faster; both for the machine, and in many cases for the programmer.

[editline]19th March 2015[/editline]

The functions code_gs just posted is pretty much what would have to be done, anyway.