Web Animations: define distance for AnimatableColor

Distance is defined in
http://dev.w3.org/fxtf/web-animations/ section 4.1.3.5

Web Animations use property distance calculations in paced keyframe animation effects, in assigning the timing for keyframes so that a 'paced' property changes at constant velocity.

BUG=348896

Review URL: https://codereview.chromium.org/186463002

git-svn-id: svn://svn.chromium.org/blink/trunk@168559 bbb929c8-8fbe-4397-9dbb-9b2b20218538
parent e038811d
...@@ -34,6 +34,15 @@ ...@@ -34,6 +34,15 @@
#include "platform/animation/AnimationUtilities.h" #include "platform/animation/AnimationUtilities.h"
#include "wtf/MathExtras.h" #include "wtf/MathExtras.h"
namespace {
double square(double x)
{
return x * x;
}
} // namespace
namespace WebCore { namespace WebCore {
AnimatableColorImpl::AnimatableColorImpl(float red, float green, float blue, float alpha) AnimatableColorImpl::AnimatableColorImpl(float red, float green, float blue, float alpha)
...@@ -83,6 +92,14 @@ bool AnimatableColorImpl::operator==(const AnimatableColorImpl& other) const ...@@ -83,6 +92,14 @@ bool AnimatableColorImpl::operator==(const AnimatableColorImpl& other) const
&& m_alpha == other.m_alpha; && m_alpha == other.m_alpha;
} }
double AnimatableColorImpl::distanceTo(const AnimatableColorImpl& other) const
{
return sqrt(square(m_red - other.m_red)
+ square(m_green - other.m_green)
+ square(m_blue - other.m_blue)
+ square(m_alpha - other.m_alpha));
}
PassRefPtr<AnimatableColor> AnimatableColor::create(const AnimatableColorImpl& color, const AnimatableColorImpl& visitedLinkColor) PassRefPtr<AnimatableColor> AnimatableColor::create(const AnimatableColorImpl& color, const AnimatableColorImpl& visitedLinkColor)
{ {
return adoptRef(new AnimatableColor(color, visitedLinkColor)); return adoptRef(new AnimatableColor(color, visitedLinkColor));
...@@ -108,4 +125,10 @@ bool AnimatableColor::equalTo(const AnimatableValue* value) const ...@@ -108,4 +125,10 @@ bool AnimatableColor::equalTo(const AnimatableValue* value) const
return m_color == color->m_color && m_visitedLinkColor == color->m_visitedLinkColor; return m_color == color->m_color && m_visitedLinkColor == color->m_visitedLinkColor;
} }
double AnimatableColor::distanceTo(const AnimatableValue* value) const
{
const AnimatableColor* color = toAnimatableColor(value);
return m_color.distanceTo(color->m_color);
}
} // namespace WebCore } // namespace WebCore
...@@ -44,6 +44,7 @@ public: ...@@ -44,6 +44,7 @@ public:
AnimatableColorImpl interpolateTo(const AnimatableColorImpl&, double fraction) const; AnimatableColorImpl interpolateTo(const AnimatableColorImpl&, double fraction) const;
AnimatableColorImpl addWith(const AnimatableColorImpl&) const; AnimatableColorImpl addWith(const AnimatableColorImpl&) const;
bool operator==(const AnimatableColorImpl&) const; bool operator==(const AnimatableColorImpl&) const;
double distanceTo(const AnimatableColorImpl&) const;
private: private:
float m_alpha; float m_alpha;
...@@ -74,6 +75,7 @@ private: ...@@ -74,6 +75,7 @@ private:
} }
virtual AnimatableType type() const OVERRIDE { return TypeColor; } virtual AnimatableType type() const OVERRIDE { return TypeColor; }
virtual bool equalTo(const AnimatableValue*) const OVERRIDE; virtual bool equalTo(const AnimatableValue*) const OVERRIDE;
virtual double distanceTo(const AnimatableValue*) const OVERRIDE;
const AnimatableColorImpl m_color; const AnimatableColorImpl m_color;
const AnimatableColorImpl m_visitedLinkColor; const AnimatableColorImpl m_visitedLinkColor;
}; };
......
...@@ -75,5 +75,16 @@ TEST(AnimationAnimatableColorTest, Add) ...@@ -75,5 +75,16 @@ TEST(AnimationAnimatableColorTest, Add)
EXPECT_EQ(AnimatableColorImpl(Color(0x10204080)).addWith(Color(0x104080C0)).toColor().rgb(), 0x203060A0u); EXPECT_EQ(AnimatableColorImpl(Color(0x10204080)).addWith(Color(0x104080C0)).toColor().rgb(), 0x203060A0u);
} }
TEST(AnimationAnimatableColorTest, Distance)
{
EXPECT_NEAR(1.0, AnimatableColorImpl(Color(0xFF000000)).distanceTo(Color(0xFFFF0000)), 0.00000001);
EXPECT_NEAR(13.0 / 255, AnimatableColorImpl(Color(0xFF53647C)).distanceTo(Color(0xFF506070)), 0.00000001);
EXPECT_NEAR(60.0 / 255, AnimatableColorImpl(Color(0x3C000000)).distanceTo(Color(0x00FFFFFF)), 0.00000001);
EXPECT_NEAR(60.0 / 255, AnimatableColorImpl(Color(0x3C000000)).distanceTo(Color(0x3C00FF00)), 0.00000001);
RefPtrWillBeRawPtr<AnimatableColor> first = AnimatableColor::create(AnimatableColorImpl(Color(0xFF53647C)), AnimatableColorImpl(Color(0xFF000000)));
RefPtrWillBeRawPtr<AnimatableColor> second = AnimatableColor::create(AnimatableColorImpl(Color(0xFF506070)), AnimatableColorImpl(Color(0xFF000000)));
EXPECT_NEAR(13.0 / 255, AnimatableValue::distance(first.get(), second.get()), 0.00000001);
} }
}
...@@ -34,6 +34,7 @@ ...@@ -34,6 +34,7 @@
#include "core/css/CSSPrimitiveValue.h" #include "core/css/CSSPrimitiveValue.h"
#include "core/css/CSSValuePool.h" #include "core/css/CSSValuePool.h"
#include "platform/animation/AnimationUtilities.h" #include "platform/animation/AnimationUtilities.h"
#include <math.h>
namespace WebCore { namespace WebCore {
...@@ -74,4 +75,10 @@ bool AnimatableDouble::equalTo(const AnimatableValue* value) const ...@@ -74,4 +75,10 @@ bool AnimatableDouble::equalTo(const AnimatableValue* value) const
return m_number == toAnimatableDouble(value)->m_number; return m_number == toAnimatableDouble(value)->m_number;
} }
double AnimatableDouble::distanceTo(const AnimatableValue* value) const
{
const AnimatableDouble* other = toAnimatableDouble(value);
return fabs(m_number - other->m_number);
}
} // namespace WebCore } // namespace WebCore
...@@ -66,6 +66,7 @@ private: ...@@ -66,6 +66,7 @@ private:
} }
virtual AnimatableType type() const OVERRIDE { return TypeDouble; } virtual AnimatableType type() const OVERRIDE { return TypeDouble; }
virtual bool equalTo(const AnimatableValue*) const OVERRIDE; virtual bool equalTo(const AnimatableValue*) const OVERRIDE;
virtual double distanceTo(const AnimatableValue*) const OVERRIDE;
double m_number; double m_number;
Constraint m_constraint; Constraint m_constraint;
......
...@@ -88,4 +88,15 @@ TEST(AnimationAnimatableDoubleTest, Add) ...@@ -88,4 +88,15 @@ TEST(AnimationAnimatableDoubleTest, Add)
EXPECT_EQ(30, toAnimatableDouble(AnimatableValue::add(AnimatableDouble::create(30).get(), AnimatableDouble::create(0).get()).get())->toDouble()); EXPECT_EQ(30, toAnimatableDouble(AnimatableValue::add(AnimatableDouble::create(30).get(), AnimatableDouble::create(0).get()).get())->toDouble());
} }
TEST(AnimationAnimatableDoubleTest, Distance)
{
RefPtrWillBeRawPtr<AnimatableDouble> first = AnimatableDouble::create(-1.5);
RefPtrWillBeRawPtr<AnimatableDouble> second = AnimatableDouble::create(2.25);
RefPtrWillBeRawPtr<AnimatableDouble> third = AnimatableDouble::create(3);
EXPECT_DOUBLE_EQ(3.75, AnimatableValue::distance(first.get(), second.get()));
EXPECT_DOUBLE_EQ(0.75, AnimatableValue::distance(second.get(), third.get()));
EXPECT_DOUBLE_EQ(4.5, AnimatableValue::distance(third.get(), first.get()));
}
} }
...@@ -34,6 +34,12 @@ ...@@ -34,6 +34,12 @@
#include "wtf/StdLibExtras.h" #include "wtf/StdLibExtras.h"
#include <algorithm> #include <algorithm>
namespace {
const double defaultDistance = 1;
} // namespace
namespace WebCore { namespace WebCore {
const AnimatableValue* AnimatableValue::neutralValue() const AnimatableValue* AnimatableValue::neutralValue()
...@@ -76,4 +82,20 @@ PassRefPtr<AnimatableValue> AnimatableValue::addWith(const AnimatableValue* valu ...@@ -76,4 +82,20 @@ PassRefPtr<AnimatableValue> AnimatableValue::addWith(const AnimatableValue* valu
return defaultAddWith(this, value); return defaultAddWith(this, value);
} }
double AnimatableValue::distance(const AnimatableValue* left, const AnimatableValue* right)
{
ASSERT(left);
ASSERT(right);
if (left->isSameType(right))
return left->distanceTo(right);
return defaultDistance;
}
double AnimatableValue::distanceTo(const AnimatableValue*) const
{
return defaultDistance;
}
} // namespace WebCore } // namespace WebCore
...@@ -46,6 +46,7 @@ public: ...@@ -46,6 +46,7 @@ public:
static PassRefPtr<AnimatableValue> interpolate(const AnimatableValue*, const AnimatableValue*, double fraction); static PassRefPtr<AnimatableValue> interpolate(const AnimatableValue*, const AnimatableValue*, double fraction);
// For noncommutative values read add(A, B) to mean the value A with B composed onto it. // For noncommutative values read add(A, B) to mean the value A with B composed onto it.
static PassRefPtr<AnimatableValue> add(const AnimatableValue*, const AnimatableValue*); static PassRefPtr<AnimatableValue> add(const AnimatableValue*, const AnimatableValue*);
static double distance(const AnimatableValue* from, const AnimatableValue* to);
static bool usesDefaultInterpolation(const AnimatableValue* from, const AnimatableValue* to) static bool usesDefaultInterpolation(const AnimatableValue* from, const AnimatableValue* to)
{ {
return !from->isSameType(to) || from->usesDefaultInterpolationWith(to); return !from->isSameType(to) || from->usesDefaultInterpolationWith(to);
...@@ -132,6 +133,8 @@ private: ...@@ -132,6 +133,8 @@ private:
// Implementations can assume that the object being compared has the same type as the object this is called on // Implementations can assume that the object being compared has the same type as the object this is called on
virtual bool equalTo(const AnimatableValue*) const = 0; virtual bool equalTo(const AnimatableValue*) const = 0;
virtual double distanceTo(const AnimatableValue*) const;
friend class KeyframeEffectModel; friend class KeyframeEffectModel;
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment