-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
color.go
141 lines (125 loc) · 2.8 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package utils
import (
"fmt"
"math"
"os"
"strconv"
)
var (
Black, White *Color
)
func init() {
initColorCube()
Black, _ = NewColor("000000")
White, _ = NewColor("ffffff")
}
type Color struct {
Red uint8
Green uint8
Blue uint8
}
func NewColor(hex string) (*Color, error) {
red, err := strconv.ParseUint(hex[0:2], 16, 8)
if err != nil {
return nil, err
}
green, err := strconv.ParseUint(hex[2:4], 16, 8)
if err != nil {
return nil, err
}
blue, err := strconv.ParseUint(hex[4:6], 16, 8)
if err != nil {
return nil, err
}
return &Color{
Red: uint8(red),
Green: uint8(green),
Blue: uint8(blue),
}, nil
}
func (c *Color) Distance(other *Color) float64 {
return math.Sqrt(float64(math.Pow(float64(c.Red-other.Red), 2) +
math.Pow(float64(c.Green-other.Green), 2) +
math.Pow(float64(c.Blue-other.Blue), 2)))
}
func rgbComponentToBoldValue(component uint8) float64 {
srgb := float64(component) / 255
if srgb <= 0.03928 {
return srgb / 12.92
} else {
return math.Pow(((srgb + 0.055) / 1.055), 2.4)
}
}
func (c *Color) Luminance() float64 {
return 0.2126*rgbComponentToBoldValue(c.Red) +
0.7152*rgbComponentToBoldValue(c.Green) +
0.0722*rgbComponentToBoldValue(c.Blue)
}
func (c *Color) ContrastRatio(other *Color) float64 {
L := c.Luminance()
otherL := other.Luminance()
var L1, L2 float64
if L > otherL {
L1, L2 = L, otherL
} else {
L1, L2 = otherL, L
}
ratio := (L1 + 0.05) / (L2 + 0.05)
return ratio
}
var x6colorIndexes = [6]uint8{0, 95, 135, 175, 215, 255}
var x6colorCube [216]Color
func initColorCube() {
i := 0
for iR := 0; iR < 6; iR++ {
for iG := 0; iG < 6; iG++ {
for iB := 0; iB < 6; iB++ {
x6colorCube[i] = Color{
x6colorIndexes[iR],
x6colorIndexes[iG],
x6colorIndexes[iB],
}
i++
}
}
}
}
func ditherTo256ColorCode(color *Color) (code int) {
iMatch := -1
minDistance := float64(99999)
for i := 0; i < 216; i++ {
distance := color.Distance(&x6colorCube[i])
if distance < minDistance {
iMatch = i
minDistance = distance
}
}
return iMatch + 16
}
var non24bitColorTerms = []string{
"Apple_Terminal",
}
var isTerm24bitColorCapableCache bool
var isTerm24bitColorCapableCacheIsInit bool = false
func isTerm24bitColorCapable() bool {
if !isTerm24bitColorCapableCacheIsInit {
isTerm24bitColorCapableCache = true
myTermProg := os.Getenv("TERM_PROGRAM")
for _, brokenTerm := range non24bitColorTerms {
if myTermProg == brokenTerm {
isTerm24bitColorCapableCache = false
break
}
}
isTerm24bitColorCapableCacheIsInit = true
}
return isTerm24bitColorCapableCache
}
func RgbToTermColorCode(color *Color) string {
if isTerm24bitColorCapable() {
return fmt.Sprintf("2;%d;%d;%d", color.Red, color.Green, color.Blue)
} else {
intCode := ditherTo256ColorCode(color)
return fmt.Sprintf("5;%d", intCode)
}
}