Skip to content

Commit

Permalink
Ensure Okhsl and Okhsv return undefined hues for achromatic colors
Browse files Browse the repository at this point in the history
Fixes #516
  • Loading branch information
facelessuser committed May 16, 2024
1 parent d49b1ae commit af37eb5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/spaces/okhsl.js
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,11 @@ function oklabToOkhsl (lab, lmsToRgb, okCoeff) {
}
}

if (Math.abs(s) < 1e-4 || l === 0.0 || Math.abs(1 - l) < 1e-7) {
h = NaN;
s = 0.0;
}

return [constrain(h * 360), s, l];
}

Expand Down
5 changes: 5 additions & 0 deletions src/spaces/okhsv.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ function oklabToOkhsv (lab, lmsToRgb, okCoeff) {
s = (s0 + tMax) * cv / ((tMax * s0) + tMax * k * cv);
}

if (Math.abs(s) < 1e-4 || v === 0.0) {
h = NaN;
s = 0.0;
}

return [constrain(h * 360), s, v];
}

Expand Down
8 changes: 4 additions & 4 deletions test/conversions.js
Original file line number Diff line number Diff line change
Expand Up @@ -969,7 +969,7 @@ const tests = {
{
name: "sRGB white to Okhsl",
args: "white",
expect: [180, 0.6519721306444567, 1.0000000000000002],
expect: [NaN, 0.0, 1.0000000000000002],
},
{
name: "sRGB red to Okhsl",
Expand Down Expand Up @@ -1004,7 +1004,7 @@ const tests = {
{
name: "sRGB black to Okhsl",
args: "black",
expect: [0.0, 0.0, 0.0],
expect: [NaN, 0.0, 0.0],
},
],
},
Expand All @@ -1017,7 +1017,7 @@ const tests = {
{
name: "sRGB white to Okhsv",
args: "white",
expect: [ 180, 1.3189507366749435e-15, 1.0000000000000007 ],
expect: [ NaN, 0.0, 1.0000000000000007 ],
},
{
name: "sRGB red to Okhsv",
Expand Down Expand Up @@ -1052,7 +1052,7 @@ const tests = {
{
name: "sRGB black to Okhsv",
args: "black",
expect: [0.0, 0.0, 0.0],
expect: [NaN, 0.0, 0.0],
},
],
},
Expand Down

0 comments on commit af37eb5

Please sign in to comment.