d2/lib/color/color.go

73 lines
1.1 KiB
Go
Raw Normal View History

2022-11-10 19:21:14 +00:00
package color
import (
"regexp"
2022-11-10 19:21:14 +00:00
"github.com/lucasb-eyer/go-colorful"
"github.com/mazznoer/csscolorparser"
)
var themeRegex = regexp.MustCompile("^B[1-6]$")
2022-11-10 19:21:14 +00:00
func Darken(colorString string) (string, error) {
if themeRegex.MatchString(colorString) {
switch colorString[1] {
case '1':
return B1, nil
case '2':
return B1, nil
case '3':
return B2, nil
case '4':
return B3, nil
case '5':
return B4, nil
case '6':
return B5, nil
}
}
return DarkenCSS(colorString)
}
func DarkenCSS(colorString string) (string, error) {
2022-11-10 19:21:14 +00:00
c, err := csscolorparser.Parse(colorString)
if err != nil {
return "", err
}
h, s, l := colorful.Color{R: c.R, G: c.G, B: c.B}.Hsl()
// decrease luminance by 10%
return colorful.Hsl(h, s, l-.1).Clamped().Hex(), nil
}
2023-01-09 18:16:28 +00:00
const (
N1 = "N1"
N2 = "N2"
N3 = "N3"
N4 = "N4"
N5 = "N5"
N6 = "N6"
N7 = "N7"
// Base Colors: used for containers
B1 = "B1"
B2 = "B2"
B3 = "B3"
B4 = "B4"
B5 = "B5"
B6 = "B6"
// Alternative colors A
AA2 = "AA2"
AA4 = "AA4"
AA5 = "AA5"
// Alternative colors B
AB4 = "AB4"
AB5 = "AB4"
// Special
Empty = ""
None = "none"
)