Search code examples
image-processingcomputer-visioninterpolationspatial-interpolationbilinear-interpolation

How to interpolate between n colors by a fractional contribution for each color?


How can I interpolate between n colors.

Simple case of 2 colors

Consider a more simple case first, where we want to find the mid-point of 2 colors.

Color1 is RGB ( 255, 0, 0 ) // Red
Color2 is RGB ( 128, 128, 128 ) // Grey

The solution being the mid-point between each R, G, B considered separately.

RGB ( 128 + 64, 128 / 2, 128 / 2 ) = RGB ( 192, 64, 64 )

Since the mid-point is exactly in between the two and there is a linear relationship to the interpolation, then its possiable to interpolate by a fractional amount such as 0.25 between Color1 and Color2, the color should be closer to Color1.

RGB ( 255 - 32, 32, 32 ) = RGB ( 223, 32, 32 )

Case of n colors

The case I wish to find a solution for is where there are n colors where each color has a fractional weighting totaling up to 1.0.

(Guessing, I guess each color could be considered to be a point in a 3 dimensional space and the weighting describes how far relatively the interpolated point is to each color point)

The color interpolation is linear RGB only.

Under some conditions I guess there MAY be multiple integer values which are solutions to the problem, for example if there are a n few colors which have similar value.

I read there is bi-linear interpolation which may help to solve this.

Usually the number of colors wouldn't exceed 5, it would usually be 2, 3 or 4 colors.


Solution

  • Some Java code which solves the problem is located here, note that it builds with Scala SBT but its Java code.

    https://github.com/PhilAndrew/betweenrgb

    The merging of weighted colors is tested here:

    https://github.com/PhilAndrew/betweenrgb/blob/master/src/test/java/between/rgb/MergeWeightedColorsTest.java