Home | History | Annotate | Download | only in graphics
      1 /*
      2  * Copyright (C) 2003, 2004, 2005, 2006, 2010 Apple Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  * 1. Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  * 2. Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
     14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
     17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #ifndef Color_h
     27 #define Color_h
     28 
     29 #include "platform/animation/AnimationUtilities.h"
     30 #include "wtf/FastAllocBase.h"
     31 #include "wtf/Forward.h"
     32 #include "wtf/unicode/Unicode.h"
     33 
     34 namespace WebCore {
     35 
     36 class Color;
     37 
     38 typedef unsigned RGBA32; // RGBA quadruplet
     39 
     40 PLATFORM_EXPORT RGBA32 makeRGB(int r, int g, int b);
     41 PLATFORM_EXPORT RGBA32 makeRGBA(int r, int g, int b, int a);
     42 
     43 PLATFORM_EXPORT RGBA32 colorWithOverrideAlpha(RGBA32 color, float overrideAlpha);
     44 PLATFORM_EXPORT RGBA32 makeRGBA32FromFloats(float r, float g, float b, float a);
     45 PLATFORM_EXPORT RGBA32 makeRGBAFromHSLA(double h, double s, double l, double a);
     46 PLATFORM_EXPORT RGBA32 makeRGBAFromCMYKA(float c, float m, float y, float k, float a);
     47 
     48 PLATFORM_EXPORT int differenceSquared(const Color&, const Color&);
     49 
     50 inline int redChannel(RGBA32 color) { return (color >> 16) & 0xFF; }
     51 inline int greenChannel(RGBA32 color) { return (color >> 8) & 0xFF; }
     52 inline int blueChannel(RGBA32 color) { return color & 0xFF; }
     53 inline int alphaChannel(RGBA32 color) { return (color >> 24) & 0xFF; }
     54 
     55 struct NamedColor {
     56     const char* name;
     57     unsigned ARGBValue;
     58 };
     59 
     60 const NamedColor* findColor(register const char* str, register unsigned len);
     61 
     62 class PLATFORM_EXPORT Color {
     63     WTF_MAKE_FAST_ALLOCATED;
     64 public:
     65     Color() : m_color(0), m_valid(false) { }
     66     Color(RGBA32 color, bool valid = true) : m_color(color), m_valid(valid) { ASSERT(!m_color || m_valid); }
     67     Color(int r, int g, int b) : m_color(makeRGB(r, g, b)), m_valid(true) { }
     68     Color(int r, int g, int b, int a) : m_color(makeRGBA(r, g, b, a)), m_valid(true) { }
     69     // Color is currently limited to 32bit RGBA, perhaps some day we'll support better colors
     70     Color(float r, float g, float b, float a) : m_color(makeRGBA32FromFloats(r, g, b, a)), m_valid(true) { }
     71     // Creates a new color from the specific CMYK and alpha values.
     72     Color(float c, float m, float y, float k, float a) : m_color(makeRGBAFromCMYKA(c, m, y, k, a)), m_valid(true) { }
     73     explicit Color(const String&);
     74     explicit Color(const char*);
     75 
     76     static Color createUnchecked(int r, int g, int b)
     77     {
     78         RGBA32 color = 0xFF000000 | r << 16 | g << 8 | b;
     79         return Color(color);
     80     }
     81     static Color createUnchecked(int r, int g, int b, int a)
     82     {
     83         RGBA32 color = a << 24 | r << 16 | g << 8 | b;
     84         return Color(color);
     85     }
     86 
     87     // Returns the color serialized according to HTML5
     88     // - http://www.whatwg.org/specs/web-apps/current-work/#serialization-of-a-color
     89     String serialized() const;
     90 
     91     // Returns the color serialized as either #RRGGBB or #RRGGBBAA
     92     // The latter format is not a valid CSS color, and should only be seen in DRT dumps.
     93     String nameForRenderTreeAsText() const;
     94 
     95     void setNamedColor(const String&);
     96 
     97     bool isValid() const { return m_valid; }
     98 
     99     bool hasAlpha() const { return alpha() < 255; }
    100 
    101     int red() const { return redChannel(m_color); }
    102     int green() const { return greenChannel(m_color); }
    103     int blue() const { return blueChannel(m_color); }
    104     int alpha() const { return alphaChannel(m_color); }
    105 
    106     RGBA32 rgb() const { return m_color; } // Preserve the alpha.
    107     void setRGB(int r, int g, int b) { m_color = makeRGB(r, g, b); m_valid = true; }
    108     void setRGB(RGBA32 rgb) { m_color = rgb; m_valid = true; }
    109     void getRGBA(float& r, float& g, float& b, float& a) const;
    110     void getRGBA(double& r, double& g, double& b, double& a) const;
    111     void getHSL(double& h, double& s, double& l) const;
    112 
    113     Color light() const;
    114     Color dark() const;
    115 
    116     // This is an implementation of Porter-Duff's "source-over" equation
    117     Color blend(const Color&) const;
    118     Color blendWithWhite() const;
    119 
    120     static bool parseHexColor(const String&, RGBA32&);
    121     static bool parseHexColor(const LChar*, unsigned, RGBA32&);
    122     static bool parseHexColor(const UChar*, unsigned, RGBA32&);
    123 
    124     static const RGBA32 black = 0xFF000000;
    125     static const RGBA32 white = 0xFFFFFFFF;
    126     static const RGBA32 darkGray = 0xFF808080;
    127     static const RGBA32 gray = 0xFFA0A0A0;
    128     static const RGBA32 lightGray = 0xFFC0C0C0;
    129     static const RGBA32 transparent = 0x00000000;
    130 
    131 private:
    132     RGBA32 m_color;
    133     bool m_valid;
    134 };
    135 
    136 inline bool operator==(const Color& a, const Color& b)
    137 {
    138     return a.rgb() == b.rgb() && a.isValid() == b.isValid();
    139 }
    140 
    141 inline bool operator!=(const Color& a, const Color& b)
    142 {
    143     return !(a == b);
    144 }
    145 
    146 PLATFORM_EXPORT Color colorFromPremultipliedARGB(RGBA32);
    147 PLATFORM_EXPORT RGBA32 premultipliedARGBFromColor(const Color&);
    148 
    149 inline Color blend(const Color& from, const Color& to, double progress, bool blendPremultiplied = true)
    150 {
    151     // We need to preserve the state of the valid flag at the end of the animation
    152     if (progress == 1 && !to.isValid())
    153         return Color();
    154 
    155     if (blendPremultiplied) {
    156         // Contrary to the name, RGBA32 actually stores ARGB, so we can initialize Color directly from premultipliedARGBFromColor().
    157         // Also, premultipliedARGBFromColor() bails on zero alpha, so special-case that.
    158         Color premultFrom = from.alpha() ? premultipliedARGBFromColor(from) : 0;
    159         Color premultTo = to.alpha() ? premultipliedARGBFromColor(to) : 0;
    160 
    161         Color premultBlended(blend(premultFrom.red(), premultTo.red(), progress),
    162                              blend(premultFrom.green(), premultTo.green(), progress),
    163                              blend(premultFrom.blue(), premultTo.blue(), progress),
    164                              blend(premultFrom.alpha(), premultTo.alpha(), progress));
    165 
    166         return Color(colorFromPremultipliedARGB(premultBlended.rgb()));
    167     }
    168 
    169     return Color(blend(from.red(), to.red(), progress),
    170                  blend(from.green(), to.green(), progress),
    171                  blend(from.blue(), to.blue(), progress),
    172                  blend(from.alpha(), to.alpha(), progress));
    173 }
    174 } // namespace WebCore
    175 
    176 #endif // Color_h
    177