-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathgetScale.ts
More file actions
44 lines (39 loc) · 1.3 KB
/
getScale.ts
File metadata and controls
44 lines (39 loc) · 1.3 KB
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
import mix from './mix';
import guard from './guard';
/**
* Given a series colors, this function will return a `scale(x)` function that
* accepts a percentage as a decimal between 0 and 1 and returns the color at
* that percentage in the scale.
*
* ```js
* const scale = getScale('red', 'yellow', 'green');
* console.log(scale(0)); // rgba(255, 0, 0, 1)
* console.log(scale(0.5)); // rgba(255, 255, 0, 1)
* console.log(scale(1)); // rgba(0, 128, 0, 1)
* ```
*
* If you'd like to limit the domain and range like chroma-js, we recommend
* wrapping scale again.
*
* ```js
* const _scale = getScale('red', 'yellow', 'green');
* const scale = x => _scale(x / 100);
*
* console.log(scale(0)); // rgba(255, 0, 0, 1)
* console.log(scale(50)); // rgba(255, 255, 0, 1)
* console.log(scale(100)); // rgba(0, 128, 0, 1)
* ```
*/
function getScale(...colors: string[]): (n: number) => string {
return (n) => {
const lastIndex = colors.length - 1;
const lowIndex = guard(0, lastIndex, Math.floor(n * lastIndex));
const highIndex = guard(0, lastIndex, Math.ceil(n * lastIndex));
const color1 = colors[lowIndex];
const color2 = colors[highIndex];
const unit = 1 / lastIndex;
const weight = (n - unit * lowIndex) / unit;
return mix(color1, color2, weight);
};
}
export default getScale;