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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10071756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:46:18  来源:igfitidea点击:

Is there function to convert UIColor to Hue Saturation Brightness?

iosuicolorhsb

提问by Horhe Garcia

i can set uicolor with RGB values:

我可以用 RGB 值设置 uicolor:

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

i can set uicolor with hsb values:

我可以使用 hsb 值设置 uicolor:

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

i also could convert it back to RGB:

我也可以将其转换回 RGB:

CGFloat* colors = CGColorGetComponents(Color1.CGColor);

But how i can get HSB from uicolor?

但是我如何从 uicolor 获得 HSB?

回答by zaph

Use the UIColormethod: getHue:saturation:brightness:alpha:

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

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

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

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

Example:

例子:

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 output:

NSLog 输出:

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

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

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

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

NSLog 输出:

hue: 0.10, saturation: 0.79, value: 0.53

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

回答by apouche

Here is a nice way of using Swift features (extensions, computed properties and tuples) to do the same thing in just a few lines of code.

这是使用 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 minor update

Swift 3.2 / 4 小更新

Swift 3.2 / 4 enforced a new warning triggered with the previous code because you were modifying the hsbavariable several times within the same call to getHue

Swift 3.2 / 4 强制执行了一个由先前代码触发的新警告,因为您hsba在同一次调用中多次修改变量getHue

Simultaneous accesses to parameter 'hsba', but modification requires exclusive access; consider copying to a local variable.

同时访问参数'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)
    }
 }

回答by WhiteTiger

Beware that it is a draft, but you can try this code, if you're lower to version 5.0

注意这是一个草稿,但你可以试试这个代码,如果你低于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;
}
...

回答by Jiachen Ren

Just use the following segment of code and you are done for good with rgba, hsba conversion.

只需使用以下代码段,即可完成 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)
        }
    }
}
}