我想一个褪色UIColor
到另外一个drawRect
。 我创建了这个功能在一定的百分比来计算的颜色:
- (UIColor *)colorFromColor:(UIColor *)fromColor toColor:(UIColor *)toColor percent:(float)percent
{
float dec = percent / 100.f;
CGFloat fRed, fBlue, fGreen, fAlpha;
CGFloat tRed, tBlue, tGreen, tAlpha;
CGFloat red, green, blue, alpha;
if(CGColorGetNumberOfComponents(fromColor.CGColor) == 2) {
[fromColor getWhite:&fRed alpha:&fAlpha];
fGreen = fRed;
fBlue = fRed;
}
else {
[fromColor getRed:&fRed green:&fGreen blue:&fBlue alpha:&fAlpha];
}
if(CGColorGetNumberOfComponents(toColor.CGColor) == 2) {
[toColor getWhite:&tRed alpha:&tAlpha];
tGreen = tRed;
tBlue = tRed;
}
else {
[toColor getRed:&tRed green:&tGreen blue:&tBlue alpha:&tAlpha];
}
red = (dec * (tRed - fRed)) + fRed;
blue = (dec * (tGreen - fGreen)) + fGreen;
green = (dec * (tBlue - fBlue)) + fBlue;
alpha = (dec * (tAlpha - fAlpha)) + fAlpha;
return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}
这样做是需要每个R / G / B / A的值,并递增它取决于百分比。
这样的工作,但不褪色[UIColor purpleColor]
至[UIColor redColor]
正确。 在0.1%,它显示为绿色,而不是紫色的,所以才出现掉色从绿色变为红色,而不是紫色到红色。
任何人都知道我在做什么错? 这是错误的做法来计算呢? 有没有更准确的方式来做到这一点?