from CieXYZ import *
from pylab import *
cie = CIEXYZ("../ciexyz/ciexyz31.txt")
figure(1)
xlabel('x')
ylabel('y')
xy=cie.xyColor()
plot(xy[0],xy[1],c='k',label='Spectrum locus')
plot([0.64,0.3,0.15,0.64],[0.33,0.6,0.06,0.33],c='r',marker='o',label='sRGB')
plot([0.3127],[0.329],c='k',marker='o',label='D65')
legend()
grid(True)
            
from XYZ2RGB import *
xw=0.3127
yw=0.329
sRGB=XYZ2RGB([0.64,0.33],[0.3,0.6],[0.15,0.06],[xw,yw])
            
RGB=sRGB.xyY2RGB(xw,yw,1)
RGB=sRGB.xyY2RGB(xw,yw,0.5)
RGB=sRGB.xyY2RGB(0.3,0.6,0.7151)
cie.readD65("../ciexyz/IlluminantD65.txt")
cie.setIlluminant("D65",0)
def Rf(L):
    if L>600:
        return 0
    else:
        return 1
XYZ=cie.spectralF2XYZ(Rf)
xy=cie.XYZ2xy(XYZ[0],XYZ[1],XYZ[2])
                
RGB=sRGB.rgb(XYZ)

def Rf(L):
    if (L>530)|(L<490):
        return 0.0
    else:
        return 1.0
XYZ=cie.spectralF2XYZ(Rf) 
xy=cie.XYZ2xy(XYZ[0],XYZ[1],XYZ[2])
RGB=sRGB.rgb(XYZ)
                
figure(1)
plot([xy[0]],[xy[1]],c='g',marker='o',label='C')
plot([xy[0],xw],[xy[1],yw],c='g',label='CW')
text(0.26,0.42,'M')
text(0.06,0.71,'C')
text(0.32,0.32,"W")
legend()
                

cie.setIlluminant("E",0)
r=[]
g=[]
b=[]
Lambda=[]
for k in range(300):
    L=400.0+float(k)
    Lambda.append(L)
    XYZ=cie.XYZMonochrome(L)
    lu=40
    XYZ[0] *= lu
    XYZ[1] *= lu
    XYZ[2] *= lu
    RGB=sRGB.rgb(XYZ)
    r.append(RGB[0])
    g.append(RGB[1])
    b.append(RGB[2])
clf()
xlabel('lambda (nm)')
plot(Lambda,r,c='r',label='r')
plot(Lambda,g,c='g',label='g')
plot(Lambda,b,c='b',label='b')
legend()
grid(True)
                
RGB=sRGB.rgbN2(XYZ)
                

im=[]
for k in range(400):
    l=float(k)/400.0;
    im.append([l,l,l])
figure(figsize=(9,2.5))
imshow([im],aspect=0.2,extent=[0,1,0,1])
xlabel('Y')
                

from math import pow
im=[]
gamma=2.2
for k in range(400):
    l=math.pow(float(k)/400.0,1/gamma);
    im.append([l,l,l])
figure(figsize=(9,2.5))
imshow([im],aspect=0.2,extent=[0,1,0,1])
xlabel('Y')
                

im=[]
x=0.25
y=0.25
for k in range(100):
    Y=float(k)*0.01
    XYZ=cie.xyY2XYZ(x,y,Y)
    RGB=sRGB.rgbN2G(XYZ)
    im.append(RGB)
figure(figsize=(9,2.5))
imshow([im],aspect=0.2,extent=[0,1,0,1])
xlabel('Y')
                

im=[]
x=0.15
y=0.7
for k in range(100):
    Lu=float(k)*0.01
    RGB=sRGB.xyL2rgb(x,y,Lu)
    im.append(RGB)
figure(figsize=(9,2.5))
imshow([im],aspect=0.2,extent=[0,1,0,1])
xlabel('Y/Ymax')
                
cie.setIlluminant("CN",6500)
im1=[]
im2=[]
for k in range(300):
    L=400.0+float(k)
    XYZ=cie.XYZMonochrome(L)
    lu=80
    XYZ[0] *= lu
    XYZ[1] *= lu
    XYZ[2] *= lu
    RGB=sRGB.rgbN2G(XYZ)
    im1.append(RGB)
    RGB=sRGB.rgbN1G(XYZ)
    im2.append(RGB)
figure(figsize=(9,2))
imshow([im1],aspect=50,extent=[400,700,0,1])
xlabel("lambda (nm)")
                

figure(figsize=(9,2))
imshow([im2],aspect=50,extent=[400,700,0,1])
xlabel("lambda (nm)")
                

figure(figsize=(8,6))
xlabel('x')
ylabel('y')
xy=cie.xyColor()
plot(xy[0],xy[1],c='k',label='Spectrum locus')
plot([0.64,0.3,0.15,0.64],[0.33,0.6,0.06,0.33],c='r',marker='o',label='sRGB')
plot([0.64,0.21,0.15,0.64],[0.33,0.71,0.06,0.33],c='b',marker='o',label='Adobe RGB')
plot([0.3127],[0.329],c='k',marker='o',label='D65')
legend()
grid(True)
            
adobeRGB=XYZ2RGB([0.64,0.33],[0.21,0.71],[0.15,0.06],[xw,yw])

cie.setIlluminant("CN",6500)
im=[]
for k in range(300):
    L=400.0+float(k)
    XYZ=cie.XYZMonochrome(L)
    lu=80
    XYZ[0] *= lu
    XYZ[1] *= lu
    XYZ[2] *= lu
    RGB=adobeRGB.rgbN2G(XYZ)
    im.append(RGB)
figure(figsize=(9,2))
imshow([im],aspect=50,extent=[400,700,0,1])
xlabel("lambda (nm)")
                
