0

Currently I'm working on a DMX Library for C# .NET. At the moment i'm stuck at creating a color transition based on a "start color" and an "end color".

The function takes 3 arguments, first is the DMXController object (basically an extended SerialPort), second is the startColor and third is endColor.

The whole trahsition will be handled in a separate thread, so the application won't hang.

The DMX Client is just a RGB LED controller so it accepts literally RGB values (e.g. Red = 255, 0, 0)

I've seen some examples with fixed colors but for this project any color can be used.

If I'm correct the maximum number of steps will be 255 steps.

What's the most efficient way to get this done? Every step in the cycle will be send to the DMXController so it must be some kind of for-next or while loop and every step will be send.

Here's my code so far:

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

If it's faster to extract r, g, and b values from the color objects before starting the transition I'll implement that.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
  • 1
    I found that color transitions can very nicely be pulled from the result of a lineargradientbrush fill. – TaW Jan 31 '15 at 22:33
  • TaW, thanks for your comment. You mean generating a lineargradientbrush with a rectangle of 255 wide and 1 height to get the gradient and then get the color pixel by pixel (incrementing x position)? – Matthijs de Ridder Jan 31 '15 at 22:39
  • 1
    Exactly. Of course there will not be 256 distinct between __all__ colors but in 24bit RGB space there will also never be more. ( (And in case you want to use more colors there is a nice format to expand the lgb..) – TaW Jan 31 '15 at 23:00
  • In my opinion it's a bit ugly to use the graphics card only for calculating RGB values but at least it works :) – Matthijs de Ridder Jan 31 '15 at 23:30
  • I don't think the graphics card is involved.. – TaW Feb 01 '15 at 00:18

1 Answers1

1

OK, fixed! This is the working code for now:

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