ios - Calculate the color at a given point on a gradient between two colors? -
so method write (in objective-c/cocoa, using uicolors
, i'm interested in underlying math):
+ (uicolor *)colorbetweencolor:(uicolor *)startcolor andcolor:(uicolor *)endcolor atlocation:(cgfloat)location;
so example, have 2 colors, pure red , pure blue. given linear gradient between two, want calculate color that's at, say, 33% mark on gradient:
if call method so:
uicolor *resultingcolor = [uicolor colorbetweencolor:[uicolor redcolor] andcolor:[uicolor bluecolor] atlocation:0.33f];
i resulting color @ 'b', , similarly, passing 0.0f
location return color 'a', , 1.0f
return color 'c'.
so question is, how go mixing rgb values of 2 colors , determining color @ 'location' between them?
you linearly interpolate red, green, , blue channels this:
double resultred = color1.red + percent * (color2.red - color1.red); double resultgreen = color1.green + percent * (color2.green - color1.green); double resultblue = color1.blue + percent * (color2.blue - color1.blue);
where percent
value between 0 , 1 (location
in first method prototype).
Comments
Post a Comment