2015-11-03 143 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,所以第二个示例可能会达到此限制。