Metadata-Version: 2.1
Name: coloured-spectra
Version: 0.0.2
Summary: Got a spectra? Use this to find what is the observed colour.
Home-page: https://github.com/YiweiMao/coloured_spectra/tree/master/
Author: Yiwei Mao
Author-email: ewaymao@gmail.com
License: Creative Commons Attribution 3.0 Australia License
Keywords: spectral spectra spectrum colour color coloured colored
Platform: UNKNOWN
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: Other/Proprietary License
Classifier: Natural Language :: English
Classifier: Programming Language :: Python :: 3.6
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Requires-Python: >=3.6
Description-Content-Type: text/markdown

# coloured_spectra
> Got a spectra? Use this to find the observed colour!


Works great when applied to a single spectra. Using this for hyperspectral though... this Python implementation is too slow to be practical. (Can be magnitudes faster if coded in Julia)

Documentation site: [https://yiweimao.github.io/coloured_spectra/](https://yiweimao.github.io/coloured_spectra/)

## Install

`pip install coloured_spectra`

## Examples

Find the observed colour of a blackbody at 6500 K.

```python
from coloured_spectra import *
import numpy as np
import pickle
import matplotlib.pyplot as plt
import cv2

```

```python
bb = Blackbody(5778)
bb.plot()
```


![png](docs/images/output_6_0.png)


```python
def show_blackbody_colour(T_K):
    bb = Blackbody(T_K,np.linspace(380,750))

    sRGB = spectra2sRGB(bb.λ_nm,bb.B_λT)

    # Due to normalisation choices, the brightness can change depending on the spectra
    # show the colour at max brightness
    HSV = cv2.cvtColor(np.reshape(sRGB,(1,1,3)), cv2.COLOR_RGB2HSV_FULL)
    HSV[0,0,2] = 255
    RGB = cv2.cvtColor(HSV, cv2.COLOR_HSV2RGB_FULL)

    plot_colour(RGB)
```

This approximates the colour of the Sun with a blackbody temperature of 5778 K.

```python
show_blackbody_colour(5778)
```


![png](docs/images/output_9_0.png)


```python
show_blackbody_colour(11000) # what about the star Rigel?
```


![png](docs/images/output_10_0.png)


If you have a hyperspectral line, you can colour it in by the observed colour. For this, the invisible UV and NIR is painted white on top of a black background. This look up table spectrum is:

```python
plot_hsv_LUT_spectrum()
```


![png](docs/images/output_12_0.png)


```python
# load a hyperspectral line (imaged HgAr lamp)
with open('../wave_cal.pkl','rb') as handle:
    img = np.float32(np.rot90(pickle.load(handle),1))

    # get rid of sensor abnormalities
    img[669,467] = img[670,467]
    img[587,1174] = img[588,1174]

    img /= np.max(img)

plt.imshow(img,cmap='gray',extent=[400,850,0,np.shape(img)[0]])
plt.xlabel("wavelengths (nm)")
plt.ylabel("cross-track")
plt.colorbar()
```




    <matplotlib.colorbar.Colorbar at 0x118865c10>




![png](docs/images/output_13_1.png)


You can see the emission lines have been coloured.

```python
colour_hyperspectral_line(np.linspace(400,850,2064),img)
```

    100%|██████████| 772/772 [00:41<00:00, 18.53it/s]



![png](docs/images/output_15_1.png)


You can also return a column of observed colour given a hyperspectral line

```python
hyperspec_line2colour(np.linspace(400,850,2064),img)
```

    100%|██████████| 772/772 [01:07<00:00, 11.46it/s]



![png](docs/images/output_17_1.png)



