Show hex instead of hsl in palette gen

This commit is contained in:
Ninjdai 2023-10-01 18:21:49 +02:00
parent 6fe05437bb
commit fd889cd823

View File

@ -27,7 +27,7 @@ async function generateRandomPalette() {
const saturation = Math.random() * 40 + 50; // Random saturation between 50% and 90%
const brightness = Math.random() * 40 + 50; // Random brightness between 50% and 90%
const color = `hsl(${hue + i * (360 / numColors)}, ${saturation}%, ${brightness}%)`;
const color = hslToHex(hue + i * (360 / numColors) , saturation} , brightness);
palette.push(color);
}
@ -47,3 +47,14 @@ async function createPaletteImage(colors) {
return canvas.toBuffer(); // Convertit le canvas en un buffer d'image
}
function hslToHex(h, s, l) {
l /= 100;
const a = s * Math.min(l, 1 - l) / 100;
const f = n => {
const k = (n + h / 30) % 12;
const color = l - a * Math.max(Math.min(k - 3, 9 - k, 1), -1);
return Math.round(255 * color).toString(16).padStart(2, '0'); // convert to Hex and prefix "0" if needed
};
return `#${f(0)}${f(8)}${f(4)}`;
}