Algorithm for calculating inverse color

前端 未结 2 624
遇见更好的自我
遇见更好的自我 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: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.

提交回复
热议问题