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/safe_integer_conversions.h"
22 double Tween::CalculateValue(Tween::Type type, double state) {
35 return pow(state * 2, 2) / 2.0;
36 return 1.0 - (pow((state - 1.0) * 2, 2) / 2.0);
39 return (pow(state - 0.5, 3) + 0.125) / 0.25;
45 state = 0.95 * (1.0 - pow(1.0 - state, 2));
49 return 1.0 - pow(1.0 - state, 2);
63 uint8 FloatToColorByte(float f) {
64 return std::min(std::max(ToRoundedInt(f * 255.f), 0), 255);
67 uint8 BlendColorComponents(uint8 start,
73 // Since progress can be outside [0, 1], blending can produce a value outside
75 float blended_premultiplied = Tween::FloatValueBetween(
76 progress, start / 255.f * start_alpha, target / 255.f * target_alpha);
77 return FloatToColorByte(blended_premultiplied / blended_alpha);
83 SkColor Tween::ColorValueBetween(double value, SkColor start, SkColor target) {
84 float start_a = SkColorGetA(start) / 255.f;
85 float target_a = SkColorGetA(target) / 255.f;
86 float blended_a = FloatValueBetween(value, start_a, target_a);
88 return SkColorSetARGB(0, 0, 0, 0);
89 blended_a = std::min(blended_a, 1.f);
91 uint8 blended_r = BlendColorComponents(SkColorGetR(start),
97 uint8 blended_g = BlendColorComponents(SkColorGetG(start),
103 uint8 blended_b = BlendColorComponents(SkColorGetB(start),
110 return SkColorSetARGB(
111 FloatToColorByte(blended_a), blended_r, blended_g, blended_b);
115 double Tween::DoubleValueBetween(double value, double start, double target) {
116 return start + (target - start) * value;
120 float Tween::FloatValueBetween(double value, float start, float target) {
121 return static_cast<float>(start + (target - start) * value);
125 int Tween::IntValueBetween(double value, int start, int target) {
128 double delta = static_cast<double>(target - start);
134 return start + static_cast<int>(value * _nextafter(delta, 0));
136 return start + static_cast<int>(value * nextafter(delta, 0));
141 int Tween::LinearIntValueBetween(double value, int start, int target) {
142 return std::floor(0.5 + DoubleValueBetween(value, start, target));
146 gfx::Rect Tween::RectValueBetween(double value,
147 const gfx::Rect& start_bounds,
148 const gfx::Rect& target_bounds) {
150 LinearIntValueBetween(value, start_bounds.x(), target_bounds.x()),
151 LinearIntValueBetween(value, start_bounds.y(), target_bounds.y()),
152 LinearIntValueBetween(value, start_bounds.width(), target_bounds.width()),
153 LinearIntValueBetween(
154 value, start_bounds.height(), target_bounds.height()));
158 gfx::Transform Tween::TransformValueBetween(
160 const gfx::Transform& start_transform,
161 const gfx::Transform& end_transform) {
163 return end_transform;
165 return start_transform;
167 gfx::Transform to_return = end_transform;
168 to_return.Blend(start_transform, value);