Sign in to follow this  

RGB to Hex

This topic is 3597 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Dudes, My graphics engine uses hex for color codes. eg fnt->SetColor(0xB166FF66); How do I convert from RGB to hex? I've googled it but all the answers I've found give me a hex result two digits short and don't work, even when I make up the last two digits. eg the result from a google conversion: For example, a golden yellow in RGB is (177,102,0). My resulting hexadecimal code is #B16600. Is it the alpha channel I need? Is there a chart anywhere with at least a few primary color conversions? m0ng00se

Share this post


Link to post
Share on other sites
Quote:
Original post by m0ng00se
My graphics engine uses hex for color codes. eg

fnt->SetColor(0xB166FF66);

How do I convert from RGB to hex?
Something along these lines... you just need to get single bytes into R8G8B8A8 format, right?

int32_t col;
col = red << 24;
col |= green << 16;
col |= blue << 8;
col |= alpha;

Or, you could do something like:

char rgba[4];
rgba[0] = red;
rgba[1] = green;
rgba[2] = blue;
rgba[3] = alpha;

SetColor(*((int32_t*)rgba));

Share this post


Link to post
Share on other sites
Hmmm that's an interesting solution.

Yes your assumption is correct and I never thought of doing it like that. I assumed I was locked into using the actual hex format.

I'll give it a try.

thanx.

m0ng00se

Share this post


Link to post
Share on other sites
Try these macros, which are neater and more self-documenting:


#define RGBA( r, g, b, a ) ( (r<<24) | (g<<16) | (b<<8) | a )
#define ARGB( a, r, g, b ) ( (a<<24) | (r<<16) | (g<<8) | b )


Share this post


Link to post
Share on other sites
Quote:
Original post by pcmattman
Try these macros, which are neater and more self-documenting:

*** Source Snippet Removed ***

Why use macros instead of (inlined) functions?

@m0ng00se: You may want to change the interface of your graphics engine (assuming 'my graphics engine' refers to one you've written yourself) to accept r, g, b and a values rather than a single color integer, and do the conversion inside.

Note that the hexadecimal notation is just a notation. 255 and 0xFF have the same value and can both be passed to a function that expects an integer, for example.

Share this post


Link to post
Share on other sites
well I meant "my" as opposed to "yours" whatever it may be... I certainly didn't write it myself. I'm not that clever.

It's HGE 2D graphics library but my question also applies to other graphics engines I use as well that also use hex notation in all their color functions within their tutorials. Off the top of my head without checking I think Illricht does and I can't remember what OGRE does because I've never had reason to change colors in it yet. I know Dark GDK just expected RGB codes which was great because I just picked any color I liked out of Photo Paint or Photo Shop and both these programs gave me the RGB code.

Thanx for the feedback. I'll test those ideas as soon as I fix my main control loop that I just broke. And yeah I did write the control loop myself.

m0ng00se

Share this post


Link to post
Share on other sites
Doesn't Photoshop display the hexadecimal value of a color then? Paint.NET does...


Anyway, maybe you've figured this out already, maybe not, but hexadecimal notation isn't too bad for colors. Every two characters stand for a single byte, so when you know the order in which the color channels are stored, you can get a rough idea of what sort of color you're working with.

For example, if the system accepts colors as ARGB and you've got a color 0xFFA0F000, then the first two characters (FF) are the alpha value. FF is 255, so it's not transparant. The second pair, A0, which is 160, is the R channel. That's more than 50% red. The third pair, F0, is the green channel. That's pretty green. The last pair, 00, is blue, but alas, there's little blue in this color.

Share this post


Link to post
Share on other sites
Thanx for that. I didn't realise Photo Shop could give me hex codes. I only just started using it after the Corel Photo Paint 10.01 patch trashed my system and made Photo Paint unusable. Rather than re-install Photo Paint without the patch (it used to freeze a lot anyway) I whacked Photo Shop on instead and love it so far.

m0ng00se

Share this post


Link to post
Share on other sites

This topic is 3597 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this