Public Group

# Hex To RGB how to ?(C++)

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

## Recommended Posts

How do you convert hex into RGB?

##### Share on other sites

If so, you'll need to be more specific about what's holding you up so we don't answer all of your questions but the right one. Trying to parse a string of hexadecimal? Turning a hex value into a decimal one? Getting a floating point color value from integers?

##### Share on other sites
have I checked google.. thats like the typical thing you --edit-- do before asking a programming question. I've been on google for around 8 hours now searching for different questions and getting annoyed. Found some answers and nothing about C++..hexidecimal code to RGB...If you want to help them help, else don't. Need a specific answer.

i.e(FireFox color zilla):

#68BC31 HEX
rgb(104, 188, 49) RGB

[Edited by - MadKeithV on October 26, 2007 8:02:55 AM]

##### Share on other sites
Quote:
 Original post by scheolshave I checked google.. thats like the typical thing you --edit-- do before asking a programming question. I've been on google for around 8 hours now searching for different questions and getting annoyed. Found some answers and nothing about C++..hexidecimal code to RGB...If you want to help them help, else don't. Need a specific answer.i.e(FireFox color zilla):#68BC31 HEXrgb(104, 188, 49) RGB

Calm down, goodness. jouley is right, he asked if you'd checked google because many answers can easily be found that way but people often don't do it. Him asking was harmless and he was just checking.

How is your Hex data stored? In a string? Is there a prefix (is it #, 0x, or some other prefix, and is it only one prefix or does it change)?

For the red, take the first two letters after the prefix. strtol() can be used to convert those first two letters from base-16 to base-10. Then you do the same thing for the next two letters, and then the next two.

next time you post, be polite. It makes people much more willing to help out

[Edited by - MadKeithV on October 26, 2007 8:13:22 AM]

##### Share on other sites
an rgb value is really just a number or rather three 8 bit numbers stored into a single 32 bit number.

So bits [0:7] is the red, bits [8:15] is the green and bits [16:25] is the blue.

So the hex format for a C++ literal is 0xBBGGRR so you would just replace you hex value for blue in the BB green to GG and red to RR.

so here are a few examples
0xFFFFFF = white
0xFF0000 = blue
0x00FF00 = green
0x0000FF = red
0x800000 = dark blue
0x0080FF = orange

##### Share on other sites
There's a formalization for conversion from any base to decimal (and this is what you'll need to do for HEX to RGB conversion):

So using HEX as an example, let's convert the number F47D8C to RGB. First let's convert R.

(F4)s10 = 16^1 * F + 4 * 16^0 = 16 * 15 + 4 * 1 = 240 + 4 = 244 = R

G: (7D)s10 = 16^1 * 7 + 16^0 * D = 16 * 7 + 1 * 13 = 112 + 13 = 125 = G

B: (8C)s10 = 16^1 * 8 + 16^0 * C = 16 * 8 + 1 * 12 = 128 + 12 = 140 = B

Therefore,

#F47D8C = (255, 125, 140) in RGB.

The rule is # base 10 = SUM(i=0, i < digit count, BASE^(i)*digit)

Hope that helped!

##### Share on other sites
Quote:
 Original post by HappyCoderan rgb value is really just a number or rather three 8 bit numbers stored into a single 32 bit number.So bits [0:7] is the red, bits [8:15] is the green and bits [16:25] is the blue.So the hex format for a C++ literal is 0xBBGGRR so you would just replace you hex value for blue in the BB green to GG and red to RR.so here are a few examples0xFFFFFF = white0xFF0000 = blue0x00FF00 = green0x0000FF = red0x800000 = dark blue0x0080FF = orange

Are you talking about converting a C++ hex literal to a color index? Because, while that might be true for a C++ hex literal, people usualy write hex colors as strings in the format of 0xRRGGBB, especially in web programming (which the OP is apparently referencing). I'm afraid we might be confusing the OP by switching back and forth.

So scheols, now it becomes important, are you using C++ hex literals or strings to store your hex values? I'm going to guess strings...

##### Share on other sites
I'm officially the world's nicest guy

#include <iostream>#include <string>#include <vector> using namespace std; int convertFromHex(string hex){    int value = 0;        int a = 0;    int b = hex.length() - 1;    for (; b >= 0; a++, b--)    {        if (hex >= '0' && hex <= '9')        {            value += (hex - '0') * (1 << (a * 4));        }        else        {            switch (hex)            {                case 'A':                case 'a':                    value += 10 * (1 << (a * 4));                    break;                                    case 'B':                case 'b':                    value += 11 * (1 << (a * 4));                    break;                                    case 'C':                case 'c':                    value += 12 * (1 << (a * 4));                    break;                                    case 'D':                case 'd':                    value += 13 * (1 << (a * 4));                    break;                                    case 'E':                case 'e':                    value += 14 * (1 << (a * 4));                    break;                                    case 'F':                case 'f':                    value += 15 * (1 << (a * 4));                    break;                                    default:                    cout << "Error, invalid charactare '" << hex[a] << "' in hex number" << endl;                    break;            }        }    }        return value;} void hextodec(string hex, vector<unsigned char>& rgb){    // since there is no prefix attached to hex, use this code    string redString = hex.substr(0, 2);    string greenString = hex.substr(2, 2);    string blueString = hex.substr(4, 2);    /*    if the prefix # was attached to hex, use the following code    string redString = hex.substr(1, 2);    string greenString = hex.substr(3, 2);    string blueString = hex.substr(5, 2);*/    /*    if the prefix 0x was attached to hex, use the following code    string redString = hex.substr(2, 2);    string greenString = hex.substr(4, 2);    string blueString = hex.substr(6, 2);*/        unsigned char red = (unsigned char)(convertFromHex(redString));    unsigned char green = (unsigned char)(convertFromHex(greenString));    unsigned char blue = (unsigned char)(convertFromHex(blueString));        rgb[0] = red;    rgb[1] = green;    rgb[2] = blue;} int main(){    string hexColor;    vector<unsigned char> rgbColor(3);        cout << "Enter Hex value (must be 6 valid Hex digits): 0x";    cin >> hexColor;        hextodec(hexColor, rgbColor);        cout << "\nWhen 0x" << hexColor << " is converted to RGB, it becomes:" << endl;    cout << "R: " << int(rgbColor[0]) << endl;    cout << "G: " << int(rgbColor[1]) << endl;    cout << "B: " << int(rgbColor[2]) << endl;        system("pause");}

Complete source, compiled using the g++ compiler in Dev-C++ (which means that there may be some warnings that should have been reported but weren't). I wrote my own converter so you can see how it works, however, there might be some stuff that confuses you.

As for now, it's 1:00 am, I have school tomorrow, homework I haven't finished, and I've spent the past forever writing code I will never use and never should have spent my time writing. And yes, a thank you would be nice

##### Share on other sites
This is how i do mine using DirectX data types in my 3dsmax exporter and game import

reads in a hex color value stored in my model and converts it to a vector4 for rgba values(a is always 1 for mine)

D3DXVECTOR4 DWORD_2_D3DXVECTOR4(DWORD color){	float r,g,b,a;	r = (BYTE)color;	g = (WORD)color >>8;	b = (DWORD)color >>16;	a = 1.0;	return D3DXVECTOR4(r,g,b,a);}

might not be the best but its fast and works like a charm for me

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 28
• 16
• 10
• 10
• 11
• ### Forum Statistics

• Total Topics
634113
• Total Posts
3015570
×