webengine/osswebengine/WebCore/platform/graphics/Color.h
changeset 0 dd21522fd290
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     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/Platform.h>
       
    30 
       
    31 #if PLATFORM(CG)
       
    32 typedef struct CGColor* CGColorRef;
       
    33 #endif
       
    34 
       
    35 #if PLATFORM(QT)
       
    36 class QColor;
       
    37 #endif
       
    38 
       
    39 namespace WebCore {
       
    40 
       
    41 class String;
       
    42 class Color;
       
    43 
       
    44 typedef unsigned RGBA32;        // RGBA quadruplet
       
    45 
       
    46 RGBA32 makeRGB(int r, int g, int b);
       
    47 RGBA32 makeRGBA(int r, int g, int b, int a);
       
    48 RGBA32 makeRGBAFromHSLA(double h, double s, double l, double a);
       
    49 
       
    50 int differenceSquared(const Color&, const Color&);
       
    51 
       
    52 class Color {
       
    53 public:
       
    54     Color() : m_color(0), m_valid(false) { }
       
    55     Color(RGBA32 col) : m_color(col), m_valid(true) { }
       
    56     Color(int r, int g, int b) : m_color(makeRGB(r, g, b)), m_valid(true) { }
       
    57     Color(int r, int g, int b, int a) : m_color(makeRGBA(r, g, b, a)), m_valid(true) { }
       
    58     explicit Color(const String&);
       
    59     explicit Color(const char*);
       
    60     
       
    61     String name() const;
       
    62     void setNamedColor(const String&);
       
    63 
       
    64     bool isValid() const { return m_valid; }
       
    65 
       
    66     bool hasAlpha() const { return alpha() < 255; }
       
    67 
       
    68     int red() const { return (m_color >> 16) & 0xFF; }
       
    69     int green() const { return (m_color >> 8) & 0xFF; }
       
    70     int blue() const { return m_color & 0xFF; }
       
    71     int alpha() const { return (m_color >> 24) & 0xFF; }
       
    72     
       
    73     RGBA32 rgb() const { return m_color; } // Preserve the alpha.
       
    74     void setRGB(int r, int g, int b) { m_color = makeRGB(r, g, b); m_valid = true; }
       
    75     void setRGB(RGBA32 rgb) { m_color = rgb; m_valid = true; }
       
    76     void getRGBA(float& r, float& g, float& b, float& a) const;
       
    77     void getRGBA(double& r, double& g, double& b, double& a) const;
       
    78 
       
    79     Color light() const;
       
    80     Color dark() const;
       
    81 
       
    82     Color blend(const Color&) const;
       
    83     Color blendWithWhite() const;
       
    84 
       
    85 #if PLATFORM(QT)
       
    86     Color(const QColor&);
       
    87     operator QColor() const;
       
    88 #endif
       
    89 
       
    90     static bool parseHexColor(const String& name, RGBA32& rgb);
       
    91 
       
    92     static const RGBA32 black = 0xFF000000;
       
    93     static const RGBA32 white = 0xFFFFFFFF;
       
    94     static const RGBA32 darkGray = 0xFF808080;
       
    95     static const RGBA32 gray = 0xFFA0A0A0;
       
    96     static const RGBA32 lightGray = 0xFFC0C0C0;
       
    97     static const RGBA32 transparent = 0x00000000;
       
    98 
       
    99 private:
       
   100     RGBA32 m_color;
       
   101     bool m_valid : 1;
       
   102 };
       
   103 
       
   104 inline bool operator==(const Color& a, const Color& b)
       
   105 {
       
   106     return a.rgb() == b.rgb() && a.isValid() == b.isValid();
       
   107 }
       
   108 
       
   109 inline bool operator!=(const Color& a, const Color& b)
       
   110 {
       
   111     return !(a == b);
       
   112 }
       
   113 
       
   114 Color focusRingColor();
       
   115 void setFocusRingColorChangeFunction(void (*)());
       
   116 
       
   117 #if PLATFORM(CG)
       
   118 CGColorRef cgColor(const Color&);
       
   119 #endif
       
   120 
       
   121 } // namespace WebCore
       
   122 
       
   123 #endif // Color_h