I have a requirement to display a particular colour r开发者_JAVA百科epresentation 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.
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);
}
Is this what you mean?
Color GetColor(int scale) { // scale is between 1 and 255 return Color.FromArgb(255, scale, 255 - scale, 0); }
Aside from a linear scale, you might find you get the message across more clearly with a little colour theory. Depending on how much time you want to spend, you might also want to look at e.g.
Cynthia Brewer
Adobe Kuler
Stanford lecture on Colour for visualization
among many, many others -- google for Processing and data visualization too.
精彩评论