3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

sRGBの値をxy色度図にプロットする(Python)

Last updated at Posted at 2021-09-04

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)

実行結果

xy_sRGB.png

参考

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?