Skip to content

Commit

Permalink
feat: improve how "Min Contrast" color adjustment chooses to darken v…
Browse files Browse the repository at this point in the history
…s lighten
  • Loading branch information
MichaelMakesGames committed Aug 21, 2024
1 parent fe18ee2 commit 0e72f57
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
31 changes: 28 additions & 3 deletions src/renderer/src/lib/map/mapUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,35 @@ export function resolveColor({
}),
);
if (Math.abs(color.l - bgColor.l) < adjustment.value * 100) {
if (bgColor.l < 50) {
color.l = Math.min(100, bgColor.l + adjustment.value * 100);
const lightenedL = bgColor.l + adjustment.value * 100;
const lightenSuccess = lightenedL <= 100;

const darkenedL = bgColor.l - adjustment.value * 100;
const darkenSuccess = darkenedL >= 0;

const lightenedIsCloserToOriginal =
Math.abs(lightenedL - color.l) < Math.abs(darkenedL - color.l);
const darkenedIsCloserToOriginal =
Math.abs(lightenedL - color.l) < Math.abs(darkenedL - color.l);

if (lightenSuccess && darkenSuccess) {
if (lightenedIsCloserToOriginal) {
color.l = lightenedL;
} else if (darkenedIsCloserToOriginal) {
color.l = darkenedL;
} else if (color.l < bgColor.l) {
color.l = darkenedL;
} else if (color.l > bgColor.l) {
color.l = lightenedL;
} else {
color.l = bgColor.l >= 50 ? darkenedL : lightenedL;
}
} else if (lightenSuccess) {
color.l = lightenedL;
} else if (darkenSuccess) {
color.l = darkenedL;
} else {
color.l = Math.max(0, bgColor.l - adjustment.value * 100);
color.l = bgColor.l >= 50 ? 0 : 100;
}
}
colorString = color.formatRgb();
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/src/lib/settings/mapSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ export const defaultMapSettings: MapSettings = {
occupation: false,
occupationColor: {
color: 'border',
colorAdjustments: [],
colorAdjustments: [{ type: 'MIN_CONTRAST', value: 0.25 }],
},
};

Expand Down

0 comments on commit 0e72f57

Please sign in to comment.