0

目前我正在为 C# .NET 开发 DMX 库。目前,我坚持基于“开始颜色”和“结束颜色”创建颜色过渡。

该函数有 3 个参数,第一个是 DMXController 对象(基本上是扩展的 SerialPort),第二个是 startColor,第三个是 endColor。

整个 trahsition 将在单独的线程中处理,因此应用程序不会挂起。

DMX 客户端只是一个 RGB LED 控制器,因此它接受字面意义上的 RGB 值(例如 Red = 255, 0, 0)

我见过一些固定颜色的例子,但对于这个项目,任何颜色都可以使用。

如果我是正确的,最大步数将是 255 步。

完成这项工作的最有效方法是什么?循环中的每一步都将发送到 DMXController,因此它必须是某种 for-next 或 while 循环,并且每一步都将被发送。

到目前为止,这是我的代码:

    public static void FadeColor(DMXController controller, Color startColor, Color endColor)
    {
        Color currentColor = startColor;

        Thread fadeColorThread = new Thread(delegate()
        {
            // Start For-Next / While loop

            // Update currentColor with new RGB values

            controller.SetChannel(1, currentColor.R);
            controller.SetChannel(2, currentColor.G);
            controller.SetChannel(3, currentColor.B);
            controller.Update();

            // If neccesary a delay like Thread.Sleep(5);

            // End For-Next / While loop

        });
        fadeColorThread.Name = "DMX Color Transition Thread";
        fadeColorThread.Start();
    }

如果在开始转换之前从颜色对象中提取 r、g 和 b 值更快,我将实现它。

4

1 回答 1

1

好的,修好了!这是现在的工作代码:

    public static void FadeColor(DMXController controller, Color startColor, Color endColor, double accuracy = 1)
    {
        if (accuracy <= 0)
            return;

        Thread fadeColorThread = new Thread(delegate()
        {
            Color color = Color.Empty;
            using (Bitmap bmp = new Bitmap((int)(256 * accuracy), 1))
            {
                using (Graphics gfx = Graphics.FromImage(bmp))
                {
                    using (LinearGradientBrush brush = new LinearGradientBrush(new Point(0, 0), new Point(bmp.Width, bmp.Height), startColor, endColor))
                    {
                        gfx.FillRectangle(brush, brush.Rectangle);

                        controller.SetColor(startColor);

                        for (int i = 0; i < bmp.Width; i++)
                            controller.SetColor(bmp.GetPixel(i, 0));

                        controller.SetColor(endColor);
                    }
                }
            }
        });
        fadeColorThread.Name = "DMX Color Transition Thread";
        fadeColorThread.Start();
    }
}
于 2015-01-31T23:31:59.453 回答