public void ColorConversionRoundtripAllPoints() { // Use a consistent seed for test reproducability Random r = new Random(0); for (int trial = 0; trial < 1000; trial++) { CIE1931Point originalXy; // Randomly generate a test color that is at a valid CIE1931 coordinate. do { originalXy = new CIE1931Point(r.NextDouble(), r.NextDouble()); }while (originalXy.x + originalXy.y >= 1.0); RGBColor rgb = HueColorConverter.XYToRgb(originalXy, "LCT001"); var xy = HueColorConverter.RgbToXY(rgb, "LCT001"); // We expect a point that is both inside the lamp's gamut and the "wide gamut" // used for XYZ->RGB and RGB->XYZ conversion. // Conversion from XY to RGB var expectedXy = CIE1931Gamut.ForModel("LCT001").NearestContainedPoint(originalXy); expectedXy = CIE1931Gamut.PhilipsWideGamut.NearestContainedPoint(expectedXy); // RGB to XY expectedXy = CIE1931Gamut.ForModel("LCT001").NearestContainedPoint(expectedXy); AssertAreEqual(expectedXy, xy, 0.0001); } }
private Bitmap DrawBitmap(string model) { var gamut = CIE1931Gamut.ForModel(model); int dimension = 500; Bitmap b = new Bitmap(dimension, dimension); for (int x = 0; x < dimension; x++) { for (int y = 0; y < dimension; y++) { CIE1931Point point = new CIE1931Point(x / (dimension * 1.0), y / (dimension * 1.0)); var rgb = HueColorConverter.XYToRgb(point, model); Color c; if (point.x + point.y > 1.0) { c = Color.Black; } else if (gamut.Contains(point)) { c = Color.FromArgb((int)(rgb.R * 255.999), (int)(rgb.G * 255.999), (int)(rgb.B * 255.999)); } else { c = Color.FromArgb((int)(rgb.R * 127.999), (int)(rgb.G * 127.999), (int)(rgb.B * 127.999)); } // CIE1931 charts are drawn with y-increasing being upwards, not downwards as in bitmaps. b.SetPixel(x, (dimension - 1) - y, c); } } return(b); }
public void SetBulbColor(int redComponent, int greenComponent, int blueComponent, string bulbID = "", Action callbackMethod = null) { try { Task setColorTask = new Task(async() => { if ((hueClient != null) && (await hueClient.CheckConnection() == true)) { if (colorBulbs.Count > 0) { List <string> bulbIDs = new List <string>(); LightCommand lightCommand = new LightCommand(); if (bulbID == string.Empty) { for (int i = 0; i < colorBulbs.Count; i++) { bulbIDs.Add(colorBulbs[i].Id); } } else { bulbIDs.Add(bulbID); } lightCommand.TurnOn(); lightCommand.SetColor(HueColorConverter.XyFromColor(redComponent, greenComponent, blueComponent).x, HueColorConverter.XyFromColor(redComponent, greenComponent, blueComponent).y); lightCommand.Brightness = 200; await hueClient.SendCommandAsync(lightCommand, bulbIDs); } callbackMethod?.Invoke(); } else { errorLog += "\r\n\r\n" + DateTime.Now.ToString() + ": Philips Hue bridge is not connected."; callbackMethod?.Invoke(); } }); setColorTask.Start(); } catch (Exception setColorException) { errorLog += "\r\n\r\n" + DateTime.Now.ToString() + ": " + setColorException; callbackMethod?.Invoke(); } }
public static T SetColor <T>(this T UpdateLight, RGBColor color, string model = "LCT001") where T : IUpdateColor { if (UpdateLight == null) { throw new ArgumentNullException(nameof(UpdateLight)); } var point = HueColorConverter.CalculateXY(color, model); return(UpdateLight.SetColor(point.x, point.y)); }
public void SetLightColor(ScriptNumber lightId, ScriptString value) { Logger.InfoFormat("Setting light #{0} color to '{1}'", lightId, value); var cgPoint = HueColorConverter.XyFromColor(value.ToPrimitiveString().Replace(" ", "")); _lightService.SetLightStateAsync(lightId.ToPrimitiveInt32(), new { on = true, // cannot modify color when light is off xy = new[] { cgPoint.x, cgPoint.y } }); }
/// <summary> /// Set state on a single light /// </summary> /// <param name="light"></param> /// <param name="xy"></param> /// <param name="gamut"></param> /// <param name="brightness"></param> /// <param name="timeSpan"></param> /// <param name="cancellationToken"></param> /// <returns></returns> public static void SetState(this EntertainmentLight light, CancellationToken cancellationToken, CIE1931Point xy, CIE1931Gamut gamut, double?brightness = null, TimeSpan timeSpan = default(TimeSpan)) { var rgb = HueColorConverter.XYToRgb(xy, gamut); //Create a new transition for this light Transition transition = new Transition(rgb, brightness, timeSpan); light.Transition = transition; //Start the transition transition.Start(light.State.RGBColor, light.State.Brightness, cancellationToken); }
public void ColorsOutsideGamutAdjustedToInBeInGamutOnConversion() { // The green primary of Gamut A. CIE1931Point gamutGreen = new CIE1931Point(0.2151, 0.7106); // A color green outside Gamut A. CIE1931Point greenOutsideGamut = new CIE1931Point(0.21, 0.75); var a = HueColorConverter.XYToRgb(gamutGreen, "LST001"); var b = HueColorConverter.XYToRgb(greenOutsideGamut, "LST001"); // Points should be equal, since the green outside the gamut should // be adjusted the the nearest green in-gamut. Assert.AreEqual(a.R, b.R); Assert.AreEqual(a.G, b.G); Assert.AreEqual(a.B, b.B); }
public void ColorConversionWhitePoint() { // These light models are capable of Gamuts A, B and C respectively. // See http://www.developers.meethue.com/documentation/supported-lights string[] models = new string[] { "LST001", "LCT001", "LST002" }; foreach (string model in models) { // Make sure that Philips' white point resolves to #FFFFFF for all lights. var rgb = HueColorConverter.XYToRgb(CIE1931Point.PhilipsWhite, model); Assert.AreEqual(rgb.R, 1.0, 0.0001); Assert.AreEqual(rgb.G, 1.0, 0.0001); Assert.AreEqual(rgb.B, 1.0, 0.0001); var xy = HueColorConverter.RgbToXY(new RGBColor(1.0, 1.0, 1.0), model); AssertAreEqual(CIE1931Point.PhilipsWhite, xy, 0.0001); } }
public void CompareColorConversionWithReference() { // Use a consistent seed for test reproducability Random r = new Random(0); for (int trial = 0; trial < 1000; trial++) { double red = r.NextDouble(); double green = r.NextDouble(); double blue = r.NextDouble(); var referenceXy = ReferenceColorConverter.XyFromColor(red, green, blue); // LCT001 uses Gamut B, which is the gamut used in the reference implementation. var actualXy = HueColorConverter.RgbToXY(new RGBColor(red, green, blue), "LCT001"); AssertAreEqual(referenceXy, actualXy, 0.0001); } }
public void ColorConversionRoundtripInsideGamut() { // Use a consistent seed for test reproducability Random r = new Random(0); for (int trial = 0; trial < 1000; trial++) { CIE1931Point originalXy; // Randomly generate a test color that is at a valid CIE1931 coordinate. do { originalXy = new CIE1931Point(r.NextDouble(), r.NextDouble()); }while (originalXy.x + originalXy.y >= 1.0 || !ReferenceColorConverter.CheckPointInLampsReach(originalXy) || !CIE1931Gamut.PhilipsWideGamut.Contains(originalXy)); RGBColor rgb = HueColorConverter.XYToRgb(originalXy, "LCT001"); var xy = HueColorConverter.RgbToXY(rgb, "LCT001"); AssertAreEqual(originalXy, xy, 0.0001); } }
private void XSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs <double> e) { XValue = e.NewValue; RecColorXy.Fill = new SolidColorBrush(HueColorConverter.ColorFromXY(new CGPoint((float)XValue, (float)YValue), _modelid)); }
/// <summary> /// Convert xy color to hexadecimal. /// </summary> /// <param name="format">The format.</param> /// <returns> /// Hex color string. /// </returns> public string AsHexColor(string format = "{0} {1} {2}") { return(HueColorConverter.HexFromXy(ColorCoordinates[0], ColorCoordinates[1], format)); }
public static RGBColor ToRGBColor(this Light state, string model = "LCT001") { return(HueColorConverter.RGBColorFromState(state, model)); }
public static string ToHex(this Light state, string model = "LCT001") { return(HueColorConverter.HexColorFromState(state, model)); }
/// <summary> /// /// </summary> /// <param name="light"></param> /// <param name="xy"></param> /// <param name="gamut">The gamut to use</param> /// <param name="timeSpan"></param> /// <param name="cancellationToken"></param> /// <returns></returns> public static void SetColor(this EntertainmentLight light, CancellationToken cancellationToken, CIE1931Point xy, CIE1931Gamut gamut, TimeSpan timeSpan = default) { var rgb = HueColorConverter.XYToRgb(xy, gamut); light.SetState(cancellationToken, rgb, null, timeSpan); }