Skip to content

Commit f47a59d

Browse files
feat(color): update multiColorGradient()
- add support for automatic conversion to packed ARGB/ABGR ints (for use with indexed color models in @thi.ng/pixel)
1 parent 9d9ecae commit f47a59d

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

packages/color/src/gradients.ts

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@ import { tween } from "@thi.ng/transducers/tween";
33
import { setS4 } from "@thi.ng/vectors/sets";
44
import type { TypedColor } from "./api.js";
55
import type { GradientOpts } from "./api/gradients.js";
6+
import { intArgb32Abgr32 } from "./int/int-int.js";
7+
import { argb32 } from "./int/int.js";
68
import { mix as $mix } from "./mix.js";
79

810
/**
911
* Similar to {@link multiCosineGradient}, but using any number of gradient
10-
* color stops and isn't limited to RGB, but for arbitrary color types.
12+
* color stops and isn't limited to RGB, but for arbitrary color types. The
13+
* optional `isABGR` boolean arg can be used to autoconvert resulting colors
14+
* into packed ARGB (false) or ABGR (true) integers. If that arg is given, an
15+
* array of numbers will be returned.
1116
*
1217
* @remarks
1318
* @see {@link @thi.ng/transducers#tween}
@@ -41,11 +46,25 @@ import { mix as $mix } from "./mix.js";
4146
* );
4247
* ```
4348
*
44-
* @param opts -
49+
* @param opts -
50+
* @param isABGR -
4551
*/
46-
export const multiColorGradient = <T extends TypedColor<any>>(
52+
export function multiColorGradient<T extends TypedColor<any>>(
4753
opts: GradientOpts<T>
48-
) => [...gradient(opts)];
54+
): T[];
55+
export function multiColorGradient<T extends TypedColor<any>>(
56+
opts: GradientOpts<T>,
57+
isABGR: boolean
58+
): number[];
59+
export function multiColorGradient<T extends TypedColor<any>>(
60+
opts: GradientOpts<T>,
61+
isABGR?: boolean
62+
) {
63+
const cols = [...gradient(opts)];
64+
if (isABGR === undefined) return cols;
65+
const rgba = cols.map((x) => argb32(x)[0]);
66+
return isABGR ? rgba.map(intArgb32Abgr32) : rgba;
67+
}
4968

5069
/**
5170
* Similar to {@link multiColorGradient}, but writes results into `buffer` from
@@ -55,7 +74,7 @@ export const multiColorGradient = <T extends TypedColor<any>>(
5574
* Intended use case for this function: 1D texturemap/tonemap generation, e.g.
5675
* for dataviz etc. Also @see {@link cosineGradientBuffer}.
5776
*
58-
* @param opts -
77+
* @param opts -
5978
* @param buffer - target buffer/array
6079
* @param offset - start index (default: 0)
6180
* @param cstride - channel stride (default: 1)

0 commit comments

Comments
 (0)