I have a ProgressBar to which I want to assign a customColor color and based on the progress fade to another color. Using the method below I get a dark rainbow effect color including reds and dark brown and dark green. The start color will be a light blue one and the destination color a light green.
-(UIColor *) makeCustomColorFromProgressValue:(float) progress{
UIColor *color;
// startColor Color - lightBlue
float red = 0.53;
float green = 0.82;
float blue = 1;
//Destination Color - lightGreen
float finalRed = 0.53;
float finalGreen = 1;
float finalBlue = 0.82;
float newRed = 80;//finalRed *255;
float newGreen = (finalGreen *progress) *255;
float newBlue = (finalBlue *progress) *255;
color = Rgb2UIColor(newRed, newGreen, newBlue);
return color;
}
Here's a Swift 3 version that uses a convenience initializer and adds a function to return an array of intermediate colors.
Usage:
Output:
The swift version provided above doesn't work with the white color, here the solution:
You can do a "linear interpolation" between the colors:
This gives the initial color for
progress == 0
and the final color forprogress == 1
.Another port to swift, as an extension to UIColor. This time both interpolation functions.
Swift version converted from the code from Jonathan Ellis
Here is a category for
UIColor
that can be used to linearly interpolate between twoUIColor
s in either RGB or HSV: