颜色表算法 [英] Color Table Algorithm

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

问题描述

我一直在搜索有关如何创建类似内容的一个多星期的内容。





我有代码和for循环通过X和Y坐标创建所有面板。每个面板都是数组的一部分,从左上角的0开始,到右上角的90结束,但是我不在乎它的完成方式,面板及其工作方式。颜色不必相同但可以相似,这样我就可以拥有全屏颜色选择器。如果有人知道某种代码可以采用一种特定的颜色并使它变亮十倍,则可以使用Color.FromARGB或仅使用Color类将面板设置为backColor,然后请帮助我。谢谢。



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

解决方案

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





这里有很多东西。这是我使用的一个:

 颜色HsvToRgb(double h,double S,double V)
{
///将HSV转换为RGB
/// h从0d-360d
/// s,v值为0d-1d
/// r,g,b值为0-255

int hi = Convert.ToInt32(Math.Floor(hue / 60))%6;
double f =色相/ 60-Math.Floor(hue / 60);

value =值* 255;
int v = Convert.ToInt32(值);
int p = Convert.ToInt32(值*(1-饱和度));
int q = Convert.ToInt32(值*(1- f *饱和度));
int t = Convert.ToInt32(值*(1-(1-f)*饱和度));

如果(hi == 0)返回Color.FromArgb(255,v,t,p);
else if(hi == 1)返回Color.FromArgb(255,q,v,p);
else if(hi == 2)返回Color.FromArgb(255,p,v,t);
else if(hi == 3)返回Color.FromArgb(255,p,q,v);
else if(hi == 4)返回Color.FromArgb(255,t,p,v);
else return Color.FromArgb(255,v,p,q);
}

请注意输入范围!



现在很容易在类级别设置 Color 数组:

  int宽度= 10; 
int高度= 9;
颜色[,]颜色;

并填写:

  void loadColors()
{
colors = new Color [width,height];

//为(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);
//负载矩阵:(int j = 2; j< height; j ++)
for(int i = 0; i< width; i ++)
颜色[i,j] = HsvToRgb(i * 360f /宽度,1f,1f *(高度-j + 2)/高度);
}

这是设置 BackColors的快捷方式您的面板中的



这里是表格。 Paint 函数,我曾用来创建上述屏幕截图:

  private void Form1_Paint(对象发送者,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 ++)
{
使用(SolidBrush笔刷=新的SolidBrush(colors [i,j]))
e.Graphics.FillRectangle(brush,i * w,j * h,w,h);
}
}

当然,将两个数字更改为制作一个更好的网格,这里是20x20:





还要注意,色调的均匀间隔实际上并不是很好地工作,因为



使用这种单线:

  private void pictureBox1_MouseClick(对象发送者,MouseEventArgs e)
{
int hue =(int)((Bitmap)pictureBox1.Image).GetPixel (eX,eY).GetHue();
}

上面的图片为我们提供了这样的色调列表:

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

我做了一些修改,也许是为了使其在显示器上更好地工作:

  List< int>色调=新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 函数替换为大大简化的函数。


I've been searching for just over a week now on how to create something like this.

I have the code and for loops to create each Panel by X, and Y coords and everything. Each Panel is part of an array and starts at 0 at top left and ends at 90 at top right, but I don't care how its done as long as its done and its panels and works. The colors don't need to be the same but something similar so that I can have a fullscreen color picker. If someone knows some code to take one specific color and make it brighter ten times to set the panels backColor, using Color.FromARGB or just the Color class, to then please help me out. Thank you.

(This is a app I'm making for windows tablet and is touchscreen. The purpose of the app is to be fullscreen and not reviele that its a windows tablet, There for I have to make the color picker myself and cannot use the built in color dialog.)

解决方案

For best control I suggest using a color calculation function.

There are many out there; here is one I use:

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

Do note the input ranges!!

Now it is easy to setup a Color array at class level:

int width = 10;
int height = 9;
Color[,] colors;

And fill it:

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

From this is is a snap to set the BackColors of your Panels.

Here is a Form.Paint function, I used to create the above screenshot:

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

Of course it is as simple as changing two numbers to make a finer grid, here 20x20:

Also note how the even spacing of hues doesn't really work well, as neither the human eye nor our common display systems are equally sensitive to changes in hues across the spectrum..

The eye is actually rather sensitive to greenish hues

the just-noticeable difference in wavelength varies from about 1 nm in the blue-green and yellow wavelengths, to 10 nm and more in the longer red and shorter blue wavelengths

but our monitors do a pretty bad job at creating different green hues..

Using an adapted list of perceptionally evenly spaced hues might help, depending on what you want..

Using this one-liner:

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

on the image above gives us one such list of hues:

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

I have modified it a little, maybe to make it work better with my monitor:

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

And changing the above code (keeping width = 20) to

HsvToRgb(hues[i],..

results in this:

Update: I have replaced the HsvToRgb function by a greatly simplified one.

这篇关于颜色表算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆