Search code examples
colorsprocessing

Unexpected result in color conversion from LAB to RGB


My goal is to create high-resolution images of color charts in LAB. I'm a beginner in programming and I use Processing because that's the best I know to do it. However, it works only in RGB or HSB so I have to convert LAB to RGB in order to display it.

I used the formulas found on the web (LAB to XYZ and XYZ to RGB) I included them in my code, then I use a "for" loop to determine the color for each pixel.

I saw a few topics on color conversion, but I'm kind of stuck as I don't know where my problem is coming from ....

So here it is: for a fixed value of L = 100, everything is working perfectly, I'm getting this image as expected : https://drive.google.com/file/d/0ByjuuWpChE01X3otSFRQNFUyVjA/edit?usp=sharing

But when I try to make another image for a fixed value of a = 0, I get a horizontal line in the bottom, as if there was a problem with lower values of L ... here it is : https://drive.google.com/file/d/0ByjuuWpChE01RzJWUVZnR2U3VW8/edit?usp=sharing

Here is my code, I hope it will be clear, ask me if you need anything, and thank you very much for your help.

// 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 !");
}

Solution

  • Ok I found out !

    The problem was dividing by integers.

    I don't know if it works like that in other languages, but in processing if you write x = 2/5 the result will be x = 0 instead of x = 0.4 ; it's because with the denominator being an integer, the result will always be an integer .... so x = 2/5.0 will give x = 0.4 !

    I had to put a ".0" after every integer dividing, and turn to float any integer data that would divide.

    The result is perfect, no more problems !

    https://github.com/processing/processing/wiki/Troubleshooting#Why_does_2_.2F_5_.3D_0_instead_of_0.4.3F