为了更好的控制,我建议使用颜色计算函数。
有很多选择;这是我使用的其中之一:
Color HsvToRgb(double h, double S, double V)
{
/// Convert HSV to RGB
/// h is from 0d - 360d
/// s,v values are 0d - 1d
/// r,g,b values are 0 - 255
int hi = Convert.ToInt32(Math.Floor(hue / 60)) % 6;
double f = hue / 60 - Math.Floor(hue / 60);
value = value * 255;
int v = Convert.ToInt32(value);
int p = Convert.ToInt32(value * (1 - saturation));
int q = Convert.ToInt32(value * (1 - f * saturation));
int t = Convert.ToInt32(value * (1 - (1 - f) * saturation));
if (hi == 0) return Color.FromArgb(255, v, t, p);
else if (hi == 1) return Color.FromArgb(255, q, v, p);
else if (hi == 2) return Color.FromArgb(255, p, v, t);
else if (hi == 3) return Color.FromArgb(255, p, q, v);
else if (hi == 4) return Color.FromArgb(255, t, p, v);
else return Color.FromArgb(255, v, p, q);
}
请注意输入范围!!
现在可以轻松设置一个类级别的
Color
数组:
int width = 10;
int height = 9;
Color[,] colors;
并填充它:
void loadColors()
{
colors = new Color[width, height];
for (int i = 0; i < width; i++ ) colors[i, 0] = HsvToRgb(0f, 0f, 1f * i / width);
for (int i = 0; i < width; i++) colors[i, 1] = HsvToRgb(i* 360f / width, 0.33f, 1f);
for (int j = 2; j < height; j++)
for (int i = 0; i < width; i++)
colors[i, j] = HsvToRgb(i * 360f / width, 1f, 1f * (height - j + 2) / height);
}
从这里开始,设置您的面板的BackColors就轻而易举了。
以下是我用于创建上面截图的Form.Paint函数:
private void Form1_Paint(object sender, PaintEventArgs e)
{
int w = ClientSize.Width / width;
int h = ClientSize.Height / height;
for (int j = 0; j < height; j++)
for (int i = 0; i < width; i++)
{
using (SolidBrush brush = new SolidBrush(colors[i,j]))
e.Graphics.FillRectangle(brush, i * w, j * h, w, h);
}
}
当然,只需更改两个数字即可使网格更细,这里是20x20:
此外,还要注意到色调的均匀间距并不起作用,因为无论是
人眼还是我们常见的显示系统对光谱中色调的变化都不是同样敏感的。
事实上,眼睛对绿色色调非常
敏感。
在蓝绿和黄色波长中,可察觉的波长差异约为1纳米,而在较长的红色和较短的蓝色波长中则达到10纳米甚至更多
但是我们的监视器在创建不同的绿色色调方面表现得相当糟糕。
使用一个经过适应的
感知均匀间距色调列表可能会有所帮助,具体取决于您想要什么。
使用这个一行代码:
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
int hue = (int) ((Bitmap)pictureBox1.Image).GetPixel(e.X, e.Y).GetHue();
}
在上面的图片中,我们可以看到这样一个颜色列表:
20 32 41 50 58 72 133 163 170 177 183 190 197 206 269 288 307 324 334 346
我稍微修改了一下,可能让它在我的显示器上更好地运行:
List<int> hues = new List<int>
{ 20, 32, 41, 50, 58, 72, 133, 162, 180, 188, 195, 205, 215, 223, 246, 267, 288, 300, 320, 346 };
我希望你能帮忙翻译以下内容:将上述代码更改(保持宽度为20)为
HsvToRgb(hues[i],..
结果为:
更新:我已经用一个简化了的函数替换了HsvToRgb
函数。