Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkColor.h
Go to the documentation of this file.
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
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*/
26typedef 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*/
37typedef 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*/
49static 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*/
82[[nodiscard]] static constexpr inline SkColor 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*/
90
91/** Represents fully opaque SkAlpha value. SkAlpha ranges from zero,
92 fully transparent; to 255, fully opaque.
93*/
94constexpr 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*/
99constexpr SkColor SK_ColorTRANSPARENT = SkColorSetARGB(0x00, 0x00, 0x00, 0x00);
100
101/** Represents fully opaque black.
102*/
103constexpr 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*/
108constexpr SkColor SK_ColorDKGRAY = SkColorSetARGB(0xFF, 0x44, 0x44, 0x44);
109
110/** Represents fully opaque gray.
111 Note that HTML gray is equivalent to 0xFF808080.
112*/
113constexpr 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*/
118constexpr SkColor SK_ColorLTGRAY = SkColorSetARGB(0xFF, 0xCC, 0xCC, 0xCC);
119
120/** Represents fully opaque white.
121*/
122constexpr SkColor SK_ColorWHITE = SkColorSetARGB(0xFF, 0xFF, 0xFF, 0xFF);
123
124/** Represents fully opaque red.
125*/
126constexpr 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*/
131constexpr SkColor SK_ColorGREEN = SkColorSetARGB(0xFF, 0x00, 0xFF, 0x00);
132
133/** Represents fully opaque blue.
134*/
135constexpr SkColor SK_ColorBLUE = SkColorSetARGB(0xFF, 0x00, 0x00, 0xFF);
136
137/** Represents fully opaque yellow.
138*/
139constexpr SkColor SK_ColorYELLOW = SkColorSetARGB(0xFF, 0xFF, 0xFF, 0x00);
140
141/** Represents fully opaque cyan. HTML aqua is equivalent.
142*/
143constexpr SkColor SK_ColorCYAN = SkColorSetARGB(0xFF, 0x00, 0xFF, 0xFF);
144
145/** Represents fully opaque magenta. HTML fuchsia is equivalent.
146*/
147constexpr 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*/
159SK_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*/
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*/
184SK_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*/
196static 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*/
205typedef 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*/
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*/
224
225/** \enum SkColorChannel
226 Describes different color channels one can manipulate
227*/
228enum 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. */
250static_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*/
261template <SkAlphaType kAT>
262struct 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 */
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 */
308 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 */
314 float* vec() { return &fR; }
315
316 /** As a std::array<float, 4> */
317 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 */
344 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]. */
350 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 */
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 */
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 */
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*/
427
429template <> SK_API SkColor SkColor4f::toSkColor() const;
430template <> SK_API uint32_t SkColor4f::toBytes_RGBA() const;
431template <> SK_API SkColor4f SkColor4f::FromBytes_RGBA(uint32_t color);
432
433namespace SkColors {
434constexpr SkColor4f kTransparent = {0, 0, 0, 0};
435constexpr SkColor4f kBlack = {0, 0, 0, 1};
436constexpr SkColor4f kDkGray = {0.25f, 0.25f, 0.25f, 1};
437constexpr SkColor4f kGray = {0.50f, 0.50f, 0.50f, 1};
438constexpr SkColor4f kLtGray = {0.75f, 0.75f, 0.75f, 1};
439constexpr SkColor4f kWhite = {1, 1, 1, 1};
440constexpr SkColor4f kRed = {1, 0, 0, 1};
441constexpr SkColor4f kGreen = {0, 1, 0, 1};
442constexpr SkColor4f kBlue = {0, 0, 1, 1};
443constexpr SkColor4f kYellow = {1, 1, 0, 1};
444constexpr SkColor4f kCyan = {0, 1, 1, 1};
445constexpr SkColor4f kMagenta = {1, 0, 1, 1};
446} // namespace SkColors
447#endif
SkColor4f color
kUnpremul_SkAlphaType
#define SK_API
Definition SkAPI.h:35
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition SkAlphaType.h:29
#define SkASSERT(cond)
Definition SkAssert.h:116
unsigned U8CPU
Definition SkCPUTypes.h:18
constexpr SkColor SK_ColorYELLOW
Definition SkColor.h:139
constexpr SkColor SK_ColorLTGRAY
Definition SkColor.h:118
#define SkColorGetR(color)
Definition SkColor.h:65
constexpr SkColor SK_ColorMAGENTA
Definition SkColor.h:147
#define SkColorGetG(color)
Definition SkColor.h:69
SK_API SkPMColor SkPreMultiplyARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
Definition SkColor.cpp:17
SK_API SkPMColor SkPreMultiplyColor(SkColor c)
Definition SkColor.cpp:21
uint32_t SkColor
Definition SkColor.h:37
constexpr SkColor SK_ColorCYAN
Definition SkColor.h:143
uint8_t SkAlpha
Definition SkColor.h:26
constexpr SkColor SK_ColorTRANSPARENT
Definition SkColor.h:99
uint32_t SkPMColor
Definition SkColor.h:205
SK_API SkColor SkHSVToColor(U8CPU alpha, const SkScalar hsv[3])
Definition SkColor.cpp:78
constexpr SkColor SK_ColorGRAY
Definition SkColor.h:113
SkColorChannel
Definition SkColor.h:228
constexpr SkColor SK_ColorBLUE
Definition SkColor.h:135
static constexpr SkColor SkColorSetA(SkColor c, U8CPU a)
Definition SkColor.h:82
constexpr SkAlpha SK_AlphaOPAQUE
Definition SkColor.h:94
SK_API void SkRGBToHSV(U8CPU red, U8CPU green, U8CPU blue, SkScalar hsv[3])
Definition SkColor.cpp:38
constexpr SkColor SK_ColorRED
Definition SkColor.h:126
static constexpr SkColor SkColorSetARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
Definition SkColor.h:49
static void SkColorToHSV(SkColor color, SkScalar hsv[3])
Definition SkColor.h:169
constexpr SkColor SK_ColorBLACK
Definition SkColor.h:103
constexpr SkColor SK_ColorGREEN
Definition SkColor.h:131
SkColorChannelFlag
Definition SkColor.h:238
@ kRGB_SkColorChannelFlags
Definition SkColor.h:247
@ kRGBA_SkColorChannelFlags
Definition SkColor.h:248
@ kRed_SkColorChannelFlag
Definition SkColor.h:239
@ kGrayAlpha_SkColorChannelFlags
Definition SkColor.h:245
@ kRG_SkColorChannelFlags
Definition SkColor.h:246
@ kGreen_SkColorChannelFlag
Definition SkColor.h:240
@ kAlpha_SkColorChannelFlag
Definition SkColor.h:242
@ kGray_SkColorChannelFlag
Definition SkColor.h:243
@ kBlue_SkColorChannelFlag
Definition SkColor.h:241
#define SkColorGetB(color)
Definition SkColor.h:73
constexpr SkColor SK_ColorWHITE
Definition SkColor.h:122
constexpr SkAlpha SK_AlphaTRANSPARENT
Definition SkColor.h:89
constexpr SkColor SK_ColorDKGRAY
Definition SkColor.h:108
float SkScalar
Definition extension.cpp:12
static bool b
struct MyStruct a[10]
constexpr SkColor4f kLtGray
Definition SkColor.h:438
constexpr SkColor4f kMagenta
Definition SkColor.h:445
constexpr SkColor4f kGreen
Definition SkColor.h:441
constexpr SkColor4f kRed
Definition SkColor.h:440
constexpr SkColor4f kWhite
Definition SkColor.h:439
constexpr SkColor4f kCyan
Definition SkColor.h:444
constexpr SkColor4f kTransparent
Definition SkColor.h:434
constexpr SkColor4f kBlack
Definition SkColor.h:435
constexpr SkColor4f kGray
Definition SkColor.h:437
constexpr SkColor4f kBlue
Definition SkColor.h:442
constexpr SkColor4f kYellow
Definition SkColor.h:443
constexpr SkColor4f kDkGray
Definition SkColor.h:436
const Scalar scale
const float * vec() const
Definition SkColor.h:308
SkColor toSkColor() const
float fB
blue component
Definition SkColor.h:265
SkRGBA4f< kPremul_SkAlphaType > premul() const
Definition SkColor.h:385
SkRGBA4f operator*(float scale) const
Definition SkColor.h:291
bool fitsInBytes() const
Definition SkColor.h:350
SkRGBA4f operator*(const SkRGBA4f &scale) const
Definition SkColor.h:300
static SkRGBA4f FromBytes_RGBA(uint32_t color)
bool operator!=(const SkRGBA4f &other) const
Definition SkColor.h:282
bool operator==(const SkRGBA4f &other) const
Definition SkColor.h:273
SkRGBA4f< kUnpremul_SkAlphaType > unpremul() const
Definition SkColor.h:395
float * vec()
Definition SkColor.h:314
std::array< float, 4 > array() const
Definition SkColor.h:317
float fR
red component
Definition SkColor.h:263
SkRGBA4f makeOpaque() const
Definition SkColor.h:415
uint32_t toBytes_RGBA() const
static SkRGBA4f FromColor(SkColor color)
float operator[](int index) const
Definition SkColor.h:324
float fG
green component
Definition SkColor.h:264
static SkRGBA4f FromPMColor(SkPMColor)
float fA
alpha component
Definition SkColor.h:266
bool isOpaque() const
Definition SkColor.h:344
float & operator[](int index)
Definition SkColor.h:334