-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRGB_wave.py
69 lines (63 loc) · 1.6 KB
/
RGB_wave.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
# From http://hsugawa.blogspot.com/2010/01/matplotlib-colormap-for-visible.html
import numpy as np
# These functions compute the transfor from wavelength (wl) to separate R, G,
# and B values
#
# Wavelength should be in Angstroms
def factor(wl):
return np.select(
[ wl > 7000.,
wl < 4200.,
True ],
[ .3+.7*(7800.-wl)/(7800.-7000.),
.3+.7*(wl-3800.)/(4200.-3800.),
1.0 ] )
def raw_r(wl):
return np.select(
[ wl >= 5800.,
wl >= 5100.,
wl >= 4400.,
wl >= 3800.,
True ],
[ 1.0,
(wl-5100.)/(5800.-5100.),
0.0,
(wl-4400.)/(3800.-4400.),
0.0 ] )
def raw_g(wl):
return np.select(
[ wl >= 6450.,
wl >= 5800.,
wl >= 4900.,
wl >= 4400.,
True ],
[ 0.0,
(wl-6450.)/(5800.-6450.),
1.0,
(wl-4400.)/(4900.-4400.),
0.0 ] )
def raw_b(wl):
return np.select(
[ wl >= 5100.,
wl >= 4900.,
wl >= 3800.,
True ],
[ 0.0,
(wl-5100.)/(4900.-5100.),
1.0,
0.0 ] )
gamma = 0.80
def correct_r(wl):
return np.power(factor(wl)*raw_r(wl),gamma)
def correct_g(wl):
return np.power(factor(wl)*raw_g(wl),gamma)
def correct_b(wl):
return np.power(factor(wl)*raw_b(wl),gamma)
## And here's the function you actually use. Call it like this:
#
# ax.plot(x,y,color=RGB.rgb(5500))
#
# or whatever. The wavelength needs to be in Angstroms
##
def rgb(wl):
return np.transpose([correct_r(wl),correct_g(wl),correct_b(wl)])