Runar Ovesen Hjerpbakk

Software Philosopher

Gradient Colors on iOS

I needed a way to get a color between two colors given a ratio while working on the 1.1 version of Golden Ratio Calculator. I use it to show a color between blue and gold, then gold and silver, and lastly between silver and blue.

public static UIColor ColorForRatio(float[] startColor, float[] endColor, float colorRatio) {
 return UIColor.FromRGB(
  startColor[0] + (endColor[0] - startColor[0]) * colorRatio, 
  startColor[1] + (endColor[1] - startColor[1]) * colorRatio, 
  startColor[2] + (endColor[2] - startColor[2]) * colorRatio);
Color Ratio Example

The start and end colors are represented as arrays of RGB ratios, but this is easy to work with.

Either get the RGB-ratios from a UIColor:

var startColor = new nfloat[4];
UIColor.White.GetRGBA(out startColor[1], out startColor[2], out startColor[2], out startColor[3]);

Or create the array directly:

var upperAtmosphereColor = new nfloat[] { 32/255f, 79/255f, 205/255f };