Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert RBG to HEX

How can I convert a RGB color into a HEX in AS3?. For example: R=253 G=132 B=58.

Tks.

like image 705
oscarm Avatar asked Nov 22 '10 17:11

oscarm


2 Answers

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.

like image 125
Aurel Bílý Avatar answered Sep 28 '22 03:09

Aurel Bílý


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.

like image 21
Robusto Avatar answered Sep 28 '22 03:09

Robusto