-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolortest.py
More file actions
executable file
·280 lines (240 loc) · 9.34 KB
/
colortest.py
File metadata and controls
executable file
·280 lines (240 loc) · 9.34 KB
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
270
271
272
273
274
275
276
277
278
279
280
#!/Users/davidaccomazzo/Projects/python_colors/bin/python
""" Resources
* https://www.lihaoyi.com/post/BuildyourownCommandLinewithANSIescapecodes.html
* Good resource on how to manipulate terminal commands with Python
* https://askubuntu.com/questions/821157/print-a-256-color-test-pattern-in-the-terminal
* Contains links to powerful libraries that do similar things as this, as well as ways
to do the same thing in bash
* https://github.com/Markus00000/colorschemer/blob/master/colorschemer.py
* Contains good explanation on how contrast gets calculated.
* https://jonasjacek.github.io/colors/
* contains JSON of xterm color/name/hex/rgb/hsl
* https://medium.muz.li/the-science-of-color-contrast-an-expert-designers-guide-33e84c41d156
* Good article on accessibility contrast calculations
* Might want to use this instead of color delta calculations
* https://www.w3.org/TR/WCAG20/#relativeluminancedef
* How WCAG defines contrast
TO DO:
Convert to using WCAG contrast definitions instead of delta calcs
"""
# Python colors
import sys
import argparse
import time
import json
import os
from colormath.color_objects import sRGBColor, LabColor
from colormath.color_conversions import convert_color
from colormath.color_diff import delta_e_cie2000, delta_e_cie1994
# Dull Colors
def dullForegroundColors():
print("Standard (Dull) Colors")
for i in range(30, 38):
print(f"\033[{i}m {i} \033[0m \\033[{i}m")
# Bright Colors
def brightForegroundColors():
print("Bright Colors")
for i in range(30, 38):
print(f"\033[{i};1m {i} \033[0m \\033[{i};1m")
# 256 Color range
def allForegroundColors():
print("All 256 Color Foregrounds")
for i in range(0, 256):
num = str(i)
color = f"\033[38;5;{num}m {num} \033[0m \\033[38;5;{num}m"
sys.stdout.write(color.ljust(4))
if i % 5 == 0 and i != 0:
print("")
# Background Dull Colors
def dullBackgroundColors():
print("Background Dull Colors")
for i in range(40, 48):
print(f"\033[{i}m {i} \033[0m \\033[{i}m")
# Background Bright Colors
def brightBackgroundColors():
print("Background Bright Colors")
for i in range(40, 48):
print(f"\033[{i};1m {i} \033[0m \\033[{i};1m")
# Background 256 Colors
def allBackgroundColors():
print("All 256 Color Backgrounds")
for i in range(0, 256):
num = str(i)
color = f"\033[48;5;{num}m {num} \033[0m \\033[48;5;{num}m"
sys.stdout.write(color.ljust(4))
if i % 5 == 0 and i != 0:
print("")
# Getting Crazy with it
def allForegroundAllBackground():
for i in range(0, 256):
time.sleep(0.01)
for j in range(0, 256):
time.sleep(0.001)
foreground = str(j)
background = str(i)
color = f"\033[38;5;{foreground}m\033[48;5;{background}m {foreground} on {background}\033[0m "
sys.stdout.write(color.ljust(4))
if j % 16 == 0 and j != 0:
print("")
print("All 65,536 permutations printed.")
# Test out a foreground on a background
def testForegroundOnBackground(foreground, background):
if foreground and background in range(0, 256):
print(f"Testing {foreground} on {background}")
print(
f"\n\\033[38;5;{foreground}m\\033[48;5;{background}m TEXT HERE \\033[0m\n"
)
print(
f"\033[38;5;{foreground}m\033[48;5;{background}m{foreground} on {background} test\033[0m"
)
print(
f"\033[38;5;{foreground}m\033[48;5;{background}mThe quick brown fox jumps over the lazy dog. \033[0m"
)
print(
f"\033[38;5;{foreground}m\033[48;5;{background}mABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz 0123456789\033[0m"
)
else:
print("Both foreground and background number must be between 0 and 255")
# Print out all 256 foreground and background
def allForegroundAndAllBackground():
allForegroundColors()
allBackgroundColors()
# Create a map of all ANSI colors and their conversions to different formats
def _getColorMap():
f = open(os.path.dirname(os.path.realpath(__file__)) + "/colors.json")
data = json.load(f)
# Map the ANSI code to actual color dict
colorsDict = {}
for i in data:
colorsDict[i["colorId"]] = i
return colorsDict
def _getRGBDictFromANSICode(color):
colorMap = _getColorMap()
return {
"r": colorMap[color]["rgb"]["r"],
"g": colorMap[color]["rgb"]["g"],
"b": colorMap[color]["rgb"]["b"],
}
def getContrast(color1, color2):
colorMap = _getColorMap()
rgb1 = sRGBColor(
colorMap[color1]["rgb"]["r"],
colorMap[color1]["rgb"]["g"],
colorMap[color1]["rgb"]["b"],
True,
)
rgb2 = sRGBColor(
colorMap[color2]["rgb"]["r"],
colorMap[color2]["rgb"]["g"],
colorMap[color2]["rgb"]["b"],
True,
)
lab1 = convert_color(rgb1, LabColor)
lab2 = convert_color(rgb2, LabColor)
diff = delta_e_cie1994(lab1, lab2)
return diff
def getAllFgBgWithDiff(delta):
print(f"Requested Delta: {delta}")
for i in range(0, 256):
for j in range(0, 256):
contrast = getContrast(i, j)
if contrast >= delta:
foreground = str(j)
background = str(i)
color = f"\033[38;5;{foreground}m\033[48;5;{background}m {foreground} on {background}\033[0m. Contrast: {round(contrast)}"
sys.stdout.write(color.ljust(4))
def printColorsByDeltaOnBg(background, delta):
print(
f"Printing all foreground colors that are delta {delta} on background {background}"
)
for i in range(0, 256):
contrast = getContrast(i, background)
if contrast >= delta:
foreground = str(i)
_background = str(background)
color = f"\033[38;5;{foreground}m\033[48;5;{_background}m {foreground} on {_background}\033[0m. Delta: {round(contrast)} "
sys.stdout.write(color.ljust(4))
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(title="commands", dest="command")
dullForegroundColorsParser = subparsers.add_parser(
"dullFg", help="display the 8 dull foreground colors"
)
brightForegroundColorsParser = subparsers.add_parser(
"brightFg", help="display the 8 bright foreground colors"
)
dullBackgroundColorsParser = subparsers.add_parser(
"dullBg", help="display the 8 dull background colors"
)
backgroundBrightColorsParser = subparsers.add_parser(
"brightBg", help="display the 8 bright background colors"
)
allForegroundColorsParser = subparsers.add_parser(
"allFg", help="display 256 foreground colors"
)
allBackgroundColorsParser = subparsers.add_parser(
"allBg", help="display 256 background colors"
)
allForegroundAllBackgroundParser = subparsers.add_parser(
"allFgBg", help="display all 256 foreground and background colors separately"
)
allForegroundAllBackgroundParser = subparsers.add_parser(
"getFreaky", help="Display every combination of foreground and background color"
)
testForegroundOnBackgroundParser = subparsers.add_parser(
"test", help="Enter a foreground code and a background code and test it out"
)
testForegroundOnBackgroundParser.add_argument(
"FOREGROUND", type=int, help="foreground, must be between 0 and 256"
)
testForegroundOnBackgroundParser.add_argument(
"BACKGROUND", type=int, help="background, must be between 0 and 256"
)
getContrastParser = subparsers.add_parser(
"contrast", help="Print out a color test and get the contrast values"
)
getContrastParser.add_argument("COLOR1", type=int, help="Must be between 0 and 256")
getContrastParser.add_argument("COLOR2", type=int, help="Must be between 0 and 256")
printContrastParser = subparsers.add_parser(
"filterContrast", help="Print out all the combos that meet a given delta."
)
printContrastParser.add_argument("DELTA", type=int, help="Pick a color delta")
printColorsByDeltaParser = subparsers.add_parser(
"printGoodContrasts",
help="Print out all the combos on a certain bg of a specified delta.",
)
printColorsByDeltaParser.add_argument(
"BACKGROUND", type=int, help="background, must be between 0 and 255"
)
printColorsByDeltaParser.add_argument(
"DELTA", type=int, help="Delta, the specified difference level between colors"
)
args = parser.parse_args()
if args.command == "dullFg":
dullForegroundColors()
elif args.command == "brightFg":
brightForegroundColors()
elif args.command == "dullBg":
dullBackgroundColors()
elif args.command == "brightBg":
brightBackgroundColors()
elif args.command == "allFg":
allForegroundColors()
elif args.command == "allBg":
allBackgroundColors()
elif args.command == "allFgBg":
allForegroundAndAllBackground()
elif args.command == "getFreaky":
allForegroundAllBackground()
elif args.command == "test":
testForegroundOnBackground(args.FOREGROUND, args.BACKGROUND)
elif args.command == "contrast":
print(_getRGBDictFromANSICode(args.COLOR1))
print(_getRGBDictFromANSICode(args.COLOR2))
print(f"Difference between two colors is: {getContrast(args.COLOR1, args.COLOR2)}")
testForegroundOnBackground(args.COLOR1, args.COLOR2)
elif args.command == "filterContrast":
getAllFgBgWithDiff(args.DELTA)
elif args.command == "printGoodContrasts":
printColorsByDeltaOnBg(args.BACKGROUND, args.DELTA)
else:
parser.print_help()