Non-Linear color interpolation?

前端 未结 5 1090
天涯浪人
天涯浪人 2021-01-11 18:47

If I have a straight line that mesures from 0 to 1, then I have colorA(255,0,0) at 0 on the line, then at 0.3 I have colorB(20,160,0) then at 1 on the line I have colorC(0,0

5条回答
  •  生来不讨喜
    2021-01-11 19:25

    it looks like you are trying to interpolate in the wrong color space. First convert to HSV, then your colors become

    RGB -> HSV
    255,0,0 -> 0, 255,255
    20,160,0 -> 80,255,160
    0,0,0 -> 0,255,0
    

    so its still confusing but at least we can see that the V value is interpolating to zero, the H value is confusing, but after realizing that HSV is modeled as a cylinder, we can see that it is really just interpolating from 0 to 256 mod 256, so it just goes back to zero.

    so your equation would be (in HSV)

    nH = frac*256 mod 256
    nS = 255
    nV =  (1-frac)*255
    

    So if you substitute 0.7 for frac you will get the right calculation in HSV coordinates, if you need to go back to RGB you should see if your libraries provide such a feature (i know java's Color class supports this), but if not you can just grab the code from here. it shows how to do color space conversions in C.

    Good luck

提交回复
热议问题