提取颜色渐变像素信息 [英] Extract colour gradient pixel information

查看:21
本文介绍了提取颜色渐变像素信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 artnet 到 dmx 控制器来控制 240 长的 RGB 像素线 (ws2812b),并且需要沿着像素线的长度生成颜色渐变.

I am attempting to control a 240 long line of RGB pixels (ws2812b) using an artnet to dmx controller and need to generate colour gradients down the length of the line of pixels.

我想到了使用 C# 内置图形库来生成颜色渐变,然后提取各个像素值并将它们发送到 dmx 控制器.

I had the idea of using the C# built in graphics libraries to generate the colour gradients and then extract the individual pixel values and send these to the dmx controller.

是否可以从应用于形状(线/矩形等)的 LinearGradientBrush 或 LinearGradientBrush 中提取单独的插值值?

Is it possible to extract individual interpolated values from a LinearGradientBrush or a LinearGradientBrush applied to a shape (line/rectangle etc)?

推荐答案

这里有一个函数,它接受一个停止颜色列表并返回一个均匀插值的颜色列表:

Here is a function that takes a list of stop colors and returns a list of evenly interpolated colors:

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

240 和 740 色.要获得所有不同的颜色,请确保它们不会太多也不会太接近,因为两种颜色之间的 RGB 色调的最大数量是 256,因此第二个示例可能会达到该限制.

240 and 740 colors. To get all distinct colors make sure they are not too many and not too close, as the maximum number of RGB hues between two colors is 256, so the second example may hit that limit..

这篇关于提取颜色渐变像素信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆