Skia
2D Graphics Library
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 
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 
26 typedef uint8_t SkAlpha;
27 
37 typedef uint32_t SkColor;
38 
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 
57 #define SkColorSetRGB(r, g, b) SkColorSetARGB(0xFF, r, g, b)
58 
61 #define SkColorGetA(color) (((color) >> 24) & 0xFF)
62 
65 #define SkColorGetR(color) (((color) >> 16) & 0xFF)
66 
69 #define SkColorGetG(color) (((color) >> 8) & 0xFF)
70 
73 #define SkColorGetB(color) (((color) >> 0) & 0xFF)
74 
82 [[nodiscard]] static constexpr inline SkColor SkColorSetA(SkColor c, U8CPU a) {
83  return (c & 0x00FFFFFF) | (a << 24);
84 }
85 
89 constexpr SkAlpha SK_AlphaTRANSPARENT = 0x00;
90 
94 constexpr SkAlpha SK_AlphaOPAQUE = 0xFF;
95 
99 constexpr SkColor SK_ColorTRANSPARENT = SkColorSetARGB(0x00, 0x00, 0x00, 0x00);
100 
103 constexpr SkColor SK_ColorBLACK = SkColorSetARGB(0xFF, 0x00, 0x00, 0x00);
104 
108 constexpr SkColor SK_ColorDKGRAY = SkColorSetARGB(0xFF, 0x44, 0x44, 0x44);
109 
113 constexpr SkColor SK_ColorGRAY = SkColorSetARGB(0xFF, 0x88, 0x88, 0x88);
114 
118 constexpr SkColor SK_ColorLTGRAY = SkColorSetARGB(0xFF, 0xCC, 0xCC, 0xCC);
119 
122 constexpr SkColor SK_ColorWHITE = SkColorSetARGB(0xFF, 0xFF, 0xFF, 0xFF);
123 
126 constexpr SkColor SK_ColorRED = SkColorSetARGB(0xFF, 0xFF, 0x00, 0x00);
127 
131 constexpr SkColor SK_ColorGREEN = SkColorSetARGB(0xFF, 0x00, 0xFF, 0x00);
132 
135 constexpr SkColor SK_ColorBLUE = SkColorSetARGB(0xFF, 0x00, 0x00, 0xFF);
136 
139 constexpr SkColor SK_ColorYELLOW = SkColorSetARGB(0xFF, 0xFF, 0xFF, 0x00);
140 
143 constexpr SkColor SK_ColorCYAN = SkColorSetARGB(0xFF, 0x00, 0xFF, 0xFF);
144 
147 constexpr SkColor SK_ColorMAGENTA = SkColorSetARGB(0xFF, 0xFF, 0x00, 0xFF);
148 
159 SK_API void SkRGBToHSV(U8CPU red, U8CPU green, U8CPU blue, SkScalar hsv[3]);
160 
169 static inline void SkColorToHSV(SkColor color, SkScalar hsv[3]) {
170  SkRGBToHSV(SkColorGetR(color), SkColorGetG(color), SkColorGetB(color), hsv);
171 }
172 
184 SK_API SkColor SkHSVToColor(U8CPU alpha, const SkScalar hsv[3]);
185 
196 static inline SkColor SkHSVToColor(const SkScalar hsv[3]) {
197  return SkHSVToColor(0xFF, hsv);
198 }
199 
205 typedef uint32_t SkPMColor;
206 
215 SK_API SkPMColor SkPreMultiplyARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b);
216 
224 
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 
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),
244  // Convenience values
249 };
250 static_assert(0 == (kGray_SkColorChannelFlag & kRGBA_SkColorChannelFlags), "bitfield conflict");
251 
261 template <SkAlphaType kAT>
262 struct SkRGBA4f {
263  float fR;
264  float fG;
265  float fB;
266  float fA;
267 
273  bool operator==(const SkRGBA4f& other) const {
274  return fA == other.fA && fR == other.fR && fG == other.fG && fB == other.fB;
275  }
276 
282  bool operator!=(const SkRGBA4f& other) const {
283  return !(*this == other);
284  }
285 
291  SkRGBA4f operator*(float scale) const {
292  return { fR * scale, fG * scale, fB * scale, fA * scale };
293  }
294 
300  SkRGBA4f operator*(const SkRGBA4f& scale) const {
301  return { fR * scale.fR, fG * scale.fG, fB * scale.fB, fA * scale.fA };
302  }
303 
308  const float* vec() const { return &fR; }
309 
314  float* vec() { return &fR; }
315 
317  std::array<float, 4> array() const { return {fR, fG, fB, fA}; }
318 
324  float operator[](int index) const {
325  SkASSERT(index >= 0 && index < 4);
326  return this->vec()[index];
327  }
328 
334  float& operator[](int index) {
335  SkASSERT(index >= 0 && index < 4);
336  return this->vec()[index];
337  }
338 
344  bool isOpaque() const {
345  SkASSERT(fA <= 1.0f && fA >= 0.0f);
346  return fA == 1.0f;
347  }
348 
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 
364  static SkRGBA4f FromColor(SkColor color); // impl. depends on kAT
365 
372  SkColor toSkColor() const; // impl. depends on kAT
373 
378  static SkRGBA4f FromPMColor(SkPMColor); // impl. depends on kAT
379 
386  static_assert(kAT == kUnpremul_SkAlphaType, "");
387  return { fR * fA, fG * fA, fB * fA, fA };
388  }
389 
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 
416  return { fR, fG, fB, 1.0f };
417  }
418 };
419 
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
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition: SkAlphaType.h:29
@ kUnpremul_SkAlphaType
pixel components are independent of alpha
Definition: SkAlphaType.h:30
constexpr SkColor SK_ColorYELLOW
Represents fully opaque yellow.
Definition: SkColor.h:139
constexpr SkColor SK_ColorLTGRAY
Represents fully opaque light gray.
Definition: SkColor.h:118
#define SkColorGetR(color)
Returns red component of color, from zero to 255.
Definition: SkColor.h:65
constexpr SkColor SK_ColorMAGENTA
Represents fully opaque magenta.
Definition: SkColor.h:147
#define SkColorGetG(color)
Returns green component of color, from zero to 255.
Definition: SkColor.h:69
SK_API SkPMColor SkPreMultiplyARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
Returns a SkPMColor value from unpremultiplied 8-bit component values.
SK_API SkPMColor SkPreMultiplyColor(SkColor c)
Returns pmcolor closest to color c.
uint32_t SkColor
32-bit ARGB color value, unpremultiplied.
Definition: SkColor.h:37
constexpr SkColor SK_ColorCYAN
Represents fully opaque cyan.
Definition: SkColor.h:143
uint8_t SkAlpha
8-bit type for an alpha value.
Definition: SkColor.h:26
constexpr SkColor SK_ColorTRANSPARENT
Represents fully transparent SkColor.
Definition: SkColor.h:99
uint32_t SkPMColor
32-bit ARGB color value, premultiplied.
Definition: SkColor.h:205
SK_API SkColor SkHSVToColor(U8CPU alpha, const SkScalar hsv[3])
Converts HSV components to an ARGB color.
constexpr SkColor SK_ColorGRAY
Represents fully opaque gray.
Definition: SkColor.h:113
SkColorChannel
Describes different color channels one can manipulate.
Definition: SkColor.h:228
constexpr SkColor SK_ColorBLUE
Represents fully opaque blue.
Definition: SkColor.h:135
static constexpr SkColor SkColorSetA(SkColor c, U8CPU a)
Returns unpremultiplied color with red, blue, and green set from c; and alpha set from a.
Definition: SkColor.h:82
constexpr SkAlpha SK_AlphaOPAQUE
Represents fully opaque SkAlpha value.
Definition: SkColor.h:94
SK_API void SkRGBToHSV(U8CPU red, U8CPU green, U8CPU blue, SkScalar hsv[3])
Converts RGB to its HSV components.
constexpr SkColor SK_ColorRED
Represents fully opaque red.
Definition: SkColor.h:126
static constexpr SkColor SkColorSetARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
Returns color value from 8-bit component values.
Definition: SkColor.h:49
static void SkColorToHSV(SkColor color, SkScalar hsv[3])
Converts ARGB to its HSV components.
Definition: SkColor.h:169
constexpr SkColor SK_ColorBLACK
Represents fully opaque black.
Definition: SkColor.h:103
constexpr SkColor SK_ColorGREEN
Represents fully opaque green.
Definition: SkColor.h:131
SkColorChannelFlag
Used to represent the channels available in a color type or texture format as a mask.
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)
Returns blue component of color, from zero to 255.
Definition: SkColor.h:73
constexpr SkColor SK_ColorWHITE
Represents fully opaque white.
Definition: SkColor.h:122
constexpr SkAlpha SK_AlphaTRANSPARENT
Represents fully transparent SkAlpha value.
Definition: SkColor.h:89
constexpr SkColor SK_ColorDKGRAY
Represents fully opaque dark gray.
Definition: SkColor.h:108
float SkScalar
Definition: SkScalar.h:14
Definition: SkColor.h:433
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
RGBA color value, holding four floating point components.
RGBA color value, holding four floating point components.
Definition: SkColor.h:262
const float * vec() const
Returns a pointer to components of SkRGBA4f, for array access.
Definition: SkColor.h:308
SkColor toSkColor() const
Returns closest SkColor to SkRGBA4f.
float fB
blue component
Definition: SkColor.h:265
SkRGBA4f operator*(float scale) const
Returns SkRGBA4f multiplied by scale.
Definition: SkColor.h:291
float * vec()
Returns a pointer to components of SkRGBA4f, for array access.
Definition: SkColor.h:314
bool fitsInBytes() const
Returns true if all channels are in [0, 1].
Definition: SkColor.h:350
SkRGBA4f operator*(const SkRGBA4f &scale) const
Returns SkRGBA4f multiplied component-wise by scale.
Definition: SkColor.h:300
static SkRGBA4f FromBytes_RGBA(uint32_t color)
bool operator!=(const SkRGBA4f &other) const
Compares SkRGBA4f with other, and returns true if not all components are equal.
Definition: SkColor.h:282
bool operator==(const SkRGBA4f &other) const
Compares SkRGBA4f with other, and returns true if all components are equal.
Definition: SkColor.h:273
std::array< float, 4 > array() const
As a std::array<float, 4>
Definition: SkColor.h:317
float fR
red component
Definition: SkColor.h:263
SkRGBA4f makeOpaque() const
Returns a copy of the SkRGBA4f but with alpha component set to 1.0f.
Definition: SkColor.h:415
SkRGBA4f< kUnpremul_SkAlphaType > unpremul() const
Returns SkRGBA4f unpremultiplied by alpha.
Definition: SkColor.h:395
uint32_t toBytes_RGBA() const
static SkRGBA4f FromColor(SkColor color)
Returns closest SkRGBA4f to SkColor.
float operator[](int index) const
Returns one component.
Definition: SkColor.h:324
float fG
green component
Definition: SkColor.h:264
static SkRGBA4f FromPMColor(SkPMColor)
Returns closest SkRGBA4f to SkPMColor.
SkRGBA4f< kPremul_SkAlphaType > premul() const
Returns SkRGBA4f premultiplied by alpha.
Definition: SkColor.h:385
float & operator[](int index)
Returns one component.
Definition: SkColor.h:334
float fA
alpha component
Definition: SkColor.h:266
bool isOpaque() const
Returns true if SkRGBA4f is an opaque color.
Definition: SkColor.h:344