I have a condition in my app where user can choose 3 colors, but those colors should not match with each other, the problem is user can choose the similar color from the pal
trojanfoe's answer is great, here is a Swift version:
My suggestion: Create an extension on UIColor like so:
public extension UIColor{
func isEqualToColor(color: UIColor, withTolerance tolerance: CGFloat = 0.0) -> Bool{
var r1 : CGFloat = 0
var g1 : CGFloat = 0
var b1 : CGFloat = 0
var a1 : CGFloat = 0
var r2 : CGFloat = 0
var g2 : CGFloat = 0
var b2 : CGFloat = 0
var a2 : CGFloat = 0
self.getRed(&r1, green: &g1, blue: &b1, alpha: &a1)
color.getRed(&r2, green: &g2, blue: &b2, alpha: &a2)
return
fabs(r1 - r2) <= tolerance &&
fabs(g1 - g2) <= tolerance &&
fabs(b1 - b2) <= tolerance &&
fabs(a1 - a2) <= tolerance
}
}
Usage:
// check if label's color is white
if label.textColor.isEqualToColor(UIColor.whiteColor(), /* optional */ withTolerance: 0.0){
// if so, add shadow
label.layer.shadowColor = UIColor.blackColor().CGColor
label.layer.shadowRadius = 4.0
label.layer.shadowOpacity = 0.6
label.layer.shadowOffset = CGSizeMake(0, 0)
}