My question here is similar to the question here, except that I am working with C#.
I have two colors, and I have a predefine steps. How to retrieve a list of Color
s that are the gradients between the two?
This is an approach that I tried, which didn't work:
int argbMax = Color.Chocolate.ToArgb();
int argbMin = Color.Blue.ToArgb();
var colorList = new List<Color>();
for(int i=0; i<size; i++)
{
var colorAverage= argbMin + (int)((argbMax - argbMin) *i/size);
colorList.Add(Color.FromArgb(colorAverage));
}
If you try the above code, you will find that a gradual increase in argb
doesn't correspond to a visual gradual increase in the color.
Any idea on this?
You will have to extract the R, G, B components and perform the same linear interpolation on each of them individually, then recombine.
Maybe this function can help:
Use double instead of int:
and:
Oliver's answer was very close... but in my case some of my stepper numbers needed to be negative. When converting the stepper values into a
Color
struct my values were going from negative to the higher values e.g. -1 becomes something like 254. I setup my step values individually to fix this.