Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# rgb colour scale

Tags:

c#

rgb

I have a requirement to display a particular colour representation dependant upon data. At one end of the scale is green and the other red. The scale should cover the colours in between.

I am comfortable with the actual programming, but unsure of how to scale the rgb colours. Does anybody know any scales for this sort of thing?

Thanks.

like image 733
Darren Young Avatar asked Nov 28 '25 11:11

Darren Young


2 Answers

The following method should work for you :

    Color GetColor(Int32 rangeStart /*Complete Red*/, Int32 rangeEnd /*Complete Green*/, Int32 actualValue)
    {
        if (rangeStart >= rangeEnd) return Colors.Black;

        Int32 max = rangeEnd - rangeStart; // make the scale start from 0
        Int32 value = actualValue - rangeStart; // adjust the value accordingly

        Int32 green = (255 * value) / max; // calculate green (the closer the value is to max, the greener it gets)
        Int32 red = 255 - green; // set red as inverse of green

        return Color.FromRgb((Byte)red, (Byte)green, (Byte)0);
    }
like image 69
decyclone Avatar answered Dec 01 '25 02:12

decyclone


Is this what you mean?

Color GetColor(int scale)
{
    // scale is between 1 and 255
    return Color.FromArgb(255, scale, 255 - scale, 0);
}
like image 33
Ran Avatar answered Dec 01 '25 01:12

Ran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!