从 UIColor 中提取 RGB 值 [英] Extract RGB Values From UIColor

查看:24
本文介绍了从 UIColor 中提取 RGB 值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have seen people do this in objective-c, but I am having trouble with this in swift. I have gotten the color of a pixel from a picture, but now I need to take the individual red, green, and blue values. Here is what I have (h, w, and rgb are integers and image.getPixelColor(CGPoint) returns a UIColor):

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

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

How do I change this UIColor into the red, green, and blue values? Thanks!

推荐答案

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

You can convert UIColor to CIColor and then extract the color components from it as follow:

更新: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() 并创建一个扩展来提取组件,如下所示,但结果是可选的:



You can also use the function getRed() and create an extension to extract the components as follow but the result would be optional:

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 中提取 RGB 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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