答案包含一些想法。首先是颜色的插值。 RGB 表示可以通过对分量进行插值相当简单地进行插值,如下所示:
// take two bounding colors and answer one that is pct distance between them
- (UIColor *)colorBetween:(UIColor *)colorA and:(UIColor *)colorB distance:(CGFloat)pct {
CGFloat aR, aG, aB, aA;
[colorA getRed:&aR green:&aG blue:&aB alpha:&aA];
CGFloat bR, bG, bB, bA;
[colorB getRed:&bR green:&bG blue:&bB alpha:&bA];
CGFloat rR = (1.0-pct)*aR + pct*bR;
CGFloat rG = (1.0-pct)*aG + pct*bG;
CGFloat rB = (1.0-pct)*aB + pct*bB;
CGFloat rA = (1.0-pct)*aA + pct*bA;
return [UIColor colorWithRed:rR green:rG blue:rB alpha:rA];
}
我们还需要插入时间,这可以通过将时间表示为午夜过后的分钟数来近似完成...
- (NSInteger)minutesSinceMidnightOfDate:(NSDate *)date {
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSIntegerMax fromDate:date];
[components setHour:0];
[components setMinute:0];
[components setSecond:0];
NSDate *midnight = [[NSCalendar currentCalendar] dateFromComponents:components];
NSDateComponents *diff = [[NSCalendar currentCalendar] components:NSCalendarUnitMinute fromDate:midnight toDate:date options:0];
return [diff minute];
}
我们需要表示插值的参数,在这里尽可能直接地从问题中给出的参数中完成......
// macro to convert hex value to UIColor
#define UIColorFromRGB(rgbValue) \
[UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
green:((float)((rgbValue & 0x00FF00) >> 8))/255.0 \
blue:((float)((rgbValue & 0x0000FF) >> 0))/255.0 \
alpha:1.0]
现在,要做这项工作,找到给定时间之前和之后的时间(以午夜过去的分钟为单位),获取与这些时间相对应的颜色,以与之前和之后时间之间的时间距离相匹配的比例插入颜色.
- (UIColor *)colorForDate:(NSDate *)date {
// look up the bounds of interpolation here
NSArray *wheel = @[ @[ @0, UIColorFromRGB(0x0525FF) ],
@[ @360, UIColorFromRGB(0xFFF199) ],
@[ @720, UIColorFromRGB(0xFFD005) ],
@[ @1080, UIColorFromRGB(0x059CFF) ],
@[ @1440, UIColorFromRGB(0x0525FF) ]];
NSInteger m = [self minutesSinceMidnightOfDate:date];
// find the index in wheel where the minute bound exceeds our date's minutes (m)
NSInteger wheelIndex = 0;
for (NSArray *pair in wheel) {
NSInteger timePosition = [pair[0] intValue];
if (m < timePosition) {
break;
}
wheelIndex++;
}
// wheelIndex will always be in 1..4, get the pair of bounds at wheelIndex
// and the preceding pair (-1).
NSArray *priorPair = wheel[wheelIndex-1];
NSArray *pair = wheel[wheelIndex];
CGFloat priorMinutes = [priorPair[0] intValue];
CGFloat minutes = [pair[0] intValue];
// this is how far we are between the bounds pairs
CGFloat minutesPct = ((float)m - priorMinutes) / (minutes - priorMinutes);
// and the colors for the bounds pair
UIColor *priorColor = priorPair[1];
UIColor *color = pair[1];
// call the color interpolation
return [self colorBetween:priorColor and:color distance:minutesPct];
}