1/*
2 * Copyright (C) 2013 Google 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 are
6 * met:
7 *
8 *     * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *     * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 *     * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#ifndef StyleColor_h
32#define StyleColor_h
33
34#include "platform/graphics/Color.h"
35#include "wtf/FastAllocBase.h"
36
37namespace WebCore {
38
39class StyleColor {
40    WTF_MAKE_FAST_ALLOCATED;
41public:
42    StyleColor()
43        : m_color()
44        , m_valid(false)
45        , m_currentColor(false) { }
46    StyleColor(Color color)
47        : m_color(color)
48        , m_valid(true)
49        , m_currentColor(false) { }
50    StyleColor(RGBA32 color, bool valid = true, bool currentColor = false)
51        : m_color(color)
52        , m_valid(valid)
53        , m_currentColor(currentColor) { }
54    StyleColor(int r, int g, int b)
55        : m_color(Color(r, g, b))
56        , m_valid(true)
57        , m_currentColor(false) { }
58    StyleColor(int r, int g, int b, int a)
59        : m_color(Color(r, g, b, a))
60        , m_valid(true)
61        , m_currentColor(false) { }
62    StyleColor(const StyleColor& other)
63        : m_color(other.m_color)
64        , m_valid(other.m_valid)
65        , m_currentColor(other.m_currentColor) { }
66
67    Color color() const { return m_color; }
68    bool isValid() const { return m_valid; }
69    bool isCurrentColor() const { return m_currentColor; }
70    bool hasAlpha() const { return m_color.hasAlpha(); }
71
72    void setRGB(int r, int g, int b)
73    {
74        m_color.setRGB(r, g, b);
75        m_valid = true;
76        m_currentColor = false;
77    }
78
79    RGBA32 rgb() const { return m_color.rgb(); } // Preserve the alpha.
80    int red() const { return m_color.red(); }
81    int green() const { return m_color.green(); }
82    int blue() const { return m_color.blue(); }
83    int alpha() const { return m_color.alpha(); }
84
85    static const StyleColor invalid()
86    {
87        return StyleColor(false, false);
88    }
89    static const StyleColor currentColor()
90    {
91        return StyleColor(true, true);
92    }
93
94private:
95    StyleColor(bool invalid, bool currentColor)
96        : m_color()
97        , m_valid(invalid)
98        , m_currentColor(currentColor) { }
99
100    Color m_color;
101    bool m_valid;
102    bool m_currentColor;
103};
104
105inline bool operator==(const StyleColor& a, const StyleColor& b)
106{
107    return a.rgb() == b.rgb() && a.isValid() == b.isValid() && a.isCurrentColor() == b.isCurrentColor();
108}
109
110inline bool operator!=(const StyleColor& a, const StyleColor& b)
111{
112    return !(a == b);
113}
114
115
116} // namespace WebCore
117
118#endif // StyleColor_h
119