gpt4 book ai didi

iphone - 如何从 UIColor 获取色调、饱和度和亮度?

转载 作者:行者123 更新时间:2023-12-03 20:21:09 26 4
gpt4 key购买 nike

我想从 UIColor 获取各个值。不确定该怎么做?

最佳答案

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 & magenta
else if( g == max )
*h = 2 + ( b - r ) / delta; // between cyan & yellow
else
*h = 4 + ( r - g ) / delta; // between magenta & cyan
*h *= 60; // degrees
if( *h < 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, &h, &s, &v);

上面的代码假设 UIColor 是在 RGB 空间中设置的(这是典型的)。如果颜色位于另一个颜色空间中,它将崩溃和/或具有未定义的行为。

iOS 5.0 有 - (BOOL)getHue:(CGFloat *)hue 饱和度:(CGFloat *)saturation 亮度:(CGFloat *)brightness alpha:(CGFloat *)alpha 完成所有这些工作为你。但目前还无法使用。

关于iphone - 如何从 UIColor 获取色调、饱和度和亮度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5284427/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com