haXe Forum > rgb to hex?
-
piotr Feb 07 at 15:50
Hi again!
Problem seems simple: i want to draw a pixel o a specyfic color depending on a variable value. Pixel colors are always given in hexadecimal form. How do I convert rgb values to hexadecimal?
Second thing would be doing it as fast as possible.
:)?
-
Oleg Sivokon Feb 07 at 16:54
Erm... the same number can be written in many different way, but no matter how you write it, the program uses the value, not the string that represents the number in any notation... If you need to convert UInt values from string - you can use Std.parseInt(). The other way would be: StringTools.hex(someUInt, 6); for example.
If you need to output it in notation other then decimal or hexadecimal, this would depend on the platform, but most platform have built-in methods for doing that, so, for instance, on flash you'd dountyped someUInt.toString(base);
-
piotr Feb 07 at 17:15
It's not the case of from string conversion/
Yes, I'm using Haxe for Flash. I need to generate a color depending on variable values. I always passed a colors in 0x000000 - 0xFFFFFF format. I would like to pass it in r + g + b format. I just don't know how to convert one into another.
Hexadecimal values are Ints so I guess one can create them as a sum of red, green and blue Int components.
As in function
bitmapData.fillRect(new Rectangle(X, Y, tileSize - 1, tileSize - 1), 0xFF0000);
I would like to do something like this
var r: Int = 255; var g: Int = 0; var b: Int = 0; bitmapData.fillRect(new Rectangle(X, Y, tileSize - 1, tileSize - 1), r + g + b); ///doesn't work of course
-
aulia Feb 07 at 17:19
here:
var PACKED_COLOR = (A & 0xFF) << 24 | (R & 0xFF) << 16 | (G & 0xFF) << 8 | (B & 0xFF);
-
piotr Feb 07 at 17:24
Great :), exacly what I was looking for. Thank you!