Click to See Complete Forum and Search --> : RGB to HEX routines
Does any one have any routines for taking RGB values and turning them into HEX that they would like to "make public"?
parksie
Oct 15th, 2000, 05:44 AM
They're already hex:
0x00BBGGRR (on Intel, anyway...I think)
So:
unsigned short usRed;
unsigned short usGreen;
unsigned short usBlue;
unsigned long ulColour = 0xaabbcc;
usRed = ulColour;
usGreen = ulColour >> 8;
usBlue = ulColour >> 16;
I dont think thats HEX, but if it is, thats not the kind im talking about. Im talking about the kind used for color values in HTML.
parksie
Oct 15th, 2000, 10:11 AM
Oops...silly me. I thought you wanted to split the numbers up ;)
In that case, how about this to convert it to a hex string:
unsigned long lColour = 0xccbbaa;
char *pcHexStr = "#000000";
char cTemp;
for(int i = 0; i < 6; i++) {
cTemp = (lColour >> (i*4)) & 0x0f;
pcHexStr[i+1] = cTemp + ((cTemp > 9) ? ('a' - 0xa) : '0');
}
cout << pcHexStr << endl
I cant seem to get it to work. Every time it comes to that code, windows says the app has crashed.
parksie
Oct 16th, 2000, 01:40 PM
I don't know what it is, then, since it worked perfectly on mine. Did you copy the code directly, or have you changed it at all? Remember - the buffer must be at least 7 characters long.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.