简体   繁体   English

将int值转换为渐变颜色

[英]Converting int value to a color in a gradient

I have an int value that can range from -148 to 10 and I'm trying to map this value to a color in the following gradient: 我有一个int值,范围从-148到10,并且我试图将此值映射到以下渐变中的颜色:

  • Black (-148) 黑色(-148)
  • Blue 蓝色
  • Cyan 青色
  • Green 绿色
  • Yellow 黄色
  • Orange 橙子
  • Red 红色
  • White (10) 白(10)

Visually, my color gradient looks like this: 在视觉上,我的颜色渐变如下所示:

My first thought was to break the range (-148 to 10) down into smaller groups that correspond to these eight colors but that obviously results in multiple values translating into the same color. 我的第一个想法是将范围(-148到10)分解为与这八种颜色相对应的较小组,但是显然导致多个值转换为同一颜色。 I'd like to make use of many more or even all the colors in this gradient but I'm not sure how to go about doing this. 我想在此渐变中使用更多或什至所有颜色,但是我不确定该怎么做。 Can anyone offer some advice? 谁能提供一些建议? For what it's worth, I'm working in C#/.NET so I need to translate my int into RGB values. 对于它的价值,我正在C#/。NET中工作,因此需要将int转换为RGB值。

You are actually looking at the HSV color space. 您实际上正在查看HSV颜色空间。 What you can do is: 您可以做的是:

1 Map your [-148,10] to the [0,360] degree in H 1将[-148,10]映射为H的[0,360]度

2 Fix the S And V value. 2固定S和V值。 (I guess S=1 and V=1 in your case) (我猜在您的情况下,S = 1,V = 1)

3 Get the HSV color from the previous computed H, S, and V 3从先前计算的H,S和V获取HSV颜色

4 Convert HSV color to RGB color and use. 4将HSV颜色转换为RGB颜色并使用。

Did you mean a mapping function like this? 您是说这样的映射功能吗? (The Transparent entry in the colors list is a dummy in the case of value 10 where color1 is taken from the 9th entry of the list) (在颜色列表中的“透明”条目在值为10的情况下是虚拟的,其中,颜色1从列表的第9个条目中选取)

  Color[] colors = new Color[] { Colors.Black, Colors.Blue, Colors.Cyan, Colors.Green, Colors.Yellow, Colors.Orange, Colors.Red, Colors.White, Colors.Transparent }; public Color IntToColor(int i) { float scaled = (float)(i + 148) / 158 * 7; Color color0 = colors[(int)scaled]; Color color1 = colors[(int)scaled + 1]; float fraction = scaled - (int)scaled; Color result = new Color(); result.R = (byte)((1 - fraction) * (float)color0.R + fraction * (float)color1.R); result.G = (byte)((1 - fraction) * (float)color0.G + fraction * (float)color1.G); result.B = (byte)((1 - fraction) * (float)color0.B + fraction * (float)color1.B); result.A = 255; return result; } 

PS: For simplicity there is no check for the [-148:10] range PPS: I know my code will not win any beauty contest. PS:为简单起见,没有检查[-148:10]范围PPS:我知道我的代码不会赢得任何选美比赛。

Your image has a width of 200 and a size of 203 bytes. 您的图片的宽度为200,大小为203个字节。 I would read out 159 values and store them in a List. 我会读出159个值并将它们存储在列表中。 Of course you can calculate them in c# but that will take more resources, imo.. 当然,您可以在c#中计算它们,但这会占用更多资源,imo。

    Dictionary<int, Color> mycolors = new Dictionary<int,Color>();
    Bitmap bmp = new Bitmap("D:\\spectrum.png");

    float fx = bmp.Width / 159f;
    for (int x = 0; x < 160; x++)
        mycolors.Add(x - 148, bmp.GetPixel((int)(fx * x), 1));

This will map 159 colors. 这将映射159种颜色。 If you want more than these (or than the 200 your image holds) a calculation is called for. 如果您想要的不止这些(或者您的图像拥有的200个以上),则需要进行计算。 But you can't map them into your integer range - so please make up your mind! 但是您不能将它们映射到整数范围内-因此请下定决心! Here is code that implements a HSV->RGB mapping, which you could use, at least going from blue to red. 是实现HSV-> RGB映射的代码,您可以使用它,至少从蓝色变为红色。 The fades to black and to white would have to come from two more routines.. best in the way of Fratyx's interpolation code. 淡入黑色和淡入白色必须来自另外两个例程。最好以Fratyx的内插代码方式进行。 (Which as it is will cut a little away from the outer ring of clearest colors for each segment..) (尽管这样,它将与每个段的最清晰颜色的外环切开一点。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM