# Color Simulation of Multilayered Thin Films Using Python
## 2 Materials and Methods
### Python Coding
(4)
2 defreflection_p(N_j,N_k,ang_j,ang_k):
3r=((N_k+np.cos(ang_j)-N_j+np.cos(ang_k))
4/(N_k+np.cos(ang_j)+N_j+np.cos(ang_k)))
5 returnr
6#Eq. (8)
7 defreflection_s(N_j,N_k,ang_j,ang_k):
8r=((N_j+np.cos(ang_j)-N_k+np.cos(ang_k))
9/(N_j+np.cos(ang_j)+N_k+np.cos(ang_k)))
10 returnr
11 def reflectance(w1,N_0,N_1,N_2,N_3,d_1,d_2,angle):
12#SnelL'sLow
13 ang_0=angle*np.pi/180
14 ang_1=np.arcsin(N_0/N_1+np.sin(ang_0))
15 ang_2=np.arcsin(N_0/N_2+np.sin(ang_0))
16 ang_3=np.arcsin(N_0/N_3+np.sin(ang_0))
17#Eq. (20)
18 alpha_1=4*np.pi*d_1+N_1+np.cos(ang_1)/w1
19 alpha_2=4*np.pi*d_2+N_2+np.cos(ang_2)/w1
20#Reflectioncoefficientofp-polarizedwaveatechinterface
21r_01_p=reflection_p(N_0,N_1,ang_0,ang_1)
22r_12_p=reflection_p(N_1,N_2,ang_1,ang_2)
23r_23_p=reflection_p(N_2,N_3,ang_2,ang_3)
24#Eq. (26)
25r_123_p=((r_12_p+r_23_p+np.exp(-j+alpha_2)))
26/(1+r_12_p+r_23_p+np.exp(-j+alpha_2)))
27#Eq. (27)
28r_0123_p=((r_01_p+r_123_p+np.exp(-j+alpha_1))
29/(1+r_01_p+r_123_p+np.exp(-j+alpha_1)))
30#Reflectioncoefficientofs-polarizedwaveatechinterface
31r_01_s=reflection_s(N_0,N_1,ang_0,ang_1)
32r_12_s=reflection_s(N_1,N_2,ang_1,ang_2)
33r_23_s=reflection_s(N_2,N_3,ang_2,ang_3)
34#Eq. (26)
35r_123_s=((r_12_s+r_23_s+np.exp(-j+alpha_2))
36/(1+r_12_s+r_23_s+np.exp(-j+alpha_2)))
37#Eq. (27)
38r_0123_s=((r_01_s+r_123_s+np.exp(-j+alpha_1))
39/(1+r_01_s+r_123_s+np.exp(-j+alpha_1)))
40#Eq. (10)
41R_p=np.abs(r_0123_p)**2
42R_s=np.abs(r_0123_s)**2
43#Eq. (12)
44R_n=(R_p+R_s)/2
45 returnR_n
```  
**Listing 2** Python code used to calculate the reflection coefficients and the reflectance. By modifying the values of d_1, d_2, and the angle in line 11 of Listing 2, we can obtain reflectance spectra for various thicknesses and incident ray angles. To convert the reflectance spectra into RGB values, we apply Listing 3; in line 1, R_n is the calculated reflectance spectrum, while D65, CMFs_X, CMFs_Y, and CMFs_Z are the standard illuminant and the color matching functions for X, Y, and Z, respectively, which were imported and defined earlier (in line 38-42 of Listing 1). ```
1 defreflectance2 RG8(R_n,CMFs_X,CMFs_Y,CMFs_Z,D65):
2#ReflectancetoCIEXYZ
3#[[http://www.bucelindbloom.com/index.html?Eqn_XYZ_to_RGB.html](http://www.bucelindbloom.com/index.html?Eqn_XYZ_to_RGB.html)]([http://www.bucelindbloom.com/index.html?Eqn_XYZ_to_RGB.html](http://www.bucelindbloom.com/index.html?Eqn_XYZ_to_RGB.html))
4#Eq. (28)
5N=sum(CMFs_Y*D65)
6#Eq. (29)
7X=sum(CMFs_X*D65*R_n)/N
8#Eq. (30)
9Y=sum(CMFs_Y*D65*R_n)/N
10#Eq. (31)
11Z=sum(CMFs_Z*D65*R_n)/N
12 XYZ=np.array([X,Y,Z])
13#RGBworkingspace:sRGB/referencewhiteD65
14#XYZtoRGBmatrix
15#[[http://www.bucelindbloom.com/index.html?Eqn_XYZ_to_RGB.html](http://www.bucelindbloom.com/index.html?Eqn_XYZ_to_RGB.html)]([http://www.bucelindbloom.com/index.html?Eqn_XYZ_to_RGB.html](http://www.bucelindbloom.com/index.html?Eqn_XYZ_to_RGB.html))
16m=np.array([[3.24045,-1.53714,-0.49853],
17[-0.96927,1.87601,0.04156],
18[0.05564,-0.20403,1.05723]])
19#Eq. (34)
20 rgb_linear=np.dot(m,XYZ)
21 RGB=np.zeros(3)
22#Eq. (35)
23#LinearRGBstostoRG
24 defgamma_correct(c):
25 return12.92*cifc<=0.0831308 else1.955*(abs(c)**(1/2.4))-0.055
26
27 rgb=[gamma_correct(c)forcinrgb_linear]
28 returnnp.round(np.clip(np.array(rgb)+255,0,255)).astype(int)
```  
Listing 3: Python code used to convert the reflectance spectrum to RGB values.