Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<pre><code>static void RGBtoHSV( float r, float g, float b, float *h, float *s, float *v ) { float min, max, delta; min = MIN( r, MIN( g, b )); max = MAX( r, MAX( g, b )); *v = max; // v delta = max - min; if( max != 0 ) *s = delta / max; // s else { // r = g = b = 0 // s = 0, v is undefined *s = 0; *h = -1; return; } if( r == max ) *h = ( g - b ) / delta; // between yellow &amp; magenta else if( g == max ) *h = 2 + ( b - r ) / delta; // between cyan &amp; yellow else *h = 4 + ( r - g ) / delta; // between magenta &amp; cyan *h *= 60; // degrees if( *h &lt; 0 ) *h += 360; } static void HSVtoRGB( float *r, float *g, float *b, float h, float s, float v ) { int i; float f, p, q, t; if( s == 0 ) { // achromatic (grey) *r = *g = *b = v; return; } h /= 60; // sector 0 to 5 i = floor( h ); f = h - i; // factorial part of h p = v * ( 1 - s ); q = v * ( 1 - s * f ); t = v * ( 1 - s * ( 1 - f ) ); switch( i ) { case 0: *r = v; *g = t; *b = p; break; case 1: *r = q; *g = v; *b = p; break; case 2: *r = p; *g = v; *b = t; break; case 3: *r = p; *g = q; *b = v; break; case 4: *r = t; *g = p; *b = v; break; default: // case 5: *r = v; *g = p; *b = q; break; } } ... CGFloat r, g, b, a, h, s, v; const CGFloat *comp = CGColorGetComponents([myUIColor CGColor]); r = comp[0]; g = comp[1]; b = comp[2]; a = comp[3]; RGBtoHSV(r, g, b, &amp;h, &amp;s, &amp;v); </code></pre> <p>The code above assumes the UIColor is setup in the RGB space (which is typical). If the color is in another color space it will crash and / or have undefined behavior.</p> <p>iOS 5.0 has <code>- (BOOL)getHue:(CGFloat *)hue saturation:(CGFloat *)saturation brightness:(CGFloat *)brightness alpha:(CGFloat *)alpha</code> which does all this work for you. But it's not available yet.</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