Add coloring

This commit is contained in:
2024-06-12 18:35:10 +02:00
parent 0b26198b3b
commit 7f033b9358
5 changed files with 103 additions and 16 deletions

View File

@@ -60,3 +60,52 @@ export const flyAndScale = (
easing: cubicOut
}
}
type Color = {
h: number
s: number
l: number
}
function ColorDistance(color1: Color, color2: Color) {
return Math.abs(color1.h - color2.h)
}
function GenerateRandomHSL(): Color {
const hue = Math.floor(Math.random() * 360)
const saturation = 70
const lightness = 60
return { h: hue, s: saturation, l: lightness }
}
const existingColors: Color[] = []
function GenerateColor(): string {
const minDistance = 200
let newColor: Color
let isDistinct = false
let iterations = 0
while (!isDistinct) {
iterations++
if (iterations > 100) {
console.error('Failed to generate a distinct color after 100 iterations')
break
}
newColor = GenerateRandomHSL()
isDistinct = true
for (const color of existingColors) {
if (ColorDistance(newColor, color) < minDistance) {
isDistinct = false
break
}
}
}
// We can not reach this point without having a color generated
// @ts-ignore
return `hsl(${newColor.h}, ${newColor.s}%, ${newColor.l}%)`
}
export { GenerateColor }