颜色表算法

我一直在寻找一周以来如何创造这样的东西。

在此处输入图像描述

我有代码和for循环来创建每个Panel X,和Y coords和一切。 每个Panel都是一个数组的一部分,从左上角的0开始到右上角的90结束,但我不关心它的完成情况以及它的面板和工作。 颜色不需要相同,但类似的东西,以便我可以有一个全屏颜色选择器。 如果有人知道一些代码采用一种特定的颜色并使其更亮十倍来设置面板backColor,使用Color.FromARGB或只是Color类,那么请帮助我。 谢谢。

(这是我正在为Windows平板电脑制作的应用程序,并且是触摸屏。该应用程序的目的是全屏而不是它的Windows平板电脑,我必须自己制作颜色选择器,不能使用内置的颜色对话框。)

为了获得最佳控制,我建议使用颜色计算function。

在此处输入图像描述

那里有很多; 这是我用的一个:

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]; // load greys for (int i = 0; i < width; i++ ) colors[i, 0] = HsvToRgb(0f, 0f, 1f * i / width); // load bright stripe: for (int i = 0; i < width; i++) colors[i, 1] = HsvToRgb(i* 360f / width, 0.33f, 1f); // load matrix: 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); } 

从中可以快速设置PanelsBackColors

这是一个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 nm变化到10 nm以及更长的红色和更短的蓝色波长

但我们的显示器在创造不同的绿色色调方面做得非常差劲。

根据您的需要,使用经过调整的均匀间隔色调的改编列表可能会有所帮助。

在此处输入图像描述

使用这个单线程:

 private void pictureBox1_MouseClick(object sender, MouseEventArgs e) { int hue = (int) ((Bitmap)pictureBox1.Image).GetPixel(eX, eY).GetHue(); } 

在上图中给出了一个这样的色调列表:

 20 32 41 50 58 72 133 163 170 177 183 190 197 206 269 288 307 324 334 346 

我稍微修改了一下,也许是为了让我的显示器更好用:

 List hues = new List { 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函数替换了它。

通过这个答案,您将有可能使用颜色的HSB表示(也可以在这里查看 )。

通过使用它,您可以在第一行使用随机色调,零饱和度和亮度从所需步骤的1.0到0.0。 对于其他行,您必须采用1.0作为饱和度,并在相同的步数中将色调从0增加到360,并且还将饱和度从每行0.0增加到1.0。

我只是举个例子:

 private void OnResize(object sender, EventArgs e) { Invalidate(); } protected override void OnPaint(PaintEventArgs e) { UpdateImage(e.Graphics); } private void UpdateImage(Graphics graphics) { var columns = 10; var rows = 8; var hueSteps = 360 / columns; var columnSteps = 1.0F / columns; var width = Width / columns; var height = Height / (rows + 1); for (int i = 0; i < columns; i++) { var gray = ColorExtensions.FromAhsb(255, 0, 0, columnSteps * i); using (var brush = new SolidBrush(gray)) { graphics.FillRectangle(brush, width * i, 0, width, height); } } for (int i = 0; i < columns; i++) { for (int j = 1; j <= rows; j++) { var color = ColorExtensions.FromAhsb(255, hueSteps * i, 1, columnSteps * j); using (var brush = new SolidBrush(color)) { graphics.FillRectangle(brush, width * i, height * j, width, height); } } } } 

结果并不完全相同,但这只是重新安排循环的问题: 颜色表