Add coloring
This commit is contained in:
@@ -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 }
|
||||
|
Reference in New Issue
Block a user