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