So this is essentially the method I would like to write (in Objective-C/Cocoa, using UIColors
, but I'm really just interested in the underlying math):
+ (UIColor *)colorBetweenColor:(UIColor *)startColor andColor:(UIColor *)endColor atLocation:(CGFloat)location;
So as an example, say I have two colors, pure red and pure blue. Given a linear gradient between the two, I want to calculate the color that's at, say, the 33% mark on that gradient:
So if I were to call my method like so:
UIColor *resultingColor = [UIColor colorBetweenColor:[UIColor redColor] andColor:[UIColor blueColor] atLocation:0.33f];
I would get the resulting color at 'B', and similarly, passing 0.0f
as the location would return color 'A', and 1.0f
would return color 'C'.
So basically my question is, how would I go about mixing the RGB values of two colors and determining the color at a certain 'location' between them?
Select the Gradient tool in the toolbar. In the selected artwork you'll see the gradient annotator, which shows the gradient slider and the color stops. Double-click a color stop on the artwork to edit the color, drag the color stops, click beneath the gradient slider to add new color stops, and more.
Color gradients, or color transitions, are defined as a gradual blending from one color to another. This blending can occur between colors of the same tone (from light blue to navy blue), colors of two different tones (from blue to yellow), or even between more than two colors (from blue to purple to red to orange).
To create the most basic type of gradient, all you need is to specify two colors. These are called color stops. You must have at least two, but you can have as many as you want.
A color gradient is also known as a color ramp or a color progression. In assigning colors to a set of values, a gradient is a continuous colormap, a type of color scheme.
You simply linearly interpolate the red, the green, and the blue channels like 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
is a value between 0 and 1 (location
in your first method prototype).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With