Plot a gamut in cie1931 colour space Python 2.7










1















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()









share|improve this question




























    1















    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()









    share|improve this question


























      1












      1








      1








      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()









      share|improve this question
















      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()






      python python-2.7 color-space






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 '18 at 14:27







      Mubaraka

















      asked Nov 8 '18 at 11:31









      MubarakaMubaraka

      62




      62






















          1 Answer
          1






          active

          oldest

          votes


















          0














          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?






          share|improve this answer























          • A link to the current image imgur.com/a/7cWY0FI . I am struggling to use the chromaticity diagrams in colour.plotting, because for my program I need to manipulate the diagram and track the colour values of each pixel. I have tried using Colour to convert from xy to XYZ to sRGB but the output RGB values are outside of the 0-1 range and I don't know why.

            – Mubaraka
            Nov 12 '18 at 15:05










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206892%2fplot-a-gamut-in-cie1931-colour-space-python-2-7%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          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?






          share|improve this answer























          • A link to the current image imgur.com/a/7cWY0FI . I am struggling to use the chromaticity diagrams in colour.plotting, because for my program I need to manipulate the diagram and track the colour values of each pixel. I have tried using Colour to convert from xy to XYZ to sRGB but the output RGB values are outside of the 0-1 range and I don't know why.

            – Mubaraka
            Nov 12 '18 at 15:05















          0














          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?






          share|improve this answer























          • A link to the current image imgur.com/a/7cWY0FI . I am struggling to use the chromaticity diagrams in colour.plotting, because for my program I need to manipulate the diagram and track the colour values of each pixel. I have tried using Colour to convert from xy to XYZ to sRGB but the output RGB values are outside of the 0-1 range and I don't know why.

            – Mubaraka
            Nov 12 '18 at 15:05













          0












          0








          0







          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?






          share|improve this answer













          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?







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 11 '18 at 1:47









          Kel SolaarKel Solaar

          1,45511416




          1,45511416












          • A link to the current image imgur.com/a/7cWY0FI . I am struggling to use the chromaticity diagrams in colour.plotting, because for my program I need to manipulate the diagram and track the colour values of each pixel. I have tried using Colour to convert from xy to XYZ to sRGB but the output RGB values are outside of the 0-1 range and I don't know why.

            – Mubaraka
            Nov 12 '18 at 15:05

















          • A link to the current image imgur.com/a/7cWY0FI . I am struggling to use the chromaticity diagrams in colour.plotting, because for my program I need to manipulate the diagram and track the colour values of each pixel. I have tried using Colour to convert from xy to XYZ to sRGB but the output RGB values are outside of the 0-1 range and I don't know why.

            – Mubaraka
            Nov 12 '18 at 15:05
















          A link to the current image imgur.com/a/7cWY0FI . I am struggling to use the chromaticity diagrams in colour.plotting, because for my program I need to manipulate the diagram and track the colour values of each pixel. I have tried using Colour to convert from xy to XYZ to sRGB but the output RGB values are outside of the 0-1 range and I don't know why.

          – Mubaraka
          Nov 12 '18 at 15:05





          A link to the current image imgur.com/a/7cWY0FI . I am struggling to use the chromaticity diagrams in colour.plotting, because for my program I need to manipulate the diagram and track the colour values of each pixel. I have tried using Colour to convert from xy to XYZ to sRGB but the output RGB values are outside of the 0-1 range and I don't know why.

          – Mubaraka
          Nov 12 '18 at 15:05

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206892%2fplot-a-gamut-in-cie1931-colour-space-python-2-7%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

          Crossroads (UK TV series)

          ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế