Skip to content

Commit

Permalink
More improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
HakanL committed Jan 10, 2022
1 parent d6c0f40 commit 4516777
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 4 deletions.
1 change: 1 addition & 0 deletions Utils/DMXrecorder/PostProcessor/Arguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public enum Commands
GenerateStatic,
GenerateRamp,
GenerateSaw,
GenerateRainbow,
Duplicate
}

Expand Down
8 changes: 8 additions & 0 deletions Utils/DMXrecorder/PostProcessor/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,14 @@ public static void Main(string[] args)
arguments.TrimDuration.Value);
break;

case Arguments.Commands.GenerateRainbow:
command = new Processor.Command.Generate(
Processor.Command.Generate.GenerateSubCommands.Rainbow,
universeIds,
arguments.Frequency,
arguments.TrimDuration.Value);
break;

default:
throw new ArgumentOutOfRangeException("Unknown command");
}
Expand Down
107 changes: 103 additions & 4 deletions Utils/DMXrecorder/Processor/Command/Generate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class Generate : ICommandOutput
private readonly double durationMS;
private readonly byte fillValue;
private readonly GenerateSubCommands subCommand;
private readonly int frames;
private double timestampMS;
private double rampUpPerFrame;
private double currentValue;
Expand All @@ -22,7 +23,8 @@ public enum GenerateSubCommands
{
Static,
Ramp,
Saw
Saw,
Rainbow
}

public Generate(GenerateSubCommands subCommand, int[] universeIds, double frequency, double durationMS, byte fillValue = 0)
Expand All @@ -33,16 +35,16 @@ public Generate(GenerateSubCommands subCommand, int[] universeIds, double freque
this.fillValue = fillValue;
this.subCommand = subCommand;

int frames = (int)(this.durationMS / (1000.0 / this.frequency));
this.frames = (int)(this.durationMS / (1000.0 / this.frequency));

switch (subCommand)
{
case GenerateSubCommands.Ramp:
this.rampUpPerFrame = 255.0 / frames;
this.rampUpPerFrame = 255.0 / this.frames;
break;

case GenerateSubCommands.Saw:
this.rampUpPerFrame = 2 * 255.0 / frames;
this.rampUpPerFrame = 2 * 255.0 / this.frames;
break;
}
}
Expand Down Expand Up @@ -88,6 +90,27 @@ private OutputFrame GetFrame()
if (this.currentValue >= 255 || this.currentValue <= 0)
this.rampUpPerFrame = -this.rampUpPerFrame;
}
else if (this.subCommand == GenerateSubCommands.Rainbow)
{
int numPixels = 170;
int writePos = 0;
double hue = this.currentValue;
double frac = 1.0 / this.frames;

for (int i = 0; i < numPixels; i++)
{
var rgb = HSL2RGB(hue, 1, 0.5);

hue += 0.01;
if (hue >= 1)
hue = 0;

frame.Data[writePos++] = rgb.R;
frame.Data[writePos++] = rgb.G;
frame.Data[writePos++] = rgb.B;
}
this.currentValue += 1.0 / this.frames;
}
else
throw new ArgumentOutOfRangeException(nameof(subCommand));

Expand All @@ -99,6 +122,82 @@ private OutputFrame GetFrame()
return inputFrame;
}

public struct RGB
{
public byte Red { get; set; }

public byte Green { get; set; }

public byte Blue { get; set; }
}

// Given H,S,L in range of 0-1
// Returns a Color (RGB struct) in range of 0-255
public static System.Drawing.Color HSL2RGB(double h, double sl, double l)
{
double v;
double r, g, b;

r = l; // default to gray
g = l;
b = l;
v = (l <= 0.5) ? (l * (1.0 + sl)) : (l + sl - l * sl);
if (v > 0)
{
double m;
double sv;
int sextant;
double fract, vsf, mid1, mid2;

m = l + l - v;
sv = (v - m) / v;
h *= 6.0;
sextant = (int)h;
fract = h - sextant;
vsf = v * sv * fract;
mid1 = m + vsf;
mid2 = v - vsf;
switch (sextant)
{
case 0:
r = v;
g = mid1;
b = m;
break;
case 1:
r = mid2;
g = v;
b = m;
break;
case 2:
r = m;
g = v;
b = mid1;
break;
case 3:
r = m;
g = mid2;
b = v;
break;
case 4:
r = mid1;
g = m;
b = v;
break;
case 5:
r = v;
g = m;
b = mid2;
break;
}
}

return System.Drawing.Color.FromArgb(
(byte)(r * 255.0f),
(byte)(g * 255.0f),
(byte)(b * 255.0f));
}

public void Execute(ProcessorContext context, IOutputWriter outputWriter)
{
OutputFrame data;
Expand Down

0 comments on commit 4516777

Please sign in to comment.