Files
MujicaUI/theme/contrast.go
T

29 lines
529 B
Go

package theme
import (
"math"
"github.com/egoist/mygo/ui"
)
// Contrast returns the WCAG contrast ratio of two opaque colors.
func Contrast(a, b ui.Color) float64 {
la, lb := luminance(a), luminance(b)
if la < lb {
la, lb = lb, la
}
return (la + 0.05) / (lb + 0.05)
}
func luminance(c ui.Color) float64 {
return 0.2126*channel(c.R) + 0.7152*channel(c.G) + 0.0722*channel(c.B)
}
func channel(v uint8) float64 {
s := float64(v) / 255
if s <= 0.04045 {
return s / 12.92
}
return math.Pow((s+0.055)/1.055, 2.4)
}