OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/gfx/animation/tween.h" | 5 #include "ui/gfx/animation/tween.h" |
6 | 6 |
7 #include <math.h> | 7 #include <math.h> |
8 | 8 |
9 #if defined(OS_WIN) | 9 #if defined(OS_WIN) |
10 #include <float.h> | 10 #include <float.h> |
11 #endif | 11 #endif |
12 | 12 |
13 #include <algorithm> | 13 #include <algorithm> |
14 | 14 |
15 #include "base/basictypes.h" | 15 #include "base/basictypes.h" |
16 #include "base/logging.h" | 16 #include "base/logging.h" |
17 #include "base/numerics/safe_conversions.h" | |
17 #include "ui/gfx/geometry/cubic_bezier.h" | 18 #include "ui/gfx/geometry/cubic_bezier.h" |
18 #include "ui/gfx/safe_integer_conversions.h" | 19 #include "ui/gfx/safe_integer_conversions.h" |
19 | 20 |
20 namespace gfx { | 21 namespace gfx { |
21 | 22 |
22 // static | 23 // static |
23 double Tween::CalculateValue(Tween::Type type, double state) { | 24 double Tween::CalculateValue(Tween::Type type, double state) { |
24 DCHECK_GE(state, 0); | 25 DCHECK_GE(state, 0); |
25 DCHECK_LE(state, 1); | 26 DCHECK_LE(state, 1); |
26 | 27 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
64 case ZERO: | 65 case ZERO: |
65 return 0; | 66 return 0; |
66 } | 67 } |
67 | 68 |
68 NOTREACHED(); | 69 NOTREACHED(); |
69 return state; | 70 return state; |
70 } | 71 } |
71 | 72 |
72 namespace { | 73 namespace { |
73 uint8 FloatToColorByte(float f) { | 74 uint8 FloatToColorByte(float f) { |
74 return std::min(std::max(ToRoundedInt(f * 255.f), 0), 255); | 75 return base::saturated_cast<uint8>(ToRoundedInt(f * 255.f)); |
75 } | 76 } |
76 | 77 |
77 uint8 BlendColorComponents(uint8 start, | 78 uint8 BlendColorComponents(uint8 start, |
78 uint8 target, | 79 uint8 target, |
79 float start_alpha, | 80 float start_alpha, |
80 float target_alpha, | 81 float target_alpha, |
81 float blended_alpha, | 82 float blended_alpha, |
82 double progress) { | 83 double progress) { |
83 // Since progress can be outside [0, 1], blending can produce a value outside | 84 // Since progress can be outside [0, 1], blending can produce a value outside |
84 // [0, 255]. | 85 // [0, 255]. |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
142 delta++; | 143 delta++; |
143 #if defined(OS_WIN) | 144 #if defined(OS_WIN) |
144 return start + static_cast<int>(value * _nextafter(delta, 0)); | 145 return start + static_cast<int>(value * _nextafter(delta, 0)); |
145 #else | 146 #else |
146 return start + static_cast<int>(value * nextafter(delta, 0)); | 147 return start + static_cast<int>(value * nextafter(delta, 0)); |
147 #endif | 148 #endif |
148 } | 149 } |
149 | 150 |
150 //static | 151 //static |
151 int Tween::LinearIntValueBetween(double value, int start, int target) { | 152 int Tween::LinearIntValueBetween(double value, int start, int target) { |
152 return std::floor(0.5 + DoubleValueBetween(value, start, target)); | 153 return ToRoundedInt(DoubleValueBetween(value, start, target)); |
Peter Kasting
2014/10/21 00:53:56
Note: This causes test failures in TweenTest.Linea
danakj
2014/10/23 15:20:42
We should have ajuma decide the right thing here,
ajuma
2014/10/23 15:44:39
Rounding towards positive infinity is indeed part
Peter Kasting
2014/10/23 17:29:45
This isn't used for CSS transitions, though. This
ajuma
2014/10/23 17:37:11
It's used by cc::FilterOperation when animating CS
Peter Kasting
2014/10/23 17:39:52
I see...
I'll change this; in the future it would
ajuma
2014/10/23 17:42:14
FWIW, the .h file has such a comment :) But it's
danakj
2014/10/23 17:43:23
I'm not proposing that. ToRoundedInt should work l
| |
153 } | 154 } |
154 | 155 |
155 // static | 156 // static |
156 gfx::Rect Tween::RectValueBetween(double value, | 157 gfx::Rect Tween::RectValueBetween(double value, |
157 const gfx::Rect& start_bounds, | 158 const gfx::Rect& start_bounds, |
158 const gfx::Rect& target_bounds) { | 159 const gfx::Rect& target_bounds) { |
159 return gfx::Rect( | 160 return gfx::Rect( |
160 LinearIntValueBetween(value, start_bounds.x(), target_bounds.x()), | 161 LinearIntValueBetween(value, start_bounds.x(), target_bounds.x()), |
161 LinearIntValueBetween(value, start_bounds.y(), target_bounds.y()), | 162 LinearIntValueBetween(value, start_bounds.y(), target_bounds.y()), |
162 LinearIntValueBetween(value, start_bounds.width(), target_bounds.width()), | 163 LinearIntValueBetween(value, start_bounds.width(), target_bounds.width()), |
(...skipping 11 matching lines...) Expand all Loading... | |
174 if (value <= 0.0) | 175 if (value <= 0.0) |
175 return start_transform; | 176 return start_transform; |
176 | 177 |
177 gfx::Transform to_return = end_transform; | 178 gfx::Transform to_return = end_transform; |
178 to_return.Blend(start_transform, value); | 179 to_return.Blend(start_transform, value); |
179 | 180 |
180 return to_return; | 181 return to_return; |
181 } | 182 } |
182 | 183 |
183 } // namespace gfx | 184 } // namespace gfx |
OLD | NEW |