24

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!

Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
Kendel
  • 1,698
  • 2
  • 17
  • 33

4 Answers4

55

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 {
    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
    }
}

Usage

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
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
  • 2
    Why create 4 `CIColor` instances? Create one, assign it to a variable, and get the 4 components from it. – rmaddy Feb 16 '15 at 01:43
  • 1
    Can someone explain exactly how that works? I'm guessing "components" is a tuple that contains the elements. But whats the rest? Some kind of function-ish? – netdigger Jul 25 '15 at 18:27
  • I got it. Its "Setting a Default Property Value with a Closure or Function" from https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Initialization.html#//apple_ref/doc/uid/TP40014097-CH18-ID232 – netdigger Jul 28 '15 at 06:35
  • @everlof this is called a read-only computed property. It returns a tuple of 4 elements. It has a getter but it doesn't have a setter and to simplify declaration you can omit the getter. More info here https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Properties.html – Leo Dabus Jan 23 '16 at 18:29
  • I'm not sure if something changed in Swift 2.2, but `CoreImage.CIColor(color:)` doesn't return an optional, and `getRed(_,green:,blue:,alpha:)` can, so I'm going to edit your answer to make it more correct. :) – NiñoScript Aug 25 '16 at 16:19
  • 1
    @NiñoScript thats correct CIColor initializer used to return an optional and in the getRed case I was returning clear color in case of failure which depends on personal preference. – Leo Dabus Sep 05 '16 at 20:50
  • I don't think it's about personal preference, `getRed:green:blue:alpha` returns false if there was an error when converting to the sRGB color space; so you were hiding an error, or making the clear color components look like an "error" color, which has the same problem as NULL references: it's "The Billion Dollar Mistake" all over again. I think it's much better now. – NiñoScript Sep 05 '16 at 21:36
  • But if someone needs the old behavior, just call `let components = myColor.components ?? (red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0)` – NiñoScript Sep 05 '16 at 21:37
21

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)")
  • Agree. This is the best way so far to extract RGBA values. Excellent option if you need to modify one of them. Votes up! – MLBDG Nov 18 '16 at 03:13
4

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

Duyen-Hoa
  • 15,384
  • 5
  • 35
  • 44
1

Untested but this should work. Just found it:

   xArry[h][w][rgb] = Int(image.getPixelColor(CGPoint(x: w, y: h)).CIColor.red())
Kendel
  • 1,698
  • 2
  • 17
  • 33