4

I need to get the color of a pixel/point for my game in Swift. So I tried to find a function that inputs a CGPoint and returns a Color, everything that I have found so far has only returned (0,0,0,0). I think that this is because I am doing this in my game scene class(which is an SKScene), and the scene is only being loaded by a view controller. Any Solutions?

(If there is a better way to check if a sprite is touching a color that would work as well)

Thanks in advance.

joshLor
  • 1,034
  • 1
  • 12
  • 27
  • http://stackoverflow.com/questions/1160229/how-to-get-the-color-of-a-pixel-in-an-uiview has code for Swift 2 and 3: http://stackoverflow.com/a/26945594/1187415 – Martin R Mar 09 '17 at 06:11
  • @MartinR these solutions seem to only return transparency/black as well – joshLor Mar 09 '17 at 07:03
  • depends on when/how you are getting it. Scene and View are 2 different things entirely, so your coordinate in scene may not reflect your coordinate in the view, plus the contents may not be rendered to the context till after it is drawn. On top of this you need to worry about retina, which could also lead to an invalid pixel. Perhaps the real question is, why do you need an individual point, and is there a better alternative? – Knight0fDragon Mar 09 '17 at 12:54
  • I need to be able to check if my object is colliding with a color. Any other way of doing that would be fine but it has to work with spriteKit and in my game scene. – joshLor Mar 09 '17 at 14:54
  • @Knight0fDragon do you have any ideas? I updated my question a bit. – joshLor Mar 09 '17 at 17:42
  • What are we talking about here, black square hitting blue circle – Knight0fDragon Mar 09 '17 at 17:45
  • @Knight0fDragon I have a circle that collides with a color wheel and I want to get what color it hits (both are SKSpritenodes ) – joshLor Mar 09 '17 at 17:50
  • then I would make your color wheel several sprites contained in 1 big node, this way you can distinguish from each segment – Knight0fDragon Mar 09 '17 at 19:06
  • basically each color segment of the color wheel is a sprite (Assuming this is wheel of fortune / pizza slice style color wheel and not a spiral) – Knight0fDragon Mar 09 '17 at 19:07

1 Answers1

5

Add the below method:

extension UIView {
    func getColor(at point: CGPoint) -> UIColor{

        let pixel = UnsafeMutablePointer<CUnsignedChar>.allocate(capacity: 4)
        let colorSpace = CGColorSpaceCreateDeviceRGB()
        let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue)
        let context = CGContext(data: pixel, width: 1, height: 1, bitsPerComponent: 8, bytesPerRow: 4, space: colorSpace, bitmapInfo: bitmapInfo.rawValue)!

        context.translateBy(x: -point.x, y: -point.y)
        self.layer.render(in: context)
        let color = UIColor(red:   CGFloat(pixel[0]) / 255.0,
                            green: CGFloat(pixel[1]) / 255.0,
                            blue:  CGFloat(pixel[2]) / 255.0,
                            alpha: CGFloat(pixel[3]) / 255.0)

        pixel.deallocate(capacity: 4)
        return color
    }
}

You can get the color of the point you want with

let color = someView.getColor(at: somePoint)
Alexander
  • 59,041
  • 12
  • 98
  • 151
Parth Adroja
  • 13,198
  • 5
  • 37
  • 71