title: Convert Lab color to RGB
body: <p>I am trying to convert a Lab color into RGB. As an example for this conversion, Photoshop 
  converts   </p>
  
  <pre><code>L= 100, a = 0, b = 127 
  </code></pre>
  
  <p>into </p>
  
  <pre><code>R=255, G=252, B=0.
  </code></pre>
  
  <p>This color is bright yellow.</p>
  
  <p>I have written the following conversion routine (C/Objective C). However, something 
  is not right. The code will output</p>
  
  <pre><code>XYZ = 100.000000 100.000000 4.862713
  RGB = 163.908401 90.755638 -13.093634
  </code></pre>
  
  <p>Here the mismatch between the the RGB-result and Photoshop's RGB seems to be quite large.
  The negative blue component is also a problem.</p>
  
  <p>Any ideas on how to correct the below code would be greatly appreciated? Or, any other algorithms/formulas/pseudocode for converting Lab into RGB? Thank you.</p>
  
  <p>The code:</p>
  
  <pre><code>/*
   *   See Wikipedia artice on Lab color space
   *
   *    http://en.wikipedia.org/wiki/Lab_color_space
   *
   */
  CGFloat fInv (CGFloat t) {
  
     CGFloat T0 = (CGFloat) 6.0f/29.0f;
  
     if (t &gt; T0) {
         return t*t*t;
     }
  
     return 3.0 * T0 * T0 * (t - 4.0f/29.0f);
  
  }
  
  
  int test() {
  
      //
      //  Bright yellow test 
      //
      CGFloat Ls = 100.0f;
      CGFloat as = 0.0f;
      CGFloat bs = 127.0f;
  
      //
      //  Reference white point (ad hoc)
      //
      CGFloat Xn = 100;
      CGFloat Yn = 100;    
      CGFloat Zn = 100;
  
      //
      //   Taken Wikipedia (see above)
      //
      CGFloat X =  Xn * fInv(1/116.0f * (Ls + 16.0f) + 1/500.0f * as);
      CGFloat Y =  Yn * fInv(1/116.0f * (Ls + 16.0f));
      CGFloat Z =  Zn * fInv(1/116.0f * (Ls + 16.0f) - 1/200.0f * bs);
  
      NSLog(@" XYZ = %f %f %f", X, Y, Z);
  
      // 
      // 
      //   XYZ to RGB transfomration using AppleRGB. Taken from 
      //
      //       http://brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html
      //
      //
      CGFloat r =  2.9515373 * X  - 1.2894116 * Y - 0.4738445 * Z;
      CGFloat g = -1.0851093 * X  + 1.9908566 * Y + 0.0372026 * Z;
      CGFloat b =  0.0854934 * X  - 0.2694964 * Y + 1.0912975 * Z;
  
      NSLog(@" RGB = %f %f %f", r, g, b);
  
   }
  </code></pre>