OLD | NEW |
1 // | 1 // |
2 // Copyright 2014 Google Inc. All rights reserved. | 2 // Copyright 2014 Google Inc. All rights reserved. |
3 // | 3 // |
4 // Use of this source code is governed by a BSD-style | 4 // Use of this source code is governed by a BSD-style |
5 // license that can be found in the LICENSE file or at | 5 // license that can be found in the LICENSE file or at |
6 // https://developers.google.com/open-source/licenses/bsd | 6 // https://developers.google.com/open-source/licenses/bsd |
7 // | 7 // |
8 | 8 |
9 part of charted.core.interpolators; | 9 part of charted.core.interpolators; |
10 | 10 |
11 /// [Interpolator] accepts [t], such that 0.0 < t < 1.0 and returns | 11 /// [Interpolator] accepts [t], such that 0.0 < t < 1.0 and returns |
12 /// a value in a pre-defined range. | 12 /// a value in a pre-defined range. |
13 typedef Interpolator(num t); | 13 typedef Interpolator(num t); |
14 | 14 |
15 /// [InterpolatorGenerator] accepts two parameters [a], [b] and returns an | 15 /// [InterpolatorGenerator] accepts two parameters [a], [b] and returns an |
16 /// [Interpolator] for transitioning from [a] to [b] | 16 /// [Interpolator] for transitioning from [a] to [b] |
17 typedef Interpolator InterpolatorGenerator(a, b); | 17 typedef Interpolator InterpolatorGenerator(a, b); |
18 | 18 |
19 /// List of registered interpolators - [createInterpolatorFromRegistry] | 19 /// List of registered interpolators - [createInterpolatorFromRegistry] |
20 /// iterates through this list from backwards and the first non-null | 20 /// iterates through this list from backwards and the first non-null |
21 /// interpolate function is returned to the caller. | 21 /// interpolate function is returned to the caller. |
22 List<InterpolatorGenerator> _interpolators = [createInterpolatorByType]; | 22 List<InterpolatorGenerator> _interpolators = [createInterpolatorByType]; |
23 | 23 |
24 /// Returns a default interpolator between values [a] and [b]. Unless | 24 /// Returns a default interpolator between values [a] and [b]. Unless |
25 /// more interpolators are added, one of the internal implementations are | 25 /// more interpolators are added, one of the internal implementations are |
26 /// selected by the type of [a] and [b]. | 26 /// selected by the type of [a] and [b]. |
27 Interpolator createInterpolatorFromRegistry(a, b) { | 27 Interpolator createInterpolatorFromRegistry(a, b) { |
28 var fn, i = _interpolators.length; | 28 Interpolator fn; |
| 29 int i = _interpolators.length; |
29 while (--i >= 0 && fn == null) { | 30 while (--i >= 0 && fn == null) { |
30 fn = _interpolators[i](a, b); | 31 fn = _interpolators[i](a, b); |
31 } | 32 } |
32 return fn; | 33 return fn; |
33 } | 34 } |
34 | 35 |
35 /// Creates an interpolator based on the type of [a] and [b]. | 36 /// Creates an interpolator based on the type of [a] and [b]. |
36 /// | 37 /// |
37 /// Usage note: Use this method only when type of [a] and [b] are not known. | 38 /// Usage note: Use this method only when type of [a] and [b] are not known. |
38 /// When used, this function will prevent tree shaking of all built-in | 39 /// When used, this function will prevent tree shaking of all built-in |
39 /// interpolators. | 40 /// interpolators. |
40 Interpolator createInterpolatorByType(a, b) => (a is List && b is List) | 41 Interpolator createInterpolatorByType(a, b) { |
41 ? createListInterpolator(a, b) | 42 if (a is List && b is List) { |
42 : (a is Map && b is Map) | 43 return createListInterpolator(a, b); |
43 ? createMapInterpolator(a, b) | 44 } else if (a is Map && b is Map) { |
44 : (a is String && b is String) | 45 return createMapInterpolator(a, b); |
45 ? createStringInterpolator(a, b) | 46 } else if (a is String && b is String) { |
46 : (a is num && b is num) | 47 return createStringInterpolator(a, b); |
47 ? createNumberInterpolator(a, b) | 48 } else if (a is num && b is num) { |
48 : (a is Color && b is Color) | 49 return createNumberInterpolator(a, b); |
49 ? createRgbColorInterpolator(a, b) | 50 } else if (a is Color && b is Color) { |
50 : (t) => (t <= 0.5) ? a : b; | 51 return createRgbColorInterpolator(a, b); |
| 52 } else { |
| 53 return (t) => (t <= 0.5) ? a : b; |
| 54 } |
| 55 } |
51 | 56 |
52 // | 57 // |
53 // Implementations of InterpolatorGenerator | 58 // Implementations of InterpolatorGenerator |
54 // | 59 // |
55 | 60 |
56 /// Generate a numeric interpolator between numbers [a] and [b] | 61 /// Generate a numeric interpolator between numbers [a] and [b] |
57 Interpolator createNumberInterpolator(num a, num b) { | 62 Interpolator createNumberInterpolator(num a, num b) { |
58 b -= a; | 63 b -= a; |
59 return (t) => a + b * t; | 64 return (t) => a + b * t; |
60 } | 65 } |
(...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
358 Interpolator uninterpolateNumber(num a, num b) { | 363 Interpolator uninterpolateNumber(num a, num b) { |
359 b = 1 / (b - a); | 364 b = 1 / (b - a); |
360 return (x) => (x - a) * b; | 365 return (x) => (x - a) * b; |
361 } | 366 } |
362 | 367 |
363 /// Reverse interpolator for a clamped number. | 368 /// Reverse interpolator for a clamped number. |
364 Interpolator uninterpolateClamp(num a, num b) { | 369 Interpolator uninterpolateClamp(num a, num b) { |
365 b = 1 / (b - a); | 370 b = 1 / (b - a); |
366 return (x) => math.max(0, math.min(1, (x - a) * b)); | 371 return (x) => math.max(0, math.min(1, (x - a) * b)); |
367 } | 372 } |
OLD | NEW |