private byte[] SanitizeDataArray(int ledCount, byte[] data, LightingMode mode) { if (mode == LightingMode.Line) { return(data); } if (mode == LightingMode.Point) { List <byte> bytes = new List <byte>(); for (int i = 0; i < ledCount; i++) { bytes.Add(data[0]); bytes.Add(data[1]); bytes.Add(data[2]); } return(bytes.ToArray()); } if (mode == LightingMode.Keyboard) // TODO: Keyboard lighting mode support for led strip { int ledStripLength = 170; // TODO: hardcoded number, standard led strip length? Led[] ls = new Led[ledStripLength]; /* List<Point> pts = new List<Point>(); * List<byte> bytes = new List<byte>();*/ for (int i = 0; i < ledStripLength; i++) { ls[i] = new Led(); } List <Point> pts; for (int i = 0; i < 88; i++) { byte[] col = new byte[3] { data[i * 3], data[i * 3 + 1], data[i * 3 + 2] }; pts = KeyUtils.KeyToPoints(i); foreach (Point pt in pts) { int xScaled = (int)Utils.Scale(pt.X, 0, 19, 0, ledStripLength); ls.AddColorToLedsAround(xScaled, HSVColor.FromRGB(col), 10); } } return(ls.ToByteArray()); } else { Console.Error.WriteLine("SACN: Invalid lighting mode"); throw new ArgumentException("Invalid lighting mode"); } }
public static HSVColor FromRGB(Color color) { HSVColor toReturn = new HSVColor(); int max = Math.Max(color.R, Math.Max(color.G, color.B)); int min = Math.Min(color.R, Math.Min(color.G, color.B)); toReturn.h = (float)Math.Round(color.GetHue(), 2); toReturn.s = (float)((max == 0) ? 0 : 1d - (1d * min / max)) * 100; toReturn.s = (float)Math.Round(toReturn.s, 2); toReturn.v = (float)Math.Round(((max / 255d) * 100), 2); toReturn.h = toReturn.h / 360f; toReturn.s = toReturn.s / 100f; toReturn.v = toReturn.v / 100f; return(toReturn); }
public void FadeToColorBy(HSVColor c, float factor) { if (color.h < c.h) { color.h = color.h + (c.h - color.h) * factor; } else { color.h = color.h - (color.h - c.h) * factor; } if (Math.Abs(color.h - c.h) <= 0.025f) { color.h = c.h; } if (color.s < c.s) { color.s = color.s + (c.s - color.s) * factor; } else { color.s = color.s - (color.s - c.s) * factor; } if (Math.Abs(color.s - c.s) <= 0.025f) { color.s = c.s; } if (color.v < c.v) { color.v = color.v + (c.v - color.v) * factor; } else { color.v = color.v - (color.v - c.v) * factor; } if (Math.Abs(color.h - c.h) <= 0.025f) { color.v = c.v; } }
public static void AddColorToLedsAround(this Led[] leds, int ledNum, HSVColor colHSV, int colorMixSpread = 5) { leds[ledNum].MixNewColor(colHSV); if (colHSV.v == 0) { return; } for (int i = 1; i < colorMixSpread; i++) { double cVal = colHSV.v - Utils.Scale(i, 0, colorMixSpread - 1, 0, colHSV.v); HSVColor c = new HSVColor(colHSV.h, colHSV.s, (float)cVal); if (ledNum + i < leds.Length) { leds[ledNum + i].MixNewColor(c); } if (ledNum - i >= 0) { leds[ledNum - i].MixNewColor(c); } } }
/// <summary> /// Used by FourierAudioLED. TODO. No range checking! ledNum must be less than half the led array /// </summary> public static void AddSymmetricColorAroundLeds(this Led[] leds, int ledNum, HSVColor colHSV, int colorMixSpread = 5) { int ledCountHalf = leds.Length / 2; leds[ledCountHalf - 1 + ledNum].MixNewColor(colHSV); leds[ledCountHalf - 1 - ledNum].MixNewColor(colHSV); for (int i = 1; i < colorMixSpread; i++) { double cVal = colHSV.v - Utils.Scale(i, 0, colorMixSpread - 1, 0, colHSV.v); HSVColor c = new HSVColor(colHSV.h, colHSV.s, (float)cVal); if (ledCountHalf - 1 + ledNum + i < leds.Length) { leds[ledCountHalf - 1 + ledNum + i].MixNewColor(c); leds[ledCountHalf - 1 - ledNum + i].MixNewColor(c); } if (ledCountHalf - 1 - ledNum - i >= 0) { leds[ledCountHalf - 1 + ledNum - i].MixNewColor(c); leds[ledCountHalf - 1 - ledNum - i].MixNewColor(c); } } }
public bool AlmostEqual(HSVColor col, float threshold = 0.1f) { return(Math.Abs(h - col.h) < threshold && Math.Abs(s - col.s) < threshold && Math.Abs(v - col.v) < threshold); }
public void Color(HSVColor col) { color = col; }
public void SetBlack() { color = HSVColor.Black; }
public Led() { color = new HSVColor(0, 0, 0); }
public void FadeToColorBy(HSVColor c, float factor) { color.h = FadeHSVProperty(color.h, c.h, factor); color.s = FadeHSVProperty(color.s, c.s, factor); color.v = FadeHSVProperty(color.v, c.v, factor); }