forked from bps10/color
-
Notifications
You must be signed in to change notification settings - Fork 0
/
colorSpace.py
executable file
·584 lines (482 loc) · 19.9 KB
/
colorSpace.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
#! /usr/bin/env python
# -*- coding: utf-8 *-*
from __future__ import division
import numpy as np
import matplotlib.pylab as plt
from base import plot as pf
from base import spectsens as ss
from base import optics as op
from genLMS import genLMS
class colorSpace(object):
'''
'''
def __init__(self, stim='wright', fundamental='neitz',
LMSpeaks=[559.0, 530.0, 421.0]):
self.params = {'lights': stim.lower, }
self.setLights(stim)
self.genStockmanFilter()
self.genLMS(fundamental, LMSpeaks)
self.genConvMatrix()
self.LMStoCMFs()
self.CMFtoEE_CMF()
self.EE_CMFtoRGB()
def genLMS(self, fundamental, LMSpeaks=[559.0, 530.0, 421.0]):
'''
'''
fund = fundamental.lower()
self.Lnorm, self.Mnorm, self.Snorm = genLMS(self.spectrum,
self.filters, fundamental=fund, LMSpeaks=LMSpeaks)
def genStockmanFilter(self, maxLambda=770):
'''
'''
self.filters, self.spectrum = op.filters.stockman(minLambda=390,
maxLambda=maxLambda, RETURN_SPECTRUM=True,
resolution=1)
def genConvMatrix(self, PRINT=False):
'''
'''
self.convMatrix = np.array([
[np.interp(self.lights['l'], self.spectrum, self.Lnorm),
np.interp(self.lights['m'], self.spectrum, self.Lnorm),
np.interp(self.lights['s'], self.spectrum, self.Lnorm)],
[np.interp(self.lights['l'], self.spectrum, self.Mnorm),
np.interp(self.lights['m'], self.spectrum, self.Mnorm),
np.interp(self.lights['s'], self.spectrum, self.Mnorm)],
[np.interp(self.lights['l'], self.spectrum, self.Snorm),
np.interp(self.lights['m'], self.spectrum, self.Snorm),
np.interp(self.lights['s'], self.spectrum, self.Snorm)]])
if PRINT == True:
print self.convMatrix
def genTetraConvMatrix(self, Xpeak):
'''
'''
minspec = min(self.spectrum)
maxspec = max(self.spectrum)
Xsens = ss.neitz(Xpeak, 0.5, False, minspec,
maxspec, 1)
Xresponse = Xsens / self.filters * self.spectrum
Xnorm = Xresponse / np.max(Xresponse)
lights = {'l': 600, 'm': 510, 's': 420, 'x': 720}
convMatrix = np.array([
[np.interp(lights['l'], self.spectrum, self.Lnorm),
np.interp(lights['m'], self.spectrum, self.Lnorm),
np.interp(lights['s'], self.spectrum, self.Lnorm),
np.interp(lights['x'], self.spectrum, self.Lnorm)],
[np.interp(lights['l'], self.spectrum, self.Mnorm),
np.interp(lights['m'], self.spectrum, self.Mnorm),
np.interp(lights['s'], self.spectrum, self.Mnorm),
np.interp(lights['x'], self.spectrum, self.Mnorm)],
[np.interp(lights['l'], self.spectrum, self.Snorm),
np.interp(lights['m'], self.spectrum, self.Snorm),
np.interp(lights['s'], self.spectrum, self.Snorm),
np.interp(lights['x'], self.spectrum, self.Snorm)],
[np.interp(lights['l'], self.spectrum, Xnorm),
np.interp(lights['m'], self.spectrum, Xnorm),
np.interp(lights['s'], self.spectrum, Xnorm),
np.interp(lights['x'], self.spectrum, Xnorm)]])
return convMatrix
def genXYZ(self, plot=True):
'''
'''
rgb = np.array([self.rVal, self.gVal, self.bVal])
JuddVos = self._genJuddVos()
convXYZ = np.array([[2.768892, 1.751748, 1.130160],
[1.000000, 4.590700, 0.060100],
[0, 0.056508, 5.594292]])
neitzXYZ = np.dot(convXYZ, rgb)
xyzM = np.linalg.lstsq(neitzXYZ.T, JuddVos)[0]
xyz = np.dot(xyzM, neitzXYZ)
self.X = xyz[0, :]
self.Y = xyz[1, :]
self.Z = xyz[2, :]
if plot:
self._plotColorSpace(self.X, self.Y, self.spectrum)
plt.show()
def setLights(self, stim):
'''
'''
if (stim.lower() != 'wright' and stim.lower() != 'stiles and burch'
and stim.lower() != 'cie 1931'):
print 'Sorry, stim light not understood, using wright'
stim = 'wright'
if stim.lower() == 'wright':
self.lights = {
'l': 650.0,
'm': 530.0,
's': 460.0,
}
if stim.lower() == 'stiles and burch':
self.lights = {'l': 645.0,
'm': 526.0,
's': 444.0, }
if stim.lower() == 'cie 1931':
self.lights = {'l': 700.0,
'm': 546.1,
's': 435.8, }
def TrichromaticEquation(self, r, g, b):
'''
'''
rgb = r + g + b
r_ = r / rgb
g_ = g / rgb
b_ = b / rgb
return r_, g_, b_
def LMStoCMFs(self):
'''
'''
LMSsens = np.array([self.Lnorm, self.Mnorm, self.Snorm])
self.CMFs = np.dot(np.linalg.inv(self.convMatrix), LMSsens)
#save sums for later normalization:
Rnorm = sum(self.CMFs[0, :])
Gnorm = sum(self.CMFs[1, :])
Bnorm = sum(self.CMFs[2, :])
self.EEfactors = {'r': Rnorm, 'g': Gnorm, 'b': Bnorm, }
def CMFtoEE_CMF(self):
'''
'''
self.CMFs[0, :], self.CMFs[1, :], self.CMFs[2, :] = self._EEcmf(
self.CMFs[0, :],
self.CMFs[1, :],
self.CMFs[2, :])
def lambda2BY(self, lam, verbose=False):
'''
'''
r, g, b = self.find_testLightMatch(lam)
line = self._lineEq(r, g)
self.find_copunctuals()
imagLine = self._lineEq(self.copunctuals['deutan'][0],
self.copunctuals['deutan'][1],
self.copunctuals['tritan'][0],
self.copunctuals['tritan'][1])
xpoints = np.arange(self.copunctuals['tritan'][0],
self.copunctuals['deutan'][0], 0.01)
ypoints = imagLine(xpoints)
neutPoint = self._findDataIntercept(xpoints, ypoints, line)
if verbose is True:
return neutPoint, [r, g]
else:
return neutPoint
def lambda2RG(self, lam, equal_energy=True, verbose=False):
'''
'''
self.find_copunctuals()
imagLine = self._lineEq(self.copunctuals['protan'][0],
self.copunctuals['protan'][1],
self.copunctuals['deutan'][0],
self.copunctuals['deutan'][1])
xpoints = np.arange(self.copunctuals['protan'][0],
self.copunctuals['deutan'][0], 0.01)
ypoints = imagLine(xpoints)
r, g, b = self.find_testLightMatch(lam)
if equal_energy:
line = self._lineEq(r, g)
else:
ind = int(len(xpoints) * (2/3))
line = self._lineEq(r, g, xpoints[ind], ypoints[ind])
#print xpoints, ypoints
neutPoint = self._findDataIntercept(xpoints, ypoints, line)
if verbose is True:
return neutPoint, [r, g]
else:
return neutPoint
def BY2lambda(self, propS, propM, propL=0, verbose=False):
'''
'''
l = propL
m = -propM
s = propS
r, g, b = self.find_rgb(np.array([l, m, s]))
line = self._lineEq(r, g)
neutPoint = self._findDataIntercept(self.rVal, self.gVal, line)
if verbose is True:
return neutPoint, [r, g]
else:
return neutPoint
def RG2lambda(self, propS, propM, propL=0, verbose=False):
'''
'''
l = propL
m = -propM
s = propS
r, g, b = self.find_rgb(np.array([l, m, s]))
line = self._lineEq(r, g)
neutPoint = self._findDataIntercept(self.rVal, self.gVal, line)
if verbose is True:
return neutPoint, [r, g]
else:
return neutPoint
def EE_CMFtoRGB(self, rgb=None):
'''
'''
if rgb is None:
self.rVal, self.gVal, self.bVal = self.TrichromaticEquation(
self.CMFs[0, :], self.CMFs[1, :], self.CMFs[2, :])
else:
return self.TrichromaticEquation(rgb[0], rgb[1], rgb[2])
def find_copunctuals(self):
'''
'''
protan = self.find_rgb(np.array([1, 0, 0]))
deutan = self.find_rgb(np.array([0, 1, 0]))
tritan = self.find_rgb(np.array([0, 0, 1]))
self.copunctuals = {'protan': protan,
'deutan': deutan,
'tritan': tritan, }
def find_testLightMatch(self, testLight=600, R=None, G=None, B=None):
'''
'''
if R == None or G == None or B == None:
Lnorm = self.Lnorm
Mnorm = self.Mnorm
Snorm = self.Snorm
else:
Lnorm = R
Mnorm = G
Snorm = B
l_ = np.interp(testLight, self.spectrum, Lnorm)
m_ = np.interp(testLight, self.spectrum, Mnorm)
s_ = np.interp(testLight, self.spectrum, Snorm)
if R == None or G == None or B == None:
rOut, gOut, bOut = self.find_rgb(LMS=np.array([l_, m_, s_]))
else:
rOut, gOut, bOut = l_, m_, s_
return [rOut, gOut, bOut]
def find_testlightFromRG(self, r, g):
'''
'''
err = lambda r, g, lam: ((r - self.rVal[lam])**2 + (g -
self.gVal[lam])**2)
i = 0
startE = err(r, g, i)
error = True
try:
while error:
e = err(r, g, i)
if startE < e and e < 10e-2:
error = False
else:
startE = e
i += 1
#linear interpolate between points
t0 = err(r, g, i)
t1 = err(r, g, i + 1)
outLam = self.spectrum[i] + (0 - t0 / (t1 - t0))
return outLam
except IndexError:
raise IndexError('Pure light not found. Are you sure the [r, g] \
coords lie on the spectral line?')
def find_rgb(self, LMS):
'''
'''
cmf = np.dot(np.linalg.inv(self.convMatrix), LMS)
cmf[0], cmf[1], cmf[2] = self._EEcmf(cmf[0], cmf[1], cmf[2])
out = self.TrichromaticEquation(cmf[0], cmf[1], cmf[2])
return out
def find_BYweights(self):
'''Function not finished
'''
neut, RG = self.BY2lambda(s, m, 0, True)
n, rg = self.BY2lambda(0.48, 0.52, 0, True)
print self.find_testlightFromRG(n[0], n[1])
def _EEcmf(self, r_, g_, b_):
'''
'''
r_ *= 100. / self.EEfactors['r']
g_ *= 100. / self.EEfactors['g']
b_ *= 100. / self.EEfactors['b']
return [r_, g_, b_]
def _lineEq(self, x1, y1, x2=None, y2=None):
'''Return the equation of a line from a given point that will pass
through equal energy. Returns a function that takes one variable, x,
and returns y.
'''
if x2 == None:
x2 = 1. / 3.
if y2 == None:
y2 = 1. / 3.
m_ = (y2 - y1) / (x2 - x1)
b_ = (y1) - (m_ * (x1))
return lambda x: (m_ * x) + b_
def _findDataIntercept(self, x, y, func):
'''
'''
diff = True
s = np.sign(func(x[0]) - y[0])
i = 0
while diff is True:
err = func(x[i]) - y[i]
sig = np.sign(err)
if sig != s:
diff = False
else:
i +=1
#linear interpolate between points
t0 = func(x[i - 1]) - y[i - 1]
t1 = func(x[i]) - y[i]
outX = x[i - 1] + ((x[i] - x[i - 1]) * (0 - t0 / (t1 - t0)))
outY = func(outX)
return [outX, outY]
def _genJuddVos2Neitz(self, juddVos):
'''
'''
neitz = np.array([self.rVal, self.gVal, self.bVal]).T
JuddVos_Neitz_lights = np.array([
[np.interp(self.lights['l'], self.spectrum, juddVos[:, 0]),
np.interp(self.lights['m'], self.spectrum, juddVos[:, 0]),
np.interp(self.lights['s'], self.spectrum, juddVos[:, 0])],
[np.interp(self.lights['l'], self.spectrum, juddVos[:, 1]),
np.interp(self.lights['m'], self.spectrum, juddVos[:, 1]),
np.interp(self.lights['s'], self.spectrum, juddVos[:, 1])],
[np.interp(self.lights['l'], self.spectrum, juddVos[:, 2]),
np.interp(self.lights['m'], self.spectrum, juddVos[:, 2]),
np.interp(self.lights['s'], self.spectrum, juddVos[:, 2])]])
foo = np.dot(np.linalg.inv(JuddVos_Neitz_lights), juddVos.T).T
tempMat = np.linalg.lstsq(foo, neitz)[0]
JuddVos_Neitz_transMatrix = np.dot(np.linalg.inv(JuddVos_Neitz_lights),
(tempMat))
return JuddVos_Neitz_transMatrix
def _genJuddVos(self):
'''
'''
try:
from scipy import interpolate as interp
except ImportError:
raise ImportError('Sorry cannot import scipy')
#lights = np.array([700, 546.1, 435.8])
juddVos = np.genfromtxt('data/ciexyzjv.csv', delimiter=',')
spec = juddVos[:, 0]
juddVos = juddVos[:, 1:]
juddVos[:, 0] *= 100. / sum(juddVos[:, 0])
juddVos[:, 1] *= 100. / sum(juddVos[:, 1])
juddVos[:, 2] *= 100. / sum(juddVos[:, 2])
r, g, b = self.TrichromaticEquation(juddVos[:, 0],
juddVos[:, 1],
juddVos[:, 2])
juddVos[:, 0], juddVos[:, 1], juddVos[:, 2] = r, g, b
L_spline = interp.splrep(spec, juddVos[:, 0], s=0)
M_spline = interp.splrep(spec, juddVos[:, 1], s=0)
S_spline = interp.splrep(spec, juddVos[:, 2], s=0)
L_interp = interp.splev(self.spectrum, L_spline, der=0)
M_interp = interp.splev(self.spectrum, M_spline, der=0)
S_interp = interp.splev(self.spectrum, S_spline, der=0)
JVinterp = np.array([L_interp, M_interp, S_interp]).T
return JVinterp
def returnConvMat(self):
'''
'''
return self.convMatrix
def returnCMFs(self):
'''
'''
return {'cmfs': self.CMFs, 'wavelengths': self.spectrum, }
def return_rgb(self):
'''
'''
return {'r': self.rVal, 'g': self.gVal, 'b': self.bVal, }
def _plotColorSpace(self, rVal=None, gVal=None, spec=None, ee=True,
invert=False, Luv=False, skipLam=None, color=False):
'''
'''
downSamp = 10
minLam = 460
maxLam = 630
if rVal == None or gVal == None or spec == None:
rVal = self.rVal
gVal = self.gVal
spec = self.spectrum
JuddV = False
offset = 0.02
turn = [500, 510]
elif Luv:
JuddV = False
offset = 0.015
turn = [500, 510]
minLam = 420
maxLam = 630
else:
JuddV = True
offset = 0.01
turn = [510, 520]
fig = plt.figure()
fig.set_tight_layout(True)
self.cs_ax = fig.add_subplot(111)
pf.AxisFormat(FONTSIZE=10, TickSize=6)
if not JuddV:
pf.AxisFormat(FONTSIZE=10, TickSize=6)
pf.centerAxes(self.cs_ax)
if JuddV:
pf.AxisFormat(FONTSIZE=10, TickSize=8)
pf.centerAxes(self.cs_ax)
if color:
import matplotlib.nxutils as nx
verts = []
for i, val in enumerate(rVal[:-10]):
verts.append([rVal[i], gVal[i]])
verts = np.asarray(verts)
white = np.linalg.norm([1 / 3, 1 / 3, 1 / 3])
for x in np.arange(-0.3, 1.1, 0.005):
for y in np.arange(-0.15, 1.1, 0.01):
if x + y <= 1:
if nx.points_inside_poly(np.array([[x, y]]), verts):
_x = _boundval(x)
_y = _boundval(y)
_z = 1 - (_x + _y)
norm = np.linalg.norm([x, y, 1 - (x + y)])
dist = abs(norm - white)
if dist <= (1 / 3):
_x += ((1 / 3) - dist)
_y += ((1 / 3) - dist)
_z += ((1 / 3) - dist)
self.cs_ax.plot((x), (y),
'o', c=[_x, _y, _z],
ms=6, mec='none', alpha=0.7)
self.cs_ax.plot(rVal[:-10], gVal[:-10], 'k', linewidth=5)
self.cs_ax.plot([rVal[0], rVal[-10]], [gVal[0], gVal[-10]], 'k', linewidth=5)
self.cs_ax.plot(rVal[:-10], gVal[:-10], 'k', linewidth=3.5)
self.cs_ax.plot([rVal[0], rVal[-10]], [gVal[0], gVal[-10]], 'k', linewidth=3.5)
# add equi-energy location to plot
if ee:
self.cs_ax.plot(1.0/3.0, 1.0/3.0, 'ko', markersize=5)
self.cs_ax.annotate(s='{}'.format('E'), xy=(1./3.,1./3.),
xytext=(2,8),
ha='right', textcoords='offset points',
fontsize=14)
#rgb = np.reshape([self.Lnorm,self.Mnorm,self.Snorm],
# [len(self.Lnorm) / 2, len(self.Lnorm) / 2, 3])
# annotate plot
dat = zip(spec[::downSamp], rVal[::downSamp], gVal[::downSamp])
for text, X, Y in dat:
if text > minLam and text < maxLam and not np.any(
text == np.asarray(skipLam)):
if text <= turn[0]:
self.cs_ax.scatter(X - offset, Y, marker='_', s=150, c='k')
self.cs_ax.annotate(s='{}'.format(int(text)),
xy=(X, Y),
xytext=(-15, -5),
ha='right',
textcoords='offset points',
fontsize=16)
elif text > turn[0] and text <= turn[1]:
self.cs_ax.scatter(X, Y + offset, marker='|', s=150, c='k')
self.cs_ax.annotate(s='{}'.format(int(text)),
xy=(X, Y),
xytext=(5, 20),
ha='right',
textcoords='offset points',
fontsize=16)
else:
self.cs_ax.scatter(X + offset, Y, marker='_', s=150, c='k')
self.cs_ax.annotate(s='{}'.format(int(text)),
xy=(X, Y),
xytext=(45, -5),
ha='right',
textcoords='offset points',
fontsize=16)
if invert:
pf.invert(self.cs_ax, fig)
def _boundval(v):
if v > 1:
v = 1
if v < 0:
v = 0
return round(v, 5)