Home | History | Annotate | Download | only in core
      1 /*
      2  * Copyright (C) 2006 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef SkColor_DEFINED
     18 #define SkColor_DEFINED
     19 
     20 #include "SkScalar.h"
     21 
     22 /** \file SkColor.h
     23 
     24     Types and macros for colors
     25 */
     26 
     27 /** 8-bit type for an alpha value. 0xFF is 100% opaque, 0x00 is 100% transparent.
     28 */
     29 typedef uint8_t SkAlpha;
     30 /** 32 bit ARGB color value, not premultiplied. The color components are always in
     31     a known order. This is different from SkPMColor, which has its bytes in a configuration
     32     dependent order, to match the format of kARGB32 bitmaps. SkColor is the type used to
     33     specify colors in SkPaint and in gradients.
     34 */
     35 typedef uint32_t SkColor;
     36 
     37 /** Return a SkColor value from 8 bit component values
     38 */
     39 static inline SkColor SkColorSetARGBInline(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
     40 {
     41     SkASSERT(a <= 255 && r <= 255 && g <= 255 && b <= 255);
     42 
     43     return (a << 24) | (r << 16) | (g << 8) | (b << 0);
     44 }
     45 
     46 #define SkColorSetARGBMacro(a, r, g, b) \
     47     static_cast<SkColor>( \
     48         (static_cast<U8CPU>(a) << 24) | \
     49         (static_cast<U8CPU>(r) << 16) | \
     50         (static_cast<U8CPU>(g) << 8) | \
     51         (static_cast<U8CPU>(b) << 0))
     52 
     53 /** gcc will generate static initializers for code of this form:
     54  * static const SkColor kMyColor = SkColorSetARGB(0xFF, 0x01, 0x02, 0x03)
     55  * if SkColorSetARGB() is a static inline, but not if it's a macro.
     56  */
     57 #if defined(NDEBUG)
     58 #define SkColorSetARGB(a, r, g, b) SkColorSetARGBMacro(a, r, g, b)
     59 #else
     60 #define SkColorSetARGB(a, r, g, b) SkColorSetARGBInline(a, r, g, b)
     61 #endif
     62 
     63 /** Return a SkColor value from 8 bit component values, with an implied value
     64     of 0xFF for alpha (fully opaque)
     65 */
     66 #define SkColorSetRGB(r, g, b)  SkColorSetARGB(0xFF, r, g, b)
     67 
     68 /** return the alpha byte from a SkColor value */
     69 #define SkColorGetA(color)      (((color) >> 24) & 0xFF)
     70 /** return the red byte from a SkColor value */
     71 #define SkColorGetR(color)      (((color) >> 16) & 0xFF)
     72 /** return the green byte from a SkColor value */
     73 #define SkColorGetG(color)      (((color) >>  8) & 0xFF)
     74 /** return the blue byte from a SkColor value */
     75 #define SkColorGetB(color)      (((color) >>  0) & 0xFF)
     76 
     77 static inline SkColor SkColorSetA(SkColor c, U8CPU a) {
     78     return (c & 0x00FFFFFF) | (a << 24);
     79 }
     80 
     81 // common colors
     82 
     83 #define SK_ColorBLACK   0xFF000000  //!< black SkColor value
     84 #define SK_ColorDKGRAY  0xFF444444  //!< dark gray SkColor value
     85 #define SK_ColorGRAY    0xFF888888  //!< gray SkColor value
     86 #define SK_ColorLTGRAY  0xFFCCCCCC  //!< light gray SkColor value
     87 #define SK_ColorWHITE   0xFFFFFFFF  //!< white SkColor value
     88 
     89 #define SK_ColorRED     0xFFFF0000  //!< red SkColor value
     90 #define SK_ColorGREEN   0xFF00FF00  //!< green SkColor value
     91 #define SK_ColorBLUE    0xFF0000FF  //!< blue SkColor value
     92 #define SK_ColorYELLOW  0xFFFFFF00  //!< yellow SkColor value
     93 #define SK_ColorCYAN    0xFF00FFFF  //!< cyan SkColor value
     94 #define SK_ColorMAGENTA 0xFFFF00FF  //!< magenta SkColor value
     95 
     96 ////////////////////////////////////////////////////////////////////////
     97 
     98 /** Convert RGB components to HSV.
     99         hsv[0] is Hue [0 .. 360)
    100         hsv[1] is Saturation [0...1]
    101         hsv[2] is Value [0...1]
    102     @param red  red component value [0..255]
    103     @param green  green component value [0..255]
    104     @param blue  blue component value [0..255]
    105     @param hsv  3 element array which holds the resulting HSV components.
    106 */
    107 SK_API void SkRGBToHSV(U8CPU red, U8CPU green, U8CPU blue, SkScalar hsv[3]);
    108 
    109 /** Convert the argb color to its HSV components.
    110         hsv[0] is Hue [0 .. 360)
    111         hsv[1] is Saturation [0...1]
    112         hsv[2] is Value [0...1]
    113     @param color the argb color to convert. Note: the alpha component is ignored.
    114     @param hsv  3 element array which holds the resulting HSV components.
    115 */
    116 static inline void SkColorToHSV(SkColor color, SkScalar hsv[3])
    117 {
    118     SkRGBToHSV(SkColorGetR(color), SkColorGetG(color), SkColorGetB(color), hsv);
    119 }
    120 
    121 /** Convert HSV components to an ARGB color. The alpha component is passed through unchanged.
    122         hsv[0] is Hue [0 .. 360)
    123         hsv[1] is Saturation [0...1]
    124         hsv[2] is Value [0...1]
    125     If hsv values are out of range, they are pinned.
    126     @param alpha the alpha component of the returned argb color.
    127     @param hsv  3 element array which holds the input HSV components.
    128     @return the resulting argb color
    129 */
    130 SK_API SkColor SkHSVToColor(U8CPU alpha, const SkScalar hsv[3]);
    131 
    132 /** Convert HSV components to an ARGB color. The alpha component set to 0xFF.
    133         hsv[0] is Hue [0 .. 360)
    134         hsv[1] is Saturation [0...1]
    135         hsv[2] is Value [0...1]
    136     If hsv values are out of range, they are pinned.
    137     @param hsv  3 element array which holds the input HSV components.
    138     @return the resulting argb color
    139 */
    140 static inline SkColor SkHSVToColor(const SkScalar hsv[3])
    141 {
    142     return SkHSVToColor(0xFF, hsv);
    143 }
    144 
    145 ////////////////////////////////////////////////////////////////////////
    146 
    147 /** 32 bit ARGB color value, premultiplied. The byte order for this value is
    148     configuration dependent, matching the format of kARGB32 bitmaps. This is different
    149     from SkColor, which is nonpremultiplied, and is always in the same byte order.
    150 */
    151 typedef uint32_t SkPMColor;
    152 
    153 /** Return a SkPMColor value from unpremultiplied 8 bit component values
    154 */
    155 SK_API SkPMColor SkPreMultiplyARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b);
    156 /** Return a SkPMColor value from a SkColor value. This is done by multiplying the color
    157     components by the color's alpha, and by arranging the bytes in a configuration
    158     dependent order, to match the format of kARGB32 bitmaps.
    159 */
    160 SK_API SkPMColor SkPreMultiplyColor(SkColor c);
    161 
    162 /** Define a function pointer type for combining two premultiplied colors
    163 */
    164 typedef SkPMColor (*SkXfermodeProc)(SkPMColor src, SkPMColor dst);
    165 
    166 /** Define a function pointer type for combining a premultiplied src color
    167     and a 16bit device color.
    168 */
    169 typedef uint16_t (*SkXfermodeProc16)(SkPMColor src, uint16_t dst);
    170 
    171 #endif
    172 
    173