Upstream version 5.34.98.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / animation / AnimatableColor.cpp
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/AnimatableColor.h"
33
34 #include "platform/animation/AnimationUtilities.h"
35 #include "wtf/MathExtras.h"
36
37 namespace WebCore {
38
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))
44 {
45 }
46
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)
52 {
53 }
54
55 Color AnimatableColorImpl::toColor() const
56 {
57     if (!m_alpha)
58         return Color::transparent;
59     return Color(m_red / m_alpha, m_green / m_alpha, m_blue / m_alpha, m_alpha);
60 }
61
62 AnimatableColorImpl AnimatableColorImpl::interpolateTo(const AnimatableColorImpl& to, double fraction) const
63 {
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));
68 }
69
70 AnimatableColorImpl AnimatableColorImpl::addWith(const AnimatableColorImpl& addend) const
71 {
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);
76 }
77
78 bool AnimatableColorImpl::operator==(const AnimatableColorImpl& other) const
79 {
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;
84 }
85
86 PassRefPtr<AnimatableColor> AnimatableColor::create(const AnimatableColorImpl& color, const AnimatableColorImpl& visitedLinkColor)
87 {
88     return adoptRef(new AnimatableColor(color, visitedLinkColor));
89 }
90
91 PassRefPtr<AnimatableValue> AnimatableColor::interpolateTo(const AnimatableValue* value, double fraction) const
92 {
93     const AnimatableColor* color = toAnimatableColor(value);
94     return create(m_color.interpolateTo(color->m_color, fraction),
95         m_visitedLinkColor.interpolateTo(color->m_visitedLinkColor, fraction));
96 }
97
98 PassRefPtr<AnimatableValue> AnimatableColor::addWith(const AnimatableValue* value) const
99 {
100     const AnimatableColor* color = toAnimatableColor(value);
101     return create(m_color.addWith(color->m_color),
102         m_visitedLinkColor.addWith(color->m_visitedLinkColor));
103 }
104
105 bool AnimatableColor::equalTo(const AnimatableValue* value) const
106 {
107     const AnimatableColor* color = toAnimatableColor(value);
108     return m_color == color->m_color && m_visitedLinkColor == color->m_visitedLinkColor;
109 }
110
111 } // namespace WebCore