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 "SkImageInfo.h"
12#include "SkScalar.h"
13#include "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*/
22typedef 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*/
33typedef 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*/
45static 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*/
78static 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*/
85constexpr SkAlpha SK_AlphaTRANSPARENT = 0x00;
86
87/** Represents fully opaque SkAlpha value. SkAlpha ranges from zero,
88 fully transparent; to 255, fully opaque.
89*/
90constexpr 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*/
95constexpr SkColor SK_ColorTRANSPARENT = SkColorSetARGB(0x00, 0x00, 0x00, 0x00);
96
97/** Represents fully opaque black.
98*/
99constexpr 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*/
104constexpr SkColor SK_ColorDKGRAY = SkColorSetARGB(0xFF, 0x44, 0x44, 0x44);
105
106/** Represents fully opaque gray.
107 Note that HTML gray is equivalent to 0xFF808080.
108*/
109constexpr 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*/
114constexpr SkColor SK_ColorLTGRAY = SkColorSetARGB(0xFF, 0xCC, 0xCC, 0xCC);
115
116/** Represents fully opaque white.
117*/
118constexpr SkColor SK_ColorWHITE = SkColorSetARGB(0xFF, 0xFF, 0xFF, 0xFF);
119
120/** Represents fully opaque red.
121*/
122constexpr 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*/
127constexpr SkColor SK_ColorGREEN = SkColorSetARGB(0xFF, 0x00, 0xFF, 0x00);
128
129/** Represents fully opaque blue.
130*/
131constexpr SkColor SK_ColorBLUE = SkColorSetARGB(0xFF, 0x00, 0x00, 0xFF);
132
133/** Represents fully opaque yellow.
134*/
135constexpr SkColor SK_ColorYELLOW = SkColorSetARGB(0xFF, 0xFF, 0xFF, 0x00);
136
137/** Represents fully opaque cyan. HTML aqua is equivalent.
138*/
139constexpr SkColor SK_ColorCYAN = SkColorSetARGB(0xFF, 0x00, 0xFF, 0xFF);
140
141/** Represents fully opaque magenta. HTML fuchsia is equivalent.
142*/
143constexpr 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*/
155SK_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*/
165static 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*/
180SK_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*/
192static 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*/
201typedef 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*/
211SK_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*/
219SK_API SkPMColor SkPreMultiplyColor(SkColor c);
220
221/** \struct SkRGBA4f
222 RGBA color value, holding four floating point components. Color components are always in
223 a known order. kAT determines if the SkRGBA4f's R, G, and B components are premultiplied
224 by alpha or not.
225
226 Skia's public API always uses unpremultiplied colors, which can be stored as
227 SkRGBA4f<kUnpremul_SkAlphaType>. For convenience, this type can also be referred to
228 as SkColor4f.
229*/
230template <SkAlphaType kAT>
231struct SkRGBA4f {
232 float fR; //!< red component
233 float fG; //!< green component
234 float fB; //!< blue component
235 float fA; //!< alpha component
236
237 /** Compares SkRGBA4f with other, and returns true if all components are equal.
238
239 @param other SkRGBA4f to compare
240 @return true if SkRGBA4f equals other
241 */
242 bool operator==(const SkRGBA4f& other) const {
243 return fA == other.fA && fR == other.fR && fG == other.fG && fB == other.fB;
244 }
245
246 /** Compares SkRGBA4f with other, and returns true if not all components are equal.
247
248 @param other SkRGBA4f to compare
249 @return true if SkRGBA4f is not equal to other
250 */
251 bool operator!=(const SkRGBA4f& other) const {
252 return !(*this == other);
253 }
254
255 /** Returns SkRGBA4f multiplied by scale.
256
257 @param scale value to multiply by
258 @return SkRGBA4f as (fR * scale, fG * scale, fB * scale, fA * scale)
259 */
260 SkRGBA4f operator*(float scale) const {
261 return { fR * scale, fG * scale, fB * scale, fA * scale };
262 }
263
264 /** Returns SkRGBA4f multiplied component-wise by scale.
265
266 @param scale SkRGBA4f to multiply by
267 @return SkRGBA4f as (fR * scale.fR, fG * scale.fG, fB * scale.fB, fA * scale.fA)
268 */
269 SkRGBA4f operator*(const SkRGBA4f& scale) const {
270 return { fR * scale.fR, fG * scale.fG, fB * scale.fB, fA * scale.fA };
271 }
272
273 /** Returns a pointer to components of SkRGBA4f, for array access.
274
275 @return pointer to array [fR, fG, fB, fA]
276 */
277 const float* vec() const { return &fR; }
278
279 /** Returns a pointer to components of SkRGBA4f, for array access.
280
281 @return pointer to array [fR, fG, fB, fA]
282 */
283 float* vec() { return &fR; }
284
285 /** Returns one component. Asserts if index is out of range and SK_DEBUG is defined.
286
287 @param index one of: 0 (fR), 1 (fG), 2 (fB), 3 (fA)
288 @return value corresponding to index
289 */
290 float operator[](int index) const {
291 SkASSERT(index >= 0 && index < 4);
292 return this->vec()[index];
293 }
294
295 /** Returns one component. Asserts if index is out of range and SK_DEBUG is defined.
296
297 @param index one of: 0 (fR), 1 (fG), 2 (fB), 3 (fA)
298 @return value corresponding to index
299 */
300 float& operator[](int index) {
301 SkASSERT(index >= 0 && index < 4);
302 return this->vec()[index];
303 }
304
305 /** Returns true if SkRGBA4f is an opaque color. Asserts if fA is out of range and
306 SK_DEBUG is defined.
307
308 @return true if SkRGBA4f is opaque
309 */
310 bool isOpaque() const {
311 SkASSERT(fA <= 1.0f && fA >= 0.0f);
312 return fA == 1.0f;
313 }
314
315 /** Returns true if all channels are in [0, 1]. */
316 bool fitsInBytes() const {
317 SkASSERT(fA >= 0.0f && fA <= 1.0f);
318 return fR >= 0.0f && fR <= 1.0f &&
319 fG >= 0.0f && fG <= 1.0f &&
320 fB >= 0.0f && fB <= 1.0f;
321 }
322
323 /** Returns closest SkRGBA4f to SkColor. Only allowed if SkRGBA4f is unpremultiplied.
324
325 @param color Color with Alpha, red, blue, and green components
326 @return SkColor as SkRGBA4f
327 */
328 static SkRGBA4f FromColor(SkColor color); // impl. depends on kAT
329
330 /** Returns closest SkColor to SkRGBA4f. Only allowed if SkRGBA4f is unpremultiplied.
331
332 @return color as SkColor
333 */
334 SkColor toSkColor() const; // impl. depends on kAT
335
336 /** Returns closest SkRGBA4f to SkPMColor. Only allowed if SkRGBA4f is premultiplied.
337
338 @return SkPMColor as SkRGBA4f
339 */
340 static SkRGBA4f FromPMColor(SkPMColor); // impl. depends on kAT
341
342 /** Returns SkRGBA4f premultiplied by alpha. Asserts at compile time if SkRGBA4f is
343 already premultiplied.
344
345 @return premultiplied color
346 */
347 SkRGBA4f<kPremul_SkAlphaType> premul() const {
348 static_assert(kAT == kUnpremul_SkAlphaType, "");
349 return { fR * fA, fG * fA, fB * fA, fA };
350 }
351
352 /** Returns SkRGBA4f unpremultiplied by alpha. Asserts at compile time if SkRGBA4f is
353 already unpremultiplied.
354
355 @return unpremultiplied color
356 */
357 SkRGBA4f<kUnpremul_SkAlphaType> unpremul() const {
358 static_assert(kAT == kPremul_SkAlphaType, "");
359
360 if (fA == 0.0f) {
361 return { 0, 0, 0, 0 };
362 } else {
363 float invAlpha = 1 / fA;
364 return { fR * invAlpha, fG * invAlpha, fB * invAlpha, fA };
365 }
366 }
367
368 // This produces bytes in RGBA order (eg GrColor). Impl. is the same, regardless of kAT
369 uint32_t toBytes_RGBA() const;
370 static SkRGBA4f FromBytes_RGBA(uint32_t color);
371
372 SkRGBA4f makeOpaque() const {
373 return { fR, fG, fB, 1.0f };
374 }
375};
376
377/** \struct SkColor4f
378 RGBA color value, holding four floating point components. Color components are always in
379 a known order, and are unpremultiplied.
380
381 This is a specialization of SkRGBA4f. For details, @see SkRGBA4f.
382*/
383using SkColor4f = SkRGBA4f<kUnpremul_SkAlphaType>;
384
385template <> SK_API SkColor4f SkColor4f::FromColor(SkColor);
386template <> SK_API SkColor SkColor4f::toSkColor() const;
387
388#endif
389