color.h

changeset 29
55406ce7446e
parent 28
208adc847377
child 30
31ff9aabd506
equal deleted inserted replaced
28:208adc847377 29:55406ce7446e
1 #ifndef __COLOR_H__
2 #define __COLOR_H__
3
4 #include <stdint.h>
5
6 #ifdef QT_VERSION
7 #include <QColor>
8 #endif // QT_VERSION
9
10 class color {
11 public:
12 unsigned char r, g, b;
13
14 color () {}
15 color (const char* other) {
16 parseFromString (str (other));
17 }
18
19 bool parseFromString (str in) {
20 #ifdef QT_VERSION
21 // Use Qt's method for a quick color check. Handles
22 // named colors, too.
23 QColor col (in.chars ());
24 if (col.isValid()) {
25 r = col.red ();
26 g = col.green ();
27 b = col.blue ();
28 return true;
29 }
30 #else
31 if (in[0] == '#') {
32 // Hex code
33
34 if (~in != 4 && ~in != 7)
35 return false; // bad length
36
37 printf ("%s\n", in.chars ());
38
39 if (~in == 4) {
40 in.format ("#%c%c%c%c%c%c",
41 in[1], in[1],
42 in[2], in[2],
43 in[3], in[3]);
44 }
45
46 printf ("%s\n", in.chars ());
47
48 if (sscanf (in.chars(), "#%2hhx%2hhx%2hhx", &r, &g, &b))
49 return true;
50 }
51 #endif // QT_VERSION
52
53 return false;
54 }
55
56 str toString () {
57 str val;
58 val.format ("#%.2X%.2X%.2X", r, g, b);
59 return val;
60 }
61
62 char* chars() {
63 return toString ().chars ();
64 }
65
66 operator str () {
67 return toString ();
68 }
69
70 void operator= (const str other) {
71 parseFromString (other);
72 }
73 };
74
75 #endif // __COLOR_H__

mercurial