-
Notifications
You must be signed in to change notification settings - Fork 0
/
color_matrix.go
159 lines (122 loc) · 2.9 KB
/
color_matrix.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package examples
import (
"fmt"
"strconv"
"strings"
"github.com/go-color-term/go-color-term/coloring"
)
const innerBoxSize = 182
type ColorMatrixExample struct{}
func (example *ColorMatrixExample) GetTitle() string {
return "8-bit color matrix"
}
func (example *ColorMatrixExample) GetDescription() string {
return "Color matrix for the 0-255 (8-bit) colors."
}
func (example *ColorMatrixExample) Run() {
drawBoxHeader()
for i := 0; i <= 255; i++ {
colorWhite := coloring.New().Color(coloring.BRIGHTWHITE).Background().Color(i).Print()
colorBlack := coloring.New().Black().Background().Color(i).Print()
var color coloring.ColorizerPrint
if i < 16 {
if i == 0 {
drawCustomContent(fmt.Sprintf("%sStandard colors%sHigh-intensity colors%s", strings.Repeat(" ", 37), strings.Repeat(" ", 75), strings.Repeat(" ", 34)))
drawBoxOpen()
}
padding := " "
if i < 10 {
padding += " "
}
if i < 8 {
color = colorWhite
} else {
color = colorBlack
}
color(padding + strconv.Itoa(i) + " ")
if i == 7 {
fmt.Print(" ")
}
if i == 15 {
drawBoxEnd()
}
} else {
if i == 16 {
drawEmptyBoxLine()
drawCenteredTitles("6 x 6 x 6 matrix (216 colors)")
}
if i < 232 {
if (i-16)%36 == 0 {
drawBoxOpen()
}
if (i-17)-(i-16)/36*36 <= 16 {
color = colorWhite
} else {
color = colorBlack
}
padding := " "
if i < 100 {
padding += " "
}
color(" " + strconv.Itoa(i) + padding)
if (i-15)%36 == 0 {
drawBoxEnd()
}
} else {
if i == 232 {
drawEmptyBoxLine()
drawCenteredTitles("Grayscale colors")
drawBoxOpenPadded(31)
}
if i < 244 {
color = colorWhite
} else {
color = colorBlack
}
color(" " + strconv.Itoa(i) + " ")
if i == 255 {
drawBoxEndPadded(31)
}
}
}
}
drawBoxFooter()
}
func drawBoxHeader() {
fmt.Printf("┌%s┐\n", strings.Repeat("─", innerBoxSize))
}
func drawBoxFooter() {
fmt.Printf("└%s┘\n", strings.Repeat("─", innerBoxSize))
fmt.Println()
}
func drawEmptyBoxLine() {
fmt.Printf("│%s│\n", strings.Repeat(" ", innerBoxSize))
}
func drawBoxOpen() {
drawBoxOpenPadded(1)
}
func drawBoxEnd() {
drawBoxEndPadded(1)
}
func drawBoxOpenPadded(padding int) {
fmt.Printf("│%s", strings.Repeat(" ", padding))
}
func drawBoxEndPadded(padding int) {
fmt.Printf("%s│\n", strings.Repeat(" ", padding))
}
func drawCenteredTitles(titles ...string) {
titlesLen := 0
for _, title := range titles {
titlesLen += len(title)
}
paddingSize := (innerBoxSize - titlesLen) / (len(titles) + 1)
padding := strings.Repeat(" ", paddingSize)
fmt.Printf("│%s", padding)
for _, title := range titles {
fmt.Printf("%s%s", title, padding)
}
fmt.Printf("%s│\n", strings.Repeat(" ", innerBoxSize-titlesLen-paddingSize*(len(titles)+1)))
}
func drawCustomContent(s string) {
fmt.Printf("│%s│\n", s)
}