Skip to content

Commit

Permalink
Update to use null and rework Okhsl saturation handling
Browse files Browse the repository at this point in the history
  • Loading branch information
facelessuser committed May 28, 2024
1 parent 79007c5 commit ce85731
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 20 deletions.
9 changes: 4 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 16 additions & 6 deletions src/spaces/okhsl.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,8 +376,8 @@ function okhslToOklab (hsl, lmsToRgb, okCoeff) {

let [h, s, l] = hsl;
let L = toeInv(l);
let a = 0.0;
let b = 0.0;
let a = null;
let b = null;
h = constrain(h) / 360.0;

if (L !== 0.0 && L !== 1.0 && s !== 0) {
Expand Down Expand Up @@ -462,12 +462,22 @@ function oklabToOkhsl (lab, lmsToRgb, okCoeff) {
}
}

if (Math.abs(s) < εS || l === 0.0 || Math.abs(1 - l) < εL) {
h = NaN;
s = 0.0;
const achromatic = Math.abs(s) < εS;
if (achromatic || l === 0.0 || Math.abs(1 - l) < εL) {
h = null;
// Due to floating point imprecision near lightness of 1, we can end up
// with really high around white, this is to provide consistency as
// saturation can be really high for white due this imprecision.
if (!achromatic) {
s = 0.0;
}
}

else {
h = constrain(h * 360);
}

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


Expand Down
13 changes: 8 additions & 5 deletions src/spaces/okhsv.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ function okhsvToOklab (hsv, lmsToRgb, okCoeff) {
h = constrain(h) / 360.0;

let l = toeInv(v);
let a = 0.0;
let b = 0.0;
let a = null;
let b = null;

// Avoid processing gray or colors with undefined hues
if (l !== 0.0 && s !== 0.0) {
Expand Down Expand Up @@ -137,11 +137,14 @@ function oklabToOkhsv (lab, lmsToRgb, okCoeff) {
}

if (Math.abs(s) < ε || v === 0.0) {
h = NaN;
s = 0.0;
h = null;
}

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

return [h, 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 @@ -965,7 +965,7 @@ const tests = {
{
name: "sRGB white to Okhsl",
args: "white",
expect: [NaN, 0.0, 1.0000000000000002],
expect: [ null, 0.0, 1.0000000000000002 ],
},
{
name: "sRGB red to Okhsl",
Expand Down Expand Up @@ -1000,7 +1000,7 @@ const tests = {
{
name: "sRGB black to Okhsl",
args: "black",
expect: [NaN, 0.0, 0.0],
expect: [ null, 0.0, 0.0 ],
},
],
},
Expand All @@ -1013,7 +1013,7 @@ const tests = {
{
name: "sRGB white to Okhsv",
args: "white",
expect: [ NaN, 0.0, 1.0000000000000007 ],
expect: [ null, 1.3189507366749435e-15, 1.0000000000000007 ],
},
{
name: "sRGB red to Okhsv",
Expand Down Expand Up @@ -1048,7 +1048,7 @@ const tests = {
{
name: "sRGB black to Okhsv",
args: "black",
expect: [NaN, 0.0, 0.0],
expect: [ null, 0.0, 0.0],
},
],
},
Expand Down

0 comments on commit ce85731

Please sign in to comment.