1

我很想在地图上添加点。不管这张地图上有多少个图钉,每个图钉都会有自己的颜色。我只想改变色调

我正在使用一个看起来像这样的模板 png

在此处输入图像描述

我想创建一个函数,当出现新点时,该文件将随机着色。

我该怎么做?

我正在使用下面的代码 - 我无法弄清楚如何在矩阵中抛出随机值以输出任何在色板中间隔足够远的好颜色

 private Bitmap ColorMyPin()
{
    Image imgPicture = Properties.Resources.green_MarkerBlank;

    Bitmap bmp = new Bitmap(imgPicture.Width, imgPicture.Height);


    ImageAttributes iaPicture = new ImageAttributes();
    ColorMatrix cmPicture = new ColorMatrix(new float[][]
    {
        new float[] {0, 0, 0, 0, 0},
        new float[] {0, 0, 0, 0, 0},
        new float[] {0, 0, 0, 0, 0},  <-- //Hard part where do i throw random() values at
        new float[] {0, 0, 0, 0, 0},
        new float[] {0, 0, 0, 0, 0}
    });


    // Set the new color matrix
    iaPicture.SetColorMatrix(cmPicture);

    // Set the Graphics object from the bitmap
    Graphics gfxPicture = Graphics.FromImage(bmp);

    // New rectangle for the picture, same size as the original picture
    Rectangle rctPicture = new Rectangle(0, 0, imgPicture.Width, imgPicture.Height);

    // Draw the new image
    gfxPicture.DrawImage(imgPicture, rctPicture, 0, 0, imgPicture.Width, imgPicture.Height, GraphicsUnit.Pixel, iaPicture);

    return bmp;
}

在此处输入图像描述

4

2 回答 2

2

I believe you're looking for the ColorMatrix class...I admit I'm not a wizard with image manipulation, but here are some references:

And here is a quick and ugly blob of sample code I put together:

void Main()
{
        var redPinPath = @"c:\temp\pin.png";
        var redPin = Bitmap.FromFile(redPinPath);

        var window1 = new Form();
        window1.BackgroundImage = redPin;
        window1.Show();

        for(var hue = 0.01f; hue < Math.PI; hue += 0.01f)
        {
            var pinCopy = Bitmap.FromFile(redPinPath);
            AlterHue(hue, pinCopy);
            window1.BackgroundImage = pinCopy;
            window1.Invalidate();
            Application.DoEvents();
        }
}

// Define other methods and classes here
public void AlterHue(float newHue, Image image)
{
    var lumR = 0.213f;
    var lumG = 0.715f;
    var lumB = 0.072f;
    var cosVal = (float) Math.Cos(newHue);
    var sinVal = (float) Math.Sin(newHue);
    var imgGraphics = Graphics.FromImage(image);
    var pinAttributes = new ImageAttributes();
    var colorMatrix = new ColorMatrix(new float[][]
    {
        new float[] {lumR + cosVal * (1 - lumR) + sinVal * (-lumR),     lumG + cosVal * (-lumG) + sinVal * (-lumG),         lumB + cosVal * (-lumB) + sinVal * (1 - lumB), 0, 0},
        new float[] {lumR + cosVal * (-lumR) + sinVal * (0.143f),         lumG + cosVal * (1 - lumG) + sinVal * (0.140f),     lumB + cosVal * (-lumB) + sinVal * (-0.283f), 0, 0},
        new float[] {lumR + cosVal * (-lumR) + sinVal * (-(1 - lumR)),     lumG + cosVal * (-lumG) + sinVal * (lumG),             lumB + cosVal * (1 - lumB) + sinVal * (lumB), 0, 0},
        new float[] {0, 0, 0, 1, 0},
        new float[] {0, 0, 0, 0, 1}
    });

    var sizeRect = new Rectangle(0, 0, image.Width, image.Height);
    pinAttributes.SetColorMatrix(colorMatrix);
    imgGraphics.DrawImage(image, sizeRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, pinAttributes);
}
于 2013-03-19T22:25:26.203 回答
2

有据可查的是,在区分图表、图形、网格或地图上的颜色时,人眼并不能很好地做到这一点。我已经看到建议的上限低至 8 种,高至 15 种独特的颜色(假设观看者不是色盲)。使用调色板可能会更好。

如果您想增加可能的引脚数量,您可以将调色板中的颜色数量乘以一些易于区分的形状(圆形、三角形、正方形等)。您甚至可以使用双色调方法(粗边框和内部颜色),这会将您的总数(颜色 * 形状)提高到二次方。

因此,假设 8 种颜色、4 种形状和两种色调的配色方案,您将能够表示 1,024 个 unqiue 引脚。

如需进一步阅读,请查看这篇关于颜色和数据视觉表示的优秀文章: http ://www.perceptualedge.com/articles/visual_business_intelligence/rules_for_using_color.pdf

于 2013-03-19T21:20:42.603 回答