1
2 /*
3 * Copyright 2010 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9
10
11 #ifndef GrColor_DEFINED
12 #define GrColor_DEFINED
13
14 #include "GrTypes.h"
15 #include "SkColor.h"
16 #include "SkColorPriv.h"
17 #include "SkUnPreMultiply.h"
18
19 /**
20 * GrColor is 4 bytes for R, G, B, A, in a specific order defined below. Whether the color is
21 * premultiplied or not depends on the context in which it is being used.
22 */
23 typedef uint32_t GrColor;
24
25 // shift amount to assign a component to a GrColor int
26 // These shift values are chosen for compatibility with GL attrib arrays
27 // ES doesn't allow BGRA vertex attrib order so if they were not in this order
28 // we'd have to swizzle in shaders.
29 #ifdef SK_CPU_BENDIAN
30 #define GrColor_SHIFT_R 24
31 #define GrColor_SHIFT_G 16
32 #define GrColor_SHIFT_B 8
33 #define GrColor_SHIFT_A 0
34 #else
35 #define GrColor_SHIFT_R 0
36 #define GrColor_SHIFT_G 8
37 #define GrColor_SHIFT_B 16
38 #define GrColor_SHIFT_A 24
39 #endif
40
41 /**
42 * Pack 4 components (RGBA) into a GrColor int
43 */
GrColorPackRGBA(unsigned r,unsigned g,unsigned b,unsigned a)44 static inline GrColor GrColorPackRGBA(unsigned r, unsigned g, unsigned b, unsigned a) {
45 SkASSERT((uint8_t)r == r);
46 SkASSERT((uint8_t)g == g);
47 SkASSERT((uint8_t)b == b);
48 SkASSERT((uint8_t)a == a);
49 return (r << GrColor_SHIFT_R) |
50 (g << GrColor_SHIFT_G) |
51 (b << GrColor_SHIFT_B) |
52 (a << GrColor_SHIFT_A);
53 }
54
55 /**
56 * Packs a color with an alpha channel replicated across all four channels.
57 */
GrColorPackA4(unsigned a)58 static inline GrColor GrColorPackA4(unsigned a) {
59 SkASSERT((uint8_t)a == a);
60 return (a << GrColor_SHIFT_R) |
61 (a << GrColor_SHIFT_G) |
62 (a << GrColor_SHIFT_B) |
63 (a << GrColor_SHIFT_A);
64 }
65
66 // extract a component (byte) from a GrColor int
67
68 #define GrColorUnpackR(color) (((color) >> GrColor_SHIFT_R) & 0xFF)
69 #define GrColorUnpackG(color) (((color) >> GrColor_SHIFT_G) & 0xFF)
70 #define GrColorUnpackB(color) (((color) >> GrColor_SHIFT_B) & 0xFF)
71 #define GrColorUnpackA(color) (((color) >> GrColor_SHIFT_A) & 0xFF)
72
73 /**
74 * Since premultiplied means that alpha >= color, we construct a color with
75 * each component==255 and alpha == 0 to be "illegal"
76 */
77 #define GrColor_ILLEGAL (~(0xFF << GrColor_SHIFT_A))
78
79 #define GrColor_WHITE 0xFFFFFFFF
80 #define GrColor_TRANSPARENT_BLACK 0x0
81
82 /**
83 * Assert in debug builds that a GrColor is premultiplied.
84 */
GrColorIsPMAssert(GrColor SkDEBUGCODE (c))85 static inline void GrColorIsPMAssert(GrColor SkDEBUGCODE(c)) {
86 #ifdef SK_DEBUG
87 unsigned a = GrColorUnpackA(c);
88 unsigned r = GrColorUnpackR(c);
89 unsigned g = GrColorUnpackG(c);
90 unsigned b = GrColorUnpackB(c);
91
92 SkASSERT(r <= a);
93 SkASSERT(g <= a);
94 SkASSERT(b <= a);
95 #endif
96 }
97
98 /** Inverts each color channel. */
GrInvertColor(GrColor c)99 static inline GrColor GrInvertColor(GrColor c) {
100 U8CPU a = GrColorUnpackA(c);
101 U8CPU r = GrColorUnpackR(c);
102 U8CPU g = GrColorUnpackG(c);
103 U8CPU b = GrColorUnpackB(c);
104 return GrColorPackRGBA(0xff - r, 0xff - g, 0xff - b, 0xff - a);
105 }
106
GrColorMul(GrColor c0,GrColor c1)107 static inline GrColor GrColorMul(GrColor c0, GrColor c1) {
108 U8CPU r = SkMulDiv255Round(GrColorUnpackR(c0), GrColorUnpackR(c1));
109 U8CPU g = SkMulDiv255Round(GrColorUnpackG(c0), GrColorUnpackG(c1));
110 U8CPU b = SkMulDiv255Round(GrColorUnpackB(c0), GrColorUnpackB(c1));
111 U8CPU a = SkMulDiv255Round(GrColorUnpackA(c0), GrColorUnpackA(c1));
112 return GrColorPackRGBA(r, g, b, a);
113 }
114
GrColorSatAdd(GrColor c0,GrColor c1)115 static inline GrColor GrColorSatAdd(GrColor c0, GrColor c1) {
116 unsigned r = SkTMin<unsigned>(GrColorUnpackR(c0) + GrColorUnpackR(c1), 0xff);
117 unsigned g = SkTMin<unsigned>(GrColorUnpackG(c0) + GrColorUnpackG(c1), 0xff);
118 unsigned b = SkTMin<unsigned>(GrColorUnpackB(c0) + GrColorUnpackB(c1), 0xff);
119 unsigned a = SkTMin<unsigned>(GrColorUnpackA(c0) + GrColorUnpackA(c1), 0xff);
120 return GrColorPackRGBA(r, g, b, a);
121 }
122
123 /** Converts a GrColor to an rgba array of GrGLfloat */
GrColorToRGBAFloat(GrColor color,float rgba[4])124 static inline void GrColorToRGBAFloat(GrColor color, float rgba[4]) {
125 static const float ONE_OVER_255 = 1.f / 255.f;
126 rgba[0] = GrColorUnpackR(color) * ONE_OVER_255;
127 rgba[1] = GrColorUnpackG(color) * ONE_OVER_255;
128 rgba[2] = GrColorUnpackB(color) * ONE_OVER_255;
129 rgba[3] = GrColorUnpackA(color) * ONE_OVER_255;
130 }
131
132 /** Normalizes and coverts an uint8_t to a float. [0, 255] -> [0.0, 1.0] */
GrNormalizeByteToFloat(uint8_t value)133 static inline float GrNormalizeByteToFloat(uint8_t value) {
134 static const float ONE_OVER_255 = 1.f / 255.f;
135 return value * ONE_OVER_255;
136 }
137
138 /** Determines whether the color is opaque or not. */
GrColorIsOpaque(GrColor color)139 static inline bool GrColorIsOpaque(GrColor color) {
140 return (color & (0xFFU << GrColor_SHIFT_A)) == (0xFFU << GrColor_SHIFT_A);
141 }
142
GrPremulColor(GrColor color)143 static inline GrColor GrPremulColor(GrColor color) {
144 unsigned r = GrColorUnpackR(color);
145 unsigned g = GrColorUnpackG(color);
146 unsigned b = GrColorUnpackB(color);
147 unsigned a = GrColorUnpackA(color);
148 return GrColorPackRGBA(SkMulDiv255Round(r, a),
149 SkMulDiv255Round(g, a),
150 SkMulDiv255Round(b, a),
151 a);
152 }
153
154 /** Returns an unpremuled version of the GrColor. */
GrUnpremulColor(GrColor color)155 static inline GrColor GrUnpremulColor(GrColor color) {
156 GrColorIsPMAssert(color);
157 unsigned r = GrColorUnpackR(color);
158 unsigned g = GrColorUnpackG(color);
159 unsigned b = GrColorUnpackB(color);
160 unsigned a = GrColorUnpackA(color);
161 SkPMColor colorPM = SkPackARGB32(a, r, g, b);
162 SkColor colorUPM = SkUnPreMultiply::PMColorToColor(colorPM);
163
164 r = SkColorGetR(colorUPM);
165 g = SkColorGetG(colorUPM);
166 b = SkColorGetB(colorUPM);
167 a = SkColorGetA(colorUPM);
168
169 return GrColorPackRGBA(r, g, b, a);
170 }
171
172
173 /**
174 * Similarly, GrColor4f is 4 floats for R, G, B, A, in that order. And like GrColor, whether
175 * the color is premultiplied or not depends on the context.
176 */
177 struct GrColor4f {
178 float fRGBA[4];
179
GrColor4fGrColor4f180 GrColor4f() {}
GrColor4fGrColor4f181 GrColor4f(float r, float g, float b, float a) {
182 fRGBA[0] = r;
183 fRGBA[1] = g;
184 fRGBA[2] = b;
185 fRGBA[3] = a;
186 }
187
188 enum Illegal_Constructor {
189 kIllegalConstructor
190 };
GrColor4fGrColor4f191 GrColor4f(Illegal_Constructor) {
192 fRGBA[0] = SK_FloatNaN;
193 fRGBA[1] = SK_FloatNaN;
194 fRGBA[2] = SK_FloatNaN;
195 fRGBA[3] = SK_FloatNaN;
196 }
197
OpaqueWhiteGrColor4f198 static GrColor4f OpaqueWhite() {
199 return GrColor4f(1.0f, 1.0f, 1.0f, 1.0f);
200 }
201
TransparentBlackGrColor4f202 static GrColor4f TransparentBlack() {
203 return GrColor4f(0.0f, 0.0f, 0.0f, 0.0f);
204 }
205
FromGrColorGrColor4f206 static GrColor4f FromGrColor(GrColor color) {
207 GrColor4f result;
208 GrColorToRGBAFloat(color, result.fRGBA);
209 return result;
210 }
211
FromSkColor4fGrColor4f212 static GrColor4f FromSkColor4f(const SkColor4f& color) {
213 return GrColor4f(color.fR, color.fG, color.fB, color.fA);
214 }
215
modulateGrColor4f216 GrColor4f modulate(const GrColor4f& x) const {
217 return GrColor4f(fRGBA[0] * x.fRGBA[0],
218 fRGBA[1] * x.fRGBA[1],
219 fRGBA[2] * x.fRGBA[2],
220 fRGBA[3] * x.fRGBA[3]);
221 }
222
mulByScalarGrColor4f223 GrColor4f mulByScalar(float x) const {
224 return GrColor4f(fRGBA[0] * x, fRGBA[1] * x, fRGBA[2] * x, fRGBA[3] * x);
225 }
226
227 bool operator==(const GrColor4f& other) const {
228 return
229 fRGBA[0] == other.fRGBA[0] &&
230 fRGBA[1] == other.fRGBA[1] &&
231 fRGBA[2] == other.fRGBA[2] &&
232 fRGBA[3] == other.fRGBA[3];
233 }
234 bool operator!=(const GrColor4f& other) const {
235 return !(*this == other);
236 }
237
toGrColorGrColor4f238 GrColor toGrColor() const {
239 return GrColorPackRGBA(
240 SkTPin<unsigned>(static_cast<unsigned>(fRGBA[0] * 255.0f + 0.5f), 0, 255),
241 SkTPin<unsigned>(static_cast<unsigned>(fRGBA[1] * 255.0f + 0.5f), 0, 255),
242 SkTPin<unsigned>(static_cast<unsigned>(fRGBA[2] * 255.0f + 0.5f), 0, 255),
243 SkTPin<unsigned>(static_cast<unsigned>(fRGBA[3] * 255.0f + 0.5f), 0, 255));
244 }
245
toSkColor4fGrColor4f246 SkColor4f toSkColor4f() const {
247 return SkColor4f { fRGBA[0], fRGBA[1], fRGBA[2], fRGBA[3] };
248 }
249
opaqueGrColor4f250 GrColor4f opaque() const {
251 return GrColor4f(fRGBA[0], fRGBA[1], fRGBA[2], 1.0f);
252 }
253
isOpaqueGrColor4f254 bool isOpaque() const {
255 return fRGBA[3] >= 1.f; // just in case precision causes a superopaque value.
256 }
257
premulGrColor4f258 GrColor4f premul() const {
259 float a = fRGBA[3];
260 return GrColor4f(fRGBA[0] * a, fRGBA[1] * a, fRGBA[2] * a, a);
261 }
262
unpremulGrColor4f263 GrColor4f unpremul() const {
264 float a = fRGBA[3];
265 if (a <= 0.0f) {
266 return GrColor4f(0.0f, 0.0f, 0.0f, 0.0f);
267 }
268 float invAlpha = 1.0f / a;
269 return GrColor4f(fRGBA[0] * invAlpha, fRGBA[1] * invAlpha, fRGBA[2] * invAlpha, a);
270 }
271 };
272
273 /**
274 * Flags used for bitfields of color components. They are defined so that the bit order reflects the
275 * GrColor shift order.
276 */
277 enum GrColorComponentFlags {
278 kR_GrColorComponentFlag = 1 << (GrColor_SHIFT_R / 8),
279 kG_GrColorComponentFlag = 1 << (GrColor_SHIFT_G / 8),
280 kB_GrColorComponentFlag = 1 << (GrColor_SHIFT_B / 8),
281 kA_GrColorComponentFlag = 1 << (GrColor_SHIFT_A / 8),
282
283 kNone_GrColorComponentFlags = 0,
284
285 kRGB_GrColorComponentFlags = (kR_GrColorComponentFlag | kG_GrColorComponentFlag |
286 kB_GrColorComponentFlag),
287
288 kRGBA_GrColorComponentFlags = (kR_GrColorComponentFlag | kG_GrColorComponentFlag |
289 kB_GrColorComponentFlag | kA_GrColorComponentFlag)
290 };
291
292 GR_MAKE_BITFIELD_OPS(GrColorComponentFlags)
293
294 #endif
295