role color stuff
This commit is contained in:
parent
41d48bf60a
commit
f72c7a357b
9 changed files with 165 additions and 14 deletions
|
@ -1,13 +1,18 @@
|
|||
package utils
|
||||
|
||||
import "math"
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
)
|
||||
|
||||
const (
|
||||
Pred float64 = 0.299
|
||||
Pgreen float64 = 0.587
|
||||
Pblue float64 = 0.114
|
||||
)
|
||||
|
||||
func IsDarkColor(r, g, b uint8) bool {
|
||||
rC := 0.299 * math.Pow(float64(r), 2)
|
||||
gC := 0.587 * math.Pow(float64(g), 2)
|
||||
bC := 0.114 * math.Pow(float64(b), 2)
|
||||
|
||||
lum := math.Sqrt(rC + gC + bC)
|
||||
lum := Luminance(r, g, b)
|
||||
|
||||
return lum <= 130
|
||||
}
|
||||
|
@ -19,3 +24,47 @@ func IntToRgb(color uint32) (uint8, uint8, uint8) {
|
|||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue