我可以用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?
答案 0 :(得分:43)
使用UIColor
方法:
getHue:saturation:brightness: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输出:
成功:1色调:0.10,饱和度:0.79,亮度: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输出:
色调:0.10,饱和度:0.79,值:0.53
答案 1 :(得分:24)
这是使用Swift功能(扩展,计算属性和元组)在几行代码中执行相同操作的一种很好的方法。
extension UIColor {
var hsba: (h: CGFloat, s: CGFloat, b: CGFloat, a: CGFloat) {
var hsba: (h: CGFloat, s: CGFloat, b: CGFloat, a: CGFloat) = (0, 0, 0, 0)
self.getHue(&(hsba.h), saturation: &(hsba.s), brightness: &(hsba.b), alpha: &(hsba.a))
return hsba
}
}
Swift 3.2 / 4次要更新
Swift 3.2 / 4强制执行使用上一代码触发的新警告,因为您在hsba
的同一调用中多次修改getHue
变量
同时访问参数' hsba',但修改需要独占访问权限;考虑复制到局部变量。
extension UIColor {
var hsba:(h: CGFloat, s: CGFloat,b: CGFloat,a: CGFloat) {
var h: CGFloat = 0, s: CGFloat = 0, b: CGFloat = 0, a: CGFloat = 0
self.getHue(&h, saturation: &s, brightness: &b, alpha: &a)
return (h: h, s: s, b: b, a: a)
}
}
答案 2 :(得分:4)
请注意它是草稿,但如果您的版本低于5.0,则可以尝试使用此代码
...
CGFloat computedH = 0;
CGFloat computedS = 0;
CGFloat computedV = 0;
CGFloat minRGB = MIN(r, MIN(g,b));
CGFloat maxRGB = MAX(r, MAX(g,b));
if (minRGB==maxRGB) {
computedH = 0;
computedS = 0;
computedV = minRGB;
} else {
double d = (r==minRGB) ? g-b : ((b==minRGB) ? r-g : b-r);
double h = (r==minRGB) ? 3 : ((b==minRGB) ? 1 : 5);
computedH = (60*(h - d/(maxRGB - minRGB))) / 360.;
computedS = ((maxRGB - minRGB)/maxRGB);
computedV = maxRGB;
}
...
答案 3 :(得分:0)
只需使用以下代码段,即可完成rgba,hsba转换。
extension UIColor {
/**
Decomposes UIColor to its RGBA components
*/
var rgbColor: RGBColor {
get {
var red: CGFloat = 0, green: CGFloat = 0, blue: CGFloat = 0, alpha: CGFloat = 0
self.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
return RGBColor(red: red, green: green, blue: blue, alpha: alpha)
}
}
/**
Decomposes UIColor to its HSBA components
*/
var hsbColor: HSBColor {
var h: CGFloat = 0, s: CGFloat = 0, b: CGFloat = 0, a: CGFloat = 0
self.getHue(&h, saturation: &s, brightness: &b, alpha: &a)
return HSBColor(hue: h, saturation: s, brightness: b, alpha: a)
}
/**
Holds the CGFloat values of HSBA components of a color
*/
public struct HSBColor {
var hue: CGFloat
var saturation: CGFloat
var brightness: CGFloat
var alpha: CGFloat
var uiColor: UIColor {
get {
return UIColor(hue: hue, saturation: saturation, brightness: brightness, alpha: alpha)
}
}
}
/**
Holds the CGFloat values of RGBA components of a color
*/
public struct RGBColor {
var red: CGFloat
var green: CGFloat
var blue: CGFloat
var alpha: CGFloat
var uiColor: UIColor {
get {
return UIColor(red: red, green: green, blue: blue, alpha: alpha)
}
}
}
}