-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolors
executable file
·142 lines (122 loc) · 3.5 KB
/
colors
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 python3
import sys, os, math
from contextlib import contextmanager
import argparse
import collections
shown = 0
def o(s):
global shown
sys.stdout.write(s)
shown += s.count("\n")
STOP = "\033[0m"
CLS = "\033[2J"
HOME = "\033[H"
args = None
colincr = 0
Args = collections.namedtuple( "Args", [ "rows", "cols", "noalt" ] )
args = Args( cols=80, rows=24, noalt=False )
@contextmanager
def EightBitBackground(color):
o(f"\033[48;5;{color}m")
yield
o(STOP)
@contextmanager
def EightBitForeground(color):
o(f"\033[38;5;{color}m")
yield
o(STOP)
@contextmanager
def AltScreen():
if not args.noalt:
o(f"\033[?1049h")
yield
if not args.noalt:
o(f"\033[?1049l")
@contextmanager
def TrueColor(r, g, b):
o(f"\033[48;2;{r};{g};{b}m")
yield
o(STOP)
def cells(index, count, space):
# width of the Nth field of count. In theory, (index/count) * cols, but we need to distribute the fractional bits
per = float(space) / count
start = index * per
end = (index + 1) * per
return int ( math.floor(end) - math.floor(start) )
def widthof(index, count):
return cells(index, count, args.cols)
def heightof(index, count):
return cells(index, count, args.rows)
def basic():
o("Basic ANSI 16-color\n")
for ctx in ( EightBitForeground, EightBitBackground ):
# Show first 16 separately
for color in range(16):
with ctx(color):
o(f"{color :>{widthof(color,16)-1}} ")
o("\n")
o("\nRemaining 8-bit color pallette\n")
for ctx in ( EightBitForeground, EightBitBackground ):
for colormajor in range(16, 256, 24):
for colorminor in range(24):
with ctx(colormajor+colorminor):
o(f"{colorminor+colormajor :>{widthof(colorminor,24)}}")
o("\n")
def primaries():
o("\nTrue color - primaries\n")
for m in [ (1,0,0), (0,1,0), (0,0,1), (1,1,0), (1,0,1), (0,1,1), (1,1,1) ]:
intensity = 0
if shown >= args.rows - 1:
break
while intensity <= 255:
i = int(intensity)
with TrueColor(*((a * b) for a, b in zip(m, (i, i, i)))):
o(" ")
intensity += colincr
o("\n")
def cube():
# We'll draw up to 24 lines in our cube.
cubelines = min(args.rows - 3 - shown, 24)
# but we want at least 11
if cubelines <= 10:
return
o("\nTrue color - color cube\n")
greenincr = colincr
blueincr = colincr * 8
redincr = 256/cubelines
red = 0
while red <= 255:
blue = 0
green = 0
while green <= 255:
with TrueColor( int(red) % 256, int(green) % 256, int(blue)%256):
o(" ")
blue += blueincr
green += greenincr
o("\n")
red += redincr
def testChart():
basic()
primaries()
cube()
def main():
global args, colincr
argp = argparse.ArgumentParser()
try:
sz = os.get_terminal_size()
cols, rows = sz.columns, sz.lines
except:
cols, rows = 80, 24
argp.add_argument("--cols", type=int, default=cols)
argp.add_argument("--rows", type=int, default=rows)
argp.add_argument("--noalt", action='store_true', default=not sys.stdout.isatty())
args = argp.parse_args()
colincr = 256 / args.cols
with AltScreen():
if not args.noalt:
o(CLS + HOME)
testChart()
if not args.noalt:
sys.stdin.readline()
if __name__ == "__main__":
main()