-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcolortest.py
More file actions
executable file
·142 lines (114 loc) · 4.16 KB
/
Copy pathcolortest.py
File metadata and controls
executable file
·142 lines (114 loc) · 4.16 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
#!/usr/bin/env python
from __future__ import print_function
import os
import shutil
import subprocess
def get_width(default=80):
'''Attempt to detect console width and default to 80'''
try:
columns, rows = shutil.get_terminal_size()
except AttributeError:
try:
_, columns = subprocess.check_output(['stty', 'size']).split()
except OSError:
columns = os.environ.get('COLUMNS', default)
columns = int(columns) - 77
# Since we have 6 columns with 1 space on each side, we can increment the
# size for every 12 extra columns
return max(0, columns / 12)
def table():
# Loosely based on https://gist.github.com/justinabrahms/1047767
colored_16 = [0] + list(range(255, 0, 255))
colored_palette_16 = [
(r, g, b)
for r in colored_16
for g in colored_16
for b in colored_16
]
colored = [0] + list(range(95, 256, 40))
colored_palette = [
(r, g, b)
for r in colored
for g in colored
for b in colored
]
grayscale_palette16 = [(g, g, g) for g in range(0, 224, 15)]
grayscale_palette = [(g, g, g) for g in range(16, 255, 10)]
color_16_name = [
"black ",
"red ",
"green ",
"yellow ",
"blue ",
"magenta ",
"cyan ",
"white ",
"black ",
"red ",
"green ",
"yellow ",
"blue ",
"magenta ",
"cyan ",
"white "
]
esc = '\033['
# Reset all colors sequence
reset = esc + '0m'
# Regular color
normal = esc + '38;5;{i}m'
# Bold color
bold = esc + '1;' + normal
# Background color
background = esc + '48;5;{i}m'
pattern16 = (
'{normal}{background}{padding:^{width}}{i:^3d} ' # pad the background
'{color_test}' # show color text
'{padding:^{width}}' # pad the background on the other side
'{reset}' # reset again
)
pattern = (
'{normal}{background}{padding:^{width}}{i:^3d} ' # pad the background
'{r:02X}/{g:02X}/{b:02X}' # show the hex rgb code
'{padding:^{width}}' # pad the background on the other side
'{reset}' # reset again
)
base_context = dict(reset=reset, padding='', width=get_width())
for i, (r, g, b), in enumerate(colored_palette_16 + grayscale_palette16, 0):
context = dict(i=i, r=r, g=g, b=b, color=r + g + b, color_test=color_16_name[i], **base_context)
context.update(bold=bold.format(**context))
context.update(background=background.format(**context))
# Change text color from black to white when it might become unreadable
if max(r, g, b) > 0xCC:
context.update(normal=normal.format(i=0))
else:
context.update(normal=normal.format(i=255))
print(pattern16.format(**context), end='')
# Print newlines when needed
if i % 6 == 5:
print()
else:
print(' ', end='')
print()
base_context = dict(reset=reset, padding='', width=get_width())
for i, (r, g, b) in enumerate(colored_palette + grayscale_palette, 16):
context = dict(i=i, r=r, g=g, b=b, color=r + g + b, **base_context)
context.update(bold=bold.format(**context))
context.update(background=background.format(**context))
# Change text color from black to white when it might become unreadable
if max(r, g, b) > 0xCC:
context.update(normal=normal.format(i=0))
else:
context.update(normal=normal.format(i=255))
print(pattern.format(**context), end='')
# Print newlines when needed
if i % 6 == 3:
print()
else:
print(' ', end='')
print("\n* colors 8-15 are bright version of colors 0-7")
print("* colors 232-255 are shades of grey")
def main():
table()
if __name__ == '__main__':
main()