문제

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 do I go from the first UIColor to the second UIColor?

도움이 되었습니까?

해결책

Well, the answer will rely specifically on knowing the background color. Something like this would work, assuming backgroundColor and sourceColor are 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];

While the numbers aren't equal to your example, this is a reasonable blend function that is generic. Without more examples it's hard to know what the curve of your example is. The blend function you're using appears to use a different ratio for each color component, or perhaps your measurement of the resultant color values are being adjusted by the color profile of your screen.

다른 팁

NilObject's answer was good, but I ended up using something more specific to my problem.

The base color I wanted different shades from was this:

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

Here's my solution:

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];

Where rate would replace alpha

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top