-
Notifications
You must be signed in to change notification settings - Fork 9
/
colors.go
65 lines (56 loc) · 1.81 KB
/
colors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package props
import (
"fmt"
"image/color"
)
type Color struct {
Keyword string
Color color.Color
}
// String returns a string representation of the color.
// If the color is a keyword, the keyword is returned.
// Otherwise, the color is returned as an rgba string.
func (c Color) String() string {
if c.Keyword != "" {
return c.Keyword
}
r, g, b, a := c.Color.RGBA()
return fmt.Sprintf("rgba(%d,%d,%d,%.2f)", r>>8, g>>8, b>>8, float32(a>>8)/255.0)
}
// Alpha returns a new color with the alpha channel set to the given value.
func (c Color) Alpha(a uint8) Color {
r, g, b, _ := c.Color.RGBA()
c.Color = color.RGBA{R: uint8(r >> 8), G: uint8(g >> 8), B: uint8(b >> 8), A: a}
return c
}
// Mix returns a new color that is a mixture of the two colors.
// The percentage parameter is a float between 0 and 1
// and determines how much of the second color to mix with the first color.
func (c Color) Mix(with Color, percentage float64) Color {
if percentage < 0 {
percentage = 0
}
if percentage > 1 {
percentage = 1
}
r1, g1, b1, a1 := c.Color.RGBA()
r2, g2, b2, a2 := with.Color.RGBA()
r := uint8(float64(r1>>8)*(1-percentage) + float64(r2>>8)*percentage)
g := uint8(float64(g1>>8)*(1-percentage) + float64(g2>>8)*percentage)
b := uint8(float64(b1>>8)*(1-percentage) + float64(b2>>8)*percentage)
a := uint8(float64(a1>>8)*(1-percentage) + float64(a2>>8)*percentage)
return Color{Color: color.RGBA{R: r, G: g, B: b, A: a}}
}
// ColorRGBA returns a new color with the given red, green, blue, and alpha values.
func ColorRGBA(r, g, b, a uint8) Color {
return Color{Color: color.RGBA{R: r, G: g, B: b, A: a}}
}
func ColorCurrentColor() Color {
return Color{Keyword: "currentColor"}
}
func ColorInherit() Color {
return Color{Keyword: "inherit"}
}
func ColorTransparent() Color {
return Color{Keyword: "transparent"}
}