forked from anderslanglands/colorspace-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_data.py
304 lines (244 loc) · 11.7 KB
/
generate_data.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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import colour
from colour import *
import numpy as np
from colour.utilities import (dot_matrix, dot_vector, from_range_1,
row_as_diagonal, to_domain_1)
name_map = {
u'black 2 (1.5 D)': 'black_20',
u'blue': 'blue',
u'blue flower': 'blue_flower',
u'blue sky': 'blue_sky',
u'bluish green': 'bluish_green',
u'cyan': 'cyan',
u'dark skin': 'dark_skin',
u'foliage': 'foliage',
u'green': 'green',
u'light skin': 'light_skin',
u'magenta': 'magenta',
u'moderate red': 'moderate_red',
u'neutral 3.5 (1.05 D)': 'neutral_35',
u'neutral 5 (.70 D)': 'neutral_50',
u'neutral 6.5 (.44 D)': 'neutral_65',
u'neutral 8 (.23 D)': 'neutral_80',
u'orange': 'orange',
u'orange yellow': 'orange_yellow',
u'purple': 'purple',
u'purplish blue': 'purplish_blue',
u'red': 'red',
u'white 9.5 (.05 D)': 'white_95',
u'yellow': 'yellow',
u'yellow green': 'yellow_green'
}
def generate_colorchecker_xyz(name, t, checker, cmfs, ill):
print('pub static ref %s: HashMap<String, %s> = hashmap! {' % (name, t))
for swatch_name in checker.keys():
sd = checker[swatch_name]
xyz = sd_to_XYZ(sd, cmfs, ill)
print(' "%s".into() => xyz(%.24f, %.24f, %.24f),' %
(name_map[swatch_name], xyz[0], xyz[1], xyz[2]))
print('};')
# Align cmfs to standard practise shape
cmfs = CMFS['CIE 1931 2 Degree Standard Observer'].clone().align(
SpectralShape(360, 780, 1))
ill_d65 = colour.ILLUMINANTS_SDS['D65'].clone().align(
SpectralShape(360, 780, 1))
generate_colorchecker_xyz('XYZ_D65', 'XYZf64',
colour.COLOURCHECKERS_SDS['BabelColor Average'],
cmfs, ill_d65)
# Generate spectral checkers
print('pub static ref VSPD: HashMap<String, VSPD> = hashmap! {')
for swatch_name in colour.COLOURCHECKERS_SDS['BabelColor Average'].keys():
sd = colour.COLOURCHECKERS_SDS['BabelColor Average'][swatch_name]
print(' "%s".into() => vspd! {' % name_map[swatch_name])
for w, v in zip(sd.wavelengths, sd.values):
print(' %f => %f,' % (w, v))
print(' },')
print('};')
def generate_colorchecker_rgb(name, model, t, checker, cmfs, ill):
print(
'pub static ref %s_SCENE_REFERRED: HashMap<String, %s> = hashmap! {' %
(name, t))
xyz_wp = colour.models.rgb.RGB_COLOURSPACES['sRGB'].whitepoint
for swatch_name in checker.keys():
sd = checker[swatch_name]
xyz = sd_to_XYZ(sd, cmfs, ill)
rgb = colour.XYZ_to_RGB(xyz / 100.0, xyz_wp,
model.whitepoint, model.XYZ_to_RGB_matrix)
print(' "%s".into() => rgbf(%.24f, %.24f, %.24f),' %
(name_map[swatch_name], rgb[0], rgb[1], rgb[2]))
print('};\n')
print('pub static ref %s_ENCODED: HashMap<String, %s> = hashmap! {' %
(name, t))
for swatch_name in checker.keys():
sd = checker[swatch_name]
xyz = sd_to_XYZ(sd, cmfs, ill)
rgb = colour.XYZ_to_RGB(xyz / 100.0, xyz_wp,
model.whitepoint, model.XYZ_to_RGB_matrix)
rgb = model.encoding_cctf(rgb)
print(' "%s".into() => rgbf(%.24f, %.24f, %.24f),' %
(name_map[swatch_name], rgb[0], rgb[1], rgb[2]))
print('};\n')
generate_colorchecker_rgb('SRGB', colour.models.rgb.RGB_COLOURSPACES['sRGB'],
'RGBf64',
colour.COLOURCHECKERS_SDS['BabelColor Average'],
cmfs, ill_d65)
generate_colorchecker_rgb('ITUR_BT709', colour.models.rgb.RGB_COLOURSPACES['ITU-R BT.709'],
'RGBf64',
colour.COLOURCHECKERS_SDS['BabelColor Average'],
cmfs, ill_d65)
generate_colorchecker_rgb('ALEXA_WIDE_GAMUT', colour.models.rgb.RGB_COLOURSPACES['ALEXA Wide Gamut'],
'RGBf64',
colour.COLOURCHECKERS_SDS['BabelColor Average'],
cmfs, ill_d65)
generate_colorchecker_rgb('ACES_CC', colour.models.rgb.RGB_COLOURSPACES['ACEScc'],
'RGBf64',
colour.COLOURCHECKERS_SDS['BabelColor Average'],
cmfs, ill_d65)
generate_colorchecker_rgb('ACES_CCT', colour.models.rgb.RGB_COLOURSPACES['ACEScct'],
'RGBf64',
colour.COLOURCHECKERS_SDS['BabelColor Average'],
cmfs, ill_d65)
generate_colorchecker_rgb('ACES_CG', colour.models.rgb.RGB_COLOURSPACES['ACEScg'],
'RGBf64',
colour.COLOURCHECKERS_SDS['BabelColor Average'],
cmfs, ill_d65)
generate_colorchecker_rgb('ACES', colour.models.rgb.RGB_COLOURSPACES['aces'],
'RGBf64',
colour.COLOURCHECKERS_SDS['BabelColor Average'],
cmfs, ill_d65)
generate_colorchecker_rgb('ACES_PROXY', colour.models.rgb.RGB_COLOURSPACES['ACESproxy'],
'RGBf64',
colour.COLOURCHECKERS_SDS['BabelColor Average'],
cmfs, ill_d65)
generate_colorchecker_rgb('DCI_P3_P', colour.models.rgb.RGB_COLOURSPACES['DCI-P3+'],
'RGBf64',
colour.COLOURCHECKERS_SDS['BabelColor Average'],
cmfs, ill_d65)
generate_colorchecker_rgb('DCI_P3', colour.models.rgb.RGB_COLOURSPACES['DCI-P3'],
'RGBf64',
colour.COLOURCHECKERS_SDS['BabelColor Average'],
cmfs, ill_d65)
generate_colorchecker_rgb('PRO_PHOTO_RGB', colour.models.rgb.RGB_COLOURSPACES['ProPhoto RGB'],
'RGBf64',
colour.COLOURCHECKERS_SDS['BabelColor Average'],
cmfs, ill_d65)
generate_colorchecker_rgb('BETA_RGB', colour.models.rgb.RGB_COLOURSPACES['Beta RGB'],
'RGBf64',
colour.COLOURCHECKERS_SDS['BabelColor Average'],
cmfs, ill_d65)
generate_colorchecker_rgb('RED_COLOR', colour.models.rgb.RGB_COLOURSPACES['REDcolor'],
'RGBf64',
colour.COLOURCHECKERS_SDS['BabelColor Average'],
cmfs, ill_d65)
generate_colorchecker_rgb('RED_COLOR2', colour.models.rgb.RGB_COLOURSPACES['REDcolor2'],
'RGBf64',
colour.COLOURCHECKERS_SDS['BabelColor Average'],
cmfs, ill_d65)
generate_colorchecker_rgb('RED_COLOR3', colour.models.rgb.RGB_COLOURSPACES['REDcolor3'],
'RGBf64',
colour.COLOURCHECKERS_SDS['BabelColor Average'],
cmfs, ill_d65)
generate_colorchecker_rgb('RED_COLOR4', colour.models.rgb.RGB_COLOURSPACES['REDcolor4'],
'RGBf64',
colour.COLOURCHECKERS_SDS['BabelColor Average'],
cmfs, ill_d65)
generate_colorchecker_rgb('DRAGON_COLOR', colour.models.rgb.RGB_COLOURSPACES['DRAGONcolor'],
'RGBf64',
colour.COLOURCHECKERS_SDS['BabelColor Average'],
cmfs, ill_d65)
generate_colorchecker_rgb('DRAGON_COLOR2', colour.models.rgb.RGB_COLOURSPACES['DRAGONcolor2'],
'RGBf64',
colour.COLOURCHECKERS_SDS['BabelColor Average'],
cmfs, ill_d65)
generate_colorchecker_rgb('SHARP_RGB', colour.models.rgb.RGB_COLOURSPACES['Sharp RGB'],
'RGBf64',
colour.COLOURCHECKERS_SDS['BabelColor Average'],
cmfs, ill_d65)
generate_colorchecker_rgb('ITUR_BT2020', colour.models.rgb.RGB_COLOURSPACES['ITU-R BT.2020'],
'RGBf64',
colour.COLOURCHECKERS_SDS['BabelColor Average'],
cmfs, ill_d65)
generate_colorchecker_rgb('ADOBE_RGB_1998', colour.models.rgb.RGB_COLOURSPACES['Adobe RGB (1998)'],
'RGBf64',
colour.COLOURCHECKERS_SDS['BabelColor Average'],
cmfs, ill_d65)
print(colour.models.rgb.RGB_COLOURSPACES.keys())
wp_srgb = colour.models.rgb.RGB_COLOURSPACES['sRGB'].whitepoint
wp_srgb_xyz = xyY_to_XYZ(xy_to_xyY(wp_srgb))
wp_aces = colour.models.rgb.RGB_COLOURSPACES['aces'].whitepoint
wp_aces_xyz = xyY_to_XYZ(xy_to_xyY(wp_aces))
print("srgb whitepoint ", wp_srgb, wp_srgb_xyz)
print("aces whitepoint ", wp_aces, wp_aces_xyz)
print(colour.models.rgb.RGB_COLOURSPACES['aces'].XYZ_to_RGB_matrix)
cat_mtx = colour.adaptation.chromatic_adaptation_matrix_VonKries(wp_srgb_xyz, wp_aces_xyz, transform='CAT02')
print(cat_mtx)
cat_mtx = colour.adaptation.chromatic_adaptation_matrix_VonKries(wp_srgb_xyz, wp_aces_xyz, transform='Bradford')
print(cat_mtx)
# Generate data for SPD type
spd_shape = SpectralShape(380, 770, 10)
W = colour.colorimetry.tristimulus.tristimulus_weighting_factors_ASTME202211(
cmfs,
ill_d65,
spd_shape,
)
W = colour.colorimetry.tristimulus.adjust_tristimulus_weighting_factors_ASTME30815(W, SpectralShape(cmfs.shape.start, cmfs.shape.end, spd_shape.interval), spd_shape)
print('pub static ref W_X: SPD = SPD::new([')
for w in W[:,0]:
print(' %.24f,' % w)
print(']);')
print('pub static ref W_Y: SPD = SPD::new([')
for w in W[:,1]:
print(' %.24f,' % w)
print(']);')
print('pub static ref W_Z: SPD = SPD::new([')
for w in W[:,2]:
print(' %.24f,' % w)
print(']);')
print('pub static ref BABELCOLOR: HashMap<String, SPD> = hashmap! {')
for swatch_name in colour.COLOURCHECKERS_SDS['BabelColor Average'].keys():
sd = colour.COLOURCHECKERS_SDS['BabelColor Average'][swatch_name].clone().align(spd_shape)
print(' "%s".into() => SPD::new([' % name_map[swatch_name])
for v in sd.values:
print(' %.24f,' % v )
print(' ]),')
print('};')
def chromatic_adaptation_matrix_VonKries(XYZ_w, XYZ_wr, transform='CAT02'):
M = CHROMATIC_ADAPTATION_TRANSFORMS.get(transform)
print('XYZ_w', XYZ_w)
print('XYZ_wr', XYZ_wr)
if M is None:
raise KeyError(
'"{0}" chromatic adaptation transform is not defined! Supported '
'methods: "{1}".'.format(transform,
CHROMATIC_ADAPTATION_TRANSFORMS.keys()))
print('M', M)
print('M inv', np.linalg.inv(M))
rgb_w = np.einsum('...i,...ij->...j', XYZ_w, np.transpose(M))
rgb_wr = np.einsum('...i,...ij->...j', XYZ_wr, np.transpose(M))
print('rgb_w: ', rgb_w)
print('rgb_wr: ', rgb_wr)
D = rgb_wr / rgb_w
print('D: ', D)
D = row_as_diagonal(D)
M_CAT = dot_matrix(np.linalg.inv(M), D)
M_CAT = dot_matrix(M_CAT, M)
return M_CAT
M_cat02 = chromatic_adaptation_matrix_VonKries(wp_srgb_xyz, wp_aces_xyz)
print(M_cat02)
model_src = colour.models.rgb.RGB_COLOURSPACES['sRGB']
model_dst =colour.models.rgb.RGB_COLOURSPACES['aces']
print(
'pub static ref ACES_FROM_SRGB: HashMap<String, RGBf64> = hashmap! {')
xyz_wp = colour.models.rgb.RGB_COLOURSPACES['sRGB'].whitepoint
for swatch_name in colour.COLOURCHECKERS_SDS['BabelColor Average'].keys():
sd = colour.COLOURCHECKERS_SDS['BabelColor Average'][swatch_name]
xyz = sd_to_XYZ(sd, cmfs, ill_d65)
rgb_srgb = colour.XYZ_to_RGB(xyz / 100.0, xyz_wp,
model_src.whitepoint, model_src.XYZ_to_RGB_matrix)
rgb_aces = colour.RGB_to_RGB(rgb_srgb, model_src, model_dst)
print(' "%s".into() => rgbf(%.24f, %.24f, %.24f),' %
(name_map[swatch_name], rgb_aces[0], rgb_aces[1], rgb_aces[2]))
print('};\n')
# Whitepoints
d65_xyz = xy_to_XYZ([0.31270, 0.32900])
print('d65_xyz: ', d65_xyz)