ios - Comparing 2 UIColor(s) -
this must have been asked before, cannot find suitable reference. have found question, compares 3 numbers each other.
i trying compare 2 uicolor(s) avoid duplication. each color referenced in r, g, b, alpha. form colors can control number formatting.
what appropriate way handle this?
all appreciated.
if creating color same way can use ==
.
if colors in different color spaces , want compare rgba value use following:
extension uicolor { func equals(_ rhs: uicolor) -> bool { var lhsr: cgfloat = 0 var lhsg: cgfloat = 0 var lhsb: cgfloat = 0 var lhsa: cgfloat = 0 self.getred(&lhsr, green: &lhsg, blue: &lhsb, alpha: &lhsa) var rhsr: cgfloat = 0 var rhsg: cgfloat = 0 var rhsb: cgfloat = 0 var rhsa: cgfloat = 0 rhs.getred(&rhsr, green: &rhsg, blue: &rhsb, alpha: &rhsa) return lhsr == rhsr && lhsg == rhsg && lhsb == rhsb && lhsa == rhsa } }
for instance:
let white1 = uicolor.white let white2 = uicolor(colorliteralred: 1, green: 1, blue: 1, alpha: 1) white1 == white2 //false white1.cgcolor == white2.cgcolor //false white1.equals(white2) //true
Comments
Post a Comment