title: How to get the real RGBA or ARGB color values without premultiplied alpha?
body: <p>I'm creating an bitmap context using <code>CGBitmapContextCreate</code> with the <code>kCGImageAlphaPremultipliedFirst</code> option.</p>
  
  <p>I made a 5 x 5 test image with some major colors (pure red, green, blue, white, black), some mixed colors (i.e. purple) combined with some alpha variations. Every time when the alpha component is not 255, the color value is wrong.</p>
  
  <p>I found that I could re-calculate the color when I do something like:</p>
  
  <pre><code>almostCorrectRed = wrongRed * (255 / alphaValue);
  almostCorrectGreen = wrongGreen * (255 / alphaValue);
  almostCorrectBlue = wrongBlue * (255 / alphaValue);
  </code></pre>
  
  <p>But the problem is, that my calculations are sometimes off by 3 or even more. So for example I get a value of 242 instead of 245 for green, and I am 100% sure that it must be exactly 245. Alpha is 128.</p>
  
  <p>Then, for the exact same color just with different alpha opacity in the PNG bitmap, I get alpha = 255 and green = 245 as it should be.</p>
  
  <p>If alpha is 0, then red, green and blue are also 0. Here all data is lost and I can't figure out the color of the pixel.</p>
  
  <p>How can I avoid or undo this alpha premultiplication alltogether so that I can modify pixels in my image based on the true R G B pixel values as they were when the image was created in Photoshop? How can I recover the original values for R, G, B and A?</p>
  
  <hr>
  
  <p>Background info (probably not necessary for this question):</p>
  
  <p><em>What I'm doing is this: I take a UIImage, draw it to a bitmap context in order to perform some simple image manipulation algorithms on it, shifting the color of each pixel depending on what color it was before. Nothing really special. But my code needs the real colors. When a pixel is transparent (meaning it has alpha less than 255) my algorithm shouldn't care about this, it should just modify R,G,B as needed while Alpha remains at whatever it is. Sometimes though it will shift alpha up or down too. But I see them as two separate things. Alpha contorls transparency, while R G B control the color.</em></p>