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 
15 #include <array>
16 
24 typedef uint8_t SkAlpha;
25 
35 typedef uint32_t SkColor;
36 
47 static constexpr inline SkColor SkColorSetARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) {
48  return SkASSERT(a <= 255 && r <= 255 && g <= 255 && b <= 255),
49  (a << 24) | (r << 16) | (g << 8) | (b << 0);
50 }
51 
55 #define SkColorSetRGB(r, g, b) SkColorSetARGB(0xFF, r, g, b)
56 
59 #define SkColorGetA(color) (((color) >> 24) & 0xFF)
60 
63 #define SkColorGetR(color) (((color) >> 16) & 0xFF)
64 
67 #define SkColorGetG(color) (((color) >> 8) & 0xFF)
68 
71 #define SkColorGetB(color) (((color) >> 0) & 0xFF)
72 
80 static constexpr inline SkColor SK_WARN_UNUSED_RESULT SkColorSetA(SkColor c, U8CPU a) {
81  return (c & 0x00FFFFFF) | (a << 24);
82 }
83 
87 constexpr SkAlpha SK_AlphaTRANSPARENT = 0x00;
88 
92 constexpr SkAlpha SK_AlphaOPAQUE = 0xFF;
93 
97 constexpr SkColor SK_ColorTRANSPARENT = SkColorSetARGB(0x00, 0x00, 0x00, 0x00);
98 
101 constexpr SkColor SK_ColorBLACK = SkColorSetARGB(0xFF, 0x00, 0x00, 0x00);
102 
106 constexpr SkColor SK_ColorDKGRAY = SkColorSetARGB(0xFF, 0x44, 0x44, 0x44);
107 
111 constexpr SkColor SK_ColorGRAY = SkColorSetARGB(0xFF, 0x88, 0x88, 0x88);
112 
116 constexpr SkColor SK_ColorLTGRAY = SkColorSetARGB(0xFF, 0xCC, 0xCC, 0xCC);
117 
120 constexpr SkColor SK_ColorWHITE = SkColorSetARGB(0xFF, 0xFF, 0xFF, 0xFF);
121 
124 constexpr SkColor SK_ColorRED = SkColorSetARGB(0xFF, 0xFF, 0x00, 0x00);
125 
129 constexpr SkColor SK_ColorGREEN = SkColorSetARGB(0xFF, 0x00, 0xFF, 0x00);
130 
133 constexpr SkColor SK_ColorBLUE = SkColorSetARGB(0xFF, 0x00, 0x00, 0xFF);
134 
137 constexpr SkColor SK_ColorYELLOW = SkColorSetARGB(0xFF, 0xFF, 0xFF, 0x00);
138 
141 constexpr SkColor SK_ColorCYAN = SkColorSetARGB(0xFF, 0x00, 0xFF, 0xFF);
142 
145 constexpr SkColor SK_ColorMAGENTA = SkColorSetARGB(0xFF, 0xFF, 0x00, 0xFF);
146 
157 SK_API void SkRGBToHSV(U8CPU red, U8CPU green, U8CPU blue, SkScalar hsv[3]);
158 
167 static inline void SkColorToHSV(SkColor color, SkScalar hsv[3]) {
168  SkRGBToHSV(SkColorGetR(color), SkColorGetG(color), SkColorGetB(color), hsv);
169 }
170 
182 SK_API SkColor SkHSVToColor(U8CPU alpha, const SkScalar hsv[3]);
183 
194 static inline SkColor SkHSVToColor(const SkScalar hsv[3]) {
195  return SkHSVToColor(0xFF, hsv);
196 }
197 
203 typedef uint32_t SkPMColor;
204 
214 
222 
226 enum class SkColorChannel {
227  kR, // the red channel
228  kG, // the green channel
229  kB, // the blue channel
230  kA, // the alpha channel
231 
232  kLastEnum = kA,
233 };
234 
236 enum SkColorChannelFlag : uint32_t {
237  kRed_SkColorChannelFlag = 1 << static_cast<uint32_t>(SkColorChannel::kR),
238  kGreen_SkColorChannelFlag = 1 << static_cast<uint32_t>(SkColorChannel::kG),
239  kBlue_SkColorChannelFlag = 1 << static_cast<uint32_t>(SkColorChannel::kB),
240  kAlpha_SkColorChannelFlag = 1 << static_cast<uint32_t>(SkColorChannel::kA),
242  // Convenience values
247 };
248 static_assert(0 == (kGray_SkColorChannelFlag & kRGBA_SkColorChannelFlags), "bitfield conflict");
249 
259 template <SkAlphaType kAT>
260 struct SkRGBA4f {
261  float fR;
262  float fG;
263  float fB;
264  float fA;
265 
271  bool operator==(const SkRGBA4f& other) const {
272  return fA == other.fA && fR == other.fR && fG == other.fG && fB == other.fB;
273  }
274 
280  bool operator!=(const SkRGBA4f& other) const {
281  return !(*this == other);
282  }
283 
289  SkRGBA4f operator*(float scale) const {
290  return { fR * scale, fG * scale, fB * scale, fA * scale };
291  }
292 
298  SkRGBA4f operator*(const SkRGBA4f& scale) const {
299  return { fR * scale.fR, fG * scale.fG, fB * scale.fB, fA * scale.fA };
300  }
301 
306  const float* vec() const { return &fR; }
307 
312  float* vec() { return &fR; }
313 
315  std::array<float, 4> array() const { return {fR, fG, fB, fA}; }
316 
322  float operator[](int index) const {
323  SkASSERT(index >= 0 && index < 4);
324  return this->vec()[index];
325  }
326 
332  float& operator[](int index) {
333  SkASSERT(index >= 0 && index < 4);
334  return this->vec()[index];
335  }
336 
342  bool isOpaque() const {
343  SkASSERT(fA <= 1.0f && fA >= 0.0f);
344  return fA == 1.0f;
345  }
346 
348  bool fitsInBytes() const {
349  SkASSERT(fA >= 0.0f && fA <= 1.0f);
350  return fR >= 0.0f && fR <= 1.0f &&
351  fG >= 0.0f && fG <= 1.0f &&
352  fB >= 0.0f && fB <= 1.0f;
353  }
354 
362  static SkRGBA4f FromColor(SkColor color); // impl. depends on kAT
363 
370  SkColor toSkColor() const; // impl. depends on kAT
371 
376  static SkRGBA4f FromPMColor(SkPMColor); // impl. depends on kAT
377 
384  static_assert(kAT == kUnpremul_SkAlphaType, "");
385  return { fR * fA, fG * fA, fB * fA, fA };
386  }
387 
394  static_assert(kAT == kPremul_SkAlphaType, "");
395 
396  if (fA == 0.0f) {
397  return { 0, 0, 0, 0 };
398  } else {
399  float invAlpha = 1 / fA;
400  return { fR * invAlpha, fG * invAlpha, fB * invAlpha, fA };
401  }
402  }
403 
404  // This produces bytes in RGBA order (eg GrColor). Impl. is the same, regardless of kAT
405  uint32_t toBytes_RGBA() const;
406  static SkRGBA4f FromBytes_RGBA(uint32_t color);
407 
409  return { fR, fG, fB, 1.0f };
410  }
411 };
412 
420 
421 template <> SK_API SkColor4f SkColor4f::FromColor(SkColor);
422 template <> SK_API SkColor SkColor4f::toSkColor() const;
423 
424 namespace SkColors {
425 constexpr SkColor4f kTransparent = {0, 0, 0, 0};
426 constexpr SkColor4f kBlack = {0, 0, 0, 1};
427 constexpr SkColor4f kDkGray = {0.25f, 0.25f, 0.25f, 1};
428 constexpr SkColor4f kGray = {0.50f, 0.50f, 0.50f, 1};
429 constexpr SkColor4f kLtGray = {0.75f, 0.75f, 0.75f, 1};
430 constexpr SkColor4f kWhite = {1, 1, 1, 1};
431 constexpr SkColor4f kRed = {1, 0, 0, 1};
432 constexpr SkColor4f kGreen = {0, 1, 0, 1};
433 constexpr SkColor4f kBlue = {0, 0, 1, 1};
434 constexpr SkColor4f kYellow = {1, 1, 0, 1};
435 constexpr SkColor4f kCyan = {0, 1, 1, 1};
436 constexpr SkColor4f kMagenta = {1, 0, 1, 1};
437 } // namespace SkColors
438 #endif
SkRGBA4f::operator[]
float operator[](int index) const
Returns one component.
Definition: SkColor.h:322
kRG_SkColorChannelFlags
@ kRG_SkColorChannelFlags
Definition: SkColor.h:244
SkHSVToColor
SK_API SkColor SkHSVToColor(U8CPU alpha, const SkScalar hsv[3])
Converts HSV components to an ARGB color.
SkRGBA4f::operator*
SkRGBA4f operator*(const SkRGBA4f &scale) const
Returns SkRGBA4f multiplied component-wise by scale.
Definition: SkColor.h:298
kGreen_SkColorChannelFlag
@ kGreen_SkColorChannelFlag
Definition: SkColor.h:238
SkColors::kDkGray
constexpr SkColor4f kDkGray
Definition: SkColor.h:427
kRGBA_SkColorChannelFlags
@ kRGBA_SkColorChannelFlags
Definition: SkColor.h:246
kBlue_SkColorChannelFlag
@ kBlue_SkColorChannelFlag
Definition: SkColor.h:239
SkRGBA4f
Definition: SkColor.h:260
SkTypes.h
SkColor4f
SkColors::kYellow
constexpr SkColor4f kYellow
Definition: SkColor.h:434
SK_ColorCYAN
constexpr SkColor SK_ColorCYAN
Represents fully opaque cyan.
Definition: SkColor.h:141
SkColorSetA
static constexpr SkColor SK_WARN_UNUSED_RESULT SkColorSetA(SkColor c, U8CPU a)
Returns unpremultiplied color with red, blue, and green set from c; and alpha set from a.
Definition: SkColor.h:80
SK_ColorTRANSPARENT
constexpr SkColor SK_ColorTRANSPARENT
Represents fully transparent SkColor.
Definition: SkColor.h:97
SkRGBA4f::FromColor
static SkRGBA4f FromColor(SkColor color)
Returns closest SkRGBA4f to SkColor.
SkRGBA4f::makeOpaque
SkRGBA4f makeOpaque() const
Definition: SkColor.h:408
SkRGBA4f::FromPMColor
static SkRGBA4f FromPMColor(SkPMColor)
Returns closest SkRGBA4f to SkPMColor.
SK_ColorWHITE
constexpr SkColor SK_ColorWHITE
Represents fully opaque white.
Definition: SkColor.h:120
SkColors::kGray
constexpr SkColor4f kGray
Definition: SkColor.h:428
SK_ColorMAGENTA
constexpr SkColor SK_ColorMAGENTA
Represents fully opaque magenta.
Definition: SkColor.h:145
SK_ColorYELLOW
constexpr SkColor SK_ColorYELLOW
Represents fully opaque yellow.
Definition: SkColor.h:137
SkColorChannel::kR
@ kR
SkColors::kBlue
constexpr SkColor4f kBlue
Definition: SkColor.h:433
SK_ColorGRAY
constexpr SkColor SK_ColorGRAY
Represents fully opaque gray.
Definition: SkColor.h:111
SkScalar.h
SkColorChannel::kA
@ kA
kAlpha_SkColorChannelFlag
@ kAlpha_SkColorChannelFlag
Definition: SkColor.h:240
SK_ColorGREEN
constexpr SkColor SK_ColorGREEN
Represents fully opaque green.
Definition: SkColor.h:129
SkRGBA4f::fB
float fB
blue component
Definition: SkColor.h:263
SkColorChannelFlag
SkColorChannelFlag
Used to represent the channels available in a color type or texture format as a mask.
Definition: SkColor.h:236
SkRGBToHSV
SK_API void SkRGBToHSV(U8CPU red, U8CPU green, U8CPU blue, SkScalar hsv[3])
Converts RGB to its HSV components.
SkColors::kMagenta
constexpr SkColor4f kMagenta
Definition: SkColor.h:436
kUnpremul_SkAlphaType
@ kUnpremul_SkAlphaType
pixel components are independent of alpha
Definition: SkImageInfo.h:41
SkRGBA4f::premul
SkRGBA4f< kPremul_SkAlphaType > premul() const
Returns SkRGBA4f premultiplied by alpha.
Definition: SkColor.h:383
SK_API
#define SK_API
Definition: SkTypes.h:181
SkASSERT
#define SkASSERT(cond)
Definition: SkTypes.h:460
SkRGBA4f::fR
float fR
red component
Definition: SkColor.h:261
kRGB_SkColorChannelFlags
@ kRGB_SkColorChannelFlags
Definition: SkColor.h:245
SkAlpha
uint8_t SkAlpha
8-bit type for an alpha value.
Definition: SkColor.h:24
SK_ColorBLACK
constexpr SkColor SK_ColorBLACK
Represents fully opaque black.
Definition: SkColor.h:101
SkRGBA4f::operator*
SkRGBA4f operator*(float scale) const
Returns SkRGBA4f multiplied by scale.
Definition: SkColor.h:289
SK_ColorRED
constexpr SkColor SK_ColorRED
Represents fully opaque red.
Definition: SkColor.h:124
SkColorSetARGB
static constexpr SkColor SkColorSetARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
Returns color value from 8-bit component values.
Definition: SkColor.h:47
SkColors::kBlack
constexpr SkColor4f kBlack
Definition: SkColor.h:426
SK_ColorDKGRAY
constexpr SkColor SK_ColorDKGRAY
Represents fully opaque dark gray.
Definition: SkColor.h:106
SkColorGetG
#define SkColorGetG(color)
Returns green component of color, from zero to 255.
Definition: SkColor.h:67
SkPreMultiplyColor
SK_API SkPMColor SkPreMultiplyColor(SkColor c)
Returns pmcolor closest to color c.
SkColors::kGreen
constexpr SkColor4f kGreen
Definition: SkColor.h:432
SkRGBA4f::vec
const float * vec() const
Returns a pointer to components of SkRGBA4f, for array access.
Definition: SkColor.h:306
SK_ColorLTGRAY
constexpr SkColor SK_ColorLTGRAY
Represents fully opaque light gray.
Definition: SkColor.h:116
SK_ColorBLUE
constexpr SkColor SK_ColorBLUE
Represents fully opaque blue.
Definition: SkColor.h:133
SkRGBA4f::operator!=
bool operator!=(const SkRGBA4f &other) const
Compares SkRGBA4f with other, and returns true if not all components are equal.
Definition: SkColor.h:280
SkRGBA4f::toBytes_RGBA
uint32_t toBytes_RGBA() const
SkScalar
float SkScalar
Definition: SkScalar.h:16
SkPreMultiplyARGB
SK_API SkPMColor SkPreMultiplyARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
Returns a SkPMColor value from unpremultiplied 8-bit component values.
SkColorChannel::kLastEnum
@ kLastEnum
SkRGBA4f::array
std::array< float, 4 > array() const
As a std::array<float, 4>
Definition: SkColor.h:315
SkRGBA4f::fitsInBytes
bool fitsInBytes() const
Returns true if all channels are in [0, 1].
Definition: SkColor.h:348
SkRGBA4f::operator[]
float & operator[](int index)
Returns one component.
Definition: SkColor.h:332
SkColors::kTransparent
constexpr SkColor4f kTransparent
Definition: SkColor.h:425
SkRGBA4f::FromBytes_RGBA
static SkRGBA4f FromBytes_RGBA(uint32_t color)
SK_AlphaOPAQUE
constexpr SkAlpha SK_AlphaOPAQUE
Represents fully opaque SkAlpha value.
Definition: SkColor.h:92
SkRGBA4f::vec
float * vec()
Returns a pointer to components of SkRGBA4f, for array access.
Definition: SkColor.h:312
SkColors::kRed
constexpr SkColor4f kRed
Definition: SkColor.h:431
SkColors::kWhite
constexpr SkColor4f kWhite
Definition: SkColor.h:430
SkRGBA4f::isOpaque
bool isOpaque() const
Returns true if SkRGBA4f is an opaque color.
Definition: SkColor.h:342
f
float f
Definition: skcms.cc:52
kRed_SkColorChannelFlag
@ kRed_SkColorChannelFlag
Definition: SkColor.h:237
SkColorGetB
#define SkColorGetB(color)
Returns blue component of color, from zero to 255.
Definition: SkColor.h:71
SkRGBA4f::fG
float fG
green component
Definition: SkColor.h:262
SkColors::kCyan
constexpr SkColor4f kCyan
Definition: SkColor.h:435
SK_WARN_UNUSED_RESULT
#define SK_WARN_UNUSED_RESULT
Definition: SkTypes.h:60
SkColorChannel::kG
@ kG
kGray_SkColorChannelFlag
@ kGray_SkColorChannelFlag
Definition: SkColor.h:241
kGrayAlpha_SkColorChannelFlags
@ kGrayAlpha_SkColorChannelFlags
Definition: SkColor.h:243
SkColorToHSV
static void SkColorToHSV(SkColor color, SkScalar hsv[3])
Converts ARGB to its HSV components.
Definition: SkColor.h:167
SkColors
Definition: SkColor.h:424
SkRGBA4f::operator==
bool operator==(const SkRGBA4f &other) const
Compares SkRGBA4f with other, and returns true if all components are equal.
Definition: SkColor.h:271
SkColorGetR
#define SkColorGetR(color)
Returns red component of color, from zero to 255.
Definition: SkColor.h:63
SkPMColor
uint32_t SkPMColor
32-bit ARGB color value, premultiplied.
Definition: SkColor.h:203
SkColor
uint32_t SkColor
32-bit ARGB color value, unpremultiplied.
Definition: SkColor.h:35
SkColorChannel
SkColorChannel
Definition: SkColor.h:226
U8CPU
unsigned U8CPU
Fast type for unsigned 8 bits.
Definition: SkTypes.h:488
SkRGBA4f::toSkColor
SkColor toSkColor() const
Returns closest SkColor to SkRGBA4f.
kPremul_SkAlphaType
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition: SkImageInfo.h:40
SK_AlphaTRANSPARENT
constexpr SkAlpha SK_AlphaTRANSPARENT
Represents fully transparent SkAlpha value.
Definition: SkColor.h:87
SkRGBA4f::fA
float fA
alpha component
Definition: SkColor.h:264
SkColorChannel::kB
@ kB
SkColors::kLtGray
constexpr SkColor4f kLtGray
Definition: SkColor.h:429
SkImageInfo.h
SkRGBA4f::unpremul
SkRGBA4f< kUnpremul_SkAlphaType > unpremul() const
Returns SkRGBA4f unpremultiplied by alpha.
Definition: SkColor.h:393