-
Notifications
You must be signed in to change notification settings - Fork 0
/
color.mojo
269 lines (198 loc) · 7.27 KB
/
color.mojo
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
from utils.variant import Variant
import external.hue
from external.hue.math import max_float64
from .ansi_colors import ANSI_HEX_CODES
alias foreground = "38"
alias background = "48"
alias AnyColor = Variant[NoColor, ANSIColor, ANSI256Color, RGBColor]
trait Color(EqualityComparable, CollectionElement):
fn sequence(self, is_background: Bool) -> String:
"""Sequence returns the ANSI Sequence for the color."""
...
@value
struct NoColor(Color, Stringable):
fn __eq__(self, other: NoColor) -> Bool:
return True
fn __ne__(self, other: NoColor) -> Bool:
return False
fn sequence(self, is_background: Bool) -> String:
return ""
fn __str__(self) -> String:
"""String returns the ANSI Sequence for the color and the text."""
return ""
@value
struct ANSIColor(Color, Stringable):
"""ANSIColor is a color (0-15) as defined by the ANSI Standard."""
var value: Int
fn __eq__(self, other: ANSIColor) -> Bool:
return self.value == other.value
fn __ne__(self, other: ANSIColor) -> Bool:
return self.value != other.value
fn sequence(self, is_background: Bool) -> String:
"""Returns the ANSI Sequence for the color and the text.
Args:
is_background: Whether the color is a background color.
"""
var modifier: Int = 0
if is_background:
modifier += 10
if self.value < 8:
return String(modifier + self.value + 30)
else:
return String(modifier + self.value - 8 + 90)
fn __str__(self) -> String:
"""String returns the ANSI Sequence for the color and the text."""
return ANSI_HEX_CODES[self.value]
fn convert_to_rgb(self) -> hue.Color:
"""Converts an ANSI color to hue.Color by looking up the hex value and converting it."""
var hex: String = ANSI_HEX_CODES[self.value]
return hex_to_rgb(hex)
@value
struct ANSI256Color(Color, Stringable):
"""ANSI256Color is a color (16-255) as defined by the ANSI Standard."""
var value: Int
fn __eq__(self, other: ANSI256Color) -> Bool:
return self.value == other.value
fn __ne__(self, other: ANSI256Color) -> Bool:
return self.value != other.value
fn sequence(self, is_background: Bool) -> String:
"""Returns the ANSI Sequence for the color and the text.
Args:
is_background: Whether the color is a background color.
"""
var prefix: String = foreground
if is_background:
prefix = background
return prefix + String(";5;") + str(self.value)
fn __str__(self) -> String:
"""String returns the ANSI Sequence for the color and the text."""
return ANSI_HEX_CODES[self.value]
fn convert_to_rgb(self) -> hue.Color:
"""Converts an ANSI color to hue.Color by looking up the hex value and converting it."""
var hex: String = ANSI_HEX_CODES[self.value]
return hex_to_rgb(hex)
fn convert_base16_to_base10(value: String) -> Int:
"""Converts a base 16 number to base 10.
https://www.catalyst2.com/knowledgebase/dictionary/hexadecimal-base-16-numbers/#:~:text=To%20convert%20the%20hex%20number,16%20%2B%200%20%3D%2016).
Args:
value: Hexadecimal number.
Returns:
Base 10 number.
"""
var mapping = Dict[String, Int]()
mapping["0"] = 0
mapping["1"] = 1
mapping["2"] = 2
mapping["3"] = 3
mapping["4"] = 4
mapping["5"] = 5
mapping["6"] = 6
mapping["7"] = 7
mapping["8"] = 8
mapping["9"] = 9
mapping["a"] = 10
mapping["b"] = 11
mapping["c"] = 12
mapping["d"] = 13
mapping["e"] = 14
mapping["f"] = 15
# We assume mapping.find always returns a value considering the value passed in is a valid hex value
# and the mapping has all the values.
var length = len(value)
var total = 0
for i in range(length - 1, -1, -1):
var exponent = length - 1 - i
total += mapping.find(value[i]).value()[] * (16**exponent)
return total
fn hex_to_rgb(value: String) -> hue.Color:
"""Converts a hex color to hue.Color.
Args:
value: Hex color value.
Returns:
Color.
"""
var hex = value[1:]
var indices = List[Int](0, 2, 4)
var results = List[Int]()
for i in indices:
results.append(convert_base16_to_base10(hex[i[] : i[] + 2]))
return hue.Color(results[0], results[1], results[2])
@value
struct RGBColor(Color):
"""RGBColor is a hex-encoded color, e.g. '#abcdef'."""
var value: String
fn __init__(inout self, value: String):
self.value = value.lower()
fn __eq__(self, other: RGBColor) -> Bool:
return self.value == other.value
fn __ne__(self, other: RGBColor) -> Bool:
return self.value != other.value
fn sequence(self, is_background: Bool) -> String:
"""Returns the ANSI Sequence for the color and the text.
Args:
is_background: Whether the color is a background color.
"""
var rgb = hex_to_rgb(self.value)
var prefix = foreground
if is_background:
prefix = background
return prefix + String(";2;") + str(int(rgb.R)) + String(";") + str(int(rgb.G)) + String(";") + str(int(rgb.B))
fn convert_to_rgb(self) -> hue.Color:
"""Converts the Hex code value to hue.Color."""
return hex_to_rgb(self.value)
fn ansi256_to_ansi(value: Int) -> ANSIColor:
"""Converts an ANSI256 color to an ANSI color.
Args:
value: ANSI256 color value.
"""
var r = 0
var md = max_float64
var h = hex_to_rgb(ANSI_HEX_CODES[value])
var i = 0
while i <= 15:
var hb = hex_to_rgb(ANSI_HEX_CODES[i])
var d = h.distance_HSLuv(hb)
if d < md:
md = d
r = i
i += 1
return ANSIColor(r)
fn v2ci(value: Float64) -> Int:
if value < 48:
return 0
if value < 115:
return 1
return int((value - 35) / 40)
fn hex_to_ansi256(color: hue.Color) -> ANSI256Color:
"""Converts a hex code to a ANSI256 color.
Args:
color: Hex code color from hue.Color.
"""
# Calculate the nearest 0-based color index at 16..231
# Originally had * 255 in each of these
var r = v2ci(color.R) # 0..5 each
var g = v2ci(color.G)
var b = v2ci(color.B)
var ci = int((36 * r) + (6 * g) + b) # 0..215
# Calculate the represented colors back from the index
var i2cv = List[Int](0, 0x5F, 0x87, 0xAF, 0xD7, 0xFF)
var cr = i2cv[int(r)] # r/g/b, 0..255 each
var cg = i2cv[int(g)]
var cb = i2cv[int(b)]
# Calculate the nearest 0-based gray index at 232..255
var gray_index: Int
var average = (r + g + b) / 3
if average > 238:
gray_index = 23
else:
gray_index = int((average - 3) / 10) # 0..23
var gray_value = 8 + 10 * gray_index # same value for r/g/b, 0..255
# Return the one which is nearer to the original input rgb value
# Originall had / 255.0 for r, g, and b in each of these
var c2 = hue.Color(cr, cg, cb)
var g2 = hue.Color(gray_value, gray_value, gray_value)
var color_dist = color.distance_HSLuv(c2)
var gray_dist = color.distance_HSLuv(g2)
if color_dist <= gray_dist:
return ANSI256Color(16 + ci)
return ANSI256Color(232 + gray_index)