1 | /* |
2 | * Copyright (C) 2006-2017 Apple Inc. All rights reserved. |
3 | * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies) |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
8 | * 1. Redistributions of source code must retain the above copyright |
9 | * notice, this list of conditions and the following disclaimer. |
10 | * 2. Redistributions in binary form must reproduce the above copyright |
11 | * notice, this list of conditions and the following disclaimer in the |
12 | * documentation and/or other materials provided with the distribution. |
13 | * |
14 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
15 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
17 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
18 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
19 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
20 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
21 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
22 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
24 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
25 | */ |
26 | |
27 | #pragma once |
28 | |
29 | #include "CanvasGradient.h" |
30 | #include "CanvasPattern.h" |
31 | #include "Color.h" |
32 | #include <wtf/Variant.h> |
33 | |
34 | namespace WebCore { |
35 | |
36 | class Document; |
37 | class GraphicsContext; |
38 | class HTMLCanvasElement; |
39 | |
40 | class CanvasStyle { |
41 | public: |
42 | CanvasStyle(); |
43 | CanvasStyle(Color); |
44 | CanvasStyle(float grayLevel, float alpha); |
45 | CanvasStyle(float r, float g, float b, float alpha); |
46 | CanvasStyle(float c, float m, float y, float k, float alpha); |
47 | CanvasStyle(CanvasGradient&); |
48 | CanvasStyle(CanvasPattern&); |
49 | |
50 | static CanvasStyle createFromString(const String& color); |
51 | static CanvasStyle createFromStringWithOverrideAlpha(const String& color, float alpha); |
52 | |
53 | bool isValid() const { return !WTF::holds_alternative<Invalid>(m_style); } |
54 | bool isCurrentColor() const { return WTF::holds_alternative<CurrentColor>(m_style); } |
55 | bool hasOverrideAlpha() const { return isCurrentColor() && WTF::get<CurrentColor>(m_style).overrideAlpha; } |
56 | float overrideAlpha() const { return WTF::get<CurrentColor>(m_style).overrideAlpha.value(); } |
57 | |
58 | String color() const; |
59 | RefPtr<CanvasGradient> canvasGradient() const; |
60 | RefPtr<CanvasPattern> canvasPattern() const; |
61 | |
62 | void applyFillColor(GraphicsContext&) const; |
63 | void applyStrokeColor(GraphicsContext&) const; |
64 | |
65 | bool isEquivalentColor(const CanvasStyle&) const; |
66 | bool isEquivalentRGBA(float red, float green, float blue, float alpha) const; |
67 | bool isEquivalentCMYKA(float cyan, float magenta, float yellow, float black, float alpha) const; |
68 | |
69 | private: |
70 | struct Invalid { }; |
71 | |
72 | struct CMYKAColor { |
73 | Color color; |
74 | float c { 0 }; |
75 | float m { 0 }; |
76 | float y { 0 }; |
77 | float k { 0 }; |
78 | float a { 0 }; |
79 | |
80 | CMYKAColor(const CMYKAColor&) = default; |
81 | }; |
82 | |
83 | struct CurrentColor { |
84 | Optional<float> overrideAlpha; |
85 | }; |
86 | |
87 | CanvasStyle(CurrentColor); |
88 | |
89 | Variant<Invalid, Color, CMYKAColor, RefPtr<CanvasGradient>, RefPtr<CanvasPattern>, CurrentColor> m_style; |
90 | }; |
91 | |
92 | Color currentColor(HTMLCanvasElement*); |
93 | Color parseColorOrCurrentColor(const String& colorString, HTMLCanvasElement*); |
94 | |
95 | inline CanvasStyle::CanvasStyle() |
96 | : m_style(Invalid { }) |
97 | { |
98 | } |
99 | |
100 | inline RefPtr<CanvasGradient> CanvasStyle::canvasGradient() const |
101 | { |
102 | if (!WTF::holds_alternative<RefPtr<CanvasGradient>>(m_style)) |
103 | return nullptr; |
104 | return WTF::get<RefPtr<CanvasGradient>>(m_style); |
105 | } |
106 | |
107 | inline RefPtr<CanvasPattern> CanvasStyle::canvasPattern() const |
108 | { |
109 | if (!WTF::holds_alternative<RefPtr<CanvasPattern>>(m_style)) |
110 | return nullptr; |
111 | return WTF::get<RefPtr<CanvasPattern>>(m_style); |
112 | } |
113 | |
114 | inline String CanvasStyle::color() const |
115 | { |
116 | auto& color = WTF::holds_alternative<Color>(m_style) ? WTF::get<Color>(m_style) : WTF::get<CMYKAColor>(m_style).color; |
117 | return color.serialized(); |
118 | } |
119 | |
120 | } // namespace WebCore |
121 | |