How can I convert a RGB color into a HEX in AS3?. For example: R=253 G=132 B=58.
Tks.
Robusto's solution is too slow.
Because RGB values are stored like this:
8b Red | 8b Green | 8b Blue
And a value of 0-255 (that is not a coincidence) has 8b too, you can use left bitwise shifts to get the int value, and THEN you can get a hex (almost 3 times faster). So:
var intVal:int = red << 16 | green << 8 | blue;
var hexVal:String = intVal.toString(16);
hexVal = "#" + (hexVal.length < 6 ? "0" + hexVal : hexVal);
Where red, green and blue are the values of RGB that you want to convert.
Convert the RGB numbers to hex values and concatenate them.
var hexVal = (253).toString(16) + (132).toString(16) + (58).toString(16);
hexVal = "#" + hexVal;
// returns "#fd843a"
This is less elegant than it ought to be, but it should give you the idea.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With