18a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/*
2ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2006 The Android Open Source Project
38a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com *
4ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be
5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file.
68a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */
78a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
88a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifndef SkColor_DEFINED
98a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkColor_DEFINED
108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkScalar.h"
12d3ebb48320cf1b7e969974673e4bd7743816985ebungeman#include "SkTypes.h"
138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** \file SkColor.h
158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Types and macros for colors
178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** 8-bit type for an alpha value. 0xFF is 100% opaque, 0x00 is 100% transparent.
208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef uint8_t SkAlpha;
228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** 32 bit ARGB color value, not premultiplied. The color components are always in
238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    a known order. This is different from SkPMColor, which has its bytes in a configuration
248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    dependent order, to match the format of kARGB32 bitmaps. SkColor is the type used to
258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    specify colors in SkPaint and in gradients.
268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef uint32_t SkColor;
288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Return a SkColor value from 8 bit component values
308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
3137bbfe3ea4745170df3f5ef2f4c1e1332af49ceaMike Kleinstatic constexpr inline SkColor SkColorSetARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) {
3237bbfe3ea4745170df3f5ef2f4c1e1332af49ceaMike Klein    return SkASSERT(a <= 255 && r <= 255 && g <= 255 && b <= 255),
3337bbfe3ea4745170df3f5ef2f4c1e1332af49ceaMike Klein           (a << 24) | (r << 16) | (g << 8) | (b << 0);
348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3537bbfe3ea4745170df3f5ef2f4c1e1332af49ceaMike Klein// Legacy aliases.
3637bbfe3ea4745170df3f5ef2f4c1e1332af49ceaMike Klein#define SkColorSetARGBInline SkColorSetARGB
3737bbfe3ea4745170df3f5ef2f4c1e1332af49ceaMike Klein#define SkColorSetARGBMacro  SkColorSetARGB
38c9c9ebbc29b62e7d3b06ff18023b09faeb49d618agl@chromium.org
398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Return a SkColor value from 8 bit component values, with an implied value
408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    of 0xFF for alpha (fully opaque)
418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkColorSetRGB(r, g, b)  SkColorSetARGB(0xFF, r, g, b)
438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** return the alpha byte from a SkColor value */
458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkColorGetA(color)      (((color) >> 24) & 0xFF)
468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** return the red byte from a SkColor value */
478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkColorGetR(color)      (((color) >> 16) & 0xFF)
488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** return the green byte from a SkColor value */
498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkColorGetG(color)      (((color) >>  8) & 0xFF)
508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** return the blue byte from a SkColor value */
518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkColorGetB(color)      (((color) >>  0) & 0xFF)
528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
53868d52be4e78f6dff330a1b931b5e2f8c55e87b4Lei Zhangstatic constexpr inline SkColor SkColorSetA(SkColor c, U8CPU a) {
548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return (c & 0x00FFFFFF) | (a << 24);
558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// common colors
588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5924f20a8639430cfb0374c91bbee8edd36a0e9877danakj/** transparent SkAlpha value */
6024f20a8639430cfb0374c91bbee8edd36a0e9877danakj#define SK_AlphaTRANSPARENT static_cast<SkAlpha>(0x00)
6124f20a8639430cfb0374c91bbee8edd36a0e9877danakj/** opaque SkAlpha value */
6224f20a8639430cfb0374c91bbee8edd36a0e9877danakj#define SK_AlphaOPAQUE      static_cast<SkAlpha>(0xFF)
6324f20a8639430cfb0374c91bbee8edd36a0e9877danakj
6424f20a8639430cfb0374c91bbee8edd36a0e9877danakj/** transparent SkColor value */
6524f20a8639430cfb0374c91bbee8edd36a0e9877danakj#define SK_ColorTRANSPARENT static_cast<SkColor>(0x00000000)
6624f20a8639430cfb0374c91bbee8edd36a0e9877danakj
6724f20a8639430cfb0374c91bbee8edd36a0e9877danakj/** black SkColor value */
6824f20a8639430cfb0374c91bbee8edd36a0e9877danakj#define SK_ColorBLACK       static_cast<SkColor>(0xFF000000)
6924f20a8639430cfb0374c91bbee8edd36a0e9877danakj/** dark gray SkColor value */
7024f20a8639430cfb0374c91bbee8edd36a0e9877danakj#define SK_ColorDKGRAY      static_cast<SkColor>(0xFF444444)
7124f20a8639430cfb0374c91bbee8edd36a0e9877danakj/** gray SkColor value */
7224f20a8639430cfb0374c91bbee8edd36a0e9877danakj#define SK_ColorGRAY        static_cast<SkColor>(0xFF888888)
7324f20a8639430cfb0374c91bbee8edd36a0e9877danakj/** light gray SkColor value */
7424f20a8639430cfb0374c91bbee8edd36a0e9877danakj#define SK_ColorLTGRAY      static_cast<SkColor>(0xFFCCCCCC)
7524f20a8639430cfb0374c91bbee8edd36a0e9877danakj/** white SkColor value */
7624f20a8639430cfb0374c91bbee8edd36a0e9877danakj#define SK_ColorWHITE       static_cast<SkColor>(0xFFFFFFFF)
7724f20a8639430cfb0374c91bbee8edd36a0e9877danakj
7824f20a8639430cfb0374c91bbee8edd36a0e9877danakj/** red SkColor value */
7924f20a8639430cfb0374c91bbee8edd36a0e9877danakj#define SK_ColorRED         static_cast<SkColor>(0xFFFF0000)
8024f20a8639430cfb0374c91bbee8edd36a0e9877danakj/** green SkColor value */
8124f20a8639430cfb0374c91bbee8edd36a0e9877danakj#define SK_ColorGREEN       static_cast<SkColor>(0xFF00FF00)
8224f20a8639430cfb0374c91bbee8edd36a0e9877danakj/** blue SkColor value */
8324f20a8639430cfb0374c91bbee8edd36a0e9877danakj#define SK_ColorBLUE        static_cast<SkColor>(0xFF0000FF)
8424f20a8639430cfb0374c91bbee8edd36a0e9877danakj/** yellow SkColor value */
8524f20a8639430cfb0374c91bbee8edd36a0e9877danakj#define SK_ColorYELLOW      static_cast<SkColor>(0xFFFFFF00)
8624f20a8639430cfb0374c91bbee8edd36a0e9877danakj/** cyan SkColor value */
8724f20a8639430cfb0374c91bbee8edd36a0e9877danakj#define SK_ColorCYAN        static_cast<SkColor>(0xFF00FFFF)
8824f20a8639430cfb0374c91bbee8edd36a0e9877danakj/** magenta SkColor value */
8924f20a8639430cfb0374c91bbee8edd36a0e9877danakj#define SK_ColorMAGENTA     static_cast<SkColor>(0xFFFF00FF)
908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com////////////////////////////////////////////////////////////////////////
928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Convert RGB components to HSV.
948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        hsv[0] is Hue [0 .. 360)
958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        hsv[1] is Saturation [0...1]
968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        hsv[2] is Value [0...1]
978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    @param red  red component value [0..255]
988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    @param green  green component value [0..255]
998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    @param blue  blue component value [0..255]
1008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    @param hsv  3 element array which holds the resulting HSV components.
1018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
102f31663403b1b96c2a1c6717acab5093b0117f187reed@google.comSK_API void SkRGBToHSV(U8CPU red, U8CPU green, U8CPU blue, SkScalar hsv[3]);
1038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Convert the argb color to its HSV components.
1058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        hsv[0] is Hue [0 .. 360)
1068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        hsv[1] is Saturation [0...1]
1078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        hsv[2] is Value [0...1]
1088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    @param color the argb color to convert. Note: the alpha component is ignored.
1098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    @param hsv  3 element array which holds the resulting HSV components.
1108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
111fbc1e296b2e98dc76de533a2bb45d9ccc8c2498freedstatic inline void SkColorToHSV(SkColor color, SkScalar hsv[3]) {
1128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkRGBToHSV(SkColorGetR(color), SkColorGetG(color), SkColorGetB(color), hsv);
1138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
1148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Convert HSV components to an ARGB color. The alpha component is passed through unchanged.
1168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        hsv[0] is Hue [0 .. 360)
1178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        hsv[1] is Saturation [0...1]
1188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        hsv[2] is Value [0...1]
1198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    If hsv values are out of range, they are pinned.
1208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    @param alpha the alpha component of the returned argb color.
1218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    @param hsv  3 element array which holds the input HSV components.
1228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    @return the resulting argb color
1238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
124f31663403b1b96c2a1c6717acab5093b0117f187reed@google.comSK_API SkColor SkHSVToColor(U8CPU alpha, const SkScalar hsv[3]);
1258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Convert HSV components to an ARGB color. The alpha component set to 0xFF.
1278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        hsv[0] is Hue [0 .. 360)
1288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        hsv[1] is Saturation [0...1]
1298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        hsv[2] is Value [0...1]
1308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    If hsv values are out of range, they are pinned.
1318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    @param hsv  3 element array which holds the input HSV components.
1328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    @return the resulting argb color
1338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
134fbc1e296b2e98dc76de533a2bb45d9ccc8c2498freedstatic inline SkColor SkHSVToColor(const SkScalar hsv[3]) {
1358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return SkHSVToColor(0xFF, hsv);
1368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
1378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com////////////////////////////////////////////////////////////////////////
1398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** 32 bit ARGB color value, premultiplied. The byte order for this value is
1418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    configuration dependent, matching the format of kARGB32 bitmaps. This is different
1428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    from SkColor, which is nonpremultiplied, and is always in the same byte order.
1438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
1448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef uint32_t SkPMColor;
1458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Return a SkPMColor value from unpremultiplied 8 bit component values
1478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
1487ffb1b21abcc7bbed5a0fc711f6dd7b9dbb4f577ctguil@chromium.orgSK_API SkPMColor SkPreMultiplyARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b);
1498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Return a SkPMColor value from a SkColor value. This is done by multiplying the color
1508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    components by the color's alpha, and by arranging the bytes in a configuration
1518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    dependent order, to match the format of kARGB32 bitmaps.
1528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
1537ffb1b21abcc7bbed5a0fc711f6dd7b9dbb4f577ctguil@chromium.orgSK_API SkPMColor SkPreMultiplyColor(SkColor c);
1548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1556d3cef930ad19b0f55543ca40f7a07030f4fe508reed///////////////////////////////////////////////////////////////////////////////////////////////////
1566d3cef930ad19b0f55543ca40f7a07030f4fe508reed
157dd9ffea9ce051a49dbc6544e6aa3cb68fe987f47reedstruct SkPM4f;
1583125565804054691b110b4731bc5a32070fab780reed
1596d3cef930ad19b0f55543ca40f7a07030f4fe508reed/*
1606d3cef930ad19b0f55543ca40f7a07030f4fe508reed *  The float values are 0...1 unpremultiplied
1616d3cef930ad19b0f55543ca40f7a07030f4fe508reed */
1622fbf1bc8c96f749a4c098bcfc827053445c2e12dDerek Sollenbergerstruct SK_API SkColor4f {
1636d3cef930ad19b0f55543ca40f7a07030f4fe508reed    float fR;
1646d3cef930ad19b0f55543ca40f7a07030f4fe508reed    float fG;
1656d3cef930ad19b0f55543ca40f7a07030f4fe508reed    float fB;
166e074d1fa6ace83dd77971b971c07166bedeb28efbrianosman    float fA;
1676d3cef930ad19b0f55543ca40f7a07030f4fe508reed
1686d3cef930ad19b0f55543ca40f7a07030f4fe508reed    bool operator==(const SkColor4f& other) const {
1696d3cef930ad19b0f55543ca40f7a07030f4fe508reed        return fA == other.fA && fR == other.fR && fG == other.fG && fB == other.fB;
1706d3cef930ad19b0f55543ca40f7a07030f4fe508reed    }
1716d3cef930ad19b0f55543ca40f7a07030f4fe508reed    bool operator!=(const SkColor4f& other) const {
1726d3cef930ad19b0f55543ca40f7a07030f4fe508reed        return !(*this == other);
1736d3cef930ad19b0f55543ca40f7a07030f4fe508reed    }
1746d3cef930ad19b0f55543ca40f7a07030f4fe508reed
175e074d1fa6ace83dd77971b971c07166bedeb28efbrianosman    const float* vec() const { return &fR; }
176e074d1fa6ace83dd77971b971c07166bedeb28efbrianosman    float* vec() { return &fR; }
1776d3cef930ad19b0f55543ca40f7a07030f4fe508reed
178e074d1fa6ace83dd77971b971c07166bedeb28efbrianosman    static SkColor4f Pin(float r, float g, float b, float a);
1794cba3fe576de7f48edd0271498745462564fdd6dHal Canary    /** Convert to SkColor4f, assuming SkColor is sRGB */
1806d3cef930ad19b0f55543ca40f7a07030f4fe508reed    static SkColor4f FromColor(SkColor);
1816d3cef930ad19b0f55543ca40f7a07030f4fe508reed
182e074d1fa6ace83dd77971b971c07166bedeb28efbrianosman    SkColor toSkColor() const;
183e074d1fa6ace83dd77971b971c07166bedeb28efbrianosman
1846d3cef930ad19b0f55543ca40f7a07030f4fe508reed    SkColor4f pin() const {
185e074d1fa6ace83dd77971b971c07166bedeb28efbrianosman        return Pin(fR, fG, fB, fA);
1866d3cef930ad19b0f55543ca40f7a07030f4fe508reed    }
1876d3cef930ad19b0f55543ca40f7a07030f4fe508reed
1886d3cef930ad19b0f55543ca40f7a07030f4fe508reed    SkPM4f premul() const;
1896d3cef930ad19b0f55543ca40f7a07030f4fe508reed};
1906d3cef930ad19b0f55543ca40f7a07030f4fe508reed
1918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
192