package utils import ( "fmt" "math" ) const ( Pred float64 = 0.299 Pgreen float64 = 0.587 Pblue float64 = 0.114 ) func IsDarkColor(r, g, b uint8) bool { lum := Luminance(r, g, b) return lum <= 130 } func IntToRgb(color uint32) (uint8, uint8, uint8) { b := color % 256 g := color / 256 % 256 r := color / (256 * 256) % 256 return uint8(r), uint8(g), uint8(b) } func RgbToString(r, g, b uint8) string { return fmt.Sprintf("#%02x%02x%02x", r, g, b) } func AltColor(r, g, b uint8) (uint8, uint8, uint8) { brightnessAmount := -0.6 if IsDarkColor(r, g, b) { brightnessAmount = 0.85 } return Brighten(r, g, b, brightnessAmount) } func Brighten(r, g, b uint8, amount float64) (uint8, uint8, uint8) { return multiply(r, amount), multiply(g, amount), multiply(b, amount) } func multiply(i uint8, amount float64) uint8 { iF := float64(i) return uint8( math.Max( 0, math.Min( 255, iF+255*amount, ), ), ) } func Luminance(r, g, b uint8) float64 { rC := Pred * math.Pow(float64(r), 2) gC := Pgreen * math.Pow(float64(g), 2) bC := Pblue * math.Pow(float64(b), 2) return math.Sqrt(rC + gC + bC) } func WCAGRatio(l1, l2 float64) float64 { if l1 < l2 { return (l1 + 0.05) / (l2 + 0.05) } else { return (l2 + 0.05) / (l1 + 0.05) } }