Search code examples
if-statementswiftuiequatable

How to compare three Color in SwiftUI


I have three Colors and I want to compare them. Should I make Color conform to Equatable protocol?

Code :

if Color.red == Color.green == Color.blue  {
   // do something
}

Solution

  • I really dont undestand what you want to achieve but you can compare them like

            let colorr = UIColor(red: 122/255, green: 100/255, blue: 180/255, alpha: 1.0)
            let rgbColorr = colorr.cgColor
            let rgbColorrs = rgbColorr.components
    

    This rgbColorrs prints and array like [0.47843137254901963, 0.39215686274509803, 0.7058823529411765, 1.0] its like [red,green,blue,alpha]

    if rgbColorrs[0] == rgbColors[1] == rgbColors[2]{
      //....
    }
    

    Or if you want to compare equality

     if UIColor.red.isEqual(UIColor.green.isEqual(UIColor.blue)){
            ...
        }