是否有将UIColor转换为色相饱和度亮度的功能? [英] Is there function to convert UIColor to Hue Saturation Brightness?

查看:143
本文介绍了是否有将UIColor转换为色相饱和度亮度的功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以用RGB值设置uicolor:

i can set uicolor with RGB values:

[UIColor colorWithRed:0.53 green:0.37 blue:0.11 alpha:1.00];

我可以用hsb值设置uicolor:

i can set uicolor with hsb values:

[UIColor colorWithHue:0.10 saturation:0.16 brightness:0.13 alpha:1.00];

我也可以将它转换回RGB:

i also could convert it back to RGB:

CGFloat* colors = CGColorGetComponents(Color1.CGColor);

但我如何从uicolor获得HSB?

But how i can get HSB from uicolor?

推荐答案

使用 UIColor 方法:
getHue:saturation:brightness:alpha:

来自Apple文档:

返回组件弥补HSB颜色空间中的颜色。

From the Apple docs:
"Returns the components that make up the color in the HSB color space."

- (BOOL)getHue:(CGFloat *)hue saturation:(CGFloat *)saturation brightness:(CGFloat *)brightness alpha:(CGFloat *)alpha

示例:

UIColor *testColor = [UIColor colorWithRed:0.53 green:0.37 blue:0.11 alpha:1.00];

CGFloat hue;
CGFloat saturation;
CGFloat brightness;
CGFloat alpha;
BOOL success = [testColor getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha];
NSLog(@"success: %i hue: %0.2f, saturation: %0.2f, brightness: %0.2f, alpha: %0.2f", success, hue, saturation, brightness, alpha);

NSLog输出:


成功:1色调:0.10,饱和度:0.79,亮度:0.53,alpha:1.00

success: 1 hue: 0.10, saturation: 0.79, brightness: 0.53, alpha: 1.00

这是更正后的版本@WhiteTiger提供的方法:

Here is a corrected version of the method provided by @WhiteTiger:

// Test values
CGFloat red = 0.53;
CGFloat green = 0.37;
CGFloat blue = 0.11;

CGFloat hue = 0;
CGFloat saturation = 0;
CGFloat brightness = 0;

CGFloat minRGB = MIN(red, MIN(green,blue));
CGFloat maxRGB = MAX(red, MAX(green,blue));

if (minRGB==maxRGB) {
    hue = 0;
    saturation = 0;
    brightness = minRGB;
} else {
    CGFloat d = (red==minRGB) ? green-blue : ((blue==minRGB) ? red-green : blue-red);
    CGFloat h = (red==minRGB) ? 3 : ((blue==minRGB) ? 1 : 5);
    hue = (h - d/(maxRGB - minRGB)) / 6.0;
    saturation = (maxRGB - minRGB)/maxRGB;
    brightness = maxRGB;
}
NSLog(@"hue: %0.2f, saturation: %0.2f, value: %0.2f", hue, saturation, brightness);

NSLog输出:


色调:0.10,饱和度:0.79,值:0.53

hue: 0.10, saturation: 0.79, value: 0.53

这篇关于是否有将UIColor转换为色相饱和度亮度的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆