https://dev.kronoz.win/r/marzneshin.git
| 1 |
a47eb4c |
Dawsh |
2y ago |
export function generateDistinctColors(numColors: number) { |
| 2 |
a47eb4c |
Dawsh |
2y ago |
const hueStep = 360 / numColors; |
| 3 |
a47eb4c |
Dawsh |
2y ago |
const saturation = 90; |
| 4 |
a47eb4c |
Dawsh |
2y ago |
const lightness = 47; |
| 5 |
a47eb4c |
Dawsh |
2y ago |
const colors = []; |
| 6 |
a47eb4c |
Dawsh |
2y ago |
|
| 7 |
a47eb4c |
Dawsh |
2y ago |
for (let i = 0; i < numColors; i++) { |
| 8 |
a47eb4c |
Dawsh |
2y ago |
const hue = ((i * hueStep) % 360) + 140; |
| 9 |
a47eb4c |
Dawsh |
2y ago |
const color = hslToHex(hue, saturation, lightness); |
| 10 |
a47eb4c |
Dawsh |
2y ago |
colors.push(color); |
| 11 |
a47eb4c |
Dawsh |
2y ago |
} |
| 12 |
a47eb4c |
Dawsh |
2y ago |
|
| 13 |
a47eb4c |
Dawsh |
2y ago |
return colors; |
| 14 |
a47eb4c |
Dawsh |
2y ago |
} |
| 15 |
a47eb4c |
Dawsh |
2y ago |
|
| 16 |
a47eb4c |
Dawsh |
2y ago |
function hslToHex(h: number, s: number, l: number) { |
| 17 |
a47eb4c |
Dawsh |
2y ago |
h /= 360; |
| 18 |
a47eb4c |
Dawsh |
2y ago |
s /= 100; |
| 19 |
a47eb4c |
Dawsh |
2y ago |
l /= 100; |
| 20 |
a47eb4c |
Dawsh |
2y ago |
|
| 21 |
a47eb4c |
Dawsh |
2y ago |
let r, g, b; |
| 22 |
a47eb4c |
Dawsh |
2y ago |
|
| 23 |
a47eb4c |
Dawsh |
2y ago |
if (s === 0) { |
| 24 |
a47eb4c |
Dawsh |
2y ago |
r = g = b = l; |
| 25 |
a47eb4c |
Dawsh |
2y ago |
} else { |
| 26 |
a47eb4c |
Dawsh |
2y ago |
const hueToRgb = (p: number, q: number, t: number) => { |
| 27 |
a47eb4c |
Dawsh |
2y ago |
if (t < 0) t += 1; |
| 28 |
a47eb4c |
Dawsh |
2y ago |
if (t > 1) t -= 1; |
| 29 |
a47eb4c |
Dawsh |
2y ago |
if (t < 1 / 6) return p + (q - p) * 6 * t; |
| 30 |
a47eb4c |
Dawsh |
2y ago |
if (t < 1 / 2) return q; |
| 31 |
a47eb4c |
Dawsh |
2y ago |
if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6; |
| 32 |
a47eb4c |
Dawsh |
2y ago |
return p; |
| 33 |
a47eb4c |
Dawsh |
2y ago |
}; |
| 34 |
a47eb4c |
Dawsh |
2y ago |
|
| 35 |
a47eb4c |
Dawsh |
2y ago |
const q = l < 0.5 ? l * (1 + s) : l + s - l * s; |
| 36 |
a47eb4c |
Dawsh |
2y ago |
const p = 2 * l - q; |
| 37 |
a47eb4c |
Dawsh |
2y ago |
|
| 38 |
a47eb4c |
Dawsh |
2y ago |
r = Math.round(hueToRgb(p, q, h + 1 / 3) * 255); |
| 39 |
a47eb4c |
Dawsh |
2y ago |
g = Math.round(hueToRgb(p, q, h) * 255); |
| 40 |
a47eb4c |
Dawsh |
2y ago |
b = Math.round(hueToRgb(p, q, h - 1 / 3) * 255); |
| 41 |
a47eb4c |
Dawsh |
2y ago |
} |
| 42 |
a47eb4c |
Dawsh |
2y ago |
|
| 43 |
a47eb4c |
Dawsh |
2y ago |
const toHex = (c: number) => { |
| 44 |
a47eb4c |
Dawsh |
2y ago |
const hex = c.toString(16); |
| 45 |
bf7c131 |
Mardin |
2y ago |
return hex.length === 1 ? '0' + hex : hex; |
| 46 |
a47eb4c |
Dawsh |
2y ago |
}; |
| 47 |
a47eb4c |
Dawsh |
2y ago |
|
| 48 |
a47eb4c |
Dawsh |
2y ago |
return `#${toHex(r)}${toHex(g)}${toHex(b)}`; |
| 49 |
a47eb4c |
Dawsh |
2y ago |
} |