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#include "SVGAnimatedColor.h"
22
23#include "ColorDistance.h"
24#include "RenderElement.h"
25#include "SVGAnimateElement.h"
26#include "SVGColor.h"
27
28namespace WebCore {
29
30SVGAnimatedColorAnimator::SVGAnimatedColorAnimator(SVGAnimationElement* animationElement, SVGElement* contextElement)
31    : SVGAnimatedTypeAnimator(AnimatedColor, animationElement, contextElement)
32{
33}
34
35std::unique_ptr<SVGAnimatedType> SVGAnimatedColorAnimator::constructFromString(const String& string)
36{
37    auto animatedType = SVGAnimatedType::createColor(std::make_unique<Color>());
38    animatedType->color() = string.isEmpty() ? Color() : SVGColor::colorFromRGBColorString(string);
39    return animatedType;
40}
41
42void SVGAnimatedColorAnimator::addAnimatedTypes(SVGAnimatedType* from, SVGAnimatedType* to)
43{
44    ASSERT(from->type() == AnimatedColor);
45    ASSERT(from->type() == to->type());
46    to->color() = ColorDistance::addColors(from->color(), to->color());
47}
48
49static inline void adjustForCurrentColor(SVGElement* targetElement, Color& color)
50{
51    ASSERT(targetElement);
52
53    if (RenderElement* targetRenderer = targetElement->renderer())
54        color = targetRenderer->style().visitedDependentColor(CSSPropertyColor);
55    else
56        color = Color();
57}
58
59static Color parseColorFromString(SVGAnimationElement*, const String& string)
60{
61    return SVGColor::colorFromRGBColorString(string);
62}
63
64void SVGAnimatedColorAnimator::calculateAnimatedValue(float percentage, unsigned repeatCount, SVGAnimatedType* from, SVGAnimatedType* to, SVGAnimatedType* toAtEndOfDuration, SVGAnimatedType* animated)
65{
66    ASSERT(m_animationElement);
67    ASSERT(m_contextElement);
68
69    Color fromColor = m_animationElement->animationMode() == ToAnimation ? animated->color() : from->color();
70    Color toColor = to->color();
71    const Color& toAtEndOfDurationColor = toAtEndOfDuration->color();
72    Color& animatedColor = animated->color();
73
74    // Apply CSS inheritance rules.
75    m_animationElement->adjustForInheritance<Color>(parseColorFromString, m_animationElement->fromPropertyValueType(), fromColor, m_contextElement);
76    m_animationElement->adjustForInheritance<Color>(parseColorFromString, m_animationElement->toPropertyValueType(), toColor, m_contextElement);
77
78    // Apply <animateColor> rules.
79    if (m_animationElement->fromPropertyValueType() == CurrentColorValue)
80        adjustForCurrentColor(m_contextElement, fromColor);
81    if (m_animationElement->toPropertyValueType() == CurrentColorValue)
82        adjustForCurrentColor(m_contextElement, toColor);
83
84    float animatedRed = animatedColor.red();
85    m_animationElement->animateAdditiveNumber(percentage, repeatCount, fromColor.red(), toColor.red(), toAtEndOfDurationColor.red(), animatedRed);
86
87    float animatedGreen = animatedColor.green();
88    m_animationElement->animateAdditiveNumber(percentage, repeatCount, fromColor.green(), toColor.green(), toAtEndOfDurationColor.green(), animatedGreen);
89
90    float animatedBlue = animatedColor.blue();
91    m_animationElement->animateAdditiveNumber(percentage, repeatCount, fromColor.blue(), toColor.blue(), toAtEndOfDurationColor.blue(), animatedBlue);
92
93    float animatedAlpha = animatedColor.alpha();
94    m_animationElement->animateAdditiveNumber(percentage, repeatCount, fromColor.alpha(), toColor.alpha(), toAtEndOfDurationColor.alpha(), animatedAlpha);
95
96    animatedColor = ColorDistance::clampColor(static_cast<int>(roundf(animatedRed)), static_cast<int>(roundf(animatedGreen)), static_cast<int>(roundf(animatedBlue)), static_cast<int>(roundf(animatedAlpha)));
97}
98
99float SVGAnimatedColorAnimator::calculateDistance(const String& fromString, const String& toString)
100{
101    ASSERT(m_contextElement);
102    Color from = SVGColor::colorFromRGBColorString(fromString);
103    if (!from.isValid())
104        return -1;
105    Color to = SVGColor::colorFromRGBColorString(toString);
106    if (!to.isValid())
107        return -1;
108    return ColorDistance(from, to).distance();
109}
110
111}
112