Search code examples
c#algorithmrgb

Algorithm: How do I fade from Red to Green via Yellow using RGB values?


I want to display a color based on a value from 0 to 100. At one end (100), it's pure Red, the other end (0), pure Green. In the middle (50), I want it to be yellow.

And I want the colors to fade gradually from one to another, such that at 75, the color is half red and half yellow, etc.

How do I program the RGB values to reflect this fading? Thanks.


Solution

  • The RGB values for the colors:

    • Red 255, 0, 0
    • Yellow 255, 255, 0
    • Green 0, 255, 0

    Between Red and Yellow, equally space your additions to the green channel until it reaches 255. Between Yellow and Green, equally space your subtractions from the red channel.