Created
June 4, 2024 02:39
-
-
Save morgan9e/1ffebed8b1f757cee0fa4e884008b756 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import matplotlib.pyplot as plt | |
def rgb(wavelength): | |
if 645 <= wavelength <= 780: | |
R, G, B = 1.0, 0.0, 0.0 | |
elif 580 <= wavelength <= 645: | |
R, G, B = 1.0, -(wavelength - 645) / (645 - 580), 0.0 | |
elif 510 <= wavelength <= 580: | |
R, G, B = (wavelength - 510) / (580 - 510), 1.0, 0.0 | |
elif 490 <= wavelength <= 510: | |
R, G, B = 0.0, 1.0, -(wavelength - 510) / (510 - 490) | |
elif 440 <= wavelength <= 490: | |
R, G, B = 0.0, (wavelength - 440) / (490 - 440), 1.0 | |
elif 380 <= wavelength <= 440: | |
R, G, B = -(wavelength - 440) / (440 - 380), 0.0, 1.0 | |
else: | |
R = G = B = 0.0 | |
return (R, G, B) | |
def factor(wavelength): | |
if 700 <= wavelength <= 780: | |
factor = 0.3 + 0.7 * (780 - wavelength) / (780 - 700) | |
elif 420 <= wavelength <= 700: | |
factor = 1.0 | |
elif 380 <= wavelength <= 420: | |
factor = 0.3 + 0.7 * (wavelength - 380) / (420 - 380) | |
else: | |
factor = 0.0 | |
return factor | |
wavelengths = range(380, 780) | |
colors = [tuple(map(lambda x: x * factor(w), rgb(w))) for w in wavelengths] | |
fig, ax = plt.subplots(figsize=(10, 2)) | |
for i, color in enumerate(colors): | |
ax.add_patch(plt.Rectangle((i, 0), 1, 1, color=color)) | |
ax.set_xlim(0, len(colors)) | |
ax.set_ylim(0, 1) | |
# ax.axis('off') | |
ax.set_xticks(range(0, len(colors) + 1, 50)) | |
ax.set_xticklabels(range(wavelengths[0], wavelengths[-1] + 1, 50)) | |
ax.set_yticks([]) | |
ax.tick_params(axis='x', which='both', bottom=False, top=False) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment