C# Windows颜色转换为单个整数

C# Windows颜色转换为单个整数,c#,colors,drawing,system,C#,Colors,Drawing,System,我使用的API只支持颜色的RGB组件的字节值。我试图区分不同数量的项目,比如8和12。我想为每件商品指定一种颜色。因此,如果有10个项目,我会将范围划分为10个,并为每个项目分配相应的颜色(想想彩虹)。看起来toArgb()可以工作,但更多的读数表明它可能使用十六进制。我显然需要提取RGB值以发送回API 我如何才能做到这一点?据我所知,您将输入颜色范围和项目数量。此外,您希望生成的颜色表示颜色渐变 试试这个: // set the edges of the color range var fi

我使用的API只支持颜色的RGB组件的字节值。我试图区分不同数量的项目,比如8和12。我想为每件商品指定一种颜色。因此,如果有10个项目,我会将范围划分为10个,并为每个项目分配相应的颜色(想想彩虹)。看起来
toArgb()
可以工作,但更多的读数表明它可能使用十六进制。我显然需要提取RGB值以发送回API


我如何才能做到这一点?

据我所知,您将输入颜色范围和项目数量。此外,您希望生成的颜色表示颜色渐变

试试这个:

// set the edges of the color range
var firstColor = Color.Red;
var secondColor = Color.Green;

int rMin = firstColor.R;
int rMax = secondColor.R;
int bMin = firstColor.B;
int bMax = secondColor.B;
int gMin = firstColor.G;
int gMax = secondColor.G;

int size = 10;

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);
    var currentColor = Color.FromArgb(rAverage, gAverage, bAverage);
    // TODO: use the currentColor
}
//设置颜色范围的边缘
var firstColor=Color.Red;
var secondColor=Color.Green;
int rMin=firstColor.R;
int rMax=secondColor.R;
int bMin=firstColor.B;
int bMax=secondColor.B;
int gMin=firstColor.G;
int gMax=secondColor.G;
int size=10;
对于(int i=0;i
您想要实现什么目标?你需要一个算法来生成给定数量的不同颜色吗?你的观点是什么?它是按颜色分类的吗?这些颜色应该有渐变过渡吗?是的,我会用一个算法来生成给定数量的颜色。但我真的需要知道颜色输入的格式,可以分成相等的部分。在屏幕上,用户将试图组织项目。该项目目前是黑白,分配颜色将更容易匹配。