I made a start at translating the accepted answer's code here into C#. Any suggestions on the correctness of the translation and the correctness of the original code would be very much appreciated.
public class RgbValues
{
public int Red { get; set; }
public int Green { get; set; }
public int Blue { get; set; }
}
public static RgbValues GetRgbValues(float minimumValue, float maximumValue, float value)
{
var rgbValues = new RgbValues();
var halfmax = (minimumValue + maximumValue) / 2.0;
rgbValues.Blue = (int) Math.Max(0.0, 255.0 * (1.0 - value/halfmax));
rgbValues.Red = (int)Math.Max(0.0, 255.0 * (value / halfmax - 1.0));
rgbValues.Green = 255 - rgbValues.Blue - rgbValues.Red;
return rgbValues;
}
What is a bit worrying is that:
GetRgbValues(10, 10113, 10113)
should really caclulate:
red = 255, green = 0, blue = 0
rather than:
red = 254, green = 1, blue = 0
I guess there is some rounding issue.
Any ideas?