Generating gradients programmatically?

you want an interpolation between the first and the second colour. Interpolating colours is easy by calculating the same interpolation for each of its components (R, G, B). There are many ways to interpolate. The easiest is to use linear interpolation: just take percentage p of the first colour and percentage 1 – p of the second:

R = firstCol.R * p + secondCol.R * (1 - p)

There’s another question related to this.

There are other methods of interpolation that sometimes work better. For example, using a bell-shaped (sigmoidal) interpolation function makes the transition smoother.

/EDIT: Oops, you mean using a predefined function. OK, even easier. The blog post you linked now has an example code in Python.

In Java, you could use the GradientPaint.

Leave a Comment