1

我目前正在尝试创建一个简单的绘画工具,但我遇到了一个问题,或者可能需要更多建议。

我正在尝试创建一个画笔,所以当我绘制某个像素“中心”时,它也会绘制附近的像素。

例如:

1 pixel (center point)
[ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ]
[ ][ ][*][ ][ ]
[ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ]

2 pixel:
[ ][ ][ ][ ][ ]
[ ][ ][*][ ][ ]
[ ][*][*][*][ ]
[ ][ ][*][ ][ ]
[ ][ ][ ][ ][ ]

3 pixels:
[ ][ ][*][ ][ ]
[ ][*][*][*][ ]
[*][*][*][*][*]
[ ][*][*][*][ ]
[ ][ ][*][ ][ ]

此外,只有中心点应该是 100% 的颜色。我以后怎样才能取原来的颜色并稍微淡出它。

例子:

1 像素,100% 颜色

2 像素,中心点 100% 的颜色,附近的像素略微褪色或可能 (50%)

10个像素,中心点100%的颜色,离中心点最远的像素应该是原色最淡的。

因此,一种计算附近颜色的算法和一种制作画笔图案的算法。

有什么建议么?

颜色强度方法的实现(不起作用)更新 1:更改为 Math.Pow(x, y) - 较慢且不起作用

    public static Color GetColorIntensity(Color c, double r, Point position, Point centerPosition)
    {
        double distance = Math.Sqrt((Math.Pow(position.X, 2)) + (Math.Pow(position.Y, 2)));
        double relative_distance = distance / r;
        double alpha = (1.0 - relative_distance);

        return Color.FromArgb((int)alpha, c.R, c.G, c.B);
    }
4

2 回答 2

4
  1. Calculate the distance of each pixel from the centre of the brush.
  2. Assign a transparency scaling from 1.0 (centre of brush) to 0.0 (edge of brush.)

If you use the Euclidean distance, d = sqrt(x^2 + y^2), you get a circular brush.

If you use the taxicab distance, d = abs(x) + abs(y), you get a diamond-shaped brush.

Here's Python for a function that calculates the intensity of a pixel with coordinates x,y relative to the centre of the brush, with a brush radius of r. a = 1.0 means "full opaque", and a = 0.0 means "fully transparent".

def intensity(x,y,r):
    distance = sqrt( x**2 + y **2 )
    relative_distance = distance / r
    alpha = (1.0 - relative_distance)
    return alpha

There's probably an off-by-one error in there (the brush will be one pixel smaller than r), but you can fix that yourself.

于 2012-05-10T12:03:57.167 回答
1

编辑#2 因为我需要解决难题,我接受了其他人给出的答案,纠正了他们的数学错误,并提出了我认为正是您正在寻找的东西。

试试这个 我创建了一个新的 Windows 窗体应用程序,我添加了一个 numericupdown(画笔大小)、一个轨迹栏(淡入淡出强度)和一个按钮,当您单击该按钮时,它开始从中心开始填充面板并向外移动,刷子编号将是纯色,然后根据褪色强度,每一行向外的褪色会越来越多。

 public Form1()
    {
        InitializeComponent();
    }
    Panel[,] pb = new Panel[9, 9];
    private void Form1_Load(object sender, EventArgs e)
    {
       //this is just to add the panels to the screen.  
       //We are using panels to simulate zoomed in pixels so we can see what is going on
        for (int y = 0; y < 9; y++)
            for (int x = 0; x < 9; x++)
            {
                pb[x, y] = new Panel();
                pb[x, y].BackColor = Color.MistyRose;
                pb[x, y].BorderStyle = BorderStyle.FixedSingle;
                pb[x, y].Size = new Size(20, 20);
                pb[x, y].Location = new Point(x * 20, y * 20);
                this.Controls.Add(pb[x,y]);
            }
    }
    private void button1_Click(object sender, EventArgs e)
    {
        DrawPixel(new Point(4, 4), (int)numericUpDown1.Value,trackBar1.Value+1);
    }

    public void DrawPixel(Point pixel, int radius,int intensity)
    {
        radius--;
        for (int y = 0; y <= 8; y++)
            for (int x = 0; x <= 8; x++)
            {
                //clears the screen before starting
                pb[x, y].BackColor = Color.White;
                //calculate the distance from the center pixel
                double Distance = Math.Sqrt( ((x-pixel.X)*(x-pixel.X) + (y-pixel.Y) *(y-pixel.Y))-radius*radius );
                //determine color intensity
                int alpha = (int)Distance <= 0 ? 255 : (int)Distance > intensity ? 0 : (int)(intensity - Distance) * 50 > 255 ? 255 : (int)(intensity - Distance) * 50;

                if (alpha>0)
                    pb[x, y].BackColor = Color.FromArgb(alpha, Color.Red);
            }
    }
于 2012-05-29T18:43:01.277 回答