-
Notifications
You must be signed in to change notification settings - Fork 2
/
dimidium.py
234 lines (173 loc) · 6.58 KB
/
dimidium.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
"Dimidium" color scheme for terminals
https://github.com/dofuuz/dimidium
SPDX-FileCopyrightText: (c) 2024 Myungchul Keum
SPDX-License-Identifier: Zlib
This color scheme aims enhanced readability by reducing perceptual lightness differences between
colors. In the typical terminal settings, there were issues with certain text colors (especially
blue) being poorly visible. To solve this, the lightness has been equalized using the latest color
space "CAM16".
More information about the Color appearance model, including CAM16:
https://en.wikipedia.org/wiki/Color_appearance_model
xterm's simple color scheme was chosen as the base. For readability, the lightness has been adjusted
to avoid being too bright or too dark. Hue values were set to maximize color distinction, and
saturation was adjusted to fit within the color display range.
Unlike the "helmholtz" or "kohlrausch" color schemes, this color scheme does not aim for equal
brightness. It preserves some lightness and saturation variation to keep each color's essence. In
fact, if each color were adjusted to the same lightness and saturation using CAM16, colors would
become quite "uncomfortable".
"""
import csv
import colour # colour-science
import matplotlib.pyplot as plt
import numpy as np
def plot_lightness(jchs):
PLOT_HEIGHT = 8
jchs = jchs.copy()
j_space = np.linspace(0, 100, 1000)
jc = np.zeros([PLOT_HEIGHT, 1000])
jc[...,:] = j_space
chc = np.zeros_like(jc)
xyzs = colour.CAM16UCS_to_XYZ(np.stack([jc, chc, chc], axis=-1))
rgbs = colour.XYZ_to_sRGB(xyzs).clip(0, 1)
plt.figure(figsize=(6.4, 1))
ax = plt.subplot()
ax.imshow(rgbs, extent=[j_space.min(), j_space.max(), 0, PLOT_HEIGHT])
ax.invert_yaxis()
ax.get_yaxis().set_visible(False)
for jch in jchs[1:9]: # plot colors
jch[1] *= 0.93 # desaturate plot
jab = colour.models.JCh_to_Jab(jch)
xyz = colour.CAM16UCS_to_XYZ(jab)
rgb = colour.XYZ_to_sRGB(xyz).clip(0, 1)
ax.plot(jch[0], 0.25*PLOT_HEIGHT, marker="s", markersize=8, markeredgecolor='black', markerfacecolor=rgb)
for jch in jchs[9:17]: # plot bright colors
jch[1] *= 0.93 # desaturate plot
jab = colour.models.JCh_to_Jab(jch)
xyz = colour.CAM16UCS_to_XYZ(jab)
rgb = colour.XYZ_to_sRGB(xyz).clip(0, 1)
ax.plot(jch[0], 0.75*PLOT_HEIGHT, marker="s", markersize=8, markeredgecolor='black', markerfacecolor=rgb)
plt.show()
def plot_hue(jchs):
AB_RANGE = 45
jchs = jchs.copy()
ac = np.linspace(-AB_RANGE, AB_RANGE, 1000)
bc = np.linspace(-AB_RANGE, AB_RANGE, 1000)
ac, bc = np.meshgrid(ac, bc)
jc = np.ones_like(ac) * 70
xyzs = colour.CAM16UCS_to_XYZ(np.stack([jc, ac, bc], axis=-1))
rgbs = colour.XYZ_to_sRGB(xyzs).clip(0, 1)
clipped = np.hypot(ac, bc) > 35
clipped = np.stack([clipped, clipped, clipped], axis=-1)
rgbs[clipped] = 1
plt.figure(figsize=(4.8, 4.8))
ax = plt.subplot()
ax.imshow(rgbs, extent=[ac.min(), ac.max(), bc.max(), bc.min()])
ax.invert_yaxis()
ax.axis('off')
ax.axis('equal')
for jch in jchs[1:17]:
jab = colour.models.JCh_to_Jab(jch)
xyz = colour.CAM16UCS_to_XYZ(jab)
rgb = colour.XYZ_to_sRGB(xyz).clip(0, 1)
ax.plot(jab[1], jab[2], marker="s", markersize=8, markeredgecolor='black', markerfacecolor=rgb)
plt.show()
def plot_colors(jchs):
jab = colour.models.JCh_to_Jab(jchs)
xyzs = colour.CAM16UCS_to_XYZ(jab)
rgbs = colour.XYZ_to_sRGB(xyzs).clip(0, 1)
plt.figure()
plt.imshow([rgbs[0:9], rgbs[8:17]])
plt.axis('off')
plt.show()
def get_colors_from_tsv(ref_color):
with open('recipe/tty_color.tsv', newline='') as f:
f.readline()
f.readline()
reader = csv.reader(f, delimiter='\t')
colors = list(reader)
# TTY colors
Colour = colors[ref_color]
color = [(0, 0, 0)] # Background
for c in Colour[1:]:
color.append([int(x) for x in c.split(', ')])
color = np.asarray(color, dtype=np.float32)
return color
def generate_colors(ref_color=9, plot=False):
# 0: Background
# 1: Black
# 2-7: R, G, Y, B, M, C
# 8: White, Foreground
# 9: Bright black
# 10-15: Bright R, G, Y, B, M, C
# 16: Bright white, Bold foreground
color = get_colors_from_tsv(ref_color)
color[0,:] = 20 # background
# Convert to CAM16-UCS-JCh
xyz = colour.sRGB_to_XYZ(color/255)
jab = colour.XYZ_to_CAM16UCS(xyz)
color_jch = colour.models.Jab_to_JCh(jab)
if plot:
plot_lightness(color_jch)
plot_colors(color_jch)
# Normalize lightness
j = color_jch[..., 0]
j[5] = (j[2] + j[5]) / 2 # adjust blue
# j[13] = (j[10] + j[13]) / 2 # adjust bright blue
j_mean = np.mean(j[2:8])
j[2:9] = (j[2:9] + j_mean) / 2 # colors
j_mean = np.mean(j[10:16])
j[10:17] = (j[10:17] + j_mean) / 2 # bright colors
if plot:
plot_lightness(color_jch)
plot_colors(color_jch)
plot_hue(color_jch)
# Set hue(mean delta to original is about 3)
h = color_jch[..., 2]
h[2:8] = (30, 150, 90, 270, 330, 210)
h[2:8] -= 10
h[10:16] = (30, 150, 90, 270, 330, 210)
h[10:16] += 3
if plot:
plot_hue(color_jch)
plot_colors(color_jch)
# Normalize chroma
c = color_jch[..., 1]
c_min = np.min(c[2:8])
c[2:8] = (c[2:8] + c_min) / 2
c_min = np.min(c[10:16])
c[10:16] = (c[10:16] + c_min) / 2
c[0] = 0 # background
c[8] = 0 # white
c[9] = 0 # bright black
c[16] *= 2 # bright white (give little color shift between whites)
# clip chroma into sRGB gamut
for desaturate in np.arange(1, 0.1, -0.001):
# Convert back to RGB
color_jch_adj = np.stack([j, c*desaturate, h], axis=-1)
jab = colour.models.JCh_to_Jab(color_jch_adj)
xyz = colour.CAM16UCS_to_XYZ(jab)
color_rgb = colour.XYZ_to_sRGB(xyz)
if np.all(0 <= color_rgb) and np.all(color_rgb <= 1):
color_jch = color_jch_adj
if plot:
print(desaturate)
break
if plot:
plot_hue(color_jch)
plot_colors(color_jch)
print(color_rgb)
print((color_rgb*255).round().astype('int'))
rgbs = (color_rgb*255).round().clip(0, 255).astype('uint8')
if plot:
plt.figure()
plt.imshow([rgbs[0:9], rgbs[8:17]])
plt.axis('off')
plt.show()
return rgbs
if __name__ == '__main__':
plt.rcParams['figure.autolayout'] = True
rgbs = generate_colors(plot=True)
print(rgbs)