2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "core/animation/AnimatableColor.h"
34 #include "platform/animation/AnimationUtilities.h"
35 #include "wtf/MathExtras.h"
39 AnimatableColorImpl::AnimatableColorImpl(float red, float green, float blue, float alpha)
40 : m_alpha(clampTo(alpha, 0.0f, 1.0f))
41 , m_red(clampTo(red, 0.0f, 1.0f))
42 , m_green(clampTo(green, 0.0f, 1.0f))
43 , m_blue(clampTo(blue, 0.0f, 1.0f))
47 AnimatableColorImpl::AnimatableColorImpl(Color color)
48 : m_alpha(color.alpha() / 255.0f)
49 , m_red(color.red() / 255.0f * m_alpha)
50 , m_green(color.green() / 255.0f * m_alpha)
51 , m_blue(color.blue() / 255.0f * m_alpha)
55 Color AnimatableColorImpl::toColor() const
58 return Color::transparent;
59 return Color(m_red / m_alpha, m_green / m_alpha, m_blue / m_alpha, m_alpha);
62 AnimatableColorImpl AnimatableColorImpl::interpolateTo(const AnimatableColorImpl& to, double fraction) const
64 return AnimatableColorImpl(blend(m_red, to.m_red, fraction),
65 blend(m_green, to.m_green, fraction),
66 blend(m_blue, to.m_blue, fraction),
67 blend(m_alpha, to.m_alpha, fraction));
70 AnimatableColorImpl AnimatableColorImpl::addWith(const AnimatableColorImpl& addend) const
72 return AnimatableColorImpl(m_red + addend.m_red,
73 m_green + addend.m_green,
74 m_blue + addend.m_blue,
75 m_alpha + addend.m_alpha);
78 bool AnimatableColorImpl::operator==(const AnimatableColorImpl& other) const
80 return m_red == other.m_red
81 && m_green == other.m_green
82 && m_blue == other.m_blue
83 && m_alpha == other.m_alpha;
86 PassRefPtr<AnimatableColor> AnimatableColor::create(const AnimatableColorImpl& color, const AnimatableColorImpl& visitedLinkColor)
88 return adoptRef(new AnimatableColor(color, visitedLinkColor));
91 PassRefPtr<AnimatableValue> AnimatableColor::interpolateTo(const AnimatableValue* value, double fraction) const
93 const AnimatableColor* color = toAnimatableColor(value);
94 return create(m_color.interpolateTo(color->m_color, fraction),
95 m_visitedLinkColor.interpolateTo(color->m_visitedLinkColor, fraction));
98 PassRefPtr<AnimatableValue> AnimatableColor::addWith(const AnimatableValue* value) const
100 const AnimatableColor* color = toAnimatableColor(value);
101 return create(m_color.addWith(color->m_color),
102 m_visitedLinkColor.addWith(color->m_visitedLinkColor));
105 bool AnimatableColor::equalTo(const AnimatableValue* value) const
107 const AnimatableColor* color = toAnimatableColor(value);
108 return m_color == color->m_color && m_visitedLinkColor == color->m_visitedLinkColor;
111 } // namespace WebCore