Skip to content

Commit 7c0ff5a

Browse files
committed
Fix an issue when color is string and mode is null
1 parent d052cf4 commit 7c0ff5a

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

src/map.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@ const mapper = (fn, mode = 'rgb', preserve_mode = false) => {
77
let conv = mode ? converter(mode) : prepare;
88
return color => {
99
let conv_color = conv(color);
10-
let res = (channels || getMode(color.mode).channels).reduce(
10+
let _mode = mode === null ? conv_color.mode : mode;
11+
let _channels = channels || getMode(_mode).channels;
12+
let res = _channels.reduce(
1113
(res, ch) => {
12-
let v = fn(conv_color[ch], ch, conv_color, mode);
14+
let v = fn(conv_color[ch], ch, conv_color, _mode);
1315
if (v !== undefined && !isNaN(v)) {
1416
res[ch] = v;
1517
}
1618
return res;
1719
},
18-
{ mode }
20+
{ mode: _mode }
1921
);
2022
if (!preserve_mode) {
2123
return res;

test/map.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,23 @@ tape('gamma transfer function', t => {
2929
t.equal(formatHex(brighten('#cc0033')), '#ff0070');
3030
t.end();
3131
});
32+
33+
tape(
34+
"The mode parameter can be set to null, in which case the mapper will iterate through all the channels in the color's original color space.",
35+
// https://culorijs.org/api/#mapper
36+
t => {
37+
let brighten = mapper(mapTransferGamma(2.2), null);
38+
t.deepEqual(
39+
brighten(
40+
'color(--okhsv 29.2338851923426 0.9995219692256307 0.9999999999999997)'
41+
),
42+
{
43+
mode: 'okhsv',
44+
h: 64.31454742315373,
45+
s: 2.198948332296388,
46+
v: 2.1999999999999993
47+
}
48+
);
49+
t.end();
50+
}
51+
);

0 commit comments

Comments
 (0)