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

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

问题描述

我在这里的问题类似于这里的问题,除了我使用的是 C#.

My question here is similar to the question here, except that I am working with C#.

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

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));
}

如果你尝试上面的代码,你会发现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);
    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天全站免登陆