2 * Copyright (C) 2006, 2008 Apple Inc. All rights reserved.
3 * Copyright (C) 2008, 2010 Nokia Corporation and/or its subsidiary(-ies)
4 * Copyright (C) 2007 Alp Toker <alp@atoker.com>
5 * Copyright (C) 2008 Eric Seidel <eric@webkit.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
24 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "core/html/canvas/CanvasStyle.h"
32 #include "CSSPropertyNames.h"
33 #include "core/css/parser/BisonCSSParser.h"
34 #include "core/css/StylePropertySet.h"
35 #include "core/html/HTMLCanvasElement.h"
36 #include "core/html/canvas/CanvasGradient.h"
37 #include "core/html/canvas/CanvasPattern.h"
38 #include "platform/graphics/GraphicsContext.h"
39 #include "wtf/PassRefPtr.h"
43 enum ColorParseResult { ParsedRGBA, ParsedCurrentColor, ParsedSystemColor, ParseFailed };
45 static ColorParseResult parseColor(RGBA32& parsedColor, const String& colorString)
47 if (equalIgnoringCase(colorString, "currentcolor"))
48 return ParsedCurrentColor;
49 if (BisonCSSParser::parseColor(parsedColor, colorString))
51 if (BisonCSSParser::parseSystemColor(parsedColor, colorString))
52 return ParsedSystemColor;
56 RGBA32 currentColor(HTMLCanvasElement* canvas)
58 if (!canvas || !canvas->inDocument() || !canvas->inlineStyle())
60 RGBA32 rgba = Color::black;
61 BisonCSSParser::parseColor(rgba, canvas->inlineStyle()->getPropertyValue(CSSPropertyColor));
65 bool parseColorOrCurrentColor(RGBA32& parsedColor, const String& colorString, HTMLCanvasElement* canvas)
67 ColorParseResult parseResult = parseColor(parsedColor, colorString);
68 switch (parseResult) {
70 case ParsedSystemColor:
72 case ParsedCurrentColor:
73 parsedColor = currentColor(canvas);
83 CanvasStyle::CanvasStyle(Type type, float overrideAlpha)
85 , m_overrideAlpha(overrideAlpha)
89 CanvasStyle::CanvasStyle(RGBA32 rgba)
95 CanvasStyle::CanvasStyle(float grayLevel, float alpha)
97 , m_rgba(makeRGBA32FromFloats(grayLevel, grayLevel, grayLevel, alpha))
101 CanvasStyle::CanvasStyle(float r, float g, float b, float a)
103 , m_rgba(makeRGBA32FromFloats(r, g, b, a))
107 CanvasStyle::CanvasStyle(float c, float m, float y, float k, float a)
109 , m_rgba(makeRGBAFromCMYKA(c, m, y, k, a))
110 , m_cmyka(c, m, y, k, a)
114 CanvasStyle::CanvasStyle(PassRefPtr<CanvasGradient> gradient)
116 , m_gradient(gradient)
120 CanvasStyle::CanvasStyle(PassRefPtr<CanvasPattern> pattern)
121 : m_type(ImagePattern)
126 PassRefPtr<CanvasStyle> CanvasStyle::createFromString(const String& color)
129 ColorParseResult parseResult = parseColor(rgba, color);
130 switch (parseResult) {
132 case ParsedSystemColor:
133 return adoptRef(new CanvasStyle(rgba));
134 case ParsedCurrentColor:
135 return adoptRef(new CanvasStyle(CurrentColor));
139 ASSERT_NOT_REACHED();
144 PassRefPtr<CanvasStyle> CanvasStyle::createFromStringWithOverrideAlpha(const String& color, float alpha)
147 ColorParseResult parseResult = parseColor(rgba, color);
148 switch (parseResult) {
150 return adoptRef(new CanvasStyle(colorWithOverrideAlpha(rgba, alpha)));
151 case ParsedCurrentColor:
152 return adoptRef(new CanvasStyle(CurrentColorWithOverrideAlpha, alpha));
156 ASSERT_NOT_REACHED();
161 PassRefPtr<CanvasStyle> CanvasStyle::createFromGradient(PassRefPtr<CanvasGradient> gradient)
165 return adoptRef(new CanvasStyle(gradient));
168 PassRefPtr<CanvasStyle> CanvasStyle::createFromPattern(PassRefPtr<CanvasPattern> pattern)
172 return adoptRef(new CanvasStyle(pattern));
175 bool CanvasStyle::isEquivalentColor(const CanvasStyle& other) const
177 if (m_type != other.m_type)
182 return m_rgba == other.m_rgba;
184 return m_cmyka.c == other.m_cmyka.c
185 && m_cmyka.m == other.m_cmyka.m
186 && m_cmyka.y == other.m_cmyka.y
187 && m_cmyka.k == other.m_cmyka.k
188 && m_cmyka.a == other.m_cmyka.a;
192 case CurrentColorWithOverrideAlpha:
196 ASSERT_NOT_REACHED();
200 bool CanvasStyle::isEquivalentRGBA(float r, float g, float b, float a) const
205 return m_rgba == makeRGBA32FromFloats(r, g, b, a);
208 bool CanvasStyle::isEquivalentCMYKA(float c, float m, float y, float k, float a) const
213 return c == m_cmyka.c
220 void CanvasStyle::applyStrokeColor(GraphicsContext* context)
226 context->setStrokeColor(m_rgba);
229 // FIXME: Do this through platform-independent GraphicsContext API.
230 // We'll need a fancier Color abstraction to support CMYKA correctly
231 context->setStrokeColor(m_rgba);
235 context->setStrokeGradient(canvasGradient()->gradient());
238 context->setStrokePattern(canvasPattern()->pattern());
241 case CurrentColorWithOverrideAlpha:
242 ASSERT_NOT_REACHED();
247 void CanvasStyle::applyFillColor(GraphicsContext* context)
253 context->setFillColor(m_rgba);
256 // FIXME: Do this through platform-independent GraphicsContext API.
257 // We'll need a fancier Color abstraction to support CMYKA correctly
258 context->setFillColor(m_rgba);
262 context->setFillGradient(canvasGradient()->gradient());
265 context->setFillPattern(canvasPattern()->pattern());
268 case CurrentColorWithOverrideAlpha:
269 ASSERT_NOT_REACHED();