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 #include "config.h"
32 #include "core/animation/animatable/AnimatableColor.h"
33
34 #include "platform/animation/AnimationUtilities.h"
35 #include "wtf/MathExtras.h"
36
37 namespace {
38
square(double x)39 double square(double x)
40 {
41 return x * x;
42 }
43
44 } // namespace
45
46 namespace blink {
47
AnimatableColorImpl(float red,float green,float blue,float alpha)48 AnimatableColorImpl::AnimatableColorImpl(float red, float green, float blue, float alpha)
49 : m_alpha(clampTo(alpha, 0.0f, 1.0f))
50 , m_red(clampTo(red, 0.0f, 1.0f))
51 , m_green(clampTo(green, 0.0f, 1.0f))
52 , m_blue(clampTo(blue, 0.0f, 1.0f))
53 {
54 }
55
AnimatableColorImpl(Color color)56 AnimatableColorImpl::AnimatableColorImpl(Color color)
57 : m_alpha(color.alpha() / 255.0f)
58 , m_red(color.red() / 255.0f * m_alpha)
59 , m_green(color.green() / 255.0f * m_alpha)
60 , m_blue(color.blue() / 255.0f * m_alpha)
61 {
62 }
63
toColor() const64 Color AnimatableColorImpl::toColor() const
65 {
66 if (!m_alpha)
67 return Color::transparent;
68 return Color(m_red / m_alpha, m_green / m_alpha, m_blue / m_alpha, m_alpha);
69 }
70
interpolateTo(const AnimatableColorImpl & to,double fraction) const71 AnimatableColorImpl AnimatableColorImpl::interpolateTo(const AnimatableColorImpl& to, double fraction) const
72 {
73 return AnimatableColorImpl(blend(m_red, to.m_red, fraction),
74 blend(m_green, to.m_green, fraction),
75 blend(m_blue, to.m_blue, fraction),
76 blend(m_alpha, to.m_alpha, fraction));
77 }
78
operator ==(const AnimatableColorImpl & other) const79 bool AnimatableColorImpl::operator==(const AnimatableColorImpl& other) const
80 {
81 return m_red == other.m_red
82 && m_green == other.m_green
83 && m_blue == other.m_blue
84 && m_alpha == other.m_alpha;
85 }
86
distanceTo(const AnimatableColorImpl & other) const87 double AnimatableColorImpl::distanceTo(const AnimatableColorImpl& other) const
88 {
89 return sqrt(square(m_red - other.m_red)
90 + square(m_green - other.m_green)
91 + square(m_blue - other.m_blue)
92 + square(m_alpha - other.m_alpha));
93 }
94
create(const AnimatableColorImpl & color,const AnimatableColorImpl & visitedLinkColor)95 PassRefPtrWillBeRawPtr<AnimatableColor> AnimatableColor::create(const AnimatableColorImpl& color, const AnimatableColorImpl& visitedLinkColor)
96 {
97 return adoptRefWillBeNoop(new AnimatableColor(color, visitedLinkColor));
98 }
99
interpolateTo(const AnimatableValue * value,double fraction) const100 PassRefPtrWillBeRawPtr<AnimatableValue> AnimatableColor::interpolateTo(const AnimatableValue* value, double fraction) const
101 {
102 const AnimatableColor* color = toAnimatableColor(value);
103 return create(m_color.interpolateTo(color->m_color, fraction),
104 m_visitedLinkColor.interpolateTo(color->m_visitedLinkColor, fraction));
105 }
106
equalTo(const AnimatableValue * value) const107 bool AnimatableColor::equalTo(const AnimatableValue* value) const
108 {
109 const AnimatableColor* color = toAnimatableColor(value);
110 return m_color == color->m_color && m_visitedLinkColor == color->m_visitedLinkColor;
111 }
112
distanceTo(const AnimatableValue * value) const113 double AnimatableColor::distanceTo(const AnimatableValue* value) const
114 {
115 const AnimatableColor* color = toAnimatableColor(value);
116 return m_color.distanceTo(color->m_color);
117 }
118
119 } // namespace blink
120