Algorithm for calculating inverse color

前端 未结 2 621
遇见更好的自我
遇见更好的自我 2021-02-05 03:42

I have a RGB color in the range of 0.0 to 1.0 for each fragment, I need an algorithm to get the inverse of the color, any ideas how can I do this?

相关标签:
2条回答
  • 2021-02-05 04:05
    newR = 1.0 - r
    newG = 1.0 - g
    newB = 1.0 - b
    

    If the color has a premultiplied Alpha value use the alpha instead of 1.0:

    newR = a - r
    newG = a - g
    newB = a - b
    
    0 讨论(0)
  • 2021-02-05 04:10

    If you are using RGB values to 255, you could do something like this:

    newR = 255 - r;
    newG = 255 - g;
    newB = 255 - b;
    

    To understand this concept, imagine each value as a number line going from 0 to 255. If you graph a number on that number line then the number is that distance away from the beginning of the number line. In order to negate it, the number must travel to the other end of the number line. This algorithm basically flips the number line without moving the beginning or the end. Our number line is from 0 to 255, so, if the number was 10 away from the beginning (10), now it will be 10 away from the end (245), thus negating the color.

    0 讨论(0)
提交回复
热议问题