1 /*
2 * Copyright 2006 The Android Open Source Project
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #ifndef SkColor_DEFINED
9 #define SkColor_DEFINED
10
11 #include "include/core/SkAlphaType.h"
12 #include "include/core/SkScalar.h"
13 #include "include/core/SkTypes.h"
14 #include "include/private/base/SkCPUTypes.h"
15
16 #include <array>
17 #include <cstdint>
18
19 /** \file SkColor.h
20
21 Types, consts, functions, and macros for colors.
22 */
23
24 /** 8-bit type for an alpha value. 255 is 100% opaque, zero is 100% transparent.
25 */
26 typedef uint8_t SkAlpha;
27
28 /** 32-bit ARGB color value, unpremultiplied. Color components are always in
29 a known order. This is different from SkPMColor, which has its bytes in a configuration
30 dependent order, to match the format of kBGRA_8888_SkColorType bitmaps. SkColor
31 is the type used to specify colors in SkPaint and in gradients.
32
33 Color that is premultiplied has the same component values as color
34 that is unpremultiplied if alpha is 255, fully opaque, although may have the
35 component values in a different order.
36 */
37 typedef uint32_t SkColor;
38
39 /** Returns color value from 8-bit component values. Asserts if SK_DEBUG is defined
40 if a, r, g, or b exceed 255. Since color is unpremultiplied, a may be smaller
41 than the largest of r, g, and b.
42
43 @param a amount of alpha, from fully transparent (0) to fully opaque (255)
44 @param r amount of red, from no red (0) to full red (255)
45 @param g amount of green, from no green (0) to full green (255)
46 @param b amount of blue, from no blue (0) to full blue (255)
47 @return color and alpha, unpremultiplied
48 */
SkColorSetARGB(U8CPU a,U8CPU r,U8CPU g,U8CPU b)49 static constexpr inline SkColor SkColorSetARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) {
50 return SkASSERT(a <= 255 && r <= 255 && g <= 255 && b <= 255),
51 (a << 24) | (r << 16) | (g << 8) | (b << 0);
52 }
53
54 /** Returns color value from 8-bit component values, with alpha set
55 fully opaque to 255.
56 */
57 #define SkColorSetRGB(r, g, b) SkColorSetARGB(0xFF, r, g, b)
58
59 /** Returns alpha byte from color value.
60 */
61 #define SkColorGetA(color) (((color) >> 24) & 0xFF)
62
63 /** Returns red component of color, from zero to 255.
64 */
65 #define SkColorGetR(color) (((color) >> 16) & 0xFF)
66
67 /** Returns green component of color, from zero to 255.
68 */
69 #define SkColorGetG(color) (((color) >> 8) & 0xFF)
70
71 /** Returns blue component of color, from zero to 255.
72 */
73 #define SkColorGetB(color) (((color) >> 0) & 0xFF)
74
75 /** Returns unpremultiplied color with red, blue, and green set from c; and alpha set
76 from a. Alpha component of c is ignored and is replaced by a in result.
77
78 @param c packed RGB, eight bits per component
79 @param a alpha: transparent at zero, fully opaque at 255
80 @return color with transparency
81 */
SkColorSetA(SkColor c,U8CPU a)82 static constexpr inline SkColor SK_WARN_UNUSED_RESULT SkColorSetA(SkColor c, U8CPU a) {
83 return (c & 0x00FFFFFF) | (a << 24);
84 }
85
86 /** Represents fully transparent SkAlpha value. SkAlpha ranges from zero,
87 fully transparent; to 255, fully opaque.
88 */
89 constexpr SkAlpha SK_AlphaTRANSPARENT = 0x00;
90
91 /** Represents fully opaque SkAlpha value. SkAlpha ranges from zero,
92 fully transparent; to 255, fully opaque.
93 */
94 constexpr SkAlpha SK_AlphaOPAQUE = 0xFF;
95
96 /** Represents fully transparent SkColor. May be used to initialize a destination
97 containing a mask or a non-rectangular image.
98 */
99 constexpr SkColor SK_ColorTRANSPARENT = SkColorSetARGB(0x00, 0x00, 0x00, 0x00);
100
101 /** Represents fully opaque black.
102 */
103 constexpr SkColor SK_ColorBLACK = SkColorSetARGB(0xFF, 0x00, 0x00, 0x00);
104
105 /** Represents fully opaque dark gray.
106 Note that SVG dark gray is equivalent to 0xFFA9A9A9.
107 */
108 constexpr SkColor SK_ColorDKGRAY = SkColorSetARGB(0xFF, 0x44, 0x44, 0x44);
109
110 /** Represents fully opaque gray.
111 Note that HTML gray is equivalent to 0xFF808080.
112 */
113 constexpr SkColor SK_ColorGRAY = SkColorSetARGB(0xFF, 0x88, 0x88, 0x88);
114
115 /** Represents fully opaque light gray. HTML silver is equivalent to 0xFFC0C0C0.
116 Note that SVG light gray is equivalent to 0xFFD3D3D3.
117 */
118 constexpr SkColor SK_ColorLTGRAY = SkColorSetARGB(0xFF, 0xCC, 0xCC, 0xCC);
119
120 /** Represents fully opaque white.
121 */
122 constexpr SkColor SK_ColorWHITE = SkColorSetARGB(0xFF, 0xFF, 0xFF, 0xFF);
123
124 /** Represents fully opaque red.
125 */
126 constexpr SkColor SK_ColorRED = SkColorSetARGB(0xFF, 0xFF, 0x00, 0x00);
127
128 /** Represents fully opaque green. HTML lime is equivalent.
129 Note that HTML green is equivalent to 0xFF008000.
130 */
131 constexpr SkColor SK_ColorGREEN = SkColorSetARGB(0xFF, 0x00, 0xFF, 0x00);
132
133 /** Represents fully opaque blue.
134 */
135 constexpr SkColor SK_ColorBLUE = SkColorSetARGB(0xFF, 0x00, 0x00, 0xFF);
136
137 /** Represents fully opaque yellow.
138 */
139 constexpr SkColor SK_ColorYELLOW = SkColorSetARGB(0xFF, 0xFF, 0xFF, 0x00);
140
141 /** Represents fully opaque cyan. HTML aqua is equivalent.
142 */
143 constexpr SkColor SK_ColorCYAN = SkColorSetARGB(0xFF, 0x00, 0xFF, 0xFF);
144
145 /** Represents fully opaque magenta. HTML fuchsia is equivalent.
146 */
147 constexpr SkColor SK_ColorMAGENTA = SkColorSetARGB(0xFF, 0xFF, 0x00, 0xFF);
148
149 /** Converts RGB to its HSV components.
150 hsv[0] contains hsv hue, a value from zero to less than 360.
151 hsv[1] contains hsv saturation, a value from zero to one.
152 hsv[2] contains hsv value, a value from zero to one.
153
154 @param red red component value from zero to 255
155 @param green green component value from zero to 255
156 @param blue blue component value from zero to 255
157 @param hsv three element array which holds the resulting HSV components
158 */
159 SK_API void SkRGBToHSV(U8CPU red, U8CPU green, U8CPU blue, SkScalar hsv[3]);
160
161 /** Converts ARGB to its HSV components. Alpha in ARGB is ignored.
162 hsv[0] contains hsv hue, and is assigned a value from zero to less than 360.
163 hsv[1] contains hsv saturation, a value from zero to one.
164 hsv[2] contains hsv value, a value from zero to one.
165
166 @param color ARGB color to convert
167 @param hsv three element array which holds the resulting HSV components
168 */
SkColorToHSV(SkColor color,SkScalar hsv[3])169 static inline void SkColorToHSV(SkColor color, SkScalar hsv[3]) {
170 SkRGBToHSV(SkColorGetR(color), SkColorGetG(color), SkColorGetB(color), hsv);
171 }
172
173 /** Converts HSV components to an ARGB color. Alpha is passed through unchanged.
174 hsv[0] represents hsv hue, an angle from zero to less than 360.
175 hsv[1] represents hsv saturation, and varies from zero to one.
176 hsv[2] represents hsv value, and varies from zero to one.
177
178 Out of range hsv values are pinned.
179
180 @param alpha alpha component of the returned ARGB color
181 @param hsv three element array which holds the input HSV components
182 @return ARGB equivalent to HSV
183 */
184 SK_API SkColor SkHSVToColor(U8CPU alpha, const SkScalar hsv[3]);
185
186 /** Converts HSV components to an ARGB color. Alpha is set to 255.
187 hsv[0] represents hsv hue, an angle from zero to less than 360.
188 hsv[1] represents hsv saturation, and varies from zero to one.
189 hsv[2] represents hsv value, and varies from zero to one.
190
191 Out of range hsv values are pinned.
192
193 @param hsv three element array which holds the input HSV components
194 @return RGB equivalent to HSV
195 */
SkHSVToColor(const SkScalar hsv[3])196 static inline SkColor SkHSVToColor(const SkScalar hsv[3]) {
197 return SkHSVToColor(0xFF, hsv);
198 }
199
200 /** 32-bit ARGB color value, premultiplied. The byte order for this value is
201 configuration dependent, matching the format of kBGRA_8888_SkColorType bitmaps.
202 This is different from SkColor, which is unpremultiplied, and is always in the
203 same byte order.
204 */
205 typedef uint32_t SkPMColor;
206
207 /** Returns a SkPMColor value from unpremultiplied 8-bit component values.
208
209 @param a amount of alpha, from fully transparent (0) to fully opaque (255)
210 @param r amount of red, from no red (0) to full red (255)
211 @param g amount of green, from no green (0) to full green (255)
212 @param b amount of blue, from no blue (0) to full blue (255)
213 @return premultiplied color
214 */
215 SK_API SkPMColor SkPreMultiplyARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b);
216
217 /** Returns pmcolor closest to color c. Multiplies c RGB components by the c alpha,
218 and arranges the bytes to match the format of kN32_SkColorType.
219
220 @param c unpremultiplied ARGB color
221 @return premultiplied color
222 */
223 SK_API SkPMColor SkPreMultiplyColor(SkColor c);
224
225 /** \enum SkColorChannel
226 Describes different color channels one can manipulate
227 */
228 enum class SkColorChannel {
229 kR, // the red channel
230 kG, // the green channel
231 kB, // the blue channel
232 kA, // the alpha channel
233
234 kLastEnum = kA,
235 };
236
237 /** Used to represent the channels available in a color type or texture format as a mask. */
238 enum SkColorChannelFlag : uint32_t {
239 kRed_SkColorChannelFlag = 1 << static_cast<uint32_t>(SkColorChannel::kR),
240 kGreen_SkColorChannelFlag = 1 << static_cast<uint32_t>(SkColorChannel::kG),
241 kBlue_SkColorChannelFlag = 1 << static_cast<uint32_t>(SkColorChannel::kB),
242 kAlpha_SkColorChannelFlag = 1 << static_cast<uint32_t>(SkColorChannel::kA),
243 kGray_SkColorChannelFlag = 0x10,
244 // Convenience values
245 kGrayAlpha_SkColorChannelFlags = kGray_SkColorChannelFlag | kAlpha_SkColorChannelFlag,
246 kRG_SkColorChannelFlags = kRed_SkColorChannelFlag | kGreen_SkColorChannelFlag,
247 kRGB_SkColorChannelFlags = kRG_SkColorChannelFlags | kBlue_SkColorChannelFlag,
248 kRGBA_SkColorChannelFlags = kRGB_SkColorChannelFlags | kAlpha_SkColorChannelFlag,
249 };
250 static_assert(0 == (kGray_SkColorChannelFlag & kRGBA_SkColorChannelFlags), "bitfield conflict");
251
252 /** \struct SkRGBA4f
253 RGBA color value, holding four floating point components. Color components are always in
254 a known order. kAT determines if the SkRGBA4f's R, G, and B components are premultiplied
255 by alpha or not.
256
257 Skia's public API always uses unpremultiplied colors, which can be stored as
258 SkRGBA4f<kUnpremul_SkAlphaType>. For convenience, this type can also be referred to
259 as SkColor4f.
260 */
261 template <SkAlphaType kAT>
262 struct SkRGBA4f {
263 float fR; //!< red component
264 float fG; //!< green component
265 float fB; //!< blue component
266 float fA; //!< alpha component
267
268 /** Compares SkRGBA4f with other, and returns true if all components are equal.
269
270 @param other SkRGBA4f to compare
271 @return true if SkRGBA4f equals other
272 */
273 bool operator==(const SkRGBA4f& other) const {
274 return fA == other.fA && fR == other.fR && fG == other.fG && fB == other.fB;
275 }
276
277 /** Compares SkRGBA4f with other, and returns true if not all components are equal.
278
279 @param other SkRGBA4f to compare
280 @return true if SkRGBA4f is not equal to other
281 */
282 bool operator!=(const SkRGBA4f& other) const {
283 return !(*this == other);
284 }
285
286 /** Returns SkRGBA4f multiplied by scale.
287
288 @param scale value to multiply by
289 @return SkRGBA4f as (fR * scale, fG * scale, fB * scale, fA * scale)
290 */
291 SkRGBA4f operator*(float scale) const {
292 return { fR * scale, fG * scale, fB * scale, fA * scale };
293 }
294
295 /** Returns SkRGBA4f multiplied component-wise by scale.
296
297 @param scale SkRGBA4f to multiply by
298 @return SkRGBA4f as (fR * scale.fR, fG * scale.fG, fB * scale.fB, fA * scale.fA)
299 */
300 SkRGBA4f operator*(const SkRGBA4f& scale) const {
301 return { fR * scale.fR, fG * scale.fG, fB * scale.fB, fA * scale.fA };
302 }
303
304 /** Returns a pointer to components of SkRGBA4f, for array access.
305
306 @return pointer to array [fR, fG, fB, fA]
307 */
vecSkRGBA4f308 const float* vec() const { return &fR; }
309
310 /** Returns a pointer to components of SkRGBA4f, for array access.
311
312 @return pointer to array [fR, fG, fB, fA]
313 */
vecSkRGBA4f314 float* vec() { return &fR; }
315
316 /** As a std::array<float, 4> */
arraySkRGBA4f317 std::array<float, 4> array() const { return {fR, fG, fB, fA}; }
318
319 /** Returns one component. Asserts if index is out of range and SK_DEBUG is defined.
320
321 @param index one of: 0 (fR), 1 (fG), 2 (fB), 3 (fA)
322 @return value corresponding to index
323 */
324 float operator[](int index) const {
325 SkASSERT(index >= 0 && index < 4);
326 return this->vec()[index];
327 }
328
329 /** Returns one component. Asserts if index is out of range and SK_DEBUG is defined.
330
331 @param index one of: 0 (fR), 1 (fG), 2 (fB), 3 (fA)
332 @return value corresponding to index
333 */
334 float& operator[](int index) {
335 SkASSERT(index >= 0 && index < 4);
336 return this->vec()[index];
337 }
338
339 /** Returns true if SkRGBA4f is an opaque color. Asserts if fA is out of range and
340 SK_DEBUG is defined.
341
342 @return true if SkRGBA4f is opaque
343 */
isOpaqueSkRGBA4f344 bool isOpaque() const {
345 SkASSERT(fA <= 1.0f && fA >= 0.0f);
346 return fA == 1.0f;
347 }
348
349 /** Returns true if all channels are in [0, 1]. */
fitsInBytesSkRGBA4f350 bool fitsInBytes() const {
351 SkASSERT(fA >= 0.0f && fA <= 1.0f);
352 return fR >= 0.0f && fR <= 1.0f &&
353 fG >= 0.0f && fG <= 1.0f &&
354 fB >= 0.0f && fB <= 1.0f;
355 }
356
357 /** Returns closest SkRGBA4f to SkColor. Only allowed if SkRGBA4f is unpremultiplied.
358
359 @param color Color with Alpha, red, blue, and green components
360 @return SkColor as SkRGBA4f
361
362 example: https://fiddle.skia.org/c/@RGBA4f_FromColor
363 */
364 static SkRGBA4f FromColor(SkColor color); // impl. depends on kAT
365
366 /** Returns closest SkColor to SkRGBA4f. Only allowed if SkRGBA4f is unpremultiplied.
367
368 @return color as SkColor
369
370 example: https://fiddle.skia.org/c/@RGBA4f_toSkColor
371 */
372 SkColor toSkColor() const; // impl. depends on kAT
373
374 /** Returns closest SkRGBA4f to SkPMColor. Only allowed if SkRGBA4f is premultiplied.
375
376 @return SkPMColor as SkRGBA4f
377 */
378 static SkRGBA4f FromPMColor(SkPMColor); // impl. depends on kAT
379
380 /** Returns SkRGBA4f premultiplied by alpha. Asserts at compile time if SkRGBA4f is
381 already premultiplied.
382
383 @return premultiplied color
384 */
premulSkRGBA4f385 SkRGBA4f<kPremul_SkAlphaType> premul() const {
386 static_assert(kAT == kUnpremul_SkAlphaType, "");
387 return { fR * fA, fG * fA, fB * fA, fA };
388 }
389
390 /** Returns SkRGBA4f unpremultiplied by alpha. Asserts at compile time if SkRGBA4f is
391 already unpremultiplied.
392
393 @return unpremultiplied color
394 */
unpremulSkRGBA4f395 SkRGBA4f<kUnpremul_SkAlphaType> unpremul() const {
396 static_assert(kAT == kPremul_SkAlphaType, "");
397
398 if (fA == 0.0f) {
399 return { 0, 0, 0, 0 };
400 } else {
401 float invAlpha = 1 / fA;
402 return { fR * invAlpha, fG * invAlpha, fB * invAlpha, fA };
403 }
404 }
405
406 // This produces bytes in RGBA order (eg GrColor). Impl. is the same, regardless of kAT
407 uint32_t toBytes_RGBA() const;
408 static SkRGBA4f FromBytes_RGBA(uint32_t color);
409
410 /**
411 Returns a copy of the SkRGBA4f but with alpha component set to 1.0f.
412
413 @return opaque color
414 */
makeOpaqueSkRGBA4f415 SkRGBA4f makeOpaque() const {
416 return { fR, fG, fB, 1.0f };
417 }
418 };
419
420 /** \struct SkColor4f
421 RGBA color value, holding four floating point components. Color components are always in
422 a known order, and are unpremultiplied.
423
424 This is a specialization of SkRGBA4f. For details, @see SkRGBA4f.
425 */
426 using SkColor4f = SkRGBA4f<kUnpremul_SkAlphaType>;
427
428 template <> SK_API SkColor4f SkColor4f::FromColor(SkColor);
429 template <> SK_API SkColor SkColor4f::toSkColor() const;
430 template <> SK_API uint32_t SkColor4f::toBytes_RGBA() const;
431 template <> SK_API SkColor4f SkColor4f::FromBytes_RGBA(uint32_t color);
432
433 namespace SkColors {
434 constexpr SkColor4f kTransparent = {0, 0, 0, 0};
435 constexpr SkColor4f kBlack = {0, 0, 0, 1};
436 constexpr SkColor4f kDkGray = {0.25f, 0.25f, 0.25f, 1};
437 constexpr SkColor4f kGray = {0.50f, 0.50f, 0.50f, 1};
438 constexpr SkColor4f kLtGray = {0.75f, 0.75f, 0.75f, 1};
439 constexpr SkColor4f kWhite = {1, 1, 1, 1};
440 constexpr SkColor4f kRed = {1, 0, 0, 1};
441 constexpr SkColor4f kGreen = {0, 1, 0, 1};
442 constexpr SkColor4f kBlue = {0, 0, 1, 1};
443 constexpr SkColor4f kYellow = {1, 1, 0, 1};
444 constexpr SkColor4f kCyan = {0, 1, 1, 1};
445 constexpr SkColor4f kMagenta = {1, 0, 1, 1};
446 } // namespace SkColors
447 #endif
448