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