Results 1 to 7 of 7

Thread: Convert HTML colour codes to

  1. #1

    Thread Starter
    Frenzied Member ae_jester's Avatar
    Join Date
    Jun 2001
    Location
    Kitchener Ontario Canada Earth
    Posts
    1,545

    Convert HTML colour codes to

    I have this code that works in VB for converting an html colour string into a visual basic colour. How can I do the same thing in C??????

    Keep in mind that RGB does not work in C so I would need an equivalent function for this also.

    VB Code:
    1. 'Convert a colour represented in html colour format,
    2. 'to a long value recognizable by visual basic
    3. Function HTMLToLong(sHTML As String) As Long
    4.     Dim lR As Long, lB As Long, lG As Long
    5.  
    6.     lR = CInt(Val("&h" + Mid(sHTML, 2, 2)))
    7.     lG = CInt(Val("&h" + Mid(sHTML, 4, 2)))
    8.     lB = CInt(Val("&h" + Mid(sHTML, 6, 2)))
    9.  
    10.     HTMLToLong = RGB(lR, lG, lB)
    11. End Function

  2. #2
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    I don't know C, so this may only be valid in C++... hell, I'm not that good, so it may not be valid at all...
    PHP Code:
    long int HTMLToLong(charsHTML[]) {
        
    long int lRlong int lBlong int lG;
        
        
    lR = (int)...
        
    lG = (int)...
        
    lB = (int)...

        return 
    lR lG 256 lB 65536;


    I'm not sure how to do those middle parts, so I'll let a 'guru' fill those out, sorry.
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  3. #3
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    There is an RGB macro availible.

    Code:
    #define RGB(r, g ,b)  ((DWORD) (((BYTE) (r) | \ 
        ((WORD) (g) << 8)) | \ 
        (((DWORD) (BYTE) (b)) << 16)))
    From MSDN.

    As for the hex code to int... this should work:
    Code:
    char hex_byte_string_to_byte(char* code)
    {
     int val[i];
    
     for(int i = 0; i < 2; ++i) 
     {
      if((code[i] >= '0') && (code[i] <= '9'))
      {
       val[i] = code[i] - '0';
      }
      else
      {
       val[i] = code[i] - 'A' + 10;
      }
     }
     return val[0] << 4 + val[1];
    }
    Seems correct, but I didnt test it.

    Z.

  4. #4
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    As I was taking a walk today, I realised that C has bitshifts in it. D'oh! So I was going to let you know, but of course Zaei beat me to it
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    There goes: converts any #xxxxxx string to a COLORREF:
    Code:
    #include <string>
    #include <sstream>
    
    COLORREF ReadHtmlColor(const string& str)
    {
      istringstream iss(str);
      iss.ignore(1);
      int r, g, b;
      iss >> setbase(16) >> r >> g >> b;
      return RGB(r, g, b);
    }
    Should work.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Note however that HTML colors can also be in the format #xxx, where each color component is represented by a single hex digit. It's equivalent to writing #x0x0x0. Here's a function that can read this too.

    Oh, and I noticed my function won't work, here's a corrected and complete version.
    Code:
    #include <string>
    #include <sstream>
    #include <iomanip>
    using namespace std;
    
    COLORREF ReadHtmlColor(const string& str)
    {
      istringstream iss;
      int r, g, b;
      if(str.length() == 4) {
        iss.str((str.substr(1,1)));
        iss >> setbase(16) >> r;
        r <<= 8;
        iss.str((str.substr(2,1)));
        iss >> setbase(16) >> g;
        g <<= 8;
        iss.str((str.substr(3,1)));
        iss >> setbase(16) >> b;
        b <<= 8;
      } else if(str.length() == 7) {
        iss.str((str.substr(1,2)));
        iss >> setbase(16) >> r;
        iss.str((str.substr(3,2)));
        iss >> setbase(16) >> g;
        iss.str((str.substr(5,2)));
        iss >> setbase(16) >> b;
      } else return 0;
      return RGB(r,g,b);
    }
    There's probably a more beautiful method.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Originally posted by CornedBee
    Should work.
    I use this line all the time =).

    My function does work (just tested it), except you need to change "int val[i]" to "int val[2]".

    Z.

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