生成颜色渐变在C# [英] Generate Color Gradient in C#

查看:730
本文介绍了生成颜色渐变在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题类似于这里的问题,但我使用的是C#。 / p>

我有两种颜色,我有一个预定义步骤。如何检索 Color 的列表,它们是两者之间的渐变?



尝试,这不工作:

  int argbMax = Color.Chocolate.ToArgb 
int argbMin = Color.Blue.ToArgb();
var colorList = new List< Color>();

for(int i = 0; i {
var colorAverage = argbMin +(int)((argbMax- argbMin)* i / size);
colorList.Add(Color.FromArgb(colorAverage));
}

如果你尝试上面的代码,你会发现, code> argb 不对应于颜色的视觉渐进增加。



有任何想法吗?

解决方案

  int rMax = Color(R,G,B组件)对每个单独执行相同的线性插值,然后重新组合。 。 
int rMin = Color.Blue.R;
// ...并且对于B,G
var colorList = new List< Color>();
for(int i = 0; i {
var rAverage = rMin +(int)((rMax-rMin)* i / size);
var gAverage = gMin +(int)((gMax - gMin)* i / size);
var bAverage = bMin +(int)((bMax - bMin)* i / size);
colorList.Add(Color.FromArgb(rAverage,gAverage,bAverage));
}


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 Colors 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.

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);
    var gAverage = gMin + (int)((gMax - gMin) * i / size);
    var bAverage = bMin + (int)((bMax - bMin) * i / size);
    colorList.Add(Color.FromArgb(rAverage, gAverage, bAverage));
}

这篇关于生成颜色渐变在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆