从 UIColor 中提取 RGB 值

Extract RGB Values From UIColor

我在 objective-c 见过有人这样做,但我在 swift 遇到了麻烦。我已经从图片中获取了像素的颜色,但现在我需要获取单独的红色、绿色和蓝色值。这是我所拥有的(h、w 和 rgb 是整数和 image.getPixelColor(CGPoint) returns 一个 UIColor):

 xArry[h][w][rgb] = image.getPixelColor(CGPoint(x: w, y: h))

如何将此 UIColor 更改为红色、绿色和蓝色值?谢谢!

未经测试,但这应该有效。刚刚找到它:

   xArry[h][w][rgb] = Int(image.getPixelColor(CGPoint(x: w, y: h)).CIColor.red())

您可以使用 CGColorGetComponents 获取 CGColor 的颜色数组(未在 swift3 中测试)

const CGFloat *_components = CGColorGetComponents(yourUIColor.CGColor);
    CGFloat red     = _components[0];
    CGFloat green = _components[1];
    CGFloat blue   = _components[2];
    CGFloat alpha = _components[3];

您还可以通过

找到该颜色的颜色分量数
CGColorGetNumberOfComponents

您必须在获取值之前验证组件数量。灰色有 2 个分量(灰色和 alpha),RGB 颜色有 4 个分量:R、G、B、A

https://developer.apple.com/library/ios/documentation/GraphicsImaging/Reference/CGColor/index.html#//apple_ref/c/func/CGColorGetNumberOfComponents

您可以将 UIColor 转换为 CIColor,然后从中提取颜色分量,如下所示:

更新:Xcode 8.3.2 • Swift 3.1

extension UIColor {
    var coreImageColor: CIColor {
        return CIColor(color: self)
    }
    var components: (red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) {
        let coreImageColor = self.coreImageColor
        return (coreImageColor.red, coreImageColor.green, coreImageColor.blue, coreImageColor.alpha)
    }
}

用法:

let myColor = UIColor(red: 0.5, green: 1, blue: 0.25, alpha: 0.5)
let myCIColor = myColor.coreImageColor
let greencomponent = myColor.components.green
let myColorComponents = myColor.components
print(myColorComponents.red)   // 0.5
print(myColorComponents.green) // 1.0
print(myColorComponents.blue)  // 0.25
print(myColorComponents.alpha) // 0.5


您还可以使用函数 getRed() 并创建一个扩展来提取组件,但结果是可选的:

extension UIColor {
    typealias RGBA = (red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat)
    var rgba: RGBA? {
        var (r, g, b, a): RGBA = (0, 0, 0, 0)
        return getRed(&r, green: &g, blue: &b, alpha: &a) ? (r,g,b,a) : nil
    }
    var r: CGFloat? {
        var red: CGFloat = .zero
        return getRed(&red, green: nil, blue: nil, alpha: nil) ? red : nil
    }
    var g: CGFloat? {
        var green: CGFloat = .zero
        return getRed(nil, green: &green, blue: nil, alpha: nil) ? green : nil
    }
    var b: CGFloat? {
        var blue: CGFloat = .zero
        return getRed(nil, green: nil, blue: &blue, alpha: nil) ? blue : nil
    }
    var a: CGFloat? {
        var alpha: CGFloat = .zero
        return getRed(nil, green: nil, blue: nil, alpha: &alpha) ? alpha : nil
    }
}

用法

let color = UIColor(red: 0.5, green: 1, blue: 0.25, alpha: 0.5)
if let components = color.rgba {
    print(components.red)   // 0.5
    print(components.green) // 1.0
    print(components.blue)  // 0.25
    print(components.alpha) // 0.5
}

print(color.r ?? "nil")   // 0.5
print(color.g ?? "nil") // 1.0
print(color.b ?? "nil")  // 0.25
print(color.a ?? "nil") // 0.5

对于 UIColor 你可以使用 getRed 方法:

let yourColor = UIColor(red: 0/255, green: 184/255, blue: 48/255, alpha: 1.0)
var r: CGFloat = 0, g: CGFloat = 0, b: CGFloat = 0, a: CGFloat = 0
yourColor.getRed(&r, green: &g, blue: &b, alpha: &a)
print("red: \(r), green: \(g), blue: \(b)")