Create an opaque UIColor that looks the same as another that is translucent

This color

[UIColor colorWithRed:75.0/255.0 
                green:215.0/255.0
                 blue:100.0/255.0 
                alpha:0.5]

and this one, with different RGB values ​​and no transparency,

[UIColor colorWithRed:165.0/255.0 
                green:235.0/255.0 
                 blue:177.0/255.0 
                alpha:1]

look exactly the same on a white background.

How to go from the first UIColorto the second UIColor?

+4
source share
2 answers

Well, the answer will be based only on knowledge of the background color. Something like this will work if backgroundColorand sourceColorare UIColors:

float blend(float src, float srcAlpha, float background) {
  return (background + src * srcAlpha) / 2;
}
UIColor *result = [UIColor colorWithRed:blend([sourceColor red], [sourceColor alpha], [backgroundColor red])
                                    green:blend([sourceColor green], [sourceColor alpha], [backgroundColor green])
                                    blue:blend([sourceColor blue], [sourceColor alpha], [backgroundColor blue])
                                    alpha:1.0f];

, , . , . blend, , , , , .

+5

NilObject , - .

, , :

cell.backgroundColor = [UIColor colorWithRed:75.0/255.0 green:215.0/255.0 blue:100.0/255.0 alpha:1];

:

cell.backgroundColor = [UIColor colorWithRed:1 - (255-75)*rate/255.0 green:1 - (255-215)*rate/255.0 blue:1 - (255-100)*rate/255.0 alpha:1];

rate alpha

0

Source: https://habr.com/ru/post/1538806/


All Articles