47

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: Example
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?

1

3 Answers 3

79

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).

4
  • 1
    @LudovicLandry Can you elaborate? You can linearly interpolate colors in any color space, though the results may not always be pleasing or what you expect. (In fact, even in RGB, you often pass through gray which is sometimes not desired.) In what way does the above not work for which color spaces? Commented Nov 12, 2014 at 5:27
  • For example, this will not work on [UIColor whiteColor] as that is not in RGB. See stackoverflow.com/questions/4700168/… Commented Nov 12, 2014 at 5:39
  • 2
    @LudovicLandry - Ah, I see what you mean. Yes, you would have to convert named colors to some other color space (RGB, HSV, CMYK, whatever). But once they're in a color space that you can do math on, linearly interpolating will work just fine. Commented Nov 12, 2014 at 5:41
  • Thanks man. I feel dumb for not realizing this. Thanks a ton. Commented Oct 11, 2021 at 23:46
3

I know this is not technically a javascript question but I was struggling with the same concept so I thought I'd make a demo to visualize the accepted answer

Run the below code & move the slider

var sample=document.querySelector("#sample")
var range=document.querySelector("#range")

// rgb(255,0,0)
var color1_red=255
var color1_green=0
var color1_blue=0

// rgb(0,0,255)
var color2_red=0
var color2_green=0
var color2_blue=255


document.querySelector("#range").addEventListener("input",(event)=>{
 let percent= range.value/100.0
 
 let resultRed = color1_red + percent * (color2_red - color1_red);
 let resultGreen = color1_green + percent * (color2_green - color1_green);
 let resultBlue = color1_blue + percent * (color2_blue - color1_blue);
 
 sample.style.backgroundColor=`rgb(${resultRed},${resultGreen},${resultBlue})`
})
#sample{
  height:10rem;
  width:10rem;
}

#range{
  width:10rem
}

#grad{
  width:10rem;
  height:2rem;
  background-image: linear-gradient(to right, red , blue);
}
<div id="grad"></div>
<input type="range" min="1" max="100" value="0" id="range">

<div id="sample" style="background-color:rgb(255,0,0);"></div>

Hope this helps in some way :)

0

RGB color space is like a circle. With highest saturation along the outer border, and grey in the middle. Traveling from one color to another, you'd preferably want to be doing that, along the same radius (circle) from the middle; so as to where saturation and value stay the same. In that case, the hue is changing in a linear fashion. You will not cross into more grey area than your left and right colors initially are. You can travel from an inner ring to an outer ring, simple go up (or down) the saturation; again linearly. See here for color circle (try it in e.g. paint.net)

Apple's (iOS) objective classes allow you to work with other spectra than RGB.

1
  • 9
    RGB is not like a circle, it is a cube. Spaces like HSV or HSB form a circle with the hue in degrees or radians.
    – Henrik
    Commented Dec 1, 2016 at 7:09

Not the answer you're looking for? Browse other questions tagged or ask your own question.