forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtween.cc
251 lines (202 loc) · 7.53 KB
/
tween.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/gfx/animation/tween.h"
#include <math.h>
#include <stdint.h>
#include <algorithm>
#include "base/check_op.h"
#include "base/notreached.h"
#include "base/numerics/safe_conversions.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "ui/gfx/geometry/cubic_bezier.h"
#if defined(OS_WIN)
#include <float.h>
#endif
namespace gfx {
// static
double Tween::CalculateValue(Tween::Type type, double state) {
DCHECK_GE(state, 0);
DCHECK_LE(state, 1);
switch (type) {
case EASE_IN:
return pow(state, 2);
case EASE_IN_2:
return pow(state, 4);
case EASE_IN_OUT:
if (state < 0.5)
return pow(state * 2, 2) / 2.0;
return 1.0 - (pow((state - 1.0) * 2, 2) / 2.0);
case EASE_IN_OUT_2:
return gfx::CubicBezier(0.33, 0, 0.67, 1).Solve(state);
case EASE_OUT_3:
return gfx::CubicBezier(0.6, 0, 0, 1).Solve(state);
case EASE_OUT_4:
return gfx::CubicBezier(1, 0, 0.8, 1).Solve(state);
case LINEAR:
return state;
case EASE_OUT:
return 1.0 - pow(1.0 - state, 2);
case EASE_OUT_2:
return gfx::CubicBezier(0.4, 0, 0, 1).Solve(state);
case SMOOTH_IN_OUT:
return sin(state);
case FAST_OUT_SLOW_IN:
return gfx::CubicBezier(0.4, 0, 0.2, 1).Solve(state);
case FAST_OUT_SLOW_IN_2:
return gfx::CubicBezier(0.2, 0, 0.2, 1).Solve(state);
case FAST_OUT_SLOW_IN_3:
return gfx::CubicBezier(0.2, 0, 0, 1).Solve(state);
case LINEAR_OUT_SLOW_IN:
return gfx::CubicBezier(0, 0, .2, 1).Solve(state);
case SLOW_OUT_LINEAR_IN:
return gfx::CubicBezier(0, 0, 1, .2).Solve(state);
case FAST_OUT_LINEAR_IN:
return gfx::CubicBezier(0.4, 0, 1, 1).Solve(state);
case ZERO:
return 0;
case ACCEL_LIN_DECEL_60:
return gfx::CubicBezier(0, 0, 0.4, 1).Solve(state);
case ACCEL_LIN_DECEL_100:
return gfx::CubicBezier(0, 0, 0, 1).Solve(state);
case ACCEL_20_DECEL_60:
return gfx::CubicBezier(0.2, 0, 0.4, 1).Solve(state);
}
NOTREACHED();
return state;
}
namespace {
uint8_t FloatToColorByte(float f) {
return base::ClampRound<uint8_t>(f * 255.0f);
}
uint8_t BlendColorComponents(uint8_t start,
uint8_t target,
float start_alpha,
float target_alpha,
float blended_alpha,
double progress) {
// Since progress can be outside [0, 1], blending can produce a value outside
// [0, 255].
float blended_premultiplied = Tween::FloatValueBetween(
progress, start / 255.f * start_alpha, target / 255.f * target_alpha);
return FloatToColorByte(blended_premultiplied / blended_alpha);
}
} // namespace
// static
SkColor Tween::ColorValueBetween(double value, SkColor start, SkColor target) {
float start_a = SkColorGetA(start) / 255.f;
float target_a = SkColorGetA(target) / 255.f;
float blended_a = FloatValueBetween(value, start_a, target_a);
if (blended_a <= 0.f)
return SK_ColorTRANSPARENT;
blended_a = std::min(blended_a, 1.f);
uint8_t blended_r =
BlendColorComponents(SkColorGetR(start), SkColorGetR(target), start_a,
target_a, blended_a, value);
uint8_t blended_g =
BlendColorComponents(SkColorGetG(start), SkColorGetG(target), start_a,
target_a, blended_a, value);
uint8_t blended_b =
BlendColorComponents(SkColorGetB(start), SkColorGetB(target), start_a,
target_a, blended_a, value);
return SkColorSetARGB(
FloatToColorByte(blended_a), blended_r, blended_g, blended_b);
}
// static
double Tween::DoubleValueBetween(double value, double start, double target) {
return start + (target - start) * value;
}
// static
float Tween::FloatValueBetween(double value, float start, float target) {
return static_cast<float>(start + (target - start) * value);
}
// static
float Tween::ClampedFloatValueBetween(const base::TimeTicks& time,
const base::TimeTicks& start_time,
float start,
const base::TimeTicks& target_time,
float target) {
if (time <= start_time)
return start;
if (time >= target_time)
return target;
const double progress = (time - start_time) / (target_time - start_time);
return FloatValueBetween(progress, start, target);
}
// static
int Tween::IntValueBetween(double value, int start, int target) {
if (start == target)
return start;
double delta = static_cast<double>(target - start);
if (delta < 0)
delta--;
else
delta++;
#if defined(OS_WIN)
return start + static_cast<int>(value * _nextafter(delta, 0));
#else
return start + static_cast<int>(value * nextafter(delta, 0));
#endif
}
// static
int Tween::LinearIntValueBetween(double value, int start, int target) {
// NOTE: Do not use base::ClampRound()! See comments on function declaration.
return base::ClampFloor(0.5 + DoubleValueBetween(value, start, target));
}
// static
gfx::Rect Tween::RectValueBetween(double value,
const gfx::Rect& start,
const gfx::Rect& target) {
const int x = LinearIntValueBetween(value, start.x(), target.x());
const int y = LinearIntValueBetween(value, start.y(), target.y());
const int right = LinearIntValueBetween(value, start.right(), target.right());
const int bottom =
LinearIntValueBetween(value, start.bottom(), target.bottom());
return gfx::Rect(x, y, right - x, bottom - y);
}
// static
gfx::RectF Tween::RectFValueBetween(double value,
const gfx::RectF& start,
const gfx::RectF& target) {
const float x = FloatValueBetween(value, start.x(), target.x());
const float y = FloatValueBetween(value, start.y(), target.y());
const float right = FloatValueBetween(value, start.right(), target.right());
const float bottom =
FloatValueBetween(value, start.bottom(), target.bottom());
return gfx::RectF(x, y, right - x, bottom - y);
}
// static
gfx::Transform Tween::TransformValueBetween(double value,
const gfx::Transform& start,
const gfx::Transform& target) {
if (value >= 1.0)
return target;
if (value <= 0.0)
return start;
gfx::Transform to_return = target;
to_return.Blend(start, value);
return to_return;
}
// static
gfx::TransformOperations Tween::TransformOperationsValueBetween(
double value,
const gfx::TransformOperations& start,
const gfx::TransformOperations& target) {
return target.Blend(start, value);
}
gfx::Size Tween::SizeValueBetween(double value,
const gfx::Size& start,
const gfx::Size& target) {
return gfx::Size(
Tween::LinearIntValueBetween(value, start.width(), target.width()),
Tween::LinearIntValueBetween(value, start.height(), target.height()));
}
gfx::SizeF Tween::SizeFValueBetween(double value,
const gfx::SizeF& start,
const gfx::SizeF& target) {
return gfx::SizeF(
Tween::FloatValueBetween(value, start.width(), target.width()),
Tween::FloatValueBetween(value, start.height(), target.height()));
}
} // namespace gfx