How to interpolate between n colors by a fractiona

2019-07-27 02:34发布

问题:

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.

回答1:

Your guess will give acceptable solution (yes, you need to interpolate each dimension separately).

The problem is, the color space can be described by more than one model, each having different number of dimensions. Depending on whether you choose RGB, CMYK, HSL or any other exotic description, interpolation will look differently - and since the differences will be mostly aesthetic, they will be hard to describe in a very technical terms. For example interpolation using HSL will always move between colors by using the circle of colors.

A nice description of some consequences of using different color models for calculations (and the complexity it brings) can be found in the Krita manual: https://userbase.kde.org/Krita/Manual/ColorManagement



回答2:

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