bitetti

Simple struct to convert hexa to rgba color

Jul 25th, 2012
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.96 KB | None | 0 0
  1.     static struct _hex
  2.     {
  3.         unsigned int r;
  4.         unsigned int g;
  5.         unsigned int b;
  6.         unsigned int a;
  7.  
  8.         _hex() {};
  9.  
  10.         _hex(const char* ch)
  11.         {
  12.             parse(ch);
  13.         };
  14.  
  15.         unsigned int getHex(char _ch)
  16.         {
  17.             unsigned int v = 0;
  18.             switch(_ch)
  19.             {
  20.                 case '0': v=0; break;
  21.                 case '1': v=1; break;
  22.                 case '2': v=2; break;
  23.                 case '3': v=3; break;
  24.                 case '4': v=4; break;
  25.                 case '5': v=5; break;
  26.                 case '6': v=6; break;
  27.                 case '7': v=7; break;
  28.                 case '8': v=8; break;
  29.                 case '9': v=9; break;
  30.                 case 'a': case 'A': v=10; break;
  31.                 case 'b': case 'B': v=11; break;
  32.                 case 'c': case 'C': v=12; break;
  33.                 case 'd': case 'D': v=13; break;
  34.                 case 'e': case 'E': v=14; break;
  35.                 case 'f': case 'F': v=15; break;
  36.             }
  37.             return v;
  38.         };
  39.  
  40.         void parse (const char* _chb)
  41.         {
  42.             char* _ch = (char*) _chb;
  43.             bool simple = false;
  44.             if (*_ch=='#') _ch++;
  45.             int len = strlen(_ch);
  46.             if (len<5) simple = true;
  47.             unsigned int v = 0;
  48.             v = getHex( *_ch++ );
  49.             if (simple) v = v | v*16; else v = v*16 | getHex(*_ch++);
  50.             r = v;
  51.             v = getHex( *_ch++ );
  52.             if (simple) v = v | v*16; else v = v*16 | getHex(*_ch++);
  53.             g = v;
  54.             v = getHex( *_ch++ );
  55.             if (simple) v = v | v*16; else v = v*16 | getHex(*_ch++);
  56.             b = v;
  57.             v = getHex( *_ch++ );
  58.             if (simple) v = v | v*16; else v = v*16 | getHex(*_ch++);
  59.             a = v;
  60.             if (len<8 || simple)
  61.                 if (len<4)
  62.                     a = 255;
  63.         };
  64.  
  65.     } hex( htmlCor.c_str() );
Advertisement
Add Comment
Please, Sign In to add comment