2013-03-19 61 views
1

我tyring把點添加到一個地圖。無論該地圖上有多少針腳,每個針腳都會獲得自己的顏色。我想只改變色調如何將位圖更改爲另一種隨機顏色?

我使用的模板PNG,看起來像這樣

enter image description here

我想創建功能,當一個新的點出現這個文件會得到隨機的顏色。

我該怎麼做?

代碼下面我的工作 - 我無法弄清楚如何在矩陣扔隨機值輸出間隔足夠遠的顏色齶任何良好的色彩

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

enter image description here

+0

那麼什麼是你的問題?它看起來像你正在得到你需要的結果? – 2013-03-19 21:10:23

+0

@RowlandShaw我用GIMP這樣做。我試圖在c#中以編程方式執行此操作。我不知道如何改變顏色又單獨色調只 – stackoverflow 2013-03-19 21:12:21

+1

看看這個答案,http://stackoverflow.com/a/383305/525138代碼,他們正在改變一個JPG,但應沿同樣的線條 – Dutts 2013-03-19 21:14:49

回答

2

這是相當有據可查的,當涉及到的圖表,圖形,網格或地圖的顏色區分,人的眼睛沒有這樣做很好。我已經看到推薦低至8和高達15種獨特的顏色上限(並且假定觀看者不色盲)。使用調色板可能會更好。

如果你想增加可能的引腳的數量,你可以通過一些數字很容易區分的形狀(圓形,三角形,方形等)的繁殖,在調色板中的顏色數量。你甚至可以使用雙音方法(粗邊框和內部顏色),這將提高你的總數量(色彩*形狀)的第二電源。

因此,假設8種顏色,4種形狀和雙色調着色方案,您將能夠代表1,024個獨立引腳。

對於進一步閱讀,檢查出的顏色這個優秀的文章和數據的可視化表示: http://www.perceptualedge.com/articles/visual_business_intelligence/rules_for_using_color.pdf

2

我相信你正在尋找的ColorMatrix類......我承認我不是一個嚮導圖像處理,但這裏有一些參考:

這裏是示例代碼的快速和醜陋的斑點我放在一起:

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); 
} 
相關問題