Results 1 to 6 of 6

Thread: RGB to HEX routines

  1. #1
    Guest

    Post

    Does any one have any routines for taking RGB values and turning them into HEX that they would like to "make public"?

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    They're already hex:

    0x00BBGGRR (on Intel, anyway...I think)

    So:
    Code:
    unsigned short usRed;
    unsigned short usGreen;
    unsigned short usBlue;
    unsigned long ulColour = 0xaabbcc;
    
    usRed = ulColour;
    usGreen = ulColour >> 8;
    usBlue = ulColour >> 16;
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3
    Guest
    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.

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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:
    Code:
    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 refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5
    Guest
    I cant seem to get it to work. Every time it comes to that code, windows says the app has crashed.

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width