-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrgb.go
157 lines (136 loc) · 3.59 KB
/
rgb.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
package gradient
import (
"fmt"
"strconv"
"strings"
)
// CodeToRGB Transform an RGB code to an RGB slice
func CodeToRGB(code string) ([]int, error) {
// 去掉可能存在的 '#' 字符
code = strings.TrimPrefix(strings.ToLower(code), "#")
// 确保 RGBToCode 代码有效(6 个十六进制数字)
if len(code) != 6 {
return nil, fmt.Errorf("invalid RGBToCode code: %s", code)
}
// 解析 RGBToCode 组件
r, err := strconv.ParseInt(code[0:2], 16, 0)
if err != nil {
return nil, err
}
g, err := strconv.ParseInt(code[2:4], 16, 0)
if err != nil {
return nil, err
}
b, err := strconv.ParseInt(code[4:6], 16, 0)
if err != nil {
return nil, err
}
return []int{int(r), int(g), int(b)}, nil
}
// RGBToANSI Generate an ANSI color string from an RGB slice
func RGBToANSI(rgb ...int) string {
// 确保 RGBToCode 组件有效
if len(rgb) != 3 {
return ""
}
// 计算 ANSI 颜色代码
return fmt.Sprintf("\033[38;2;%d;%d;%dm", rgb[0], rgb[1], rgb[2])
}
// RGB Convert an RGB code to an ANSI color string
func RGB(code string) (string, error) {
rgb, err := CodeToRGB(code)
if err != nil {
return "", err
}
return RGBToANSI(rgb...), nil
}
// ANSIToRGB Convert an ANSI color string to an RGB slice
func ANSIToRGB(ansi string) ([]int, error) {
// 基础颜色映射
baseColors := map[string][]int{
"\033[30m": {0, 0, 0}, // Black
"\033[31m": {255, 0, 0}, // Red
"\033[32m": {0, 255, 0}, // Green
"\033[33m": {255, 255, 0}, // Yellow
"\033[34m": {0, 0, 255}, // Blue
"\033[35m": {255, 0, 255}, // Magenta
"\033[36m": {0, 255, 255}, // Cyan
"\033[37m": {255, 255, 255}, // White
}
// 检查是否为基础颜色
if rgb, exists := baseColors[ansi]; exists {
return rgb, nil
}
// 确保 ANSI 字符串有效
if !strings.HasPrefix(ansi, "\033[38;2;") || !strings.HasSuffix(ansi, "m") {
return nil, fmt.Errorf("invalid ANSI color string: %s", ansi)
}
// 去掉前缀和后缀
ansi = strings.TrimPrefix(ansi, "\033[38;2;")
ansi = strings.TrimSuffix(ansi, "m")
// 分割 RGB 组件
components := strings.Split(ansi, ";")
if len(components) != 3 {
return nil, fmt.Errorf("invalid ANSI color string: %s", ansi)
}
// 解析 RGB 组件
r, err := strconv.Atoi(components[0])
if err != nil {
return nil, err
}
g, err := strconv.Atoi(components[1])
if err != nil {
return nil, err
}
b, err := strconv.Atoi(components[2])
if err != nil {
return nil, err
}
return []int{r, g, b}, nil
}
// RGBToCode Generate an RGB code from an RGB slice
func RGBToCode(rgb ...int) string {
// 确保 RGBToCode 组件有效
if len(rgb) != 3 {
return ""
}
// 生成 RGBToCode 代码
return fmt.Sprintf("#%02x%02x%02x", rgb[0], rgb[1], rgb[2])
}
// ANSI Convert an ANSI color string to an RGB code
func ANSI(ansi string) (string, error) {
rgb, err := ANSIToRGB(ansi)
if err != nil {
return "", err
}
return RGBToCode(rgb...), nil
}
// RGBToBgANSI Generate an ANSI background color string from an RGB slice
func RGBToBgANSI(rgb ...int) string {
// 确保 RGB 组件有效
if len(rgb) != 3 {
return ""
}
// 计算 ANSI 背景颜色代码
return fmt.Sprintf("\033[48;2;%d;%d;%dm", rgb[0], rgb[1], rgb[2])
}
// ConvertToRGB Convert various color formats to RGB slice
func ConvertToRGB(color interface{}) ([]int, error) {
switch v := color.(type) {
case string:
if strings.HasPrefix(v, "\033[") {
return ANSIToRGB(v)
} else if strings.HasPrefix(v, "#") {
return CodeToRGB(v[1:])
} else {
return CodeToRGB(v)
}
case []int:
if len(v) == 3 {
return v, nil
}
case [3]int:
return v[:], nil
}
return nil, fmt.Errorf("invalid color format")
}