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 */
SkColorSetARGB(U8CPU a,U8CPU r,U8CPU g,U8CPU b)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 */
SkColorSetA(SkColor c,U8CPU a)78 static constexpr inline SkColor 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 */
SkColorToHSV(SkColor color,SkScalar hsv[3])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 */
SkHSVToColor(const SkScalar hsv[3])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 /** \struct SkRGBA4f
234     RGBA color value, holding four floating point components. Color components are always in
235     a known order. kAT determines if the SkRGBA4f's R, G, and B components are premultiplied
236     by alpha or not.
237 
238     Skia's public API always uses unpremultiplied colors, which can be stored as
239     SkRGBA4f<kUnpremul_SkAlphaType>. For convenience, this type can also be referred to
240     as SkColor4f.
241 */
242 template <SkAlphaType kAT>
243 struct SkRGBA4f {
244     float fR;  //!< red component
245     float fG;  //!< green component
246     float fB;  //!< blue component
247     float fA;  //!< alpha component
248 
249     /** Compares SkRGBA4f with other, and returns true if all components are equal.
250 
251         @param other  SkRGBA4f to compare
252         @return       true if SkRGBA4f equals other
253     */
254     bool operator==(const SkRGBA4f& other) const {
255         return fA == other.fA && fR == other.fR && fG == other.fG && fB == other.fB;
256     }
257 
258     /** Compares SkRGBA4f with other, and returns true if not all components are equal.
259 
260         @param other  SkRGBA4f to compare
261         @return       true if SkRGBA4f is not equal to other
262     */
263     bool operator!=(const SkRGBA4f& other) const {
264         return !(*this == other);
265     }
266 
267     /** Returns SkRGBA4f multiplied by scale.
268 
269         @param scale  value to multiply by
270         @return       SkRGBA4f as (fR * scale, fG * scale, fB * scale, fA * scale)
271     */
272     SkRGBA4f operator*(float scale) const {
273         return { fR * scale, fG * scale, fB * scale, fA * scale };
274     }
275 
276     /** Returns SkRGBA4f multiplied component-wise by scale.
277 
278         @param scale  SkRGBA4f to multiply by
279         @return       SkRGBA4f as (fR * scale.fR, fG * scale.fG, fB * scale.fB, fA * scale.fA)
280     */
281     SkRGBA4f operator*(const SkRGBA4f& scale) const {
282         return { fR * scale.fR, fG * scale.fG, fB * scale.fB, fA * scale.fA };
283     }
284 
285     /** Returns a pointer to components of SkRGBA4f, for array access.
286 
287         @return       pointer to array [fR, fG, fB, fA]
288     */
vecSkRGBA4f289     const float* vec() const { return &fR; }
290 
291     /** Returns a pointer to components of SkRGBA4f, for array access.
292 
293         @return       pointer to array [fR, fG, fB, fA]
294     */
vecSkRGBA4f295     float* vec() { return &fR; }
296 
297     /** Returns one component. Asserts if index is out of range and SK_DEBUG is defined.
298 
299         @param index  one of: 0 (fR), 1 (fG), 2 (fB), 3 (fA)
300         @return       value corresponding to index
301     */
302     float operator[](int index) const {
303         SkASSERT(index >= 0 && index < 4);
304         return this->vec()[index];
305     }
306 
307     /** Returns one component. Asserts if index is out of range and SK_DEBUG is defined.
308 
309         @param index  one of: 0 (fR), 1 (fG), 2 (fB), 3 (fA)
310         @return       value corresponding to index
311     */
312     float& operator[](int index) {
313         SkASSERT(index >= 0 && index < 4);
314         return this->vec()[index];
315     }
316 
317     /** Returns true if SkRGBA4f is an opaque color. Asserts if fA is out of range and
318         SK_DEBUG is defined.
319 
320         @return       true if SkRGBA4f is opaque
321     */
isOpaqueSkRGBA4f322     bool isOpaque() const {
323         SkASSERT(fA <= 1.0f && fA >= 0.0f);
324         return fA == 1.0f;
325     }
326 
327     /** Returns true if all channels are in [0, 1]. */
fitsInBytesSkRGBA4f328     bool fitsInBytes() const {
329         SkASSERT(fA >= 0.0f && fA <= 1.0f);
330         return fR >= 0.0f && fR <= 1.0f &&
331                fG >= 0.0f && fG <= 1.0f &&
332                fB >= 0.0f && fB <= 1.0f;
333     }
334 
335     /** Returns closest SkRGBA4f to SkColor. Only allowed if SkRGBA4f is unpremultiplied.
336 
337         @param color   Color with Alpha, red, blue, and green components
338         @return        SkColor as SkRGBA4f
339     */
340     static SkRGBA4f FromColor(SkColor color);  // impl. depends on kAT
341 
342     /** Returns closest SkColor to SkRGBA4f. Only allowed if SkRGBA4f is unpremultiplied.
343 
344         @return       color as SkColor
345     */
346     SkColor toSkColor() const;  // impl. depends on kAT
347 
348     /** Returns closest SkRGBA4f to SkPMColor. Only allowed if SkRGBA4f is premultiplied.
349 
350         @return        SkPMColor as SkRGBA4f
351     */
352     static SkRGBA4f FromPMColor(SkPMColor);  // impl. depends on kAT
353 
354     /** Returns SkRGBA4f premultiplied by alpha. Asserts at compile time if SkRGBA4f is
355         already premultiplied.
356 
357         @return       premultiplied color
358     */
premulSkRGBA4f359     SkRGBA4f<kPremul_SkAlphaType> premul() const {
360         static_assert(kAT == kUnpremul_SkAlphaType, "");
361         return { fR * fA, fG * fA, fB * fA, fA };
362     }
363 
364     /** Returns SkRGBA4f unpremultiplied by alpha. Asserts at compile time if SkRGBA4f is
365         already unpremultiplied.
366 
367         @return       unpremultiplied color
368     */
unpremulSkRGBA4f369     SkRGBA4f<kUnpremul_SkAlphaType> unpremul() const {
370         static_assert(kAT == kPremul_SkAlphaType, "");
371 
372         if (fA == 0.0f) {
373             return { 0, 0, 0, 0 };
374         } else {
375             float invAlpha = 1 / fA;
376             return { fR * invAlpha, fG * invAlpha, fB * invAlpha, fA };
377         }
378     }
379 
380     // This produces bytes in RGBA order (eg GrColor). Impl. is the same, regardless of kAT
381     uint32_t toBytes_RGBA() const;
382     static SkRGBA4f FromBytes_RGBA(uint32_t color);
383 
makeOpaqueSkRGBA4f384     SkRGBA4f makeOpaque() const {
385         return { fR, fG, fB, 1.0f };
386     }
387 };
388 
389 /** \struct SkColor4f
390     RGBA color value, holding four floating point components. Color components are always in
391     a known order, and are unpremultiplied.
392 
393     This is a specialization of SkRGBA4f. For details, @see SkRGBA4f.
394 */
395 using SkColor4f = SkRGBA4f<kUnpremul_SkAlphaType>;
396 
397 template <> SK_API SkColor4f SkColor4f::FromColor(SkColor);
398 template <> SK_API SkColor   SkColor4f::toSkColor() const;
399 
400 namespace SkColors {
401 constexpr SkColor4f kTransparent = {0, 0, 0, 0};
402 constexpr SkColor4f kBlack       = {0, 0, 0, 1};
403 constexpr SkColor4f kDkGray      = {0.25f, 0.25f, 0.25f, 1};
404 constexpr SkColor4f kGray        = {0.50f, 0.50f, 0.50f, 1};
405 constexpr SkColor4f kLtGray      = {0.75f, 0.75f, 0.75f, 1};
406 constexpr SkColor4f kWhite       = {1, 1, 1, 1};
407 constexpr SkColor4f kRed         = {1, 0, 0, 1};
408 constexpr SkColor4f kGreen       = {0, 1, 0, 1};
409 constexpr SkColor4f kBlue        = {0, 0, 1, 1};
410 constexpr SkColor4f kYellow      = {1, 1, 0, 1};
411 constexpr SkColor4f kCyan        = {0, 1, 1, 1};
412 constexpr SkColor4f kMagenta     = {1, 0, 1, 1};
413 }  // namespace SkColors
414 #endif
415