public (double, double, double) ColorAtDeconstruct(double location) { if (_points.Count == 0) { return(0, 0, 0); } var i = 0; LedColorGradientPoint start, end; do { start = _points[i++]; if (i >= _points.Count) { return(start.Color.R, start.Color.G, start.Color.B); } end = _points[i]; } while (!(location >= start.Location && location <= end.Location)); var correctedLocation = (location - start.Location) / (end.Location - start.Location); return(LedColor.LerpDeconstruct(correctedLocation, start.Color, end.Color)); }
public static (double, double, double) LerpDeconstruct(double t, LedColor from, LedColor to) { var r = from.R * (1 - t) + to.R * t; var g = from.G * (1 - t) + to.G * t; var b = from.B * (1 - t) + to.B * t; return(r, g, b); }
public static (double, double, double) LerpSmooth(double t, LedColor from, LedColor to) { t = Math.Max(Math.Min(t, 1), 0); var r = from.R * (1 - t) + to.R * t; var g = from.G * (1 - t) + to.G * t; var b = from.B * (1 - t) + to.B * t; return(r, g, b); }
public static (double, double, double) ToHsv(LedColor color) { var max = Math.Max(color.R, Math.Max(color.G, color.B)); var min = Math.Min(color.R, Math.Min(color.G, color.B)); var delta = max - min; var hue = 0d; if (delta != 0) { if (color.R == max) { hue = (color.G - color.B) / (double)delta; } else if (color.G == max) { hue = 2d + (color.B - color.R) / (double)delta; } else if (color.B == max) { hue = 4d + (color.R - color.G) / (double)delta; } } hue *= 60; if (hue < 0.0) { hue += 360; } var saturation = (max == 0) ? 0 : 1d - (1d * min / max); var value = max / 255d; return(hue, saturation, value); }
public static LedColor Lerp(double t, LedColor from, LedColor to) { var(r, g, b) = LerpSmooth(t, from, to); return(new LedColor((byte)Math.Round(r), (byte)Math.Round(g), (byte)Math.Round(b))); }
public (double r, double g, double b) LerpSmooth(LedColor to, double t) => LerpSmooth(t, this, to);
public LedColor Lerp(LedColor to, double t) => Lerp(t, this, to);
public static int Pack(LedColor color) => (color.R) | (color.G << 8) | (color.B << 16);
public static LedColor ChangeValue(LedColor color, double value) { var(h, s, _) = color.ToHsv(); return(FromHsv(h, s, value)); }
public static LedColor ChangeSaturation(LedColor color, double saturation) { var(h, _, v) = color.ToHsv(); return(FromHsv(h, saturation, v)); }
public static LedColor ChangeHue(LedColor color, double hue) { var(_, s, v) = color.ToHsv(); return(FromHsv(hue, s, v)); }
public static LedColor Lerp(double t, LedColor from, LedColor to) { var(r, g, b) = LerpSmooth(t, from, to); return(new LedColor((byte)r, (byte)g, (byte)b)); }
public (double, double, double) LerpSmooth(LedColor to, double t) => LerpSmooth(t, this, to);
public LedColorGradientPoint(double location, LedColor color) { Location = location; Color = color; }