reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2006 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 3 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
Cary Clark | 9149954 | 2018-06-08 11:49:19 -0400 | [diff] [blame] | 8 | /* Generated by tools/bookmaker from include/core/SkColor.h and docs/SkColor_Reference.bmh |
Cary Clark | ca6a245 | 2018-06-14 13:21:08 -0400 | [diff] [blame] | 9 | on 2018-06-14 13:13:34. Additional documentation and examples can be found at: |
Cary Clark | 9149954 | 2018-06-08 11:49:19 -0400 | [diff] [blame] | 10 | 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 18 | #ifndef SkColor_DEFINED |
| 19 | #define SkColor_DEFINED |
| 20 | |
| 21 | #include "SkScalar.h" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 22 | #include "SkTypes.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 23 | |
| 24 | /** \file SkColor.h |
| 25 | |
Cary Clark | 9149954 | 2018-06-08 11:49:19 -0400 | [diff] [blame] | 26 | Types, consts, functions, and macros for colors. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 27 | */ |
| 28 | |
Cary Clark | 9149954 | 2018-06-08 11:49:19 -0400 | [diff] [blame] | 29 | /** 8-bit type for an alpha value. 255 is 100% opaque, zero is 100% transparent. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 30 | */ |
| 31 | typedef uint8_t SkAlpha; |
Cary Clark | 9149954 | 2018-06-08 11:49:19 -0400 | [diff] [blame] | 32 | |
| 33 | /** 32-bit ARGB color value, unpremultiplied. Color components are always in |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 34 | a known order. This is different from SkPMColor, which has its bytes in a configuration |
Cary Clark | 9149954 | 2018-06-08 11:49:19 -0400 | [diff] [blame] | 35 | 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 41 | */ |
| 42 | typedef uint32_t SkColor; |
| 43 | |
Cary Clark | 9149954 | 2018-06-08 11:49:19 -0400 | [diff] [blame] | 44 | /** 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 53 | */ |
Mike Klein | 37bbfe3 | 2017-09-28 09:47:45 -0400 | [diff] [blame] | 54 | static 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 57 | } |
agl@chromium.org | c9c9ebb | 2010-07-28 17:10:30 +0000 | [diff] [blame] | 58 | |
Cary Clark | 9149954 | 2018-06-08 11:49:19 -0400 | [diff] [blame] | 59 | /** Returns color value from 8-bit component values, with alpha set |
| 60 | fully opaque to 255. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 61 | */ |
| 62 | #define SkColorSetRGB(r, g, b) SkColorSetARGB(0xFF, r, g, b) |
| 63 | |
Cary Clark | 9149954 | 2018-06-08 11:49:19 -0400 | [diff] [blame] | 64 | /** Returns alpha byte from color value. |
| 65 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 66 | #define SkColorGetA(color) (((color) >> 24) & 0xFF) |
Cary Clark | 9149954 | 2018-06-08 11:49:19 -0400 | [diff] [blame] | 67 | |
| 68 | /** Returns red component of color, from zero to 255. |
| 69 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 70 | #define SkColorGetR(color) (((color) >> 16) & 0xFF) |
Cary Clark | 9149954 | 2018-06-08 11:49:19 -0400 | [diff] [blame] | 71 | |
| 72 | /** Returns green component of color, from zero to 255. |
| 73 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 74 | #define SkColorGetG(color) (((color) >> 8) & 0xFF) |
Cary Clark | 9149954 | 2018-06-08 11:49:19 -0400 | [diff] [blame] | 75 | |
| 76 | /** Returns blue component of color, from zero to 255. |
| 77 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 78 | #define SkColorGetB(color) (((color) >> 0) & 0xFF) |
| 79 | |
Cary Clark | 9149954 | 2018-06-08 11:49:19 -0400 | [diff] [blame] | 80 | /** 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 Zhang | 868d52b | 2017-04-07 12:28:21 -0700 | [diff] [blame] | 87 | static constexpr inline SkColor SkColorSetA(SkColor c, U8CPU a) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 88 | return (c & 0x00FFFFFF) | (a << 24); |
| 89 | } |
| 90 | |
Cary Clark | 9149954 | 2018-06-08 11:49:19 -0400 | [diff] [blame] | 91 | /** Represents fully transparent SkAlpha value. SkAlpha ranges from zero, |
| 92 | fully transparent; to 255, fully opaque. |
| 93 | */ |
Hal Canary | 84c0792 | 2018-04-16 13:44:04 -0400 | [diff] [blame] | 94 | constexpr SkAlpha SK_AlphaTRANSPARENT = 0x00; |
Cary Clark | 9149954 | 2018-06-08 11:49:19 -0400 | [diff] [blame] | 95 | |
| 96 | /** Represents fully opaque SkAlpha value. SkAlpha ranges from zero, |
| 97 | fully transparent; to 255, fully opaque. |
| 98 | */ |
Hal Canary | 84c0792 | 2018-04-16 13:44:04 -0400 | [diff] [blame] | 99 | constexpr SkAlpha SK_AlphaOPAQUE = 0xFF; |
commit-bot@chromium.org | 9fdb705 | 2013-07-19 17:43:27 +0000 | [diff] [blame] | 100 | |
Cary Clark | 9149954 | 2018-06-08 11:49:19 -0400 | [diff] [blame] | 101 | /** Represents fully transparent SkColor. May be used to initialize a destination |
| 102 | containing a mask or a non-rectangular image. |
| 103 | */ |
Hal Canary | 84c0792 | 2018-04-16 13:44:04 -0400 | [diff] [blame] | 104 | constexpr SkColor SK_ColorTRANSPARENT = SkColorSetARGB(0x00, 0x00, 0x00, 0x00); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 105 | |
Cary Clark | 9149954 | 2018-06-08 11:49:19 -0400 | [diff] [blame] | 106 | /** Represents fully opaque black. |
| 107 | */ |
Hal Canary | 84c0792 | 2018-04-16 13:44:04 -0400 | [diff] [blame] | 108 | constexpr SkColor SK_ColorBLACK = SkColorSetARGB(0xFF, 0x00, 0x00, 0x00); |
Cary Clark | 9149954 | 2018-06-08 11:49:19 -0400 | [diff] [blame] | 109 | |
| 110 | /** Represents fully opaque dark gray. |
Cary Clark | ca6a245 | 2018-06-14 13:21:08 -0400 | [diff] [blame] | 111 | Note that SVG dark gray is equivalent to 0xFFA9A9A9. |
Cary Clark | 9149954 | 2018-06-08 11:49:19 -0400 | [diff] [blame] | 112 | */ |
Hal Canary | 84c0792 | 2018-04-16 13:44:04 -0400 | [diff] [blame] | 113 | constexpr SkColor SK_ColorDKGRAY = SkColorSetARGB(0xFF, 0x44, 0x44, 0x44); |
Cary Clark | 9149954 | 2018-06-08 11:49:19 -0400 | [diff] [blame] | 114 | |
| 115 | /** Represents fully opaque gray. |
Cary Clark | ca6a245 | 2018-06-14 13:21:08 -0400 | [diff] [blame] | 116 | Note that HTML gray is equivalent to 0xFF808080. |
Cary Clark | 9149954 | 2018-06-08 11:49:19 -0400 | [diff] [blame] | 117 | */ |
Hal Canary | 84c0792 | 2018-04-16 13:44:04 -0400 | [diff] [blame] | 118 | constexpr SkColor SK_ColorGRAY = SkColorSetARGB(0xFF, 0x88, 0x88, 0x88); |
Cary Clark | 9149954 | 2018-06-08 11:49:19 -0400 | [diff] [blame] | 119 | |
Cary Clark | ca6a245 | 2018-06-14 13:21:08 -0400 | [diff] [blame] | 120 | /** Represents fully opaque light gray. HTML silver is equivalent to 0xFFC0C0C0. |
| 121 | Note that SVG light gray is equivalent to 0xFFD3D3D3. |
Cary Clark | 9149954 | 2018-06-08 11:49:19 -0400 | [diff] [blame] | 122 | */ |
Hal Canary | 84c0792 | 2018-04-16 13:44:04 -0400 | [diff] [blame] | 123 | constexpr SkColor SK_ColorLTGRAY = SkColorSetARGB(0xFF, 0xCC, 0xCC, 0xCC); |
Cary Clark | 9149954 | 2018-06-08 11:49:19 -0400 | [diff] [blame] | 124 | |
| 125 | /** Represents fully opaque white. |
| 126 | */ |
Hal Canary | 84c0792 | 2018-04-16 13:44:04 -0400 | [diff] [blame] | 127 | constexpr SkColor SK_ColorWHITE = SkColorSetARGB(0xFF, 0xFF, 0xFF, 0xFF); |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 128 | |
Cary Clark | 9149954 | 2018-06-08 11:49:19 -0400 | [diff] [blame] | 129 | /** Represents fully opaque red. |
| 130 | */ |
Hal Canary | 84c0792 | 2018-04-16 13:44:04 -0400 | [diff] [blame] | 131 | constexpr SkColor SK_ColorRED = SkColorSetARGB(0xFF, 0xFF, 0x00, 0x00); |
Cary Clark | 9149954 | 2018-06-08 11:49:19 -0400 | [diff] [blame] | 132 | |
Cary Clark | ca6a245 | 2018-06-14 13:21:08 -0400 | [diff] [blame] | 133 | /** Represents fully opaque green. HTML lime is equivalent. |
| 134 | Note that HTML green is equivalent to 0xFF008000. |
Cary Clark | 9149954 | 2018-06-08 11:49:19 -0400 | [diff] [blame] | 135 | */ |
Hal Canary | 84c0792 | 2018-04-16 13:44:04 -0400 | [diff] [blame] | 136 | constexpr SkColor SK_ColorGREEN = SkColorSetARGB(0xFF, 0x00, 0xFF, 0x00); |
Cary Clark | 9149954 | 2018-06-08 11:49:19 -0400 | [diff] [blame] | 137 | |
| 138 | /** Represents fully opaque blue. |
| 139 | */ |
Hal Canary | 84c0792 | 2018-04-16 13:44:04 -0400 | [diff] [blame] | 140 | constexpr SkColor SK_ColorBLUE = SkColorSetARGB(0xFF, 0x00, 0x00, 0xFF); |
Cary Clark | 9149954 | 2018-06-08 11:49:19 -0400 | [diff] [blame] | 141 | |
| 142 | /** Represents fully opaque yellow. |
| 143 | */ |
Hal Canary | 84c0792 | 2018-04-16 13:44:04 -0400 | [diff] [blame] | 144 | constexpr SkColor SK_ColorYELLOW = SkColorSetARGB(0xFF, 0xFF, 0xFF, 0x00); |
Cary Clark | 9149954 | 2018-06-08 11:49:19 -0400 | [diff] [blame] | 145 | |
Cary Clark | ca6a245 | 2018-06-14 13:21:08 -0400 | [diff] [blame] | 146 | /** Represents fully opaque cyan. HTML aqua is equivalent. |
Cary Clark | 9149954 | 2018-06-08 11:49:19 -0400 | [diff] [blame] | 147 | */ |
Hal Canary | 84c0792 | 2018-04-16 13:44:04 -0400 | [diff] [blame] | 148 | constexpr SkColor SK_ColorCYAN = SkColorSetARGB(0xFF, 0x00, 0xFF, 0xFF); |
Cary Clark | 9149954 | 2018-06-08 11:49:19 -0400 | [diff] [blame] | 149 | |
Cary Clark | ca6a245 | 2018-06-14 13:21:08 -0400 | [diff] [blame] | 150 | /** Represents fully opaque magenta. HTML fuchsia is equivalent. |
Cary Clark | 9149954 | 2018-06-08 11:49:19 -0400 | [diff] [blame] | 151 | */ |
Hal Canary | 84c0792 | 2018-04-16 13:44:04 -0400 | [diff] [blame] | 152 | constexpr SkColor SK_ColorMAGENTA = SkColorSetARGB(0xFF, 0xFF, 0x00, 0xFF); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 153 | |
Cary Clark | 9149954 | 2018-06-08 11:49:19 -0400 | [diff] [blame] | 154 | /** 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 158 | |
Cary Clark | 9149954 | 2018-06-08 11:49:19 -0400 | [diff] [blame] | 159 | @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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 163 | */ |
reed@google.com | f316634 | 2011-04-26 20:06:08 +0000 | [diff] [blame] | 164 | SK_API void SkRGBToHSV(U8CPU red, U8CPU green, U8CPU blue, SkScalar hsv[3]); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 165 | |
Cary Clark | 9149954 | 2018-06-08 11:49:19 -0400 | [diff] [blame] | 166 | /** 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 173 | */ |
reed | fbc1e29 | 2016-01-29 05:22:59 -0800 | [diff] [blame] | 174 | static inline void SkColorToHSV(SkColor color, SkScalar hsv[3]) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 175 | SkRGBToHSV(SkColorGetR(color), SkColorGetG(color), SkColorGetB(color), hsv); |
| 176 | } |
| 177 | |
Cary Clark | 9149954 | 2018-06-08 11:49:19 -0400 | [diff] [blame] | 178 | /** 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 188 | */ |
reed@google.com | f316634 | 2011-04-26 20:06:08 +0000 | [diff] [blame] | 189 | SK_API SkColor SkHSVToColor(U8CPU alpha, const SkScalar hsv[3]); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 190 | |
Cary Clark | 9149954 | 2018-06-08 11:49:19 -0400 | [diff] [blame] | 191 | /** 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 200 | */ |
reed | fbc1e29 | 2016-01-29 05:22:59 -0800 | [diff] [blame] | 201 | static inline SkColor SkHSVToColor(const SkScalar hsv[3]) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 202 | return SkHSVToColor(0xFF, hsv); |
| 203 | } |
| 204 | |
Cary Clark | 9149954 | 2018-06-08 11:49:19 -0400 | [diff] [blame] | 205 | /** 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 209 | */ |
| 210 | typedef uint32_t SkPMColor; |
| 211 | |
Cary Clark | 9149954 | 2018-06-08 11:49:19 -0400 | [diff] [blame] | 212 | /** 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 219 | */ |
ctguil@chromium.org | 7ffb1b2 | 2011-03-15 21:27:08 +0000 | [diff] [blame] | 220 | SK_API SkPMColor SkPreMultiplyARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b); |
Cary Clark | 9149954 | 2018-06-08 11:49:19 -0400 | [diff] [blame] | 221 | |
| 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 227 | */ |
ctguil@chromium.org | 7ffb1b2 | 2011-03-15 21:27:08 +0000 | [diff] [blame] | 228 | SK_API SkPMColor SkPreMultiplyColor(SkColor c); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 229 | |
reed | dd9ffea | 2016-02-18 12:39:14 -0800 | [diff] [blame] | 230 | struct SkPM4f; |
reed | 3125565 | 2016-02-08 12:56:56 -0800 | [diff] [blame] | 231 | |
Cary Clark | 9149954 | 2018-06-08 11:49:19 -0400 | [diff] [blame] | 232 | /** \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. |
reed | 6d3cef9 | 2016-01-22 01:04:29 -0800 | [diff] [blame] | 235 | |
Cary Clark | 9149954 | 2018-06-08 11:49:19 -0400 | [diff] [blame] | 236 | 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 | */ |
| 242 | struct 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 | */ |
reed | 6d3cef9 | 2016-01-22 01:04:29 -0800 | [diff] [blame] | 253 | bool operator==(const SkColor4f& other) const { |
| 254 | return fA == other.fA && fR == other.fR && fG == other.fG && fB == other.fB; |
| 255 | } |
Cary Clark | 9149954 | 2018-06-08 11:49:19 -0400 | [diff] [blame] | 256 | |
| 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 | */ |
reed | 6d3cef9 | 2016-01-22 01:04:29 -0800 | [diff] [blame] | 263 | bool operator!=(const SkColor4f& other) const { |
| 264 | return !(*this == other); |
| 265 | } |
| 266 | |
Cary Clark | 9149954 | 2018-06-08 11:49:19 -0400 | [diff] [blame] | 267 | /** Returns SkColor4f components as a read-only array. |
| 268 | |
| 269 | @return components as read-only array |
| 270 | */ |
brianosman | e074d1f | 2016-06-24 06:31:47 -0700 | [diff] [blame] | 271 | const float* vec() const { return &fR; } |
Cary Clark | 9149954 | 2018-06-08 11:49:19 -0400 | [diff] [blame] | 272 | |
| 273 | /** Returns SkColor4f components as a read-only array. |
| 274 | |
| 275 | @return components as read-only array |
| 276 | */ |
brianosman | e074d1f | 2016-06-24 06:31:47 -0700 | [diff] [blame] | 277 | float* vec() { return &fR; } |
reed | 6d3cef9 | 2016-01-22 01:04:29 -0800 | [diff] [blame] | 278 | |
Cary Clark | 9149954 | 2018-06-08 11:49:19 -0400 | [diff] [blame] | 279 | /** 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 | */ |
brianosman | e074d1f | 2016-06-24 06:31:47 -0700 | [diff] [blame] | 287 | static SkColor4f Pin(float r, float g, float b, float a); |
Cary Clark | 9149954 | 2018-06-08 11:49:19 -0400 | [diff] [blame] | 288 | |
| 289 | /** Converts to closest SkColor4f. |
| 290 | |
| 291 | @param SkColor color with alpha, red, blue, and green components |
| 292 | @return SkColor4f equivalent |
| 293 | */ |
reed | 6d3cef9 | 2016-01-22 01:04:29 -0800 | [diff] [blame] | 294 | static SkColor4f FromColor(SkColor); |
| 295 | |
Cary Clark | 9149954 | 2018-06-08 11:49:19 -0400 | [diff] [blame] | 296 | /** Converts to closest SkColor. |
| 297 | |
| 298 | @return closest color |
| 299 | */ |
brianosman | e074d1f | 2016-06-24 06:31:47 -0700 | [diff] [blame] | 300 | SkColor toSkColor() const; |
| 301 | |
Cary Clark | 9149954 | 2018-06-08 11:49:19 -0400 | [diff] [blame] | 302 | /** Returns SkColor4f with all components in the range from zero to one. |
| 303 | |
| 304 | @return SkColor4f with valid components |
| 305 | */ |
reed | 6d3cef9 | 2016-01-22 01:04:29 -0800 | [diff] [blame] | 306 | SkColor4f pin() const { |
brianosman | e074d1f | 2016-06-24 06:31:47 -0700 | [diff] [blame] | 307 | return Pin(fR, fG, fB, fA); |
reed | 6d3cef9 | 2016-01-22 01:04:29 -0800 | [diff] [blame] | 308 | } |
| 309 | |
Brian Osman | 81cbd03 | 2018-09-21 11:09:15 -0400 | [diff] [blame^] | 310 | /** 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 Clark | 9149954 | 2018-06-08 11:49:19 -0400 | [diff] [blame] | 327 | /** Internal use only. |
| 328 | |
| 329 | @return premultiplied color |
| 330 | */ |
Brian Osman | 81cbd03 | 2018-09-21 11:09:15 -0400 | [diff] [blame^] | 331 | SkPM4f toPM4f() const; |
reed | 6d3cef9 | 2016-01-22 01:04:29 -0800 | [diff] [blame] | 332 | }; |
| 333 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 334 | #endif |