Colors 从实验室到RGB的颜色转换出现意外结果

Colors 从实验室到RGB的颜色转换出现意外结果,colors,processing,Colors,Processing,我的目标是在实验室里创建彩色图表的高分辨率图像。 我是编程的初学者,我使用处理,因为这是我所知道的最好的方法。但是,它只在RGB或HSB中工作,所以我必须将LAB转换为RGB才能显示它 我使用了网络上的公式(从实验室到XYZ,从XYZ到RGB) 我将它们包括在代码中,然后使用“for”循环来确定每个像素的颜色 我看到了一些关于颜色转换的话题,但我有点被卡住了,因为我不知道我的问题来自哪里 这里是:对于L=100的固定值,一切正常,我得到了预期的图像: 但当我尝试为固定值a=0制作另一个图像时,

我的目标是在实验室里创建彩色图表的高分辨率图像。 我是编程的初学者,我使用处理,因为这是我所知道的最好的方法。但是,它只在RGB或HSB中工作,所以我必须将LAB转换为RGB才能显示它

我使用了网络上的公式(从实验室到XYZ,从XYZ到RGB) 我将它们包括在代码中,然后使用“for”循环来确定每个像素的颜色

我看到了一些关于颜色转换的话题,但我有点被卡住了,因为我不知道我的问题来自哪里

这里是:对于L=100的固定值,一切正常,我得到了预期的图像:

但当我尝试为固定值a=0制作另一个图像时,我在底部得到了一条水平线,就好像L的较低值有问题一样。。。这是:

这是我的代码,我希望它会很清楚,如果你需要什么,请问我,非常感谢你的帮助

// parameters for the code execution
void setup() {
  noLoop();
  size(10,10,P2D);

  nuancier = createGraphics(taille,taille);

}

// final image file and size
PGraphics nuancier ;
int taille = 1000 ;


// Arrays for color values
float[] colorLAB = new float[3];
float[] colorXYZ = new float[3];
float[] colorRGB = new float[3];

// colors
float X;
float Y;
float Z;
float L;
float a;
float b;
float R;
float G;
float B;

// pixels
int x ;
int y ;

// function to convert Lab to XYZ
float[] LABtoXYZ() {

  L = colorLAB[0];
  a = colorLAB[1];
  b = colorLAB[2];


  float ntY = ( L + 16 ) / 116 ;
  float ntX = a / 500 + ntY ;
  float ntZ = ntY - b / 200 ;
  if ( (pow(ntY,3)) > 0.008856 ) {
    ntY = (pow(ntY,3)) ;
  } else { ntY = ( ntY - 16 / 116 ) / 7.787 ; }
  if ( (pow(ntX,3)) > 0.008856 ) {
    ntX = (pow(ntX,3)) ;
  } else { ntX = ( ntX - 16 / 116 ) / 7.787 ; }
  if ( (pow(ntZ,3)) > 0.008856 ) {
    ntZ = (pow(ntZ,3)) ;
  } else { ntZ = ( ntZ - 16 / 116 ) / 7.787 ; }
  X = 95.047 * ntX ;     //ref_X =  95.047      Observateur= 2°, Illuminant= D65
  Y = 100 * ntY ;     //ref_Y = 100.000
  Z = 108.883 * ntZ ;     //ref_Z = 108.883


  colorXYZ[0] = X ;
  colorXYZ[1] = Y ;
  colorXYZ[2] = Z ;
  return colorXYZ ;

}

// function to convert  XYZ to RGB
float[] XYZtoRGB() {

  X = colorXYZ[0];
  Y = colorXYZ[1];
  Z = colorXYZ[2];


  float ntX = X / 100 ;         //X compris entre 0 et  95.047      ( Observateur = 2°, Illuminant = D65 )
  float ntY = Y / 100 ;         //Y compris entre 0 et 100.000
  float ntZ = Z / 100 ;         //Z compris entre 0 et 108.883

  float ntR = ntX *  3.2406 + ntY * (-1.5372) + ntZ * (-0.4986) ;
  float ntG = ntX * (-0.9689) + ntY *  1.8758 + ntZ *  0.0415 ;
  float ntB = ntX *  0.0557 + ntY * (-0.2040) + ntZ *  1.0570 ;

  if ( ntR > 0.0031308 ) {
    ntR = 1.055 * ( pow(ntR,( 1 / 2.4 )) ) - 0.055 ;
  } else { ntR = 12.92 * ntR ; }
  if ( ntG > 0.0031308 ) {
    ntG = 1.055 * ( pow(ntG,( 1 / 2.4 )) ) - 0.055 ;
  } else { ntG = 12.92 * ntG ; }
  if ( ntB > 0.0031308 ) {
    ntB = 1.055 * ( pow(ntB,( 1 / 2.4 )) ) - 0.055 ;
  } else { ntB = 12.92 * ntB ; }

  R = ntR * 255 ;
  G = ntG * 255 ;
  B = ntB * 255 ;

  colorRGB[0] = R ;
  colorRGB[1] = G ;
  colorRGB[2] = B ;
  return colorRGB ;

}

// I know that with RGB, not every visible color is possible
//so I just made this quick function, to bound RGB values between 0 and 255

float[] arrondirRGB () {
  for (int i=0;i<3;i++) {
    if (colorRGB[i]>255) {
      colorRGB[i]=255 ;
    }
    if (colorRGB[i]<0) {
      colorRGB[i]=0 ;
    }
  }
  return colorRGB;
}



// operating section
void draw () {

  nuancier.beginDraw();
  nuancier.noSmooth();
  nuancier.colorMode(RGB, 255);
  nuancier.endDraw();

  for (x=0;x<taille;x++) {
    for (y=0;y<taille;y++) {

      colorLAB[0] = (((taille-y)*100)/taille) ; // --------------------------------------------------------------- valeur 100  // formule ((x*100)/taille)
      colorLAB[1] = 0 ; // ----------------------------------------------------------- valeur 0    // formule ((x*256)/taille)-127
      colorLAB[2] = (((x)*256)/taille)-127 ; // -------------------------------------------------- valeur 0    // (((taille-y)*256)/taille)-127

      println(colorLAB[0]);

      LABtoXYZ () ;
      XYZtoRGB () ;
      arrondirRGB () ;

      nuancier.beginDraw();
      nuancier.stroke (colorRGB[0],colorRGB[1],colorRGB[2]);
      nuancier.point (x,y);
      nuancier.endDraw();


    }
  }

  nuancier.save("nuancier.tiff");
  println("done !");
}
//代码执行的参数
无效设置(){
noLoop();
尺寸(10,10,P2D);
nuancier=createGraphics(taille,taille);
}
//最终图像文件和大小
努安西尔;
int taille=1000;
//颜色值数组
float[]colorLAB=新的float[3];
float[]colorXYZ=新浮点[3];
float[]colorRGB=新浮点[3];
//颜色
浮动X;
浮动Y;
浮动Z;
浮子L;
浮动a;
浮球b;
浮子R;
浮球G;
浮球B;
//像素
int x;
int-y;
//函数将实验室转换为XYZ
浮动[]LABtoXYZ(){
L=colorLAB[0];
a=色度实验室[1];
b=colorLAB[2];
浮点数ntY=(L+16)/116;
浮动ntX=a/500+ntY;
浮动ntZ=ntY-b/200;
如果((功率(ntY,3))>0.008856){
ntY=(战俘(ntY,3));
}else{ntY=(ntY-16/116)/7.787;}
如果((功率(ntX,3))>0.008856){
ntX=(pow(ntX,3));
}else{ntX=(ntX-16/116)/7.787;}
如果((功率(ntZ,3))>0.008856){
ntZ=(pow(ntZ,3));
}否则{ntZ=(ntZ-16/116)/7.787;}
X=95.047*ntX;//ref_X=95.047 Observatoreur=2°,光源=D65
Y=100*ntY;//ref_Y=100.000
Z=108.883*ntZ;//ref_Z=108.883
colorXYZ[0]=X;
colorXYZ[1]=Y;
colorXYZ[2]=Z;
返回colorXYZ;
}
//函数将XYZ转换为RGB
浮点[]XYZtoRGB(){
X=colorXYZ[0];
Y=colorXYZ[1];
Z=colorXYZ[2];
浮子ntX=X/100;//X压缩中心0 et 95.047(观测者=2°,光源=D65)
浮点ntY=Y/100;//Y压缩中心0和100.000
浮动ntZ=Z/100;//Z压缩中心0 et 108.883
浮动ntR=ntX*3.2406+ntY*(-1.5372)+ntZ*(-0.4986);
浮点数ntG=ntX*(-0.9689)+ntY*1.8758+ntZ*0.0415;
浮动ntB=ntX*0.0557+ntY*(-0.2040)+ntZ*1.0570;
如果(ntR>0.0031308){
ntR=1.055*(功率(ntR,(1/2.4))-0.055;
}否则{ntR=12.92*ntR;}
如果(ntG>0.0031308){
ntG=1.055*(功率(ntG,(1/2.4))-0.055;
}否则{ntG=12.92*ntG;}
如果(ntB>0.0031308){
ntB=1.055*(功率(ntB,(1/2.4))-0.055;
}否则{ntB=12.92*ntB;}
R=ntR*255;
G=ntG*255;
B=ntB*255;
colorRGB[0]=R;
colorRGB[1]=G;
colorRGB[2]=B;
返回颜色RGB;
}
//我知道使用RGB,并非所有可见颜色都是可能的
//所以我做了一个快速函数,将RGB值绑定在0到255之间
浮点[]arndirGB(){
用于(int i=0;i255){
colorRGB[i]=255;
}
如果(colorRGB[i]行,我发现了

问题是除以整数

我不知道它在其他语言中是否也像这样工作,但是如果你写 x=2/5 结果将是x=0而不是x=0.4;这是因为分母是整数,结果将始终是整数……所以 x=2/5.0 将给出x=0.4

我必须在每一个整数除法后加一个“.0”,然后将所有要除法的整数数据都变成浮点

结果是完美的,没有更多的问题