Skip to content

Commit 3e17444

Browse files
committed
Fixes mapper() when mode = null.
1 parent 6793782 commit 3e17444

File tree

3 files changed

+57
-91
lines changed

3 files changed

+57
-91
lines changed

docs/api.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -853,9 +853,9 @@ Returns the point `t` at which the value `v` is located between the values `a` a
853853

854854
Creates a mapping that applies _fn_ on each channel of the color in the _mode_ color space.
855855

856-
The resulting function accepts a single argument (a color object or a string), which it converts to the _mode_ color space if not already there.
856+
The resulting function accepts a single argument (a color object or a string), which it converts to the _mode_ color space if necessary.
857857

858-
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.
858+
The _mode_ parameter can be set to `null`, in which case the mapper will iterate through the channels in the color's original color space.
859859

860860
The _fn_ callback has the following signature:
861861

@@ -884,9 +884,9 @@ multiplyAlpha({ r: 1, g: 0.6, b: 0.4, a: 0.5 });
884884
// ⇒ { mode: 'rgb', r: 0.5, g: 0.3, b: 0.2, a: 0.5 }
885885
```
886886

887-
All channels are included in the mapping, so you might want to exclude the `alpha` channel from your function, like we do above.
887+
All channels, including `alpha`, are included in the mapping. You may want to handle the `alpha` channel differently in the callback function, like in the example above.
888888

889-
Returning `undefined` or `NaN` from the function will omit that channel from the resulting color.
889+
Returning `undefined` or `NaN` from the callback function will omit that channel from the resulting color object.
890890

891891
#### Built-in mappings
892892

src/map.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ const mapper = (fn, mode = 'rgb', preserve_mode = false) => {
1010
if (!conv_color) {
1111
return undefined;
1212
}
13-
let res = (channels || getMode(color.mode).channels).reduce(
13+
let res = (channels || getMode(conv_color.mode).channels).reduce(
1414
(res, ch) => {
1515
let v = fn(conv_color[ch], ch, conv_color, mode);
1616
if (v !== undefined && !isNaN(v)) {
1717
res[ch] = v;
1818
}
1919
return res;
2020
},
21-
{ mode }
21+
{ mode: conv_color.mode }
2222
);
2323
if (!preserve_mode) {
2424
return res;

test/map.test.js

Lines changed: 51 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -30,95 +30,61 @@ tape('gamma transfer function', t => {
3030
t.end();
3131
});
3232

33-
tape('when mode is null and color is rgb string', t => {
33+
tape('mode = null', t => {
3434
let increaser = mapper(v => v + 0.1, null);
35-
36-
// might be incorrect
37-
t.throws(() => {
38-
increaser('#cc0033');
39-
}, "Cannot read properties of undefined (reading 'channels')");
40-
t.end();
41-
});
42-
43-
tape('when mode is null and color is rgb', t => {
44-
let increaser = mapper(v => v + 0.1, null);
45-
const res = increaser({ mode: 'rgb', r: 0.8, g: 0, b: 0.2 });
46-
47-
// might be incorrect
48-
t.equal(res.mode, null);
49-
t.end();
50-
});
51-
52-
tape('when mode is null and preserve_mode is true', t => {
53-
let increaser = mapper(v => v + 1, null, true);
54-
55-
// might be incorrect
56-
t.throws(
57-
() => {
58-
const res = increaser({ mode: 'rgb', r: 0.8, g: 0, b: 0.2 });
59-
console.log(res);
60-
},
61-
{ message: "Cannot read properties of undefined (reading 'rgb')" }
35+
t.equal(formatHex(increaser('#cc0033')), '#e61a4d', 'color is rgb string');
36+
t.equal(
37+
increaser({ mode: 'rgb', r: 0.8, g: 0, b: 0.2 }).mode,
38+
'rgb',
39+
'color is rgb'
6240
);
6341
t.end();
6442
});
6543

66-
tape('when mode is undefined and preserve_mode is true', t => {
67-
let decreaseMapper = mapper(v => v - 0.1, undefined, true);
68-
const res = decreaseMapper('#cc0033');
69-
t.equal(res.mode, 'rgb');
70-
t.end();
71-
});
72-
73-
tape('when mode is hsl and preserve_mode is true', t => {
74-
let increaser = mapper(v => v + 0.1, 'hsl', true);
75-
const res = increaser('#cc0033');
76-
t.equal(res.mode, 'rgb');
77-
t.end();
78-
});
79-
80-
tape('when mode is hsl and preserve_mode is false', t => {
81-
let increaser = mapper(v => v + 0.1, 'hsl', false);
82-
const res = increaser('#cc0033');
83-
t.equal(res.mode, 'hsl');
84-
t.end();
85-
});
86-
87-
tape('when mode is oklch and preserve_mode is false', t => {
88-
let increaser = mapper(v => v + 0.1, 'oklch', false);
89-
const res = increaser('#cc0033');
90-
t.equal(res.mode, 'oklch');
44+
tape('preserve_mode', t => {
45+
const rgbColor = { mode: 'rgb', r: 0.8, g: 0, b: 0.2 };
46+
const fn = v => v + 0.1;
47+
t.equal(
48+
formatHex(mapper(fn, null, true)(rgbColor)),
49+
'#e61a4d',
50+
'mode = null, preserve_mode = true'
51+
);
52+
t.equal(
53+
mapper(fn, undefined, true)('#cc0033').mode,
54+
'rgb',
55+
'mode = undefined, preserve_mode = true'
56+
);
57+
t.equal(
58+
mapper(fn, 'hsl', true)('#cc0033').mode,
59+
'rgb',
60+
'mode = hsl, preserve_mode = true'
61+
);
62+
t.equal(
63+
mapper(fn, 'hsl', false)('#cc0033').mode,
64+
'hsl',
65+
'mode = hsl, preserve_mode = false'
66+
);
67+
t.equal(
68+
mapper(fn, 'oklch', false)('#cc0033').mode,
69+
'oklch',
70+
'mode = oklch, preserve_mode = false'
71+
);
72+
const oklchColor = {
73+
mode: 'oklch',
74+
alpha: 0.25,
75+
l: 30,
76+
c: 0.5,
77+
h: 1
78+
};
79+
t.equal(
80+
mapper(fn, 'hsl', false)(oklchColor).mode,
81+
'hsl',
82+
'oklch color, mode = hsl, preserve_mode = false'
83+
);
84+
t.equal(
85+
mapper(fn, 'hsl', true)(oklchColor).mode,
86+
'oklch',
87+
'oklch color, mode = hsl, preserve_mode = true'
88+
);
9189
t.end();
9290
});
93-
94-
tape(
95-
`when input color is oklch('color(--oklch 30 0.5 1 / 0.25)') and when mode is hsl and preserve_mode is false`,
96-
t => {
97-
let increaser = mapper(v => v + 0.1, 'hsl', false);
98-
const res = increaser({
99-
mode: 'oklch',
100-
alpha: 0.25,
101-
l: 30,
102-
c: 0.5,
103-
h: 1
104-
});
105-
t.equal(res.mode, 'hsl');
106-
t.end();
107-
}
108-
);
109-
110-
tape(
111-
`when input color is oklch('color(--oklch 30 0.5 1 / 0.25)') and when mode is hsl and preserve_mode is true`,
112-
t => {
113-
let increaser = mapper(v => v + 0.1, 'hsl', true);
114-
const res = increaser({
115-
mode: 'oklch',
116-
alpha: 0.25,
117-
l: 30,
118-
c: 0.5,
119-
h: 1
120-
});
121-
t.equal(res.mode, 'oklch');
122-
t.end();
123-
}
124-
);

0 commit comments

Comments
 (0)