2015-11-03 51 views
0

我試圖控制240像素長的RGB像素(ws2812b)使用artnet到dmx控制器,並需要在像素線的長度下生成顏色漸變。提取顏色漸變像素信息

我曾想過使用C#內置的圖形庫來生成顏色漸變,然後提取各個像素值並將它們發送到dmx控制器。

是否可以從LinearGradientBrush或LinearGradientBrush應用於形狀(線/矩形等)中提取單個插值?

+0

它簡單且便宜繪製到所需長度和高度= 1的位圖上。然後使用GetPixel拉出所有的顏色..!看[這裏](http://stackoverflow.com/questions/30339553/fill-panel-with-gradient-in-three-colors/30341521?s=2|0.5508#30341521)爲例子,描繪條紋和[在這裏](http://stackoverflow.com/questions/26461579/displaying-heatmap-in-datagridview-from-listlistt-in-c-sharp/26482670?s=9|0.0451#26482670)提取一個漸變列表顏色(看一下'interpolateColors'函數!比編寫數學函數便宜得多 – TaW

回答

0

你可以做的是讓畫筆在位圖上畫一條線,並從中提取像素,但我認爲這將是不必要的昂貴和複雜。簡單的lerping就是你想要的顏色。使用此爲R,G和要之間線性插值的顏色的B值

float Lerp(float from, float to, float amount) 
{ 
    return from + amount * (to - from); 
} 

和:

這可通過寫一個線性插值方法,像這樣來實現。例如:

Color Lerp(Color from, Color to, float amount) 
{ 
    return Color.FromArgb(
     (int)Lerp(from.R, to.R, amount), 
     (int)Lerp(from.G, to.G, amount), 
     (int)Lerp(from.B, to.B, amount)); 
} 

我希望這會有所幫助。
〜盧卡

0

這裏是一個函數,它的停止顏色的列表,並返回均勻地插顏色列表:

List<Color> interpolateColors(List<Color> stopColors, int count) 
{ 
    List<Color> ColorList = new List<Color>(); 

    using (Bitmap bmp = new Bitmap(count, 1)) 
    using (Graphics G = Graphics.FromImage(bmp)) 
    { 
     Rectangle bmpCRect = new Rectangle(Point.Empty, bmp.Size); 
     LinearGradientBrush br = new LinearGradientBrush 
           (bmpCRect, Color.Empty, Color.Empty, 0, false); 
     ColorBlend cb = new ColorBlend(); 

     cb.Colors = stopColors.ToArray(); 
     float[] Positions = new float[stopColors.Count]; 
     for (int i = 0; i < stopColors.Count; i++) 
       Positions [i] = 1f * i/(stopColors.Count-1) 
     cb.Positions = Positions; 
     br.InterpolationColors = cb; 
     G.FillRectangle(br, bmpCRect); 
     for (int i = 0; i < count; i++) ColorList.Add(bmp.GetPixel(i, 0)); 
     br.Dispose(); 
    } 
    return ColorList; 
} 

你可以稱其爲:

List<Color> ColorList = interpolateColors(
         new List<Color>{Color.Red, Color.Blue, Color.Yellow}, 240); 

enter image description here enter image description here

240和740顏色。要獲得所有不同顏色,請確保它們不是太多,也不要太靠近,因爲兩種顏色之間的RGB色調的最大數量是256,所以第二個示例可能會達到此限制。