iOS Find Color at Point Between Two Colors
Swift - 3.0 && 4.0
extension UIColor { func toColor(_ color: UIColor, percentage: CGFloat) -> UIColor { let percentage = max(min(percentage, 100), 0) / 100 switch percentage { case 0: return self case 1: return color default: var (r1, g1, b1, a1): (CGFloat, CGFloat, CGFloat, CGFloat) = (0, 0, 0, 0) var (r2, g2, b2, a2): (CGFloat, CGFloat, CGFloat, CGFloat) = (0, 0, 0, 0) guard self.getRed(&r1, green: &g1, blue: &b1, alpha: &a1) else { return self } guard color.getRed(&r2, green: &g2, blue: &b2, alpha: &a2) else { return self } return UIColor(red: CGFloat(r1 + (r2 - r1) * percentage), green: CGFloat(g1 + (g2 - g1) * percentage), blue: CGFloat(b1 + (b2 - b1) * percentage), alpha: CGFloat(a1 + (a2 - a1) * percentage)) } }}
Usage:-
let colorRed = UIColor.redlet colorBlue = UIColor.bluelet colorOutput = colorRed.toColor(colorBlue, percentage: 50)
Result
The problem is that you're not subtracting kBottomThreshold
from farenheit
.
But let's simplify.
First, we want to map the input temperature to a parameter t
in the range [0 ... 1]. Then, we want to map t
to an output in the range [kBottomR
... kTopR
], and also to an output in the range [kBottomG
... kTopG
], and also to an output in the range [kBottomB
... kTopB
].
UIColor *colorForDegreesFahrenheit(double fahrenheit) { double t = (fahrenheit - kBottomThreshold) / (kTopThreshold - kBottomThreshold); // Clamp t to the range [0 ... 1]. t = MAX(0.0, MIN(t, 1.0)); double r = kBottomR + t * (kTopR - kBottomR); double g = kBottomG + t * (kTopG - kBottomG); double b = kBottomB + t * (kTopB - kBottomB); return [UIColor colorWithRed:r/255 green:g/255 blue:b/255 alpha:1];}
Thanks @ramchandra-n I implemented the extension to get the intermediate color from an array of colors by percentage
extension Array where Element: UIColor { func intermediate(percentage: CGFloat) -> UIColor { let percentage = Swift.max(Swift.min(percentage, 100), 0) / 100 switch percentage { case 0: return first ?? .clear case 1: return last ?? .clear default: let approxIndex = percentage / (1 / CGFloat(count - 1)) let firstIndex = Int(approxIndex.rounded(.down)) let secondIndex = Int(approxIndex.rounded(.up)) let fallbackIndex = Int(approxIndex.rounded()) let firstColor = self[firstIndex] let secondColor = self[secondIndex] let fallbackColor = self[fallbackIndex] var (r1, g1, b1, a1): (CGFloat, CGFloat, CGFloat, CGFloat) = (0, 0, 0, 0) var (r2, g2, b2, a2): (CGFloat, CGFloat, CGFloat, CGFloat) = (0, 0, 0, 0) guard firstColor.getRed(&r1, green: &g1, blue: &b1, alpha: &a1) else { return fallbackColor } guard secondColor.getRed(&r2, green: &g2, blue: &b2, alpha: &a2) else { return fallbackColor } let intermediatePercentage = approxIndex - CGFloat(firstIndex) return UIColor( red: CGFloat(r1 + (r2 - r1) * intermediatePercentage), green: CGFloat(g1 + (g2 - g1) * intermediatePercentage), blue: CGFloat(b1 + (b2 - b1) * intermediatePercentage), alpha: CGFloat(a1 + (a2 - a1) * intermediatePercentage) ) } }}
You can use it to get an intermediate color between two or more colors:
let color = [.green, .yellow, .red].intermediate(percentage: 70)