gpt4 book ai didi

ios - 是否有将 UIColor 转换为 Hue Saturation Brightness 的功能?

转载 作者:IT王子 更新时间:2023-10-29 08:16:42 25 4
gpt4 key购买 nike

我可以用 RGB 值设置 uicolor:

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

我可以用 hsb 值设置 uicolor:

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

我也可以将它转换回 RGB:

CGFloat* colors = CGColorGetComponents(Color1.CGColor);

但是我怎样才能从 uicolor 获得 HSB 呢?

最佳答案

使用 UIColor 方法:getHue:饱和度:亮度:alpha:

来自 Apple 文档:
“返回构成 HSB 颜色空间中颜色的分量。”

- (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 输出:

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

这是@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 输出:

hue: 0.10, saturation: 0.79, value: 0.53

关于ios - 是否有将 UIColor 转换为 Hue Saturation Brightness 的功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10071756/

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