Home | History | Annotate | Download | only in graphics
      1 /*
      2  * Copyright (C) 2003-6 Apple Computer, 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 <wtf/FastAllocBase.h>
     30 #include <wtf/Platform.h>
     31 
     32 #if PLATFORM(CG)
     33 typedef struct CGColor* CGColorRef;
     34 #endif
     35 
     36 #if PLATFORM(QT)
     37 #include <qglobal.h>
     38 QT_BEGIN_NAMESPACE
     39 class QColor;
     40 QT_END_NAMESPACE
     41 #endif
     42 
     43 #if PLATFORM(GTK)
     44 typedef struct _GdkColor GdkColor;
     45 #endif
     46 
     47 #if PLATFORM(WX)
     48 class wxColour;
     49 #endif
     50 
     51 #if PLATFORM(HAIKU)
     52 struct rgb_color;
     53 #endif
     54 
     55 namespace WebCore {
     56 
     57 class String;
     58 class Color;
     59 
     60 typedef unsigned RGBA32;        // RGBA quadruplet
     61 
     62 RGBA32 makeRGB(int r, int g, int b);
     63 RGBA32 makeRGBA(int r, int g, int b, int a);
     64 
     65 RGBA32 colorWithOverrideAlpha(RGBA32 color, float overrideAlpha);
     66 RGBA32 makeRGBA32FromFloats(float r, float g, float b, float a);
     67 RGBA32 makeRGBAFromHSLA(double h, double s, double l, double a);
     68 RGBA32 makeRGBAFromCMYKA(float c, float m, float y, float k, float a);
     69 
     70 int differenceSquared(const Color&, const Color&);
     71 
     72 class Color : public FastAllocBase {
     73 public:
     74     Color() : m_color(0), m_valid(false) { }
     75     Color(RGBA32 col) : m_color(col), m_valid(true) { }
     76     Color(int r, int g, int b) : m_color(makeRGB(r, g, b)), m_valid(true) { }
     77     Color(int r, int g, int b, int a) : m_color(makeRGBA(r, g, b, a)), m_valid(true) { }
     78     // Color is currently limited to 32bit RGBA, perhaps some day we'll support better colors
     79     Color(float r, float g, float b, float a) : m_color(makeRGBA32FromFloats(r, g, b, a)), m_valid(true) { }
     80     // Creates a new color from the specific CMYK and alpha values.
     81     Color(float c, float m, float y, float k, float a) : m_color(makeRGBAFromCMYKA(c, m, y, k, a)), m_valid(true) { }
     82     explicit Color(const String&);
     83     explicit Color(const char*);
     84 
     85     String name() const;
     86     void setNamedColor(const String&);
     87 
     88     bool isValid() const { return m_valid; }
     89 
     90     bool hasAlpha() const { return alpha() < 255; }
     91 
     92     int red() const { return (m_color >> 16) & 0xFF; }
     93     int green() const { return (m_color >> 8) & 0xFF; }
     94     int blue() const { return m_color & 0xFF; }
     95     int alpha() const { return (m_color >> 24) & 0xFF; }
     96 
     97     RGBA32 rgb() const { return m_color; } // Preserve the alpha.
     98     void setRGB(int r, int g, int b) { m_color = makeRGB(r, g, b); m_valid = true; }
     99     void setRGB(RGBA32 rgb) { m_color = rgb; m_valid = true; }
    100     void getRGBA(float& r, float& g, float& b, float& a) const;
    101     void getRGBA(double& r, double& g, double& b, double& a) const;
    102     void getHSL(double& h, double& s, double& l) const;
    103 
    104     Color light() const;
    105     Color dark() const;
    106 
    107     Color blend(const Color&) const;
    108     Color blendWithWhite() const;
    109 
    110 #if PLATFORM(QT)
    111     Color(const QColor&);
    112     operator QColor() const;
    113 #endif
    114 
    115 #if PLATFORM(GTK)
    116     Color(const GdkColor&);
    117     // We can't sensibly go back to GdkColor without losing the alpha value
    118 #endif
    119 
    120 #if PLATFORM(WX)
    121     Color(const wxColour&);
    122     operator wxColour() const;
    123 #endif
    124 
    125 #if PLATFORM(CG)
    126     Color(CGColorRef);
    127 #endif
    128 
    129 #if PLATFORM(HAIKU)
    130     Color(const rgb_color&);
    131     operator rgb_color() const;
    132 #endif
    133 
    134     static bool parseHexColor(const String& name, RGBA32& rgb);
    135 
    136     static const RGBA32 black = 0xFF000000;
    137     static const RGBA32 white = 0xFFFFFFFF;
    138     static const RGBA32 darkGray = 0xFF808080;
    139     static const RGBA32 gray = 0xFFA0A0A0;
    140     static const RGBA32 lightGray = 0xFFC0C0C0;
    141     static const RGBA32 transparent = 0x00000000;
    142 
    143 #ifdef ANDROID_CSS_TAP_HIGHLIGHT_COLOR
    144     static const RGBA32 tap = 0x4D1A1A1A;
    145 #endif
    146 
    147 private:
    148     RGBA32 m_color;
    149     bool m_valid;
    150 };
    151 
    152 inline bool operator==(const Color& a, const Color& b)
    153 {
    154     return a.rgb() == b.rgb() && a.isValid() == b.isValid();
    155 }
    156 
    157 inline bool operator!=(const Color& a, const Color& b)
    158 {
    159     return !(a == b);
    160 }
    161 
    162 Color colorFromPremultipliedARGB(unsigned);
    163 unsigned premultipliedARGBFromColor(const Color&);
    164 
    165 #if PLATFORM(CG)
    166 CGColorRef createCGColor(const Color&);
    167 #endif
    168 
    169 } // namespace WebCore
    170 
    171 #endif // Color_h
    172