生成颜色渐变的C# [英] Generate Color Gradient in C#
问题描述
在这里我的问题是类似这里问题,但我使用C#的工作。
My question here is similar to the question here, except that I am working with C#.
我有两个颜色,我有一个predefine步骤。如何检索颜色
s表示两者之间的渐变列表?
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));
}
如果您尝试了上面code,你会发现,在的argb
逐渐增加不对应的颜色视觉逐渐增加。
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.
在这个任何想法?
推荐答案
您必须提取R,G,B分量并分别对他们每个人执行相同的线性插值,然后重新组合。
You will have to extract the R, G, B components and perform the same linear interpolation on each of them individually, then recombine.
int rMax = Color.Chocolate.R;
int rMin = Color.Blue.R;
// ... and for B, G
var colorList = new List<Color>();
for(int i=0; i<size; i++)
{
var rAverage = rMin + (int)((rMax - rMin) * i / size);
// ... and for B, G
colorList.Add(Color.FromArgb(rAverage, gAverage, bAverage));
}
这篇关于生成颜色渐变的C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!