Smooth color transition algorithm

前端 未结 5 748
感情败类
感情败类 2020-12-09 18:41

I am looking for a general algorithm to smoothly transition between two colors.

For example, this image is taken from Wikipedia and shows a transition from orange to

5条回答
  •  没有蜡笔的小新
    2020-12-09 18:57

    A simple linear interpolation of the R,G,B values will do it.

    trumpetlicks has shown that the image you used is not a pure linear interpolation. But I think an interpolation gives you the effect you're looking for. Below I show an image with a linear interpolation on top and your original image on the bottom.

    Top half linear interpolation, bottom half original

    And here's the (Python) code that produced it:

    for y in range(height/2):
        for x in range(width):
            p = x / float(width - 1)
            r = int((1.0-p) * r1 + p * r2 + 0.5)
            g = int((1.0-p) * g1 + p * g2 + 0.5)
            b = int((1.0-p) * b1 + p * b2 + 0.5)
            pix[x,y] = (r,g,b)
    

提交回复
热议问题