Skip to content

Commit

Permalink
Add tests & fixes re #167, #155, #153
Browse files Browse the repository at this point in the history
  • Loading branch information
danburzo committed Feb 23, 2023
1 parent 3af9660 commit 51a6501
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/lab/parseLab.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ function parseLab(color, parsed) {
res.l = l.value;
}
if (a.type !== Tok.None) {
res.a = a.type === Tok.Number ? a.value : a.value / 125;
res.a = a.type === Tok.Number ? a.value : (a.value * 125) / 100;
}
if (b.type !== Tok.None) {
res.b = b.type === Tok.Number ? b.value : b.value / 125;
res.b = b.type === Tok.Number ? b.value : (b.value * 125) / 100;
}
if (alpha.type !== Tok.None) {
res.alpha = alpha.type === Tok.Number ? alpha.value : alpha.value / 100;
Expand Down
5 changes: 4 additions & 1 deletion src/lch/parseLch.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ function parseLch(color, parsed) {
res.l = l.value;
}
if (c.type !== Tok.None) {
res.c = Math.max(0, c.type === Tok.Number ? c.value : c.value / 150);
res.c = Math.max(
0,
c.type === Tok.Number ? c.value : (c.value * 150) / 100
);
}
if (h.type !== Tok.None) {
if (h.type === Tok.Percentage) {
Expand Down
6 changes: 3 additions & 3 deletions src/oklab/parseOklab.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ function parseOklab(color, parsed) {
return undefined;
}
if (l.type !== Tok.None) {
res.l = l.value;
res.l = l.type === Tok.Number ? l.value : l.value / 100;
}
if (a.type !== Tok.None) {
res.a = a.type === Tok.Number ? a.value : a.value / 0.4;
res.a = a.type === Tok.Number ? a.value : (a.value * 0.4) / 100;
}
if (b.type !== Tok.None) {
res.b = b.type === Tok.Number ? b.value : b.value / 0.4;
res.b = b.type === Tok.Number ? b.value : (b.value * 0.4) / 100;
}
if (alpha.type !== Tok.None) {
res.alpha = alpha.type === Tok.Number ? alpha.value : alpha.value / 100;
Expand Down
7 changes: 5 additions & 2 deletions src/oklch/parseOklch.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ function parseOklch(color, parsed) {
if (l.type === Tok.Hue) {
return undefined;
}
res.l = l.value;
res.l = c.type === Tok.Number ? l.value : l.value / 100;
}
if (c.type !== Tok.None) {
res.c = Math.max(0, c.type === Tok.Number ? c.value : c.value / 0.4);
res.c = Math.max(
0,
c.type === Tok.Number ? c.value : (c.value * 0.4) / 100
);
}
if (h.type !== Tok.None) {
if (h.type === Tok.Percentage) {
Expand Down
8 changes: 8 additions & 0 deletions test/lab.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ tape('lab', t => {
},
'red'
);

t.deepEqual(lab('lab(50% -10% 200% / 10%)'), {
mode: 'lab',
l: 50,
a: -12.5,
b: 250,
alpha: 0.1
});
t.end();
});

Expand Down
14 changes: 14 additions & 0 deletions test/lch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ tape('lch', t => {
},
'red'
);
t.deepEqual(lch('lch(20% 30% .5turn / 10%)'), {
mode: 'lch',
l: 20,
c: 45,
h: 180,
alpha: 0.1
});
t.deepEqual(lch('lch(20% -30% .5turn / 10%)'), {
mode: 'lch',
l: 20,
c: 0,
h: 180,
alpha: 0.1
});
t.end();
});

Expand Down
7 changes: 7 additions & 0 deletions test/oklab.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ tape('oklab()', t => {
alpha: 0.25,
mode: 'oklab'
});
t.deepEqual(oklab('oklab(25% -20% 125% / 15%)'), {
mode: 'oklab',
l: 0.25,
a: -0.08,
b: 0.5,
alpha: 0.15
});
t.end();
});

Expand Down
7 changes: 7 additions & 0 deletions test/oklch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ tape('oklch()', t => {
alpha: 0.25,
mode: 'oklch'
});
t.deepEqual(oklch('oklch(40% 50% .5turn / 15%)'), {
mode: 'oklch',
l: 0.4,
c: 0.2,
h: 180,
alpha: 0.15
});
t.end();
});

Expand Down

0 comments on commit 51a6501

Please sign in to comment.