Skip to content

Commit

Permalink
Improved accuracy
Browse files Browse the repository at this point in the history
  • Loading branch information
antiflasher committed Sep 30, 2023
1 parent a460466 commit ff1856c
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ function calcLightness(contrastConfig, chroma, hue, colorSpace) {
let iteration = 0;
let lightnessFound = false;
let chromaRange = chromaLimits(contrastConfig);
let searchWindow = { low: 0, top: 1 };

while (!lightnessFound && iteration < 20) {
iteration++;
Expand Down Expand Up @@ -453,8 +454,9 @@ function calcLightness(contrastConfig, chroma, hue, colorSpace) {
// It's needed to avoid returning lightness that gives contrast lower than the requested
let floatingPoints = contrastConfig.contrastModel === "apca" ? 0 : 1;
if (
roundToDP(calcedContrast, floatingPoints) >=
roundToDP(contrastConfig.cr, floatingPoints) &&
// roundToDP(calcedContrast, floatingPoints) >=
// roundToDP(contrastConfig.cr, floatingPoints) &&
calcedContrast >= contrastConfig.cr &&
calcedContrast < factContrast
) {
factContrast = calcedContrast;
Expand All @@ -470,12 +472,26 @@ function calcLightness(contrastConfig, chroma, hue, colorSpace) {
signOf(newDeltaContrast) !== signOf(deltaContrast)
) {
// console.log("----- lightnessPatch switch");
if (lightnessPatch > 0) {
searchWindow.top = newLightness;
} else {
searchWindow.low = newLightness;
}
// console.log(
// "searchWindow: " + searchWindow.low + " / " + searchWindow.top
// );
lightnessPatch = -lightnessPatch / 2;
} else if (
newLightness + lightnessPatch === searchWindow.low ||
newLightness + lightnessPatch === searchWindow.top
) {
lightnessPatch = lightnessPatch / 2;
}

// Check if the lightness is found
if (
Math.abs(lightnessPatch) < 0.001 ||
searchWindow.top - searchWindow.low < 0.001 ||
//Math.abs(lightnessPatch) < 0.001 ||
(iteration > 1 && newLightness === lightness)
) {
lightnessFound = true;
Expand All @@ -496,7 +512,9 @@ function calcLightness(contrastConfig, chroma, hue, colorSpace) {
// " contrast: " +
// factContrast +
// " wanted: " +
// contrastConfig.cr
// contrastConfig.cr +
// " lightnessPatch: " +
// lightnessPatch
// );
return Math.min(Math.max(factLightness, 0), 100);
}
Expand Down

0 comments on commit ff1856c

Please sign in to comment.