Note that there are some explanatory texts on larger screens.

plurals
  1. POAnomalous results getting color components of some UIColors
    text
    copied!<p>I'm trying to extract the rgb components of a <code>UIColor</code> in order to hand-build the pixels in a <code>CGBitmapContext</code>. The following sample code works fine for most of the <code>UIColor</code> constants but, confusingly, not all. To wit:</p> <pre><code>CGColorRef color = [[UIColor yellowColor] CGColor]; const float* rgba = CGColorGetComponents(color); float r = rgba[0]; float g = rgba[1]; float b = rgba[2]; float a = rgba[3]; NSLog( @"r=%f g=%f b=%f a=%f", r, g, b, a); </code></pre> <p>The results for <code>[UIColor yellowColor]</code> above are<br> r=1.000000 g=1.000000 b=0.000000 a=1.000000, as expected.</p> <p><code>[UIColor redColor]</code> gives r=1.000000 g=0.000000 b=0.000000 a=1.000000, again as expected. Similarly for <code>blueColor</code> and <code>greenColor</code>.</p> <p><em>However</em>, the results for <code>[UIColor blackColor]</code> and <code>[UIColor whiteColor]</code> seem completely anomalous, and I don't know what I'm doing wrong (if indeed I am).</p> <p>To wit, <code>[UIColor blackColor]</code> gives r=0.000000 g=1.000000 b=0.000000 a=0.000000, which is a tranparent green, </p> <p>and <code>[UIColor whiteColor]</code> gives r=1.000000 g=1.000000 b=0.000000 a=0.000000, which is a transparent yellow.</p> <p>I'd appreciate it if somebody could either:</p> <p>(1) explain what I'm doing wrong<br> (2) replicate my anomalous results and tell me it's not me, or<br> (3) hit me over the head with a big hammer so it stops hurting so much.</p> <p>Howard</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload