How to compare two UIColor which have almost same shade or range in iOS?

后端 未结 5 663
独厮守ぢ
独厮守ぢ 2020-12-13 16:18

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

5条回答
  •  甜味超标
    2020-12-13 16:58

    If I understood clearly :

    CGFloat *components1 = CGColorGetComponents([[UIColor color1] CGColor]);
    CGFloat *component1 = CGColorGetComponents([[UIColor color2] CGColor]);
    

    Then, you can compare the difference between the two colors using components[0] (red),components[1] (green), components[2] (blue) and components[3] alpha. Decide what you wanna compare. Example : fabs(components1[1]-components2[1]), or a mean between all theses differences, etc.

提交回复
热议问题