Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / svg / SVGAnimatedColor.cpp
1 /*
2  * Copyright (C) Research In Motion Limited 2011. All rights reserved.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB.  If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #include "config.h"
21
22 #include "core/svg/SVGAnimatedColor.h"
23
24 #include "core/rendering/RenderObject.h"
25 #include "core/svg/ColorDistance.h"
26 #include "core/svg/SVGAnimateElement.h"
27 #include "core/svg/SVGColor.h"
28
29 namespace WebCore {
30
31 String SVGColorProperty::valueAsString() const
32 {
33     return m_styleColor.isCurrentColor() ? "currentColor" : m_styleColor.color().serializedAsCSSComponentValue();
34 }
35
36 PassRefPtr<NewSVGPropertyBase> SVGColorProperty::cloneForAnimation(const String&) const
37 {
38     // SVGAnimatedColor is deprecated. So No SVG DOM animation.
39     ASSERT_NOT_REACHED();
40     return 0;
41 }
42
43 static inline Color fallbackColorForCurrentColor(SVGElement* targetElement)
44 {
45     ASSERT(targetElement);
46     if (RenderObject* targetRenderer = targetElement->renderer())
47         return targetRenderer->style()->visitedDependentColor(CSSPropertyColor);
48     else
49         return Color::transparent;
50 }
51
52 void SVGColorProperty::add(PassRefPtr<NewSVGPropertyBase> other, SVGElement* contextElement)
53 {
54     ASSERT(contextElement);
55
56     Color fallbackColor = fallbackColorForCurrentColor(contextElement);
57     Color fromColor = toSVGColorProperty(other)->m_styleColor.resolve(fallbackColor);
58     Color toColor = m_styleColor.resolve(fallbackColor);
59     m_styleColor = StyleColor(ColorDistance::addColors(fromColor, toColor));
60 }
61
62 void SVGColorProperty::calculateAnimatedValue(SVGAnimationElement* animationElement, float percentage, unsigned repeatCount, PassRefPtr<NewSVGPropertyBase> fromValue, PassRefPtr<NewSVGPropertyBase> toValue, PassRefPtr<NewSVGPropertyBase> toAtEndOfDurationValue, SVGElement* contextElement)
63 {
64     StyleColor fromStyleColor = toSVGColorProperty(fromValue)->m_styleColor;
65     StyleColor toStyleColor = toSVGColorProperty(toValue)->m_styleColor;
66     StyleColor toAtEndOfDurationStyleColor = toSVGColorProperty(toAtEndOfDurationValue)->m_styleColor;
67
68     // Apply currentColor rules.
69     ASSERT(contextElement);
70     Color fallbackColor = fallbackColorForCurrentColor(contextElement);
71     Color fromColor = fromStyleColor.resolve(fallbackColor);
72     Color toColor = toStyleColor.resolve(fallbackColor);
73     Color toAtEndOfDurationColor = toAtEndOfDurationStyleColor.resolve(fallbackColor);
74     Color animatedColor = m_styleColor.resolve(fallbackColor);
75
76     ASSERT(animationElement);
77     float animatedRed = animatedColor.red();
78     animationElement->animateAdditiveNumber(percentage, repeatCount, fromColor.red(), toColor.red(), toAtEndOfDurationColor.red(), animatedRed);
79
80     float animatedGreen = animatedColor.green();
81     animationElement->animateAdditiveNumber(percentage, repeatCount, fromColor.green(), toColor.green(), toAtEndOfDurationColor.green(), animatedGreen);
82
83     float animatedBlue = animatedColor.blue();
84     animationElement->animateAdditiveNumber(percentage, repeatCount, fromColor.blue(), toColor.blue(), toAtEndOfDurationColor.blue(), animatedBlue);
85
86     float animatedAlpha = animatedColor.alpha();
87     animationElement->animateAdditiveNumber(percentage, repeatCount, fromColor.alpha(), toColor.alpha(), toAtEndOfDurationColor.alpha(), animatedAlpha);
88
89     m_styleColor = StyleColor(makeRGBA(roundf(animatedRed), roundf(animatedGreen), roundf(animatedBlue), roundf(animatedAlpha)));
90 }
91
92 float SVGColorProperty::calculateDistance(PassRefPtr<NewSVGPropertyBase> toValue, SVGElement* contextElement)
93 {
94     ASSERT(contextElement);
95     Color fallbackColor = fallbackColorForCurrentColor(contextElement);
96
97     Color fromColor = m_styleColor.resolve(fallbackColor);
98     Color toColor = toSVGColorProperty(toValue)->m_styleColor.resolve(fallbackColor);
99     return ColorDistance::distance(fromColor, toColor);
100 }
101
102 }