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 | |
| 8 | #ifndef SkColor_DEFINED |
| 9 | #define SkColor_DEFINED |
| 10 | |
| 11 | #include "SkScalar.h" |
dvonbeck | af0f6ef | 2016-08-19 11:23:15 -0700 | [diff] [blame] | 12 | #include "SkPoint3.h" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 13 | #include "SkTypes.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 14 | |
| 15 | /** \file SkColor.h |
| 16 | |
| 17 | Types and macros for colors |
| 18 | */ |
| 19 | |
| 20 | /** 8-bit type for an alpha value. 0xFF is 100% opaque, 0x00 is 100% transparent. |
| 21 | */ |
| 22 | typedef uint8_t SkAlpha; |
| 23 | /** 32 bit ARGB color value, not premultiplied. The color components are always in |
| 24 | a known order. This is different from SkPMColor, which has its bytes in a configuration |
| 25 | dependent order, to match the format of kARGB32 bitmaps. SkColor is the type used to |
| 26 | specify colors in SkPaint and in gradients. |
| 27 | */ |
| 28 | typedef uint32_t SkColor; |
| 29 | |
| 30 | /** Return a SkColor value from 8 bit component values |
| 31 | */ |
agl@chromium.org | c9c9ebb | 2010-07-28 17:10:30 +0000 | [diff] [blame] | 32 | static inline SkColor SkColorSetARGBInline(U8CPU a, U8CPU r, U8CPU g, U8CPU b) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 33 | { |
| 34 | SkASSERT(a <= 255 && r <= 255 && g <= 255 && b <= 255); |
| 35 | |
| 36 | return (a << 24) | (r << 16) | (g << 8) | (b << 0); |
| 37 | } |
| 38 | |
agl@chromium.org | c9c9ebb | 2010-07-28 17:10:30 +0000 | [diff] [blame] | 39 | #define SkColorSetARGBMacro(a, r, g, b) \ |
| 40 | static_cast<SkColor>( \ |
| 41 | (static_cast<U8CPU>(a) << 24) | \ |
| 42 | (static_cast<U8CPU>(r) << 16) | \ |
| 43 | (static_cast<U8CPU>(g) << 8) | \ |
| 44 | (static_cast<U8CPU>(b) << 0)) |
| 45 | |
| 46 | /** gcc will generate static initializers for code of this form: |
| 47 | * static const SkColor kMyColor = SkColorSetARGB(0xFF, 0x01, 0x02, 0x03) |
| 48 | * if SkColorSetARGB() is a static inline, but not if it's a macro. |
| 49 | */ |
| 50 | #if defined(NDEBUG) |
| 51 | #define SkColorSetARGB(a, r, g, b) SkColorSetARGBMacro(a, r, g, b) |
| 52 | #else |
| 53 | #define SkColorSetARGB(a, r, g, b) SkColorSetARGBInline(a, r, g, b) |
| 54 | #endif |
| 55 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 56 | /** Return a SkColor value from 8 bit component values, with an implied value |
| 57 | of 0xFF for alpha (fully opaque) |
| 58 | */ |
| 59 | #define SkColorSetRGB(r, g, b) SkColorSetARGB(0xFF, r, g, b) |
| 60 | |
| 61 | /** return the alpha byte from a SkColor value */ |
| 62 | #define SkColorGetA(color) (((color) >> 24) & 0xFF) |
| 63 | /** return the red byte from a SkColor value */ |
| 64 | #define SkColorGetR(color) (((color) >> 16) & 0xFF) |
| 65 | /** return the green byte from a SkColor value */ |
| 66 | #define SkColorGetG(color) (((color) >> 8) & 0xFF) |
| 67 | /** return the blue byte from a SkColor value */ |
| 68 | #define SkColorGetB(color) (((color) >> 0) & 0xFF) |
| 69 | |
| 70 | static inline SkColor SkColorSetA(SkColor c, U8CPU a) { |
| 71 | return (c & 0x00FFFFFF) | (a << 24); |
| 72 | } |
| 73 | |
| 74 | // common colors |
| 75 | |
commit-bot@chromium.org | 9fdb705 | 2013-07-19 17:43:27 +0000 | [diff] [blame] | 76 | #define SK_AlphaTRANSPARENT 0x00 //!< transparent SkAlpha value |
| 77 | #define SK_AlphaOPAQUE 0xFF //!< opaque SkAlpha value |
| 78 | |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 79 | #define SK_ColorTRANSPARENT 0x00000000 //!< transparent SkColor value |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 80 | |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 81 | #define SK_ColorBLACK 0xFF000000 //!< black SkColor value |
| 82 | #define SK_ColorDKGRAY 0xFF444444 //!< dark gray SkColor value |
| 83 | #define SK_ColorGRAY 0xFF888888 //!< gray SkColor value |
| 84 | #define SK_ColorLTGRAY 0xFFCCCCCC //!< light gray SkColor value |
| 85 | #define SK_ColorWHITE 0xFFFFFFFF //!< white SkColor value |
| 86 | |
| 87 | #define SK_ColorRED 0xFFFF0000 //!< red SkColor value |
| 88 | #define SK_ColorGREEN 0xFF00FF00 //!< green SkColor value |
| 89 | #define SK_ColorBLUE 0xFF0000FF //!< blue SkColor value |
| 90 | #define SK_ColorYELLOW 0xFFFFFF00 //!< yellow SkColor value |
| 91 | #define SK_ColorCYAN 0xFF00FFFF //!< cyan SkColor value |
| 92 | #define SK_ColorMAGENTA 0xFFFF00FF //!< magenta SkColor value |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 93 | |
| 94 | //////////////////////////////////////////////////////////////////////// |
| 95 | |
| 96 | /** Convert RGB components to HSV. |
| 97 | hsv[0] is Hue [0 .. 360) |
| 98 | hsv[1] is Saturation [0...1] |
| 99 | hsv[2] is Value [0...1] |
| 100 | @param red red component value [0..255] |
| 101 | @param green green component value [0..255] |
| 102 | @param blue blue component value [0..255] |
| 103 | @param hsv 3 element array which holds the resulting HSV components. |
| 104 | */ |
reed@google.com | f316634 | 2011-04-26 20:06:08 +0000 | [diff] [blame] | 105 | 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] | 106 | |
| 107 | /** Convert the argb color to its HSV components. |
| 108 | hsv[0] is Hue [0 .. 360) |
| 109 | hsv[1] is Saturation [0...1] |
| 110 | hsv[2] is Value [0...1] |
| 111 | @param color the argb color to convert. Note: the alpha component is ignored. |
| 112 | @param hsv 3 element array which holds the resulting HSV components. |
| 113 | */ |
reed | fbc1e29 | 2016-01-29 05:22:59 -0800 | [diff] [blame] | 114 | static inline void SkColorToHSV(SkColor color, SkScalar hsv[3]) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 115 | SkRGBToHSV(SkColorGetR(color), SkColorGetG(color), SkColorGetB(color), hsv); |
| 116 | } |
| 117 | |
| 118 | /** Convert HSV components to an ARGB color. The alpha component is passed through unchanged. |
| 119 | hsv[0] is Hue [0 .. 360) |
| 120 | hsv[1] is Saturation [0...1] |
| 121 | hsv[2] is Value [0...1] |
| 122 | If hsv values are out of range, they are pinned. |
| 123 | @param alpha the alpha component of the returned argb color. |
| 124 | @param hsv 3 element array which holds the input HSV components. |
| 125 | @return the resulting argb color |
| 126 | */ |
reed@google.com | f316634 | 2011-04-26 20:06:08 +0000 | [diff] [blame] | 127 | SK_API SkColor SkHSVToColor(U8CPU alpha, const SkScalar hsv[3]); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 128 | |
| 129 | /** Convert HSV components to an ARGB color. The alpha component set to 0xFF. |
| 130 | hsv[0] is Hue [0 .. 360) |
| 131 | hsv[1] is Saturation [0...1] |
| 132 | hsv[2] is Value [0...1] |
| 133 | If hsv values are out of range, they are pinned. |
| 134 | @param hsv 3 element array which holds the input HSV components. |
| 135 | @return the resulting argb color |
| 136 | */ |
reed | fbc1e29 | 2016-01-29 05:22:59 -0800 | [diff] [blame] | 137 | static inline SkColor SkHSVToColor(const SkScalar hsv[3]) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 138 | return SkHSVToColor(0xFF, hsv); |
| 139 | } |
| 140 | |
| 141 | //////////////////////////////////////////////////////////////////////// |
| 142 | |
| 143 | /** 32 bit ARGB color value, premultiplied. The byte order for this value is |
| 144 | configuration dependent, matching the format of kARGB32 bitmaps. This is different |
| 145 | from SkColor, which is nonpremultiplied, and is always in the same byte order. |
| 146 | */ |
| 147 | typedef uint32_t SkPMColor; |
| 148 | |
| 149 | /** Return a SkPMColor value from unpremultiplied 8 bit component values |
| 150 | */ |
ctguil@chromium.org | 7ffb1b2 | 2011-03-15 21:27:08 +0000 | [diff] [blame] | 151 | SK_API SkPMColor SkPreMultiplyARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 152 | /** Return a SkPMColor value from a SkColor value. This is done by multiplying the color |
| 153 | components by the color's alpha, and by arranging the bytes in a configuration |
| 154 | dependent order, to match the format of kARGB32 bitmaps. |
| 155 | */ |
ctguil@chromium.org | 7ffb1b2 | 2011-03-15 21:27:08 +0000 | [diff] [blame] | 156 | SK_API SkPMColor SkPreMultiplyColor(SkColor c); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 157 | |
| 158 | /** Define a function pointer type for combining two premultiplied colors |
| 159 | */ |
| 160 | typedef SkPMColor (*SkXfermodeProc)(SkPMColor src, SkPMColor dst); |
| 161 | |
reed | 6d3cef9 | 2016-01-22 01:04:29 -0800 | [diff] [blame] | 162 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 163 | |
reed | dd9ffea | 2016-02-18 12:39:14 -0800 | [diff] [blame] | 164 | struct SkPM4f; |
reed | 3125565 | 2016-02-08 12:56:56 -0800 | [diff] [blame] | 165 | |
reed | 6d3cef9 | 2016-01-22 01:04:29 -0800 | [diff] [blame] | 166 | /* |
| 167 | * The float values are 0...1 unpremultiplied |
| 168 | */ |
| 169 | struct SkColor4f { |
reed | 6d3cef9 | 2016-01-22 01:04:29 -0800 | [diff] [blame] | 170 | float fR; |
| 171 | float fG; |
| 172 | float fB; |
brianosman | e074d1f | 2016-06-24 06:31:47 -0700 | [diff] [blame] | 173 | float fA; |
reed | 6d3cef9 | 2016-01-22 01:04:29 -0800 | [diff] [blame] | 174 | |
| 175 | bool operator==(const SkColor4f& other) const { |
| 176 | return fA == other.fA && fR == other.fR && fG == other.fG && fB == other.fB; |
| 177 | } |
| 178 | bool operator!=(const SkColor4f& other) const { |
| 179 | return !(*this == other); |
| 180 | } |
| 181 | |
brianosman | e074d1f | 2016-06-24 06:31:47 -0700 | [diff] [blame] | 182 | const float* vec() const { return &fR; } |
| 183 | float* vec() { return &fR; } |
reed | 6d3cef9 | 2016-01-22 01:04:29 -0800 | [diff] [blame] | 184 | |
brianosman | e074d1f | 2016-06-24 06:31:47 -0700 | [diff] [blame] | 185 | static SkColor4f Pin(float r, float g, float b, float a); |
Hal Canary | 4cba3fe | 2016-12-07 14:59:27 -0500 | [diff] [blame] | 186 | /** Convert to SkColor4f, assuming SkColor is sRGB */ |
reed | 6d3cef9 | 2016-01-22 01:04:29 -0800 | [diff] [blame] | 187 | static SkColor4f FromColor(SkColor); |
dvonbeck | af0f6ef | 2016-08-19 11:23:15 -0700 | [diff] [blame] | 188 | static SkColor4f FromColor3f(SkColor3f, float a); |
reed | 6d3cef9 | 2016-01-22 01:04:29 -0800 | [diff] [blame] | 189 | |
brianosman | e074d1f | 2016-06-24 06:31:47 -0700 | [diff] [blame] | 190 | SkColor toSkColor() const; |
| 191 | |
reed | 6d3cef9 | 2016-01-22 01:04:29 -0800 | [diff] [blame] | 192 | SkColor4f pin() const { |
brianosman | e074d1f | 2016-06-24 06:31:47 -0700 | [diff] [blame] | 193 | return Pin(fR, fG, fB, fA); |
reed | 6d3cef9 | 2016-01-22 01:04:29 -0800 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | SkPM4f premul() const; |
| 197 | }; |
| 198 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 199 | #endif |