-
Notifications
You must be signed in to change notification settings - Fork 7
/
example-interpolation.js
107 lines (90 loc) · 2.97 KB
/
example-interpolation.js
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
import canvasSketch from "canvas-sketch";
import {
convert,
DisplayP3Gamut,
gamutMapOKLCH,
lerp,
lerpAngle,
OKLab,
OKLCH,
serialize,
sRGB,
sRGBGamut,
} from "../src/index.js";
const settings = {
dimensions: [2048, 512],
attributes: {
// comment this out if you want sRGB output
colorSpace: "display-p3",
},
};
const mix = (() => {
const tmpA = [0, 0, 0];
const tmpB = [0, 0, 0];
// you can decide whether you'd like to interpolate in
// OKLab, OKLCH or another space (you may need to adjust interpolation
// if you use a custom space)
const interpolationSpace = OKLCH;
// e.g. mix({ space, coords }, { space, coords }, 0.5, sRGB)
return (a, b, t, outputSpace = sRGB, out = [0, 0, 0]) => {
// bring both spaces into the shared interpolation space
convert(a.coords, a.space, interpolationSpace, tmpA);
convert(b.coords, b.space, interpolationSpace, tmpB);
// now do interpolation
out[0] = lerp(tmpA[0], tmpB[0], t);
out[1] = lerp(tmpA[1], tmpB[1], t);
if (interpolationSpace.id === "oklch") {
// for cylindrical spaces, use a circular interpolation for Hue parameter
// note if you decide to use a custom space like HSL as your interpolation space,
// you'll have to use the first parameter instead...
out[2] = lerpAngle(tmpA[2], tmpB[2], t);
} else {
// otherwise can use a regular linear interpolation
out[2] = lerp(tmpA[2], tmpB[2], t);
}
// make sure we convert from interpolation space to the target output space
convert(out, interpolationSpace, outputSpace, out);
return out;
};
})();
// utility to create a ramp between two 'colors' as { space, coords }
function ramp(a, b, steps = 4, outputSpace = sRGB) {
return Array(steps)
.fill()
.map((_, i, lst) => {
const t = lst.length <= 1 ? 0 : i / (lst.length - 1);
return mix(a, b, t, outputSpace);
});
}
const sketch = ({ context }) => {
const { colorSpace = "srgb" } = context.getContextAttributes();
const gamut = colorSpace === "srgb" ? sRGBGamut : DisplayP3Gamut;
return ({ context, width, height }) => {
context.fillStyle = "white";
context.fillRect(0, 0, width, height);
const A = {
space: sRGB,
coords: [0, 0, 1],
};
const B = {
space: OKLCH,
coords: [0.55, 0.4, 30],
};
const slices = 16;
const sliceWidth = width / slices;
// the output space is whatever the canvas expects (sRGB or DisplayP3)
const outputSpace = gamut.space;
// create a ramp of colors in OKLCH
// then gamut map them to the outputSpace
const colors = ramp(A, B, slices, OKLCH).map((oklch) =>
gamutMapOKLCH(oklch, gamut, outputSpace)
);
for (let i = 0; i < slices; i++) {
const color = colors[i];
// turn the color (now in outputSpace) into a context string
context.fillStyle = serialize(color, outputSpace);
context.fillRect(i * sliceWidth, 0, sliceWidth, height);
}
};
};
canvasSketch(sketch, settings);