Extract RGB Values From UIColor

2019-01-04 02:35发布

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))

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

4条回答
该账号已被封号
2楼-- · 2019-01-04 03:16

You can use CGColorGetComponents to get array of color of a CGColor (not tested in swift3)

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

You can find also number of color components of that color with

CGColorGetNumberOfComponents

You have to verify number of components before get values. Gray color has 2 components (grey & alpha), rgb colors have 4 components: R, G, B, A

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

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-01-04 03:23

for UIColor you can just use getRed method:

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)")
查看更多
【Aperson】
4楼-- · 2019-01-04 03:26

Untested but this should work. Just found it:

   xArry[h][w][rgb] = Int(image.getPixelColor(CGPoint(x: w, y: h)).CIColor.red())
查看更多
戒情不戒烟
5楼-- · 2019-01-04 03:30

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

Update: 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)
    }
}

usage:

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


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 {
    var components: (red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat)? {
        var r: CGFloat = 0, g: CGFloat = 0, b: CGFloat = 0, a: CGFloat = 0
        return getRed(&r, green: &g, blue: &b, alpha: &a) ? (r,g,b,a) : nil
    }
}

Usage

let myColor = UIColor(red: 0.5, green: 1, blue: 0.25, alpha: 0.5)
if let myColorComponents = myColor.components {
    print(myColorComponents.red)   // 0.5
    print(myColorComponents.green) // 1.0
    print(myColorComponents.blue)  // 0.25
    print(myColorComponents.alpha) // 0.5
}
查看更多
登录 后发表回答