Pythonのcolourライブラリを使用して、sRGBの値をxy色度図(chromaticity_diagram_CIE1931)にプロットする方法です。
事前準備として、次の3つのライブラリ(colour-science, numpy, matplotlib)をpipなどでインストールしておく必要があります。
ソースコード
import colour
import numpy as np
import matplotlib.pyplot as plt
colour.plotting.colour_style()
def deGumma(data):
sdata = data / 255;
if (sdata <= 0.04045):
sdata = sdata / 12.92;
else:
sdata = ((sdata + 0.055) / 1.055)**2.4
return sdata;
def scale_sRGB(sRGB):
return np.array([deGumma(sRGB[0]), deGumma(sRGB[1]), deGumma(sRGB[2])])
def plot_sRGB_to_chromaticity_diagram_CIE1931(sRGB_list):
# colour.plotting.plot_chromaticity_diagram_CIE1931(standalone=False)
colour.plotting.plot_RGB_colourspaces_in_chromaticity_diagram_CIE1931(['sRGB'], standalone=False, diagram_opacity=0.5)
for i, _ in enumerate(sRGB_list):
sRGB = scale_sRGB(sRGB_list[i])
XYZ = colour.sRGB_to_XYZ(sRGB)
xy = colour.XYZ_to_xy(XYZ)
x, y = xy
plt.plot(x, y, 'o-', color='white')
# Annotating the plot.
plt.annotate("sRGB"+str(i+1),
xy=xy,
xytext=(-50, 30),
textcoords='offset points',
arrowprops=dict(arrowstyle='->', connectionstyle='arc3, rad=-0.2'))
# Displaying the plot.
colour.plotting.render(
standalone=True,
limits=(-0.1, 0.9, -0.1, 0.9),
x_tighten=True,
y_tighten=True)
実行方法
sRGB1 = [255, 0, 0]
sRGB2 = [0, 127, 0]
sRGB3 = [0, 0, 1]
sRGB4 = [1, 1, 0]
sRGB5 = [0, 1, 1]
sRGB6 = [1, 0, 1]
sRGB_list = [sRGB1, sRGB2, sRGB3, sRGB4, sRGB5, sRGB6]
plot_sRGB_to_chromaticity_diagram_CIE1931(sRGB_list)
実行結果
参考
-
https://www.colour-science.org/
- https://colour.readthedocs.io/en/develop/colour.plotting.html
- https://colour.readthedocs.io/en/develop/generated/colour.sRGB_to_XYZ.html
- https://colour.readthedocs.io/en/develop/generated/colour.XYZ_to_xy.html
- https://colour.readthedocs.io/en/develop/tutorial.html#convert-to-chromaticity-coordinates