blob: 2566aa6cf7ef9eba3aed224d00e762cef46a386e [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
Brian Osmand25b7c12018-09-21 16:01:59 -040021#include "SkImageInfo.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000022#include "SkScalar.h"
bungemand3ebb482015-08-05 13:57:49 -070023#include "SkTypes.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000024
25/** \file SkColor.h
26
Cary Clark91499542018-06-08 11:49:19 -040027 Types, consts, functions, and macros for colors.
reed@android.com8a1c16f2008-12-17 15:59:43 +000028*/
29
Cary Clark91499542018-06-08 11:49:19 -040030/** 8-bit type for an alpha value. 255 is 100% opaque, zero is 100% transparent.
reed@android.com8a1c16f2008-12-17 15:59:43 +000031*/
32typedef uint8_t SkAlpha;
Cary Clark91499542018-06-08 11:49:19 -040033
34/** 32-bit ARGB color value, unpremultiplied. Color components are always in
reed@android.com8a1c16f2008-12-17 15:59:43 +000035 a known order. This is different from SkPMColor, which has its bytes in a configuration
Cary Clark91499542018-06-08 11:49:19 -040036 dependent order, to match the format of kBGRA_8888_SkColorType bitmaps. SkColor
37 is the type used to specify colors in SkPaint and in gradients.
38
39 Color that is premultiplied has the same component values as color
40 that is unpremultiplied if alpha is 255, fully opaque, although may have the
41 component values in a different order.
reed@android.com8a1c16f2008-12-17 15:59:43 +000042*/
43typedef uint32_t SkColor;
44
Cary Clark91499542018-06-08 11:49:19 -040045/** Returns color value from 8-bit component values. Asserts if SK_DEBUG is defined
46 if a, r, g, or b exceed 255. Since color is unpremultiplied, a may be smaller
47 than the largest of r, g, and b.
48
49 @param a amount of alpha, from fully transparent (0) to fully opaque (255)
50 @param r amount of red, from no red (0) to full red (255)
51 @param g amount of green, from no green (0) to full green (255)
52 @param b amount of blue, from no blue (0) to full blue (255)
53 @return color and alpha, unpremultiplied
reed@android.com8a1c16f2008-12-17 15:59:43 +000054*/
Mike Klein37bbfe32017-09-28 09:47:45 -040055static constexpr inline SkColor SkColorSetARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) {
56 return SkASSERT(a <= 255 && r <= 255 && g <= 255 && b <= 255),
57 (a << 24) | (r << 16) | (g << 8) | (b << 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +000058}
agl@chromium.orgc9c9ebb2010-07-28 17:10:30 +000059
Cary Clark91499542018-06-08 11:49:19 -040060/** Returns color value from 8-bit component values, with alpha set
61 fully opaque to 255.
reed@android.com8a1c16f2008-12-17 15:59:43 +000062*/
63#define SkColorSetRGB(r, g, b) SkColorSetARGB(0xFF, r, g, b)
64
Cary Clark91499542018-06-08 11:49:19 -040065/** Returns alpha byte from color value.
66*/
reed@android.com8a1c16f2008-12-17 15:59:43 +000067#define SkColorGetA(color) (((color) >> 24) & 0xFF)
Cary Clark91499542018-06-08 11:49:19 -040068
69/** Returns red component of color, from zero to 255.
70*/
reed@android.com8a1c16f2008-12-17 15:59:43 +000071#define SkColorGetR(color) (((color) >> 16) & 0xFF)
Cary Clark91499542018-06-08 11:49:19 -040072
73/** Returns green component of color, from zero to 255.
74*/
reed@android.com8a1c16f2008-12-17 15:59:43 +000075#define SkColorGetG(color) (((color) >> 8) & 0xFF)
Cary Clark91499542018-06-08 11:49:19 -040076
77/** Returns blue component of color, from zero to 255.
78*/
reed@android.com8a1c16f2008-12-17 15:59:43 +000079#define SkColorGetB(color) (((color) >> 0) & 0xFF)
80
Cary Clark91499542018-06-08 11:49:19 -040081/** Returns unpremultiplied color with red, blue, and green set from c; and alpha set
82 from a. Alpha component of c is ignored and is replaced by a in result.
83
84 @param c packed RGB, eight bits per component
85 @param a alpha: transparent at zero, fully opaque at 255
86 @return color with transparency
87*/
Lei Zhang868d52b2017-04-07 12:28:21 -070088static constexpr inline SkColor SkColorSetA(SkColor c, U8CPU a) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000089 return (c & 0x00FFFFFF) | (a << 24);
90}
91
Cary Clark91499542018-06-08 11:49:19 -040092/** Represents fully transparent SkAlpha value. SkAlpha ranges from zero,
93 fully transparent; to 255, fully opaque.
94*/
Hal Canary84c07922018-04-16 13:44:04 -040095constexpr SkAlpha SK_AlphaTRANSPARENT = 0x00;
Cary Clark91499542018-06-08 11:49:19 -040096
97/** Represents fully opaque SkAlpha value. SkAlpha ranges from zero,
98 fully transparent; to 255, fully opaque.
99*/
Hal Canary84c07922018-04-16 13:44:04 -0400100constexpr SkAlpha SK_AlphaOPAQUE = 0xFF;
commit-bot@chromium.org9fdb7052013-07-19 17:43:27 +0000101
Cary Clark91499542018-06-08 11:49:19 -0400102/** Represents fully transparent SkColor. May be used to initialize a destination
103 containing a mask or a non-rectangular image.
104*/
Hal Canary84c07922018-04-16 13:44:04 -0400105constexpr SkColor SK_ColorTRANSPARENT = SkColorSetARGB(0x00, 0x00, 0x00, 0x00);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106
Cary Clark91499542018-06-08 11:49:19 -0400107/** Represents fully opaque black.
108*/
Hal Canary84c07922018-04-16 13:44:04 -0400109constexpr SkColor SK_ColorBLACK = SkColorSetARGB(0xFF, 0x00, 0x00, 0x00);
Cary Clark91499542018-06-08 11:49:19 -0400110
111/** Represents fully opaque dark gray.
Cary Clarkca6a2452018-06-14 13:21:08 -0400112 Note that SVG dark gray is equivalent to 0xFFA9A9A9.
Cary Clark91499542018-06-08 11:49:19 -0400113*/
Hal Canary84c07922018-04-16 13:44:04 -0400114constexpr SkColor SK_ColorDKGRAY = SkColorSetARGB(0xFF, 0x44, 0x44, 0x44);
Cary Clark91499542018-06-08 11:49:19 -0400115
116/** Represents fully opaque gray.
Cary Clarkca6a2452018-06-14 13:21:08 -0400117 Note that HTML gray is equivalent to 0xFF808080.
Cary Clark91499542018-06-08 11:49:19 -0400118*/
Hal Canary84c07922018-04-16 13:44:04 -0400119constexpr SkColor SK_ColorGRAY = SkColorSetARGB(0xFF, 0x88, 0x88, 0x88);
Cary Clark91499542018-06-08 11:49:19 -0400120
Cary Clarkca6a2452018-06-14 13:21:08 -0400121/** Represents fully opaque light gray. HTML silver is equivalent to 0xFFC0C0C0.
122 Note that SVG light gray is equivalent to 0xFFD3D3D3.
Cary Clark91499542018-06-08 11:49:19 -0400123*/
Hal Canary84c07922018-04-16 13:44:04 -0400124constexpr SkColor SK_ColorLTGRAY = SkColorSetARGB(0xFF, 0xCC, 0xCC, 0xCC);
Cary Clark91499542018-06-08 11:49:19 -0400125
126/** Represents fully opaque white.
127*/
Hal Canary84c07922018-04-16 13:44:04 -0400128constexpr SkColor SK_ColorWHITE = SkColorSetARGB(0xFF, 0xFF, 0xFF, 0xFF);
junov@google.comdbfac8a2012-12-06 21:47:40 +0000129
Cary Clark91499542018-06-08 11:49:19 -0400130/** Represents fully opaque red.
131*/
Hal Canary84c07922018-04-16 13:44:04 -0400132constexpr SkColor SK_ColorRED = SkColorSetARGB(0xFF, 0xFF, 0x00, 0x00);
Cary Clark91499542018-06-08 11:49:19 -0400133
Cary Clarkca6a2452018-06-14 13:21:08 -0400134/** Represents fully opaque green. HTML lime is equivalent.
135 Note that HTML green is equivalent to 0xFF008000.
Cary Clark91499542018-06-08 11:49:19 -0400136*/
Hal Canary84c07922018-04-16 13:44:04 -0400137constexpr SkColor SK_ColorGREEN = SkColorSetARGB(0xFF, 0x00, 0xFF, 0x00);
Cary Clark91499542018-06-08 11:49:19 -0400138
139/** Represents fully opaque blue.
140*/
Hal Canary84c07922018-04-16 13:44:04 -0400141constexpr SkColor SK_ColorBLUE = SkColorSetARGB(0xFF, 0x00, 0x00, 0xFF);
Cary Clark91499542018-06-08 11:49:19 -0400142
143/** Represents fully opaque yellow.
144*/
Hal Canary84c07922018-04-16 13:44:04 -0400145constexpr SkColor SK_ColorYELLOW = SkColorSetARGB(0xFF, 0xFF, 0xFF, 0x00);
Cary Clark91499542018-06-08 11:49:19 -0400146
Cary Clarkca6a2452018-06-14 13:21:08 -0400147/** Represents fully opaque cyan. HTML aqua is equivalent.
Cary Clark91499542018-06-08 11:49:19 -0400148*/
Hal Canary84c07922018-04-16 13:44:04 -0400149constexpr SkColor SK_ColorCYAN = SkColorSetARGB(0xFF, 0x00, 0xFF, 0xFF);
Cary Clark91499542018-06-08 11:49:19 -0400150
Cary Clarkca6a2452018-06-14 13:21:08 -0400151/** Represents fully opaque magenta. HTML fuchsia is equivalent.
Cary Clark91499542018-06-08 11:49:19 -0400152*/
Hal Canary84c07922018-04-16 13:44:04 -0400153constexpr SkColor SK_ColorMAGENTA = SkColorSetARGB(0xFF, 0xFF, 0x00, 0xFF);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000154
Cary Clark91499542018-06-08 11:49:19 -0400155/** Converts RGB to its HSV components.
156 hsv[0] contains hsv hue, a value from zero to less than 360.
157 hsv[1] contains hsv saturation, a value from zero to one.
158 hsv[2] contains hsv value, a value from zero to one.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000159
Cary Clark91499542018-06-08 11:49:19 -0400160 @param red red component value from zero to 255
161 @param green green component value from zero to 255
162 @param blue blue component value from zero to 255
163 @param hsv three element array which holds the resulting HSV components
reed@android.com8a1c16f2008-12-17 15:59:43 +0000164*/
reed@google.comf3166342011-04-26 20:06:08 +0000165SK_API void SkRGBToHSV(U8CPU red, U8CPU green, U8CPU blue, SkScalar hsv[3]);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000166
Cary Clark91499542018-06-08 11:49:19 -0400167/** Converts ARGB to its HSV components. Alpha in ARGB is ignored.
168 hsv[0] contains hsv hue, and is assigned a value from zero to less than 360.
169 hsv[1] contains hsv saturation, a value from zero to one.
170 hsv[2] contains hsv value, a value from zero to one.
171
172 @param color ARGB color to convert
173 @param hsv three element array which holds the resulting HSV components
reed@android.com8a1c16f2008-12-17 15:59:43 +0000174*/
reedfbc1e292016-01-29 05:22:59 -0800175static inline void SkColorToHSV(SkColor color, SkScalar hsv[3]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000176 SkRGBToHSV(SkColorGetR(color), SkColorGetG(color), SkColorGetB(color), hsv);
177}
178
Cary Clark91499542018-06-08 11:49:19 -0400179/** Converts HSV components to an ARGB color. Alpha is passed through unchanged.
180 hsv[0] represents hsv hue, an angle from zero to less than 360.
181 hsv[1] represents hsv saturation, and varies from zero to one.
182 hsv[2] represents hsv value, and varies from zero to one.
183
184 Out of range hsv values are pinned.
185
186 @param alpha alpha component of the returned ARGB color
187 @param hsv three element array which holds the input HSV components
188 @return ARGB equivalent to HSV
reed@android.com8a1c16f2008-12-17 15:59:43 +0000189*/
reed@google.comf3166342011-04-26 20:06:08 +0000190SK_API SkColor SkHSVToColor(U8CPU alpha, const SkScalar hsv[3]);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000191
Cary Clark91499542018-06-08 11:49:19 -0400192/** Converts HSV components to an ARGB color. Alpha is set to 255.
193 hsv[0] represents hsv hue, an angle from zero to less than 360.
194 hsv[1] represents hsv saturation, and varies from zero to one.
195 hsv[2] represents hsv value, and varies from zero to one.
196
197 Out of range hsv values are pinned.
198
199 @param hsv three element array which holds the input HSV components
200 @return RGB equivalent to HSV
reed@android.com8a1c16f2008-12-17 15:59:43 +0000201*/
reedfbc1e292016-01-29 05:22:59 -0800202static inline SkColor SkHSVToColor(const SkScalar hsv[3]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000203 return SkHSVToColor(0xFF, hsv);
204}
205
Cary Clark91499542018-06-08 11:49:19 -0400206/** 32-bit ARGB color value, premultiplied. The byte order for this value is
207 configuration dependent, matching the format of kBGRA_8888_SkColorType bitmaps.
208 This is different from SkColor, which is unpremultiplied, and is always in the
209 same byte order.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000210*/
211typedef uint32_t SkPMColor;
212
Cary Clark91499542018-06-08 11:49:19 -0400213/** Returns a SkPMColor value from unpremultiplied 8-bit component values.
214
215 @param a amount of alpha, from fully transparent (0) to fully opaque (255)
216 @param r amount of red, from no red (0) to full red (255)
217 @param g amount of green, from no green (0) to full green (255)
218 @param b amount of blue, from no blue (0) to full blue (255)
219 @return premultiplied color
reed@android.com8a1c16f2008-12-17 15:59:43 +0000220*/
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +0000221SK_API SkPMColor SkPreMultiplyARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b);
Cary Clark91499542018-06-08 11:49:19 -0400222
223/** Returns pmcolor closest to color c. Multiplies c RGB components by the c alpha,
224 and arranges the bytes to match the format of kN32_SkColorType.
225
226 @param c unpremultiplied ARGB color
227 @return premultiplied color
reed@android.com8a1c16f2008-12-17 15:59:43 +0000228*/
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +0000229SK_API SkPMColor SkPreMultiplyColor(SkColor c);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000230
reeddd9ffea2016-02-18 12:39:14 -0800231struct SkPM4f;
reed31255652016-02-08 12:56:56 -0800232
Brian Osmand25b7c12018-09-21 16:01:59 -0400233template <SkAlphaType kAT>
Mike Kleinf6a4bd82018-09-21 16:38:57 -0400234struct SkRGBA4f {
Brian Osmand25b7c12018-09-21 16:01:59 -0400235 float fR;
236 float fG;
237 float fB;
238 float fA;
reed6d3cef92016-01-22 01:04:29 -0800239
Brian Osmand25b7c12018-09-21 16:01:59 -0400240 bool operator==(const SkRGBA4f& other) const {
reed6d3cef92016-01-22 01:04:29 -0800241 return fA == other.fA && fR == other.fR && fG == other.fG && fB == other.fB;
242 }
Brian Osmand25b7c12018-09-21 16:01:59 -0400243 bool operator!=(const SkRGBA4f& other) const {
reed6d3cef92016-01-22 01:04:29 -0800244 return !(*this == other);
245 }
246
brianosmane074d1f2016-06-24 06:31:47 -0700247 const float* vec() const { return &fR; }
Brian Osmand25b7c12018-09-21 16:01:59 -0400248 float* vec() { return &fR; }
Cary Clark91499542018-06-08 11:49:19 -0400249
Brian Osmand25b7c12018-09-21 16:01:59 -0400250 static SkRGBA4f Pin(float r, float g, float b, float a); // impl. depends on kAT
251 SkRGBA4f pin() const { return Pin(fR, fG, fB, fA); }
Cary Clark91499542018-06-08 11:49:19 -0400252
Brian Osmand25b7c12018-09-21 16:01:59 -0400253 static SkRGBA4f FromColor(SkColor); // impl. depends on kAT
254 SkColor toSkColor() const; // impl. depends on kAT
reed6d3cef92016-01-22 01:04:29 -0800255
Brian Osmand25b7c12018-09-21 16:01:59 -0400256 SkRGBA4f<kPremul_SkAlphaType> premul() const {
257 static_assert(kAT == kUnpremul_SkAlphaType, "");
Brian Osman81cbd032018-09-21 11:09:15 -0400258 return { fR * fA, fG * fA, fB * fA, fA };
259 }
260
Brian Osmand25b7c12018-09-21 16:01:59 -0400261 SkRGBA4f<kUnpremul_SkAlphaType> unpremul() const {
262 static_assert(kAT == kPremul_SkAlphaType, "");
263
Brian Osman81cbd032018-09-21 11:09:15 -0400264 if (fA == 0.0f) {
265 return { 0, 0, 0, 0 };
266 } else {
267 float invAlpha = 1 / fA;
268 return { fR * invAlpha, fG * invAlpha, fB * invAlpha, fA };
269 }
270 }
271
Brian Osmand25b7c12018-09-21 16:01:59 -0400272 // TODO: remove?
273 SkPM4f toPM4f() const; // impl. depends on kAT
reed6d3cef92016-01-22 01:04:29 -0800274};
275
Brian Osmand25b7c12018-09-21 16:01:59 -0400276using SkColor4f = SkRGBA4f<kUnpremul_SkAlphaType>;
Mike Kleinf6a4bd82018-09-21 16:38:57 -0400277template <> SK_API SkColor4f SkColor4f::FromColor(SkColor);
278template <> SK_API SkColor SkColor4f::toSkColor() const;
Brian Osmand25b7c12018-09-21 16:01:59 -0400279
reed@android.com8a1c16f2008-12-17 15:59:43 +0000280#endif