-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
392 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
using System; | ||
|
||
namespace Spectrum { | ||
internal class Color { | ||
|
||
public byte R; | ||
public byte G; | ||
public byte B; | ||
public double H; | ||
public double S; | ||
public double V; | ||
|
||
public Color(byte r, byte g, byte b) { | ||
R = r; | ||
G = g; | ||
B = b; | ||
double max = Math.Max(Math.Max(r / 255.0d, g / 255.0d), b / 255.0d); | ||
double min = Math.Max(Math.Max(r / 255.0d, g / 255.0d), b / 255.0d); | ||
|
||
double d = max - min; | ||
double s = max == 0 ? 0 : d / max; | ||
double v = max; | ||
double h = 0; | ||
|
||
if (max != min) { | ||
if (r > g) { | ||
if (r > b) { | ||
h = (g - b) / d + (g < b ? 6 : 0); | ||
} else { | ||
h = (r - g) / d + 4; | ||
} | ||
} else { | ||
if (g > b) { | ||
h = (b - r) / d + 2; | ||
} else { | ||
h = (r - g) / d + 4; | ||
} | ||
} | ||
|
||
h /= 6; | ||
} | ||
H = h; | ||
S = s; | ||
V = v; | ||
} | ||
|
||
public Color(double h, double s, double v) { | ||
H = h; | ||
S = s; | ||
V = v; | ||
double r = 0, g = 0, b = 0; | ||
|
||
int i = (int)Math.Floor(h * 6); | ||
double f = h * 6 - i; | ||
double p = v * (1 - s); | ||
double q = v * (1 - f * s); | ||
double t = v * (1 - (1 - f) * s); | ||
|
||
switch (i % 6) { | ||
case 0: r = v; g = t; b = p; break; | ||
case 1: r = q; g = v; b = p; break; | ||
case 2: r = p; g = v; b = t; break; | ||
case 3: r = p; g = q; b = v; break; | ||
case 4: r = t; g = p; b = v; break; | ||
case 5: r = v; g = p; b = q; break; | ||
} | ||
|
||
R = (byte)(255 * r); | ||
G = (byte)(255 * g); | ||
B = (byte)(255 * b); | ||
} | ||
|
||
public override string ToString() { | ||
return $"0x{R:x2}{G:x2}{B:x2}"; | ||
} | ||
|
||
public int ToInt() { | ||
return 256*256*(int)R + 256*(int)G + (int)B; | ||
} | ||
|
||
private static byte ToByte(double x) { | ||
return (byte)Math.Min(Math.Max(Math.Round(x), 0), 255); | ||
} | ||
|
||
public static Color BlendRGB(double alpha, Color a, Color b) { | ||
return new Color( | ||
ToByte((b.R - a.R) * alpha + a.R), | ||
ToByte((b.G - a.G) * alpha + a.G), | ||
ToByte((b.B - a.B) * alpha + a.B) | ||
); | ||
} | ||
public static Color BlendHSV(double alpha, Color a, Color b) { | ||
return new Color( | ||
((b.H - a.H) * alpha + a.H), | ||
((b.S - a.S) * alpha + a.S), | ||
((b.V - a.V) * alpha + a.V) | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
using Spectrum.Audio; | ||
using Spectrum.Base; | ||
using Spectrum.LEDs; | ||
using System; | ||
using System.Numerics; | ||
|
||
namespace Spectrum.Visualizers { | ||
class LEDDomeQuaternionFocusVisualizer : Visualizer { | ||
|
||
|
||
private Configuration config; | ||
private AudioInput audio; | ||
private OrientationInput orientation; | ||
private LEDDomeOutput dome; | ||
private Vector3 spot = new Vector3(0, 1, 0); | ||
|
||
public LEDDomeQuaternionFocusVisualizer( | ||
Configuration config, | ||
AudioInput audio, | ||
OrientationInput orientation, | ||
LEDDomeOutput dome | ||
) { | ||
this.config = config; | ||
this.audio = audio; | ||
this.orientation = orientation; | ||
this.dome = dome; | ||
this.dome.RegisterVisualizer(this); | ||
} | ||
|
||
public int Priority { | ||
get { | ||
return this.config.domeActiveVis == 6 ? 2 : 0; | ||
} | ||
} | ||
|
||
public bool Enabled { get; set; } | ||
|
||
public Input[] GetInputs() { | ||
return new Input[] { this.orientation }; | ||
} | ||
|
||
void Render() { | ||
for (int i = 0; i < LEDDomeOutput.GetNumStruts(); i++) { | ||
var leds = LEDDomeOutput.GetNumLEDs(i); | ||
for (int j = 0; j < leds; j++) { | ||
var p = StrutLayoutFactory.GetProjectedLEDPoint(i, j); // centered on (.5, .5), [0, 1] x [0, 1] | ||
var x = 2 * p.Item1 - 1; // now centered on (0, 0) and with range [0, 1] | ||
var y = 1 - 2 * p.Item2; // this is because in the original mapping x, y come "out of" the top left corner | ||
float z = (float)Math.Sqrt(1 - x * x - y * y); | ||
Vector3 pixelPoint = new Vector3((float)x, (float)y, z); | ||
// Calibration assigns (0, 1, 0) to be 'forward' | ||
// So we want the post-transformed pixel closest to (0, 1, 0)? | ||
int color = 0; | ||
if (Vector3.Distance(Vector3.Transform(pixelPoint, orientation.rotation), spot) < .25) { | ||
color = 0xFFFFFF; | ||
} | ||
|
||
this.dome.SetPixel(i, j, color); | ||
} | ||
} | ||
} | ||
public void Visualize() { | ||
this.Render(); | ||
|
||
this.dome.Flush(); | ||
} | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
Spectrum/Visualizers/LEDDomeQuaternionPaintbrushVisualizer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using Spectrum.Audio; | ||
using Spectrum.Base; | ||
using Spectrum.LEDs; | ||
using System; | ||
using System.Numerics; | ||
|
||
namespace Spectrum.Visualizers { | ||
class LEDDomeQuaternionPaintbrushVisualizer : Visualizer { | ||
|
||
|
||
private Configuration config; | ||
private AudioInput audio; | ||
private OrientationInput orientation; | ||
private LEDDomeOutput dome; | ||
private Vector3 spot = new Vector3(0, 1, 0); | ||
|
||
public LEDDomeQuaternionPaintbrushVisualizer( | ||
Configuration config, | ||
AudioInput audio, | ||
OrientationInput orientation, | ||
LEDDomeOutput dome | ||
) { | ||
this.config = config; | ||
this.audio = audio; | ||
this.orientation = orientation; | ||
this.dome = dome; | ||
this.dome.RegisterVisualizer(this); | ||
} | ||
|
||
public int Priority { | ||
get { | ||
return this.config.domeActiveVis == 5 ? 2 : 0; | ||
} | ||
} | ||
|
||
public bool Enabled { get; set; } | ||
|
||
public Input[] GetInputs() { | ||
return new Input[] { this.orientation }; | ||
} | ||
|
||
void Render() { | ||
for (int i = 0; i < LEDDomeOutput.GetNumStruts(); i++) { | ||
var leds = LEDDomeOutput.GetNumLEDs(i); | ||
for (int j = 0; j < leds; j++) { | ||
var p = StrutLayoutFactory.GetProjectedLEDPoint(i, j); // centered on (.5, .5), [0, 1] x [0, 1] | ||
var x = 2 * p.Item1 - 1; // now centered on (0, 0) and with range [0, 1] | ||
var y = 1 - 2 * p.Item2; // this is because in the original mapping x, y come "out of" the top left corner | ||
float z = (float)Math.Sqrt(1 - x * x - y * y); | ||
Vector3 pixelPoint = new Vector3((float)x, (float)y, z); | ||
// Calibration assigns (0, 1, 0) to be 'forward' | ||
// So we want the post-transformed pixel closest to (0, 1, 0)? | ||
Color color = new Color(0, 0, 0); | ||
if(Vector3.Distance(Vector3.Transform(pixelPoint, orientation.rotation), spot) < .25) { | ||
color = new Color((256 * (orientation.rotation.W + 1)) / 256d, 1, 1); | ||
} | ||
// todo : rework to use jan karls buffer | ||
this.dome.SetPixel(i, j, color.ToInt()); | ||
} | ||
} | ||
} | ||
|
||
public void Visualize() { | ||
this.Render(); | ||
|
||
this.dome.Flush(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.