|
30 | 30 | class Color {
|
31 | 31 | public:
|
32 | 32 | Color()
|
33 |
| - : myR(0), myG(0), myB(0) {} |
| 33 | + : myR(0), myG(0), myB(0) {} |
34 | 34 | Color(float r, float g, float b)
|
35 |
| - : myR(r), myG(g), myB(b) {} |
| 35 | + : myR(r), myG(g), myB(b) {} |
36 | 36 | Color(uint32 val)
|
37 | 37 | {
|
38 |
| - myB = (val & 0xFF) / 255.0F; val >>= 8; |
39 |
| - myG = (val & 0xFF) / 255.0F; val >>= 8; |
40 |
| - myR = (val & 0xFF) / 255.0F; |
| 38 | + myB = (val & 0xFF) / 255.0F; val >>= 8; |
| 39 | + myG = (val & 0xFF) / 255.0F; val >>= 8; |
| 40 | + myR = (val & 0xFF) / 255.0F; |
41 | 41 | }
|
42 | 42 |
|
43 |
| - uint32 toInt32() const |
44 |
| - { |
45 |
| - return 0xFF000000 | |
46 |
| - (ftoc(myB) << 16) | |
47 |
| - (ftoc(myG) << 8) | |
48 |
| - (ftoc(myR)); |
49 |
| - } |
50 |
| - |
51 |
| - float luminance() const |
52 |
| - { |
53 |
| - return 0.3*myR + 0.6*myG + 0.1*myB; |
54 |
| - } |
55 |
| - |
56 |
| - Color operator+(const Color &rhs) const |
57 |
| - { |
58 |
| - return Color( |
59 |
| - myR+rhs.myR, |
60 |
| - myG+rhs.myG, |
61 |
| - myB+rhs.myB); |
62 |
| - } |
63 |
| - Color operator*(float scale) const |
64 |
| - { |
65 |
| - return Color(myR*scale, myG*scale, myB*scale); |
66 |
| - } |
67 |
| - Color lerp(const Color &rhs, float bias) const |
68 |
| - { |
69 |
| - QColor c1(QColor(toInt32()).toHsv()); |
70 |
| - QColor c2(QColor(rhs.toInt32()).toHsv()); |
71 |
| - |
72 |
| - // Lerp in HSV space |
73 |
| - float h = lerpHue((float)c1.hsvHueF(), |
74 |
| - (float)c2.hsvHueF(), bias); |
75 |
| - float s = SYSlerp((float)c1.hsvSaturationF(), |
76 |
| - (float)c2.hsvSaturationF(), bias); |
77 |
| - float l = SYSlerp((float)c1.lightnessF(), |
78 |
| - (float)c2.lightnessF(), bias); |
79 |
| - |
80 |
| - QColor c3(QColor::fromHsvF(h, s, l)); |
81 |
| - |
82 |
| - return Color(c3.redF(), c3.greenF(), c3.blueF()); |
83 |
| - } |
84 |
| - |
85 |
| - void fromHSV(float h, float s, float v) |
86 |
| - { |
87 |
| - QColor c(QColor::fromHsvF(h, s, v)); |
88 |
| - |
89 |
| - *this = Color(c.redF(), c.greenF(), c.blueF()); |
90 |
| - } |
| 43 | + uint32 toInt32() const |
| 44 | + { |
| 45 | + return 0xFF000000 | |
| 46 | + (ftoc(myB) << 16) | |
| 47 | + (ftoc(myG) << 8) | |
| 48 | + (ftoc(myR)); |
| 49 | + } |
| 50 | + |
| 51 | + float luminance() const |
| 52 | + { |
| 53 | + return 0.3*myR + 0.6*myG + 0.1*myB; |
| 54 | + } |
| 55 | + |
| 56 | + Color operator+(const Color &rhs) const |
| 57 | + { |
| 58 | + return Color( |
| 59 | + myR+rhs.myR, |
| 60 | + myG+rhs.myG, |
| 61 | + myB+rhs.myB); |
| 62 | + } |
| 63 | + Color operator*(float scale) const |
| 64 | + { |
| 65 | + return Color(myR*scale, myG*scale, myB*scale); |
| 66 | + } |
| 67 | + Color lerp(const Color &rhs, float bias) const |
| 68 | + { |
| 69 | + QColor c1(QColor(toInt32()).toHsv()); |
| 70 | + QColor c2(QColor(rhs.toInt32()).toHsv()); |
| 71 | + |
| 72 | + // Lerp in HSV space |
| 73 | + float h = lerpHue((float)c1.hsvHueF(), |
| 74 | + (float)c2.hsvHueF(), bias); |
| 75 | + float s = SYSlerp((float)c1.hsvSaturationF(), |
| 76 | + (float)c2.hsvSaturationF(), bias); |
| 77 | + float l = SYSlerp((float)c1.lightnessF(), |
| 78 | + (float)c2.lightnessF(), bias); |
| 79 | + |
| 80 | + QColor c3(QColor::fromHsvF(h, s, l)); |
| 81 | + |
| 82 | + return Color(c3.redF(), c3.greenF(), c3.blueF()); |
| 83 | + } |
| 84 | + |
| 85 | + void fromHSV(float h, float s, float v) |
| 86 | + { |
| 87 | + QColor c(QColor::fromHsvF(h, s, v)); |
| 88 | + |
| 89 | + *this = Color(c.redF(), c.greenF(), c.blueF()); |
| 90 | + } |
91 | 91 |
|
92 | 92 | private:
|
93 | 93 | static uint32 ftoc(float v)
|
94 |
| - { |
95 |
| - v *= 255.0F; |
96 |
| - return SYSclamp((int)v, 0, 255); |
97 |
| - } |
| 94 | + { |
| 95 | + v *= 255.0F; |
| 96 | + return SYSclamp((int)v, 0, 255); |
| 97 | + } |
98 | 98 |
|
99 | 99 | static float lerpHue(float h1, float h2, float bias)
|
100 |
| - { |
101 |
| - h1 = SYSclamp(h1, 0.0F, 1.0F); |
102 |
| - h2 = SYSclamp(h2, 0.0F, 1.0F); |
103 |
| - if (h2 > h1 + 0.5F) |
104 |
| - h1 += 1; |
105 |
| - else if (h1 > h2 + 0.5F) |
106 |
| - h2 += 1; |
| 100 | + { |
| 101 | + h1 = SYSclamp(h1, 0.0F, 1.0F); |
| 102 | + h2 = SYSclamp(h2, 0.0F, 1.0F); |
| 103 | + if (h2 > h1 + 0.5F) |
| 104 | + h1 += 1; |
| 105 | + else if (h1 > h2 + 0.5F) |
| 106 | + h2 += 1; |
107 | 107 |
|
108 |
| - float h = SYSlerp(h1, h2, bias); |
109 |
| - if (h > 1) h -= 1; |
110 |
| - return h; |
111 |
| - } |
| 108 | + float h = SYSlerp(h1, h2, bias); |
| 109 | + if (h > 1) h -= 1; |
| 110 | + return h; |
| 111 | + } |
112 | 112 |
|
113 | 113 |
|
114 | 114 | private:
|
115 |
| - float myR; |
116 |
| - float myG; |
117 |
| - float myB; |
| 115 | + float myR; |
| 116 | + float myG; |
| 117 | + float myB; |
118 | 118 | };
|
119 | 119 |
|
120 | 120 | #endif
|
0 commit comments