forked from BrandonSmithJ/MDN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspectrum_rgb.py
76 lines (69 loc) · 1.71 KB
/
spectrum_rgb.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
"""
Based on
http://www.physics.sfasu.edu/astro/color/spectra.html
RGB VALUES FOR VISIBLE WAVELENGTHS by Dan Bruton ([email protected])
"""
import numpy as np
select=np.select
power=np.power
transpose=np.transpose
arange=np.arange
def factor(wl):
return select(
[ wl > 700.,
wl < 420.,
True ],
[ .3+.7*(780.-wl)/(780.-700.),
.3+.7*(wl-380.)/(420.-380.),
1.0 ] )
def raw_r(wl):
return select(
[ wl >= 580.,
wl >= 510.,
wl >= 440.,
wl >= 380.,
True ],
[ 1.0,
(wl-510.)/(580.-510.),
0.0,
(wl-440.)/(380.-440.),
0.0 ] )
def raw_g(wl):
return select(
[ wl >= 645.,
wl >= 580.,
wl >= 490.,
wl >= 440.,
True ],
[ 0.0,
(wl-645.)/(580.-645.),
1.0,
(wl-440.)/(490.-440.),
0.0 ] )
def raw_b(wl):
return select(
[ wl >= 510.,
wl >= 490.,
wl >= 380.,
True ],
[ 0.0,
(wl-510.)/(490.-510.),
1.0,
0.0 ] )
gamma = 0.80
def correct_r(wl):
return power(factor(wl)*raw_r(wl),gamma)
def correct_g(wl):
return power(factor(wl)*raw_g(wl),gamma)
def correct_b(wl):
return power(factor(wl)*raw_b(wl),gamma)
ww=arange(380.,781.)
def get_spectrum_cmap():
''' use like:
cmap = get_spectrum_cmap()
plot(..., color=[cmap.to_rgba(nm) for nm in wavelengths])
'''
from matplotlib import colors, cm
cmap = transpose([correct_r(ww),correct_g(ww),correct_b(ww)])
return cm.ScalarMappable(norm=colors.Normalize(vmin=380, vmax=780),
cmap=colors.ListedColormap(cmap))