blob: caf30acfcf54d7651996619831c2f819cf6112d2 [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00003 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00004 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00006 */
7
Cary Clark91499542018-06-08 11:49:19 -04008/* Generated by tools/bookmaker from include/core/SkColor.h and docs/SkColor_Reference.bmh
Cary Clarkca6a2452018-06-14 13:21:08 -04009 on 2018-06-14 13:13:34. Additional documentation and examples can be found at:
Cary Clark91499542018-06-08 11:49:19 -040010 https://skia.org/user/api/SkColor_Reference
11
12 You may edit either file directly. Structural changes to public interfaces require
13 editing both files. After editing docs/SkColor_Reference.bmh, run:
14 bookmaker -b docs -i include/core/SkColor.h -p
15 to create an updated version of this file.
16 */
17
reed@android.com8a1c16f2008-12-17 15:59:43 +000018#ifndef SkColor_DEFINED
19#define SkColor_DEFINED
20
21#include "SkScalar.h"
bungemand3ebb482015-08-05 13:57:49 -070022#include "SkTypes.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000023
24/** \file SkColor.h
25
Cary Clark91499542018-06-08 11:49:19 -040026 Types, consts, functions, and macros for colors.
reed@android.com8a1c16f2008-12-17 15:59:43 +000027*/
28
Cary Clark91499542018-06-08 11:49:19 -040029/** 8-bit type for an alpha value. 255 is 100% opaque, zero is 100% transparent.
reed@android.com8a1c16f2008-12-17 15:59:43 +000030*/
31typedef uint8_t SkAlpha;
Cary Clark91499542018-06-08 11:49:19 -040032
33/** 32-bit ARGB color value, unpremultiplied. Color components are always in
reed@android.com8a1c16f2008-12-17 15:59:43 +000034 a known order. This is different from SkPMColor, which has its bytes in a configuration
Cary Clark91499542018-06-08 11:49:19 -040035 dependent order, to match the format of kBGRA_8888_SkColorType bitmaps. SkColor
36 is the type used to specify colors in SkPaint and in gradients.
37
38 Color that is premultiplied has the same component values as color
39 that is unpremultiplied if alpha is 255, fully opaque, although may have the
40 component values in a different order.
reed@android.com8a1c16f2008-12-17 15:59:43 +000041*/
42typedef uint32_t SkColor;
43
Cary Clark91499542018-06-08 11:49:19 -040044/** Returns color value from 8-bit component values. Asserts if SK_DEBUG is defined
45 if a, r, g, or b exceed 255. Since color is unpremultiplied, a may be smaller
46 than the largest of r, g, and b.
47
48 @param a amount of alpha, from fully transparent (0) to fully opaque (255)
49 @param r amount of red, from no red (0) to full red (255)
50 @param g amount of green, from no green (0) to full green (255)
51 @param b amount of blue, from no blue (0) to full blue (255)
52 @return color and alpha, unpremultiplied
reed@android.com8a1c16f2008-12-17 15:59:43 +000053*/
Mike Klein37bbfe32017-09-28 09:47:45 -040054static constexpr inline SkColor SkColorSetARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) {
55 return SkASSERT(a <= 255 && r <= 255 && g <= 255 && b <= 255),
56 (a << 24) | (r << 16) | (g << 8) | (b << 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +000057}
agl@chromium.orgc9c9ebb2010-07-28 17:10:30 +000058
Cary Clark91499542018-06-08 11:49:19 -040059/** Returns color value from 8-bit component values, with alpha set
60 fully opaque to 255.
reed@android.com8a1c16f2008-12-17 15:59:43 +000061*/
62#define SkColorSetRGB(r, g, b) SkColorSetARGB(0xFF, r, g, b)
63
Cary Clark91499542018-06-08 11:49:19 -040064/** Returns alpha byte from color value.
65*/
reed@android.com8a1c16f2008-12-17 15:59:43 +000066#define SkColorGetA(color) (((color) >> 24) & 0xFF)
Cary Clark91499542018-06-08 11:49:19 -040067
68/** Returns red component of color, from zero to 255.
69*/
reed@android.com8a1c16f2008-12-17 15:59:43 +000070#define SkColorGetR(color) (((color) >> 16) & 0xFF)
Cary Clark91499542018-06-08 11:49:19 -040071
72/** Returns green component of color, from zero to 255.
73*/
reed@android.com8a1c16f2008-12-17 15:59:43 +000074#define SkColorGetG(color) (((color) >> 8) & 0xFF)
Cary Clark91499542018-06-08 11:49:19 -040075
76/** Returns blue component of color, from zero to 255.
77*/
reed@android.com8a1c16f2008-12-17 15:59:43 +000078#define SkColorGetB(color) (((color) >> 0) & 0xFF)
79
Cary Clark91499542018-06-08 11:49:19 -040080/** Returns unpremultiplied color with red, blue, and green set from c; and alpha set
81 from a. Alpha component of c is ignored and is replaced by a in result.
82
83 @param c packed RGB, eight bits per component
84 @param a alpha: transparent at zero, fully opaque at 255
85 @return color with transparency
86*/
Lei Zhang868d52b2017-04-07 12:28:21 -070087static constexpr inline SkColor SkColorSetA(SkColor c, U8CPU a) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000088 return (c & 0x00FFFFFF) | (a << 24);
89}
90
Cary Clark91499542018-06-08 11:49:19 -040091/** Represents fully transparent SkAlpha value. SkAlpha ranges from zero,
92 fully transparent; to 255, fully opaque.
93*/
Hal Canary84c07922018-04-16 13:44:04 -040094constexpr SkAlpha SK_AlphaTRANSPARENT = 0x00;
Cary Clark91499542018-06-08 11:49:19 -040095
96/** Represents fully opaque SkAlpha value. SkAlpha ranges from zero,
97 fully transparent; to 255, fully opaque.
98*/
Hal Canary84c07922018-04-16 13:44:04 -040099constexpr SkAlpha SK_AlphaOPAQUE = 0xFF;
commit-bot@chromium.org9fdb7052013-07-19 17:43:27 +0000100
Cary Clark91499542018-06-08 11:49:19 -0400101/** Represents fully transparent SkColor. May be used to initialize a destination
102 containing a mask or a non-rectangular image.
103*/
Hal Canary84c07922018-04-16 13:44:04 -0400104constexpr SkColor SK_ColorTRANSPARENT = SkColorSetARGB(0x00, 0x00, 0x00, 0x00);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105
Cary Clark91499542018-06-08 11:49:19 -0400106/** Represents fully opaque black.
107*/
Hal Canary84c07922018-04-16 13:44:04 -0400108constexpr SkColor SK_ColorBLACK = SkColorSetARGB(0xFF, 0x00, 0x00, 0x00);
Cary Clark91499542018-06-08 11:49:19 -0400109
110/** Represents fully opaque dark gray.
Cary Clarkca6a2452018-06-14 13:21:08 -0400111 Note that SVG dark gray is equivalent to 0xFFA9A9A9.
Cary Clark91499542018-06-08 11:49:19 -0400112*/
Hal Canary84c07922018-04-16 13:44:04 -0400113constexpr SkColor SK_ColorDKGRAY = SkColorSetARGB(0xFF, 0x44, 0x44, 0x44);
Cary Clark91499542018-06-08 11:49:19 -0400114
115/** Represents fully opaque gray.
Cary Clarkca6a2452018-06-14 13:21:08 -0400116 Note that HTML gray is equivalent to 0xFF808080.
Cary Clark91499542018-06-08 11:49:19 -0400117*/
Hal Canary84c07922018-04-16 13:44:04 -0400118constexpr SkColor SK_ColorGRAY = SkColorSetARGB(0xFF, 0x88, 0x88, 0x88);
Cary Clark91499542018-06-08 11:49:19 -0400119
Cary Clarkca6a2452018-06-14 13:21:08 -0400120/** Represents fully opaque light gray. HTML silver is equivalent to 0xFFC0C0C0.
121 Note that SVG light gray is equivalent to 0xFFD3D3D3.
Cary Clark91499542018-06-08 11:49:19 -0400122*/
Hal Canary84c07922018-04-16 13:44:04 -0400123constexpr SkColor SK_ColorLTGRAY = SkColorSetARGB(0xFF, 0xCC, 0xCC, 0xCC);
Cary Clark91499542018-06-08 11:49:19 -0400124
125/** Represents fully opaque white.
126*/
Hal Canary84c07922018-04-16 13:44:04 -0400127constexpr SkColor SK_ColorWHITE = SkColorSetARGB(0xFF, 0xFF, 0xFF, 0xFF);
junov@google.comdbfac8a2012-12-06 21:47:40 +0000128
Cary Clark91499542018-06-08 11:49:19 -0400129/** Represents fully opaque red.
130*/
Hal Canary84c07922018-04-16 13:44:04 -0400131constexpr SkColor SK_ColorRED = SkColorSetARGB(0xFF, 0xFF, 0x00, 0x00);
Cary Clark91499542018-06-08 11:49:19 -0400132
Cary Clarkca6a2452018-06-14 13:21:08 -0400133/** Represents fully opaque green. HTML lime is equivalent.
134 Note that HTML green is equivalent to 0xFF008000.
Cary Clark91499542018-06-08 11:49:19 -0400135*/
Hal Canary84c07922018-04-16 13:44:04 -0400136constexpr SkColor SK_ColorGREEN = SkColorSetARGB(0xFF, 0x00, 0xFF, 0x00);
Cary Clark91499542018-06-08 11:49:19 -0400137
138/** Represents fully opaque blue.
139*/
Hal Canary84c07922018-04-16 13:44:04 -0400140constexpr SkColor SK_ColorBLUE = SkColorSetARGB(0xFF, 0x00, 0x00, 0xFF);
Cary Clark91499542018-06-08 11:49:19 -0400141
142/** Represents fully opaque yellow.
143*/
Hal Canary84c07922018-04-16 13:44:04 -0400144constexpr SkColor SK_ColorYELLOW = SkColorSetARGB(0xFF, 0xFF, 0xFF, 0x00);
Cary Clark91499542018-06-08 11:49:19 -0400145
Cary Clarkca6a2452018-06-14 13:21:08 -0400146/** Represents fully opaque cyan. HTML aqua is equivalent.
Cary Clark91499542018-06-08 11:49:19 -0400147*/
Hal Canary84c07922018-04-16 13:44:04 -0400148constexpr SkColor SK_ColorCYAN = SkColorSetARGB(0xFF, 0x00, 0xFF, 0xFF);
Cary Clark91499542018-06-08 11:49:19 -0400149
Cary Clarkca6a2452018-06-14 13:21:08 -0400150/** Represents fully opaque magenta. HTML fuchsia is equivalent.
Cary Clark91499542018-06-08 11:49:19 -0400151*/
Hal Canary84c07922018-04-16 13:44:04 -0400152constexpr SkColor SK_ColorMAGENTA = SkColorSetARGB(0xFF, 0xFF, 0x00, 0xFF);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000153
Cary Clark91499542018-06-08 11:49:19 -0400154/** Converts RGB to its HSV components.
155 hsv[0] contains hsv hue, a value from zero to less than 360.
156 hsv[1] contains hsv saturation, a value from zero to one.
157 hsv[2] contains hsv value, a value from zero to one.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000158
Cary Clark91499542018-06-08 11:49:19 -0400159 @param red red component value from zero to 255
160 @param green green component value from zero to 255
161 @param blue blue component value from zero to 255
162 @param hsv three element array which holds the resulting HSV components
reed@android.com8a1c16f2008-12-17 15:59:43 +0000163*/
reed@google.comf3166342011-04-26 20:06:08 +0000164SK_API void SkRGBToHSV(U8CPU red, U8CPU green, U8CPU blue, SkScalar hsv[3]);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000165
Cary Clark91499542018-06-08 11:49:19 -0400166/** Converts ARGB to its HSV components. Alpha in ARGB is ignored.
167 hsv[0] contains hsv hue, and is assigned a value from zero to less than 360.
168 hsv[1] contains hsv saturation, a value from zero to one.
169 hsv[2] contains hsv value, a value from zero to one.
170
171 @param color ARGB color to convert
172 @param hsv three element array which holds the resulting HSV components
reed@android.com8a1c16f2008-12-17 15:59:43 +0000173*/
reedfbc1e292016-01-29 05:22:59 -0800174static inline void SkColorToHSV(SkColor color, SkScalar hsv[3]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000175 SkRGBToHSV(SkColorGetR(color), SkColorGetG(color), SkColorGetB(color), hsv);
176}
177
Cary Clark91499542018-06-08 11:49:19 -0400178/** Converts HSV components to an ARGB color. Alpha is passed through unchanged.
179 hsv[0] represents hsv hue, an angle from zero to less than 360.
180 hsv[1] represents hsv saturation, and varies from zero to one.
181 hsv[2] represents hsv value, and varies from zero to one.
182
183 Out of range hsv values are pinned.
184
185 @param alpha alpha component of the returned ARGB color
186 @param hsv three element array which holds the input HSV components
187 @return ARGB equivalent to HSV
reed@android.com8a1c16f2008-12-17 15:59:43 +0000188*/
reed@google.comf3166342011-04-26 20:06:08 +0000189SK_API SkColor SkHSVToColor(U8CPU alpha, const SkScalar hsv[3]);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000190
Cary Clark91499542018-06-08 11:49:19 -0400191/** Converts HSV components to an ARGB color. Alpha is set to 255.
192 hsv[0] represents hsv hue, an angle from zero to less than 360.
193 hsv[1] represents hsv saturation, and varies from zero to one.
194 hsv[2] represents hsv value, and varies from zero to one.
195
196 Out of range hsv values are pinned.
197
198 @param hsv three element array which holds the input HSV components
199 @return RGB equivalent to HSV
reed@android.com8a1c16f2008-12-17 15:59:43 +0000200*/
reedfbc1e292016-01-29 05:22:59 -0800201static inline SkColor SkHSVToColor(const SkScalar hsv[3]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000202 return SkHSVToColor(0xFF, hsv);
203}
204
Cary Clark91499542018-06-08 11:49:19 -0400205/** 32-bit ARGB color value, premultiplied. The byte order for this value is
206 configuration dependent, matching the format of kBGRA_8888_SkColorType bitmaps.
207 This is different from SkColor, which is unpremultiplied, and is always in the
208 same byte order.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000209*/
210typedef uint32_t SkPMColor;
211
Cary Clark91499542018-06-08 11:49:19 -0400212/** Returns a SkPMColor value from unpremultiplied 8-bit component values.
213
214 @param a amount of alpha, from fully transparent (0) to fully opaque (255)
215 @param r amount of red, from no red (0) to full red (255)
216 @param g amount of green, from no green (0) to full green (255)
217 @param b amount of blue, from no blue (0) to full blue (255)
218 @return premultiplied color
reed@android.com8a1c16f2008-12-17 15:59:43 +0000219*/
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +0000220SK_API SkPMColor SkPreMultiplyARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b);
Cary Clark91499542018-06-08 11:49:19 -0400221
222/** Returns pmcolor closest to color c. Multiplies c RGB components by the c alpha,
223 and arranges the bytes to match the format of kN32_SkColorType.
224
225 @param c unpremultiplied ARGB color
226 @return premultiplied color
reed@android.com8a1c16f2008-12-17 15:59:43 +0000227*/
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +0000228SK_API SkPMColor SkPreMultiplyColor(SkColor c);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000229
reeddd9ffea2016-02-18 12:39:14 -0800230struct SkPM4f;
reed31255652016-02-08 12:56:56 -0800231
Cary Clark91499542018-06-08 11:49:19 -0400232/** \struct SkColor4f
233 Each component is stored as a 32-bit single precision floating point float value.
234 All values are allowed, but only the range from zero to one is meaningful.
reed6d3cef92016-01-22 01:04:29 -0800235
Cary Clark91499542018-06-08 11:49:19 -0400236 Each component is independent of the others; fA alpha is not premultiplied
237 with fG green, fB blue, or fR red.
238
239 Values smaller than zero or larger than one are allowed. Values out of range
240 may be used with SkBlendMode so that the final component is in range.
241*/
242struct SK_API SkColor4f {
243 float fR; //!< red component
244 float fG; //!< green component
245 float fB; //!< blue component
246 float fA; //!< alpha component
247
248 /** Compares SkColor4f with other, and returns true if all components are equivalent.
249
250 @param other SkColor4f to compare
251 @return true if SkColor4f equals other
252 */
reed6d3cef92016-01-22 01:04:29 -0800253 bool operator==(const SkColor4f& other) const {
254 return fA == other.fA && fR == other.fR && fG == other.fG && fB == other.fB;
255 }
Cary Clark91499542018-06-08 11:49:19 -0400256
257 /** Compares SkColor4f with other, and returns true if all components are not
258 equivalent.
259
260 @param other SkColor4f to compare
261 @return true if SkColor4f is not equal to other
262 */
reed6d3cef92016-01-22 01:04:29 -0800263 bool operator!=(const SkColor4f& other) const {
264 return !(*this == other);
265 }
266
Cary Clark91499542018-06-08 11:49:19 -0400267 /** Returns SkColor4f components as a read-only array.
268
269 @return components as read-only array
270 */
brianosmane074d1f2016-06-24 06:31:47 -0700271 const float* vec() const { return &fR; }
Cary Clark91499542018-06-08 11:49:19 -0400272
273 /** Returns SkColor4f components as a read-only array.
274
275 @return components as read-only array
276 */
brianosmane074d1f2016-06-24 06:31:47 -0700277 float* vec() { return &fR; }
reed6d3cef92016-01-22 01:04:29 -0800278
Cary Clark91499542018-06-08 11:49:19 -0400279 /** Constructs and returns SkColor4f with each component pinned from zero to one.
280
281 @param r red component
282 @param g green component
283 @param b blue component
284 @param a alpha component
285 @return SkColor4f with valid components
286 */
brianosmane074d1f2016-06-24 06:31:47 -0700287 static SkColor4f Pin(float r, float g, float b, float a);
Cary Clark91499542018-06-08 11:49:19 -0400288
289 /** Converts to closest SkColor4f.
290
291 @param SkColor color with alpha, red, blue, and green components
292 @return SkColor4f equivalent
293 */
reed6d3cef92016-01-22 01:04:29 -0800294 static SkColor4f FromColor(SkColor);
295
Cary Clark91499542018-06-08 11:49:19 -0400296 /** Converts to closest SkColor.
297
298 @return closest color
299 */
brianosmane074d1f2016-06-24 06:31:47 -0700300 SkColor toSkColor() const;
301
Cary Clark91499542018-06-08 11:49:19 -0400302 /** Returns SkColor4f with all components in the range from zero to one.
303
304 @return SkColor4f with valid components
305 */
reed6d3cef92016-01-22 01:04:29 -0800306 SkColor4f pin() const {
brianosmane074d1f2016-06-24 06:31:47 -0700307 return Pin(fR, fG, fB, fA);
reed6d3cef92016-01-22 01:04:29 -0800308 }
309
Brian Osman81cbd032018-09-21 11:09:15 -0400310 /** Returns SkColor4f with all components premultiplied by alpha.
311
312 @return premultiplied color
313 */
314 SkColor4f premul() const {
315 return { fR * fA, fG * fA, fB * fA, fA };
316 }
317
318 SkColor4f unpremul() const {
319 if (fA == 0.0f) {
320 return { 0, 0, 0, 0 };
321 } else {
322 float invAlpha = 1 / fA;
323 return { fR * invAlpha, fG * invAlpha, fB * invAlpha, fA };
324 }
325 }
326
Cary Clark91499542018-06-08 11:49:19 -0400327 /** Internal use only.
328
329 @return premultiplied color
330 */
Brian Osman81cbd032018-09-21 11:09:15 -0400331 SkPM4f toPM4f() const;
reed6d3cef92016-01-22 01:04:29 -0800332};
333
reed@android.com8a1c16f2008-12-17 15:59:43 +0000334#endif