-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolor.go
71 lines (63 loc) · 1.63 KB
/
color.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
66
67
68
69
70
71
package deepx
import (
"fmt"
"image/color"
"math"
"strconv"
"strings"
)
// Color represents color type.
type Color color.RGBA
// Hex returns color representation in hex format: #RRGGBBAA.
func (c Color) Hex() string {
return fmt.Sprintf("#%.2X%.2X%.2X%.2X", c.R, c.G, c.B, c.A)
}
// RGBA returns color in native RGBA format.
func (c Color) RGBA() color.RGBA {
return color.RGBA{
R: c.R,
G: c.G,
B: c.B,
A: c.A,
}
}
// Equal compares two color models.
func (c Color) Equal(other Color) bool {
return c.A == other.A && c.B == other.B && c.R == other.R && c.G == other.G
}
// ColorFromHex converts hex color string (#RRGGBBAA) into color model.
func ColorFromHex(hex string) (*Color, error) {
if hex = strings.TrimPrefix(hex, "#"); len(hex) < 8 {
hex += strings.Repeat("F", 8-len(hex))
}
values, err := strconv.ParseUint(hex, 16, 32)
if err != nil {
return nil, err
}
return &Color{
A: uint8(values & 0xFF),
B: uint8((values >> 8) & 0xFF),
G: uint8((values >> 16) & 0xFF),
R: uint8(values >> 24),
}, nil
}
// MustColorFromHex converts hex color string (#RRGGBBAA) into color model.
//
// It panics if the provided hes string is not a valid hexidecimal encoded color.
func MustColorFromHex(hex string) Color {
c, err := ColorFromHex(hex)
if err != nil {
panic(err)
}
return *c
}
// ColorRGBA converts built-in color.Color model into Color representation.
func ColorRGBA(c color.Color) Color {
r, g, b, a := c.RGBA()
return Color{
R: uint8(255 * float64(r) / math.MaxUint16),
G: uint8(255 * float64(g) / math.MaxUint16),
B: uint8(255 * float64(b) / math.MaxUint16),
A: uint8(255 * float64(a) / math.MaxUint16),
}
}