I need to compare two UIColors, but for some reason it always return false. I tried to compare using ==
and .isEqual()
, but neither of them seem to work.
//This is a sample of the colors I have created
let blue_color = UIColor(red: 122/255, green: 180/255, blue: 190/255, alpha: 1)
//This is the SpriteNode I have to compare
let square = SKSpriteNode(color: randomColorController(), size: ksquaresize)
randomColorController() is just a function that randomizes colors and returns it, so it is called when square is created.
func randomColorController() -> UIColor {
let random = arc4random_uniform(3) + 1
switch random {
case 1:
let color = blue_color
return color
case 2:
let color = yellow_color
return color
case 3:
let color = yellow_color
return color
default:
let color = UIColor.clearColor()
return color
}
Then, depending on the position of the square I have created it will check collision comparing the color of the square I have created and the colors I have initialized at the beginning.
func checkCollision(currentTime: CFTimeInterval, Square: SKSpriteNode) -> Int{
let color = Square.color
print(color.isEqual(blue_color))
print(color.isEqual(red_color))
print(color.isEqual(yellow_color))
if Square.position.y >= 0 && Square.position.y <= 40 {
if color.isEqual(blue_color) && (Square.position.x < basesize.width) {
// ADDS 1 POINT TO THE SCORE LABEL
flag = 1
points += 1
} else if color.isEqual(red_color) && (Square.position.x > (basesize.width*2)){
flag = 1
points += 1
} else if color.isEqual(yellow_color) && (Square.position.x < (basesize.width*2)) && (Square.position.x > basesize.width) {
flag = 1
points += 1
} else {
flag = -1
}
}
But color.isEqual(blue_color)
or any of the other colors, doesn't seem to work. I have printed Square.color
and blue_color
(and the others), and they match. But it would always return false
.
extension UIColor {
func isEqual(color: UIColor?) -> Bool {
guard let color = color else { return false }
var red:CGFloat = 0
var green:CGFloat = 0
var blue:CGFloat = 0
var alpha:CGFloat = 0
self.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
var targetRed:CGFloat = 0
var targetGreen:CGFloat = 0
var targetBlue:CGFloat = 0
var targetAlpha:CGFloat = 0
color.getRed(&targetRed, green: &targetGreen, blue: &targetBlue, alpha: &targetAlpha)
return (Int(red*255.0) == Int(targetRed*255.0) && Int(green*255.0) == Int(targetGreen*255.0) && Int(blue*255.0) == Int(targetBlue*255.0) && alpha == targetAlpha)
}
}
I tested in swift 3, 4
You can use "==" or isEqual. I have just tested both of them and they work fine:
let redColor = UIColor.redColor()
let greenColor = UIColor.greenColor()
let blueColor = UIColor.blueColor()
let testColor = UIColor.greenColor()
println( testColor == redColor ) // false
println( testColor == greenColor ) // true
println( testColor == blueColor ) // false
println( testColor.isEqual(redColor) ) // false
println( testColor.isEqual(greenColor) ) // true
println( testColor.isEqual(blueColor) ) // false
I have just reproduced the issue only happens after extracting the SKSpriteNode color and as you said only with fraction colors. You can work your way around this issue comparing the color description as follow:
let blue_color = UIColor(red: 122/255, green: 180/255, blue: 190/255, alpha: 1)
let yellow_color = UIColor(red: 253/255, green: 213/255, blue: 123/255, alpha: 1)
let red_color = UIColor(red: 238/255, green: 116/255, blue: 71/255, alpha: 1)
func randomColorController() -> UIColor {
let random = arc4random_uniform(3) + 1
switch random {
case 1:
return blue_color
case 2:
return red_color
case 3:
return yellow_color
default:
return UIColor.clearColor()
}
}
let square = SKSpriteNode(color: randomColorController(), size: CGSize(width: 30, height: 30))
if square.color.description == blue_color.description {
println(true)
}
if square.color.description == red_color.description {
println(true)
}
if square.color.description == yellow_color.description {
println(true)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With