1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/gfx/animation/tween.h"
15 #include "base/basictypes.h"
16 #include "base/logging.h"
17 #include "ui/gfx/geometry/cubic_bezier.h"
18 #include "ui/gfx/safe_integer_conversions.h"
23 double Tween::CalculateValue(Tween::Type type
, double state
) {
36 return pow(state
* 2, 2) / 2.0;
37 return 1.0 - (pow((state
- 1.0) * 2, 2) / 2.0);
40 return (pow(state
- 0.5, 3) + 0.125) / 0.25;
46 state
= 0.95 * (1.0 - pow(1.0 - state
, 2));
50 return 1.0 - pow(1.0 - state
, 2);
55 case FAST_OUT_SLOW_IN
:
56 return gfx::CubicBezier(0.4, 0, 0.2, 1).Solve(state
);
58 case LINEAR_OUT_SLOW_IN
:
59 return gfx::CubicBezier(0, 0, .2, 1).Solve(state
);
61 case FAST_OUT_LINEAR_IN
:
62 return gfx::CubicBezier(0.4, 0, 1, 1).Solve(state
);
73 uint8
FloatToColorByte(float f
) {
74 return std::min(std::max(ToRoundedInt(f
* 255.f
), 0), 255);
77 uint8
BlendColorComponents(uint8 start
,
83 // Since progress can be outside [0, 1], blending can produce a value outside
85 float blended_premultiplied
= Tween::FloatValueBetween(
86 progress
, start
/ 255.f
* start_alpha
, target
/ 255.f
* target_alpha
);
87 return FloatToColorByte(blended_premultiplied
/ blended_alpha
);
93 SkColor
Tween::ColorValueBetween(double value
, SkColor start
, SkColor target
) {
94 float start_a
= SkColorGetA(start
) / 255.f
;
95 float target_a
= SkColorGetA(target
) / 255.f
;
96 float blended_a
= FloatValueBetween(value
, start_a
, target_a
);
98 return SkColorSetARGB(0, 0, 0, 0);
99 blended_a
= std::min(blended_a
, 1.f
);
101 uint8 blended_r
= BlendColorComponents(SkColorGetR(start
),
107 uint8 blended_g
= BlendColorComponents(SkColorGetG(start
),
113 uint8 blended_b
= BlendColorComponents(SkColorGetB(start
),
120 return SkColorSetARGB(
121 FloatToColorByte(blended_a
), blended_r
, blended_g
, blended_b
);
125 double Tween::DoubleValueBetween(double value
, double start
, double target
) {
126 return start
+ (target
- start
) * value
;
130 float Tween::FloatValueBetween(double value
, float start
, float target
) {
131 return static_cast<float>(start
+ (target
- start
) * value
);
135 int Tween::IntValueBetween(double value
, int start
, int target
) {
138 double delta
= static_cast<double>(target
- start
);
144 return start
+ static_cast<int>(value
* _nextafter(delta
, 0));
146 return start
+ static_cast<int>(value
* nextafter(delta
, 0));
151 int Tween::LinearIntValueBetween(double value
, int start
, int target
) {
152 return std::floor(0.5 + DoubleValueBetween(value
, start
, target
));
156 gfx::Rect
Tween::RectValueBetween(double value
,
157 const gfx::Rect
& start_bounds
,
158 const gfx::Rect
& target_bounds
) {
160 LinearIntValueBetween(value
, start_bounds
.x(), target_bounds
.x()),
161 LinearIntValueBetween(value
, start_bounds
.y(), target_bounds
.y()),
162 LinearIntValueBetween(value
, start_bounds
.width(), target_bounds
.width()),
163 LinearIntValueBetween(
164 value
, start_bounds
.height(), target_bounds
.height()));
168 gfx::Transform
Tween::TransformValueBetween(
170 const gfx::Transform
& start_transform
,
171 const gfx::Transform
& end_transform
) {
173 return end_transform
;
175 return start_transform
;
177 gfx::Transform to_return
= end_transform
;
178 to_return
.Blend(start_transform
, value
);