简体   繁体   中英

apply a color value depending on decimal value imported

To start with, I'm very new to C#!

I have a file with approximately 3 million lines. Each line contains a number between 0 and 1 (6 decimal places).

What I want to do is create a RGB value for each number depending where between 0 and 1 that number lies. For example, 0 would be green, 1 would be red, and all the numbers between would have a RGB value between green and red, created to represent how far down the color scale it is.

How do I calculate that RGB value?

Well, the simplest approach would be:

int red = (int)(Num * 255);
int green = (int)((1 - Num) * 255);
int blue = 0;

This way you get a gradient between red and green with green at 0 and red at 1.

绿色-红色渐变

However it sounds like you want to produce a visual scale for values that range from "good" to "bad". In that people often also want to place yellow in the middle to denote "okayish" values. Here's the code for that:

int red, green, blue;
if ( Num < 0.5 )
{
    red = (int)(Num * 2 * 255);
    green = 255;
    blue = 0;
}
else
{
    red = 255;
    green = (int)((2 - 2 * Num) * 255);
    blue = 0;
}

在此处输入图片说明

You'll need to derive an algorithm to convert your decimal value to three separate values, Red, Green, and Blue. Then use the built-in Color type to create your color with those values ( https://msdn.microsoft.com/en-us/library/cce5h557(v=vs.110).aspx ).

What you do with the colors from there is up to you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM