SkColor.h revision 80bacfeb4bda06541e8695bd502229727bccfea
180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/*
380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Copyright 2006 The Android Open Source Project
480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru *
580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Use of this source code is governed by a BSD-style license that can be
680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * found in the LICENSE file.
780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */
880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#ifndef SkColor_DEFINED
1180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#define SkColor_DEFINED
1280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkScalar.h"
1480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/** \file SkColor.h
1680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    Types and macros for colors
1880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru*/
1980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
2080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/** 8-bit type for an alpha value. 0xFF is 100% opaque, 0x00 is 100% transparent.
2180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru*/
2280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querutypedef uint8_t SkAlpha;
2380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/** 32 bit ARGB color value, not premultiplied. The color components are always in
2480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    a known order. This is different from SkPMColor, which has its bytes in a configuration
2580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    dependent order, to match the format of kARGB32 bitmaps. SkColor is the type used to
2680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    specify colors in SkPaint and in gradients.
2780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru*/
2880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querutypedef uint32_t SkColor;
2980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
3080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/** Return a SkColor value from 8 bit component values
3180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru*/
3280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querustatic inline SkColor SkColorSetARGBInline(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
3380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru{
3480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkASSERT(a <= 255 && r <= 255 && g <= 255 && b <= 255);
3580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
3680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    return (a << 24) | (r << 16) | (g << 8) | (b << 0);
3780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
3880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
3980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#define SkColorSetARGBMacro(a, r, g, b) \
4080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    static_cast<SkColor>( \
4180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        (static_cast<U8CPU>(a) << 24) | \
4280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        (static_cast<U8CPU>(r) << 16) | \
4380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        (static_cast<U8CPU>(g) << 8) | \
4480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        (static_cast<U8CPU>(b) << 0))
4580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
4680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/** gcc will generate static initializers for code of this form:
4780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * static const SkColor kMyColor = SkColorSetARGB(0xFF, 0x01, 0x02, 0x03)
4880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * if SkColorSetARGB() is a static inline, but not if it's a macro.
4980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */
5080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#if defined(NDEBUG)
5180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#define SkColorSetARGB(a, r, g, b) SkColorSetARGBMacro(a, r, g, b)
5280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#else
5380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#define SkColorSetARGB(a, r, g, b) SkColorSetARGBInline(a, r, g, b)
5480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#endif
5580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
5680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/** Return a SkColor value from 8 bit component values, with an implied value
5780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    of 0xFF for alpha (fully opaque)
5880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru*/
5980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#define SkColorSetRGB(r, g, b)  SkColorSetARGB(0xFF, r, g, b)
6080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
6180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/** return the alpha byte from a SkColor value */
6280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#define SkColorGetA(color)      (((color) >> 24) & 0xFF)
6380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/** return the red byte from a SkColor value */
6480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#define SkColorGetR(color)      (((color) >> 16) & 0xFF)
6580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/** return the green byte from a SkColor value */
6680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#define SkColorGetG(color)      (((color) >>  8) & 0xFF)
6780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/** return the blue byte from a SkColor value */
6880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#define SkColorGetB(color)      (((color) >>  0) & 0xFF)
6980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
7080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querustatic inline SkColor SkColorSetA(SkColor c, U8CPU a) {
7180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    return (c & 0x00FFFFFF) | (a << 24);
7280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
7380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
7480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru// common colors
7580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
7680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#define SK_ColorBLACK   0xFF000000  //!< black SkColor value
7780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#define SK_ColorDKGRAY  0xFF444444  //!< dark gray SkColor value
7880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#define SK_ColorGRAY    0xFF888888  //!< gray SkColor value
7980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#define SK_ColorLTGRAY  0xFFCCCCCC  //!< light gray SkColor value
8080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#define SK_ColorWHITE   0xFFFFFFFF  //!< white SkColor value
8180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
8280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#define SK_ColorRED     0xFFFF0000  //!< red SkColor value
8380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#define SK_ColorGREEN   0xFF00FF00  //!< green SkColor value
8480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#define SK_ColorBLUE    0xFF0000FF  //!< blue SkColor value
8580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#define SK_ColorYELLOW  0xFFFFFF00  //!< yellow SkColor value
8680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#define SK_ColorCYAN    0xFF00FFFF  //!< cyan SkColor value
8780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#define SK_ColorMAGENTA 0xFFFF00FF  //!< magenta SkColor value
8880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
8980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru////////////////////////////////////////////////////////////////////////
9080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
9180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/** Convert RGB components to HSV.
9280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        hsv[0] is Hue [0 .. 360)
9380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        hsv[1] is Saturation [0...1]
9480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        hsv[2] is Value [0...1]
9580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    @param red  red component value [0..255]
9680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    @param green  green component value [0..255]
9780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    @param blue  blue component value [0..255]
9880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    @param hsv  3 element array which holds the resulting HSV components.
9980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru*/
10080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste QueruSK_API void SkRGBToHSV(U8CPU red, U8CPU green, U8CPU blue, SkScalar hsv[3]);
10180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
10280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/** Convert the argb color to its HSV components.
10380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        hsv[0] is Hue [0 .. 360)
10480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        hsv[1] is Saturation [0...1]
10580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        hsv[2] is Value [0...1]
10680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    @param color the argb color to convert. Note: the alpha component is ignored.
10780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    @param hsv  3 element array which holds the resulting HSV components.
10880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru*/
10980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querustatic inline void SkColorToHSV(SkColor color, SkScalar hsv[3])
11080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru{
11180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkRGBToHSV(SkColorGetR(color), SkColorGetG(color), SkColorGetB(color), hsv);
11280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
11380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
11480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/** Convert HSV components to an ARGB color. The alpha component is passed through unchanged.
11580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        hsv[0] is Hue [0 .. 360)
11680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        hsv[1] is Saturation [0...1]
11780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        hsv[2] is Value [0...1]
11880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    If hsv values are out of range, they are pinned.
11980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    @param alpha the alpha component of the returned argb color.
12080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    @param hsv  3 element array which holds the input HSV components.
12180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    @return the resulting argb color
12280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru*/
12380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste QueruSK_API SkColor SkHSVToColor(U8CPU alpha, const SkScalar hsv[3]);
12480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
12580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/** Convert HSV components to an ARGB color. The alpha component set to 0xFF.
12680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        hsv[0] is Hue [0 .. 360)
12780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        hsv[1] is Saturation [0...1]
12880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        hsv[2] is Value [0...1]
12980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    If hsv values are out of range, they are pinned.
13080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    @param hsv  3 element array which holds the input HSV components.
13180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    @return the resulting argb color
13280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru*/
13380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querustatic inline SkColor SkHSVToColor(const SkScalar hsv[3])
13480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru{
13580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    return SkHSVToColor(0xFF, hsv);
13680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
13780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
13880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru////////////////////////////////////////////////////////////////////////
13980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
14080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/** 32 bit ARGB color value, premultiplied. The byte order for this value is
14180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    configuration dependent, matching the format of kARGB32 bitmaps. This is different
14280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    from SkColor, which is nonpremultiplied, and is always in the same byte order.
14380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru*/
14480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querutypedef uint32_t SkPMColor;
14580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
14680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/** Return a SkPMColor value from unpremultiplied 8 bit component values
14780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru*/
14880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste QueruSK_API SkPMColor SkPreMultiplyARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b);
14980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/** Return a SkPMColor value from a SkColor value. This is done by multiplying the color
15080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    components by the color's alpha, and by arranging the bytes in a configuration
15180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    dependent order, to match the format of kARGB32 bitmaps.
15280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru*/
15380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste QueruSK_API SkPMColor SkPreMultiplyColor(SkColor c);
15480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
15580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/** Define a function pointer type for combining two premultiplied colors
15680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru*/
15780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querutypedef SkPMColor (*SkXfermodeProc)(SkPMColor src, SkPMColor dst);
15880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
15980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/** Define a function pointer type for combining a premultiplied src color
16080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    and a 16bit device color.
16180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru*/
16280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querutypedef uint16_t (*SkXfermodeProc16)(SkPMColor src, uint16_t dst);
16380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
16480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#endif
16580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
166