# Color Simulation of Multilayered Thin Films Using Python
## 2 Materials and Methods
### Python Coding  
Based on the aforementioned theoretical background, we developed a Python 3.9.13 code to simulate the colors of multilayered thin films. We imported libraries--NumPy, pandas, SciPy, and matplotlib--for numerical computations, data manipulation, interpolation, and visualization, respectively. A wavelength array in the visible range with a fixed step size was generated using the _arange_ function in the NumPy library (line 2 in Listing 1). To use the dispersion relation of each layer, we needed to import the refractive indices (\(n\)) and extinction coefficients (\(k\)) of the wavelengths in the array (lines 4-10 in Listing 1). Using this code, we aimed to simulate the color of SnO\({}_{2}\) layers on a Si substrate, which has a native oxide layer; thus we used the \(n\) and \(k\) values of SnO\({}_{2}\), SiO\({}_{2}\), and Si from the literature [31; 32; 33; 34]. We imported a standard illuminant (D65) and applied it to the obtained reflectance spectra to simulate the spectra from the sample, and the color-matching functions (CMFs) were imported and applied to extract RGB values from the simulated spectra (lines 35-42 in Listing 1). Here, missing \(n\), \(k\), and CMFs values, which are required but absent in the literature, can be estimated by interpolation using the _interpId_ function in the SciPy library. Since both the extinction coefficients and dispersion of air in the visible range are negligible, we set the refractive index of air as the constant, 1.0003, and defined the imaginary unit as \(j\) (lines 12-15 in Listing 1). ```
1#Standardwavelength
2 wl=np.arange(380,750.1,1)
3
4 defload_refractive_index(filename):
5 data=pd.read_csv(filename)
6 returndata.iloc[:,0].values,data.iloc[:,1].values
7
8 defload_complex_index(filename):
9 data=pd.read_csv(filename)
10 returndata.iloc[:,0].values,data.iloc[:,1].values,data.iloc[:,2].values
11
12#Airn,k
13N_air=complex(1.0003,-0)
14#imagemoryunit
15j=complex(0,1)
16
17#SnO2
18 wl_sno2_n,n_sno2=load_refractive_index('SnO2n.csv')
19 wl_sno2_k,k_sno2=load_refractive_index('SnO2k.csv')
20n_sno2_interp=interpolate_property(wl_sno2_n,n_sno2,wl)
21k_sno2_interp=interpolate_property(wl_sno2_k,k_sno2,wl)
22N_sno2=n_sno2_interp-1j^k_sno2_interp
23
24#5102
25 wl_sio2_n,n_sio2=load_refractive_index('SiO2n.csv')
26n_sio2_interp=interpolate_property(wl_sio2_n,n_sio2,wl)
27N_sio2=n_sio2_interp-1j^0##Noabsorption
28
29#S4
30 wl_si,n_si,k_si=load_complex_index('Sink.csv')
31n_si_interp=interpolate_property(wl_si,n_si,wl)
32k_si_interp=interpolate_property(wl_si,k_si,wl)
33N_si=n_si_interp-1j^k_si_interp
34
35#[[http://www.ntheiss.com/docs/code/?bb_d65_spectrum.htm](http://www.ntheiss.com/docs/code/?bb_d65_spectrum.htm)]([http://www.ntheiss.com/docs/code/?bb_d65_spectrum.htm](http://www.ntheiss.com/docs/code/?bb_d65_spectrum.htm))
36D65_spec=pd.read_csv(\"D65_step_1.csv\")
37D65=D65_spec.iloc[0:371,1].values
38#[[http://cvrl.ucl.ac.uk/](http://cvrl.ucl.ac.uk/)]([http://cvrl.ucl.ac.uk/](http://cvrl.ucl.ac.uk/))
39#CMFs->CIF19312-deg,XYZCMFs
40 cie_data=pd.read_csv('CIE_cc_19312_deg_cvrl.csv')
41 cie_wavelength=cie_data.iloc[:,0]
42 CMFs_X_b=cie_data.iloc[:,1];CMFs_Y_b=cie_data.iloc[:,2];CMFs_Z_b=cie_data.iloc[:,3]
```  
Listing 1: Python code for data preprocessing. The reflection coefficients of the \(p\)- and \(s\)-polarized waves and the reflectance are calculated in lines 1-10 and lines 11-45 of Listing 2, respectively. Here, N_j and N_k indicate the complex refractive indices of media j and k, respectively. ang_j and ang_k are the angles of incidence and transmission, respectively. In line 11 of Listing 2, wl, N_0, N_1, N_2, and N_3 are the wavelength and the complex refractive indices of the atmosphere (i.e., air), top, middle, and bottom layers (here the bottom layer corresponds to the substrate), respectively. d_1, and d_2 are the thicknesses of the top and middle layers, respectively. The mathematical expression corresponding to each line of code is given above it using the equation numbers described in Section 2.1 (Theoretical Background). ```
1#Eq.