ColorsConvert: cleanup

This commit is contained in:
ItsLemmy
2025-11-01 00:05:02 -04:00
parent 7ec1f42ede
commit cb4059b5c4
+14 -57
View File
@@ -40,55 +40,8 @@ function hexToHSL(hex) {
* Convert HSL to hex color
*/
function hslToHex(h, s, l) {
s /= 100;
l /= 100;
const c = (1 - Math.abs(2 * l - 1)) * s;
const x = c * (1 - Math.abs(((h / 60) % 2) - 1));
const m = l - c / 2;
let r = 0,
g = 0,
b = 0;
if (0 <= h && h < 60) {
r = c;
g = x;
b = 0;
} else if (60 <= h && h < 120) {
r = x;
g = c;
b = 0;
} else if (120 <= h && h < 180) {
r = 0;
g = c;
b = x;
} else if (180 <= h && h < 240) {
r = 0;
g = x;
b = c;
} else if (240 <= h && h < 300) {
r = x;
g = 0;
b = c;
} else if (300 <= h && h < 360) {
r = c;
g = 0;
b = x;
}
r = Math.round((r + m) * 255);
g = Math.round((g + m) * 255);
b = Math.round((b + m) * 255);
return (
"#" +
[r, g, b]
.map((x) => {
const hex = x.toString(16);
return hex.length === 1 ? "0" + hex : hex;
})
.join("")
);
const rgb = hslToRgb(h, s, l);
return rgbToHex(rgb.r, rgb.g, rgb.b);
}
function hexToRgb(hex) {
@@ -188,20 +141,24 @@ function isLightColor(hex) {
// Adjust color lightness
function adjustLightness(hex, amount) {
const rgb = hexToRgb(hex);
const hsl = rgbToHsl(rgb.r, rgb.g, rgb.b);
const hsl = hexToHSL(hex);
hsl.l = Math.max(0, Math.min(100, hsl.l + amount));
const newRgb = hslToRgb(hsl.h, hsl.s, hsl.l);
return rgbToHex(newRgb.r, newRgb.g, newRgb.b);
return hslToHex(hsl.h, hsl.s, hsl.l);
}
// Adjust color saturation
function adjustSaturation(hex, amount) {
const rgb = hexToRgb(hex);
const hsl = rgbToHsl(rgb.r, rgb.g, rgb.b);
const hsl = hexToHSL(hex);
hsl.s = Math.max(0, Math.min(100, hsl.s + amount));
const newRgb = hslToRgb(hsl.h, hsl.s, hsl.l);
return rgbToHex(newRgb.r, newRgb.g, newRgb.b);
return hslToHex(hsl.h, hsl.s, hsl.l);
}
// Adjust both lightness and saturation
function adjustLightnessAndSaturation(hex, lightnessAmount, saturationAmount) {
const hsl = hexToHSL(hex);
hsl.l = Math.max(0, Math.min(100, hsl.l + lightnessAmount));
hsl.s = Math.max(0, Math.min(100, hsl.s + saturationAmount));
return hslToHex(hsl.h, hsl.s, hsl.l);
}
// Generate "on" color with proper contrast (for text/icons)