Plot a gamut in cie1931 colour space Python 2.7

Viewed 1969

Gamut I want to plot in CIE1931 space: https://www.google.co.uk/search?biw=1337&bih=1257&tbm=isch&sa=1&ei=9x3kW7rqBo3ygQb-8aWYBw&q=viewpixx+gamut&oq=viewpixx+gamut&gs_l=img.3...2319.2828.0.3036.5.5.0.0.0.0.76.270.5.5.0....0...1c.1.64.img..0.0.0....0.KT8w80tcZik#imgrc=77Ufw31S6UVlYM

I want to create a triangle plot of the ciexyY colours within the these coordinates: (.119,.113),(.162,.723),(.695,.304) as in the image - with a set luminance Y at 30.0.

I have created a 3d array of xy values between 0-1. I then created a matrix with 1s inside the triangle and 0s outside the triangle. I multiplied the triangle matrix by the xyY ndarray. Then I looped through the xyY ndarray and converted xyY values to rgb, and displayed them.

The result is somewhat close but not correct. I think the error is in the last section when I convert to rgb, but I'm not sure why. This is the current image: https://imgur.com/a/7cWY0FI. Any recommendations would be really appreciated.

from __future__ import division
import numpy as np
from colormath.color_objects import sRGBColor, xyYColor
from colormath.color_conversions import convert_color
import matplotlib.pyplot as plt

def frange(x,y,jump):
    while x < y:
        yield x
        x += jump

def onSameSide(p1,p2, A,B):
    cp1 = np.cross(B-A, p1-A)
    cp2 = np.cross(B-A, p2-A)
    if(np.dot(cp1, cp2) >= 0):
        return True
    else:
        return False

def isPointInTriangle(p,A,B,C):
    if(onSameSide(p,A,B,C) and onSameSide(p,B,A,C) and onSameSide(p,C,A,B)):
        return True
    else:
        return False

xlen = 400
ylen = 400

#CIExyY colour space
#Make an array (1,1,3) with each plane representing how x,y,Y vary in the coordinate space
ciexyY = np.zeros((3,xlen,ylen))
ciexyY[2,:,:]=30.0
for x in frange(0,1,1/xlen):
    ciexyY[0,:,int(xlen*x)]=x
    for y in frange(0,1,1/xlen):
        ciexyY[1,int(ylen*y),:]=y

#coordinates from Viewpixx gamut, scaled up to 100
blue=np.array((.119,.113,30.0))
green=np.array((.162,.723,30.0))
red=np.array((.695,.304,30.0))
#scale up to size of image
blue = np.multiply(blue,xlen)
green = np.multiply(green,xlen)
red = np.multiply(red,xlen)

#make an array of zeros and ones to plot the shape of Viewpixx triangle
triangleZeros = np.zeros((xlen,ylen))
for x in frange(0,xlen,1):
    for y in frange(0,ylen,1):
        if(isPointInTriangle((x,y,0),blue,green,red)):
            triangleZeros[x,y]=1
        else:
            triangleZeros[x,y]=0

#cieTriangle
cieTriangle = np.multiply(ciexyY,triangleZeros)

#convert cieTriangle xyY to rgb
rgbTriangle = np.zeros((3,xlen,ylen))
for x in frange(0,xlen,1):
    for y in range(0,ylen,1):
        xyYcolour = xyYColor(cieTriangle[0,x,y],cieTriangle[1,x,y],cieTriangle[2,x,y])
        rgbColour = convert_color(xyYcolour,sRGBColor)
        rgbTriangle[0,x,y] = rgbColour.rgb_r
        rgbTriangle[1,x,y] = rgbColour.rgb_g
        rgbTriangle[2,x,y] = rgbColour.rgb_b

rgbTriangle = np.transpose(rgbTriangle)
plt.imshow(rgbTriangle)
plt.show()
2 Answers

We have all the common Chromaticity Diagrams in Colour, I would recommend it over python-colormath because Colour is vectorised and thus much faster.

Do you have a render of your current image to share though?

from colour.plotting import plot_chromaticity_diagram_CIE1931
plot_chromaticity_diagram_CIE1931()
Related