-
-
Notifications
You must be signed in to change notification settings - Fork 150
/
cosine-gradients.ts
297 lines (291 loc) · 7.1 KB
/
cosine-gradients.ts
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import type { FnU, FnU2, NumericArray } from "@thi.ng/api";
import { partial } from "@thi.ng/compose/partial";
import { TAU } from "@thi.ng/math/api";
import { clamp01 } from "@thi.ng/math/interval";
import { comp } from "@thi.ng/transducers/comp";
import { map } from "@thi.ng/transducers/map";
import { noop } from "@thi.ng/transducers/noop";
import { normRange } from "@thi.ng/transducers/norm-range";
import { push } from "@thi.ng/transducers/push";
import { transduce } from "@thi.ng/transducers/transduce";
import { tween } from "@thi.ng/transducers/tween";
import { zip } from "@thi.ng/transducers/zip";
import { setS4 } from "@thi.ng/vectors/sets";
import type { Color, ReadonlyColor } from "./api.js";
import type {
CosGradientSpec,
CosineCoeffs,
CosineGradientOpts,
CosineGradientPreset,
} from "./api/gradients.js";
import { clamp } from "./clamp.js";
/**
* Preset cosine gradient definitions. See previews at:
* https://github.com/thi-ng/umbrella/tree/develop/packages/color#cosine-gradients
*
* @remarks
* See http://dev.thi.ng/gradients/ for a gradient designer. Note: unlike the
* linked original Clojure version, these presets here are for RGBA (though the
* alpha channel is configured to always be 1.0)
*/
export const COSINE_GRADIENTS: Record<CosineGradientPreset, CosGradientSpec> = {
"blue-cyan": [
[0, 0.5, 0.5, 1],
[0, 0.5, 0.5, 0],
[0, 0.5, 0.3333, 0],
[0, 0.5, 0.6666, 0],
],
"blue-magenta-orange": [
[0.938, 0.328, 0.718, 1],
[0.659, 0.438, 0.328, 0],
[0.388, 0.388, 0.296, 0],
[2.538, 2.478, 0.168, 0],
],
"blue-white-red": [
[0.66, 0.56, 0.68, 1],
[0.718, 0.438, 0.72, 0],
[0.52, 0.8, 0.52, 0],
[-0.43, -0.397, -0.083, 0],
],
"cyan-magenta": [
[0.61, 0.498, 0.65, 1],
[0.388, 0.498, 0.35, 0],
[0.53, 0.498, 0.62, 0],
[3.438, 3.012, 4.025, 0],
],
"green-blue-orange": [
[0.892, 0.725, 0, 1],
[0.878, 0.278, 0.725, 0],
[0.332, 0.518, 0.545, 0],
[2.44, 5.043, 0.732, 0],
],
"green-cyan": [
[0, 0.5, 0.5, 1],
[0, 0.5, 0.5, 0],
[0, 0.3333, 0.5, 0],
[0, 0.6666, 0.5, 0],
],
"green-magenta": [
[0.6666, 0.5, 0.5, 1],
[0.5, 0.6666, 0.5, 0],
[0.6666, 0.666, 0.5, 0],
[0.2, 0, 0.5, 0],
],
"green-red": [
[0.5, 0.5, 0, 1],
[0.5, 0.5, 0, 0],
[0.5, 0.5, 0, 0],
[0.5, 0, 0, 0],
],
heat1: [
[0.5, 0.4, 0.25, 1],
[0.5, 0.5, 0.666, 0],
[0.5, 0.666, 0.8, 0],
[0.5, 0.666, 0.8, 0],
],
"magenta-green": [
[0.59, 0.811, 0.12, 1],
[0.41, 0.392, 0.59, 0],
[0.94, 0.548, 0.278, 0],
[-4.242, -6.611, -4.045, 0],
],
"orange-blue": [
[0.5, 0.5, 0.5, 1],
[0.5, 0.5, 0.5, 0],
[0.8, 0.8, 0.5, 0],
[0, 0.2, 0.5, 0],
],
"orange-magenta-blue": [
[0.821, 0.328, 0.242, 1],
[0.659, 0.481, 0.896, 0],
[0.612, 0.34, 0.296, 0],
[2.82, 3.026, -0.273, 0],
],
"purple-orange-cyan": [
[0.5, 0.5, 0.5, 1],
[0.5, 0.5, 0.5, 0],
[0.5, 0.5, 1, 0],
[-0.25, 0.5, 1, 0],
],
rainbow1: [
[0.5, 0.5, 0.5, 1],
[0.5, 0.5, 0.5, 0],
[1.0, 1.0, 1.0, 0],
[0, 0.3333, 0.6666, 0],
],
rainbow2: [
[0.5, 0.5, 0.5, 1],
[0.666, 0.666, 0.666, 0],
[1.0, 1.0, 1.0, 0],
[0, 0.3333, 0.6666, 0],
],
rainbow3: [
[0.5, 0.5, 0.5, 1],
[0.75, 0.75, 0.75, 0],
[1.0, 1.0, 1.0, 0],
[0, 0.3333, 0.6666, 0],
],
rainbow4: [
[0.5, 0.5, 0.5, 1],
[1, 1, 1, 0],
[1.0, 1.0, 1.0, 0],
[0, 0.3333, 0.6666, 0],
],
"red-blue": [
[0.5, 0, 0.5, 1],
[0.5, 0, 0.5, 0],
[0.5, 0, 0.5, 0],
[0, 0, 0.5, 0],
],
"yellow-green-blue": [
[0.65, 0.5, 0.31, 1],
[-0.65, 0.5, 0.6, 0],
[0.333, 0.278, 0.278, 0],
[0.66, 0, 0.667, 0],
],
"yellow-magenta-cyan": [
[1, 0.5, 0.5, 1],
[0.5, 0.5, 0.5, 0],
[0.75, 1.0, 0.6666, 0],
[0.8, 1.0, 0.3333, 0],
],
"yellow-purple-magenta": [
[0.731, 1.098, 0.192, 1],
[0.358, 1.09, 0.657, 0],
[1.077, 0.36, 0.328, 0],
[0.965, 2.265, 0.837, 0],
],
"yellow-red": [
[0.5, 0.5, 0, 1],
[0.5, 0.5, 0, 0],
[0.1, 0.5, 0, 0],
[0, 0, 0, 0],
],
};
/**
* Computes a single linear RGBA color for given gradient spec and normalized
* position `t` (in [0..1] interval).
*
* @param spec -
* @param t -
*/
export const cosineColor = (spec: CosGradientSpec, t: number): Color =>
transduce(
map<number[], number>(([a, b, c, d]) =>
clamp01(a + b * Math.cos(TAU * (c * t + d)))
),
push(),
// @ts-ignore
zip(...spec)
);
/**
* Computes a full cosine gradient and returns an array of `n` sampled linear
* RGBA colors. If the optional `tx` is given, each computed color will be
* re-interpreted and/or transformed into another {@link Color} type.
*
* @remarks
* For CSS/SVG use cases you could use {@link srgb} as transformation function
* to not convert, but to reinterpret & wrap raw color values as SRGBA.
*
* @param n -
* @param spec -
* @param tx -
*/
export const cosineGradient = (
n: number,
spec: CosGradientSpec,
tx?: FnU<Color>
): Color[] =>
transduce(
comp(map(partial(cosineColor, spec)), tx ? map(tx) : noop()),
push<Color>(),
normRange(n - 1)
);
/**
* Similar to {@link cosineGradient}, but writes results into `buffer` from
* given `offset` and component/element strides. Returns buffer.
*
* @remarks
* Intended use case for this function: 1D texturemap/tonemap generation, e.g.
* for dataviz etc. Also @see {@link multiColorGradientBuffer}.
*
* @param n - number of colors
* @param spec - gradient spec
* @param buffer - target buffer/array
* @param offset - start index (default: 0)
* @param cstride - channel stride (default: 1)
* @param estride - element stride (default: 4)
*/
export const cosineGradientBuffer = (
n: number,
spec: CosGradientSpec,
buffer: NumericArray = [],
offset = 0,
cstride = 1,
estride = 4
) => {
for (let t of normRange(n - 1)) {
setS4(buffer, cosineColor(spec, t), offset, 0, cstride);
offset += estride;
}
return buffer;
};
/**
* Returns coefficients to produce a cosine gradient between the two
* given RGBA colors.
*
* @param from - start color
* @param to - end color
*/
export const cosineCoeffs: FnU2<ReadonlyColor, CosGradientSpec> = (
from,
to
) => {
from = clamp([], from);
to = clamp([], to);
const amp = [...map(([a, b]) => 0.5 * (a - b), zip(from, to))];
return [
<CosineCoeffs>[...map(([s, a]) => s - a, zip(from, amp))],
<CosineCoeffs>amp,
[-0.5, -0.5, -0.5, -0.5],
[0, 0, 0, 0],
];
};
/**
* Multi-color cosine gradient generator using linear RGBA color stops. Returns
* an array of `n+1` linear RGBA color samples.
*
* @remarks
* See
* [`tween()`](https://docs.thi.ng/umbrella/transducers/functions/tween.html)
*
* @example
* ```ts tangle:../export/cosine-gradient.ts
* import { multiCosineGradient, srgb } from "@thi.ng/color";
*
* multiCosineGradient({
* num: 10,
* // gradient stops (normalized positions)
* stops: [[0.1, [1, 0, 0, 1]], [0.5, [0, 1, 0, 1]], [0.9, [0, 0, 1, 1]]],
* // optional color transform/coercion
* tx: srgb
* })
* ```
*
* @param num - number of color steps to produce
* @param stops - gradient stops
*/
export const multiCosineGradient = (opts: CosineGradientOpts): Color[] =>
transduce(
opts.tx ? map(opts.tx) : noop(),
push<Color>(),
tween({
num: opts.num,
stops: opts.stops,
easing: opts.easing,
min: 0,
max: 1,
init: cosineCoeffs,
mix: cosineColor,
})
);