/// <summary> /// Applies the screen mirror effect to the lights. /// This method is called 10x per second by <see cref="ScreenMirrorEffect"/> /// </summary> public async Task ApplyEffect() { const int numberOfPanelsPerIteration = 5; //5x10 = 50hz. Note: 50hz seems to work good, higher values can make Canvas stop its external control const int minimumColorDifference = 30; var panelsToUpdate = _panels.Take(numberOfPanelsPerIteration * 2).ToList(); //Take 2 times the number of panels, in case any color differences are not large enough var colors = ScreenGrabber.CalculateAverageColor(panelsToUpdate.Select(panel => panel.ScreenshotArea), 0); var numberOfPanelsChanged = 0; for (var i = 0; i < panelsToUpdate.Count; i++) { //Only update the color of a panel that has a large enough color difference if (ColorDistance(panelsToUpdate[i].CurrentColor, colors[i]) > minimumColorDifference) { numberOfPanelsChanged++; panelsToUpdate[i].CurrentColor = colors[i]; _externalControlEndpoint.SetPanelColor(_deviceType, panelsToUpdate[i].PanelId, colors[i].R, colors[i].G, colors[i].B); _panels.Remove(panelsToUpdate[i]); //Remove the current panel and place it at the back of the list _panels.Add(panelsToUpdate[i]); } if (numberOfPanelsChanged >= numberOfPanelsPerIteration) { break; } } }
/// <summary> /// Applies the ambilight effect to the lights. Ambilight is the average color of the whole screen /// Sets the color of the nanoleaf with the logging disabled. /// Seeing as a maximum of 10 requests per second can be set this will generate a lot of unwanted log data. /// See https://github.com/StijnOostdam/Winleafs/issues/40. /// </summary> public async Task ApplyEffect() { var color = ScreenGrabber.CalculateAverageColor(_screenBounds)[0]; //Safe since we always have 1 area var hue = (int)color.GetHue(); var sat = (int)(color.GetSaturation() * 100); await _nanoleafClient.StateEndpoint.SetHueAndSaturationAsync(hue, sat, disableLogging : true); }
/// <summary> /// Applies the screen mirror effect to the lights. /// This method is called X times per second by <see cref="ScreenMirrorEffect"/> /// </summary> public async Task ApplyEffect() { var colors = ScreenGrabber.CalculateAverageColor(_panelAreas, 0); if (colors == null) { //This can happen when before the first screen shot is taken when the effect is enabled return; } _externalControlEndpoint.SetPanelsColors(_deviceType, _panelIds, colors); }
/// <summary> /// Applies the ambilight effect to the lights. Ambilight is the average color of the whole screen /// Sets the color of the nanoleaf with the logging disabled. /// Seeing as a maximum of 10 requests per second can be set this will generate a lot of unwanted log data. /// See https://github.com/StijnOostdam/Winleafs/issues/40. /// </summary> public async Task ApplyEffect() { var colors = ScreenGrabber.CalculateAverageColor(_screenBounds); if (colors == null) { //This can happen when before the first screen shot is taken when the effect is enabled return; } var hue = (int)colors[0].GetHue(); //Safe since we always have 1 area var sat = (int)(colors[0].GetSaturation() * 100); //Safe since we always have 1 area await _nanoleafClient.StateEndpoint.SetHueAndSaturationAsync(hue, sat, disableLogging : true); }
/// <summary> /// Applies the screen mirror effect to the ligts. /// Screen mirror takes the average color of each triangle and applies it to that triangle /// </summary> public async Task ApplyEffect() { foreach (var panel in _panels) { //For each panel, draw a rectangle around its midpoint, according to the set rectangle size //Then get the average color of that rectangle and apply the color to the panel var startX = (int)Math.Floor(panel.MidPoint.X - (_rectangleSize / 2)); var startY = (int)Math.Floor(panel.MidPoint.Y - (_rectangleSize / 2)); // In multi monitor setup, all screens are joined in one larger pixel area. For example, if you want to take a screenshot of the second from left monitor, // you need to start at the right of the first left monitor. Hence, we need to add _screenBounds X and Y here to the location of the rectangle we want to capture var bounds = new Rectangle(_screenBounds.X + startX, _screenBounds.Y + startY, _rectangleSize, _rectangleSize); var bitmap = ScreenGrabber.CaptureScreen(bounds); var color = ScreenGrabber.CalculateAverageColor(bitmap, _capturedBounds, 0); await _externalControlEndpoint.SetPanelColorAsync(panel.PanelId, color.R, color.G, color.B); } }
/// <summary> /// Applies the ambilight effect to the lights. Ambilight is the average color of the whole screen /// Sets the color of the nanoleaf with the logging disabled. /// Seeing as a maximum of 10 requests per second can be set this will generate a lot of unwanted log data. /// See https://github.com/StijnOostdam/Winleafs/issues/40. /// </summary> public async Task ApplyEffect() { var bitmap = ScreenGrabber.CaptureScreen(_screenBounds); var color = ScreenGrabber.CalculateAverageColor(bitmap, _screenBounds); var hue = (int)color.GetHue(); var sat = (int)(color.GetSaturation() * 100); if (_controlBrightness) { //For brightness calculation see: https://stackoverflow.com/a/596243 and https://www.w3.org/TR/AERT/#color-contrast //We do not use Color.GetBrightness() since that value is always null because we use Color.FromArgb in the screengrabber. //Birghtness can be maximum 100 var brightness = Math.Min(100, (int)(0.299 * color.R + 0.587 * color.G + 0.114 * color.B)); await _nanoleafClient.StateEndpoint.SetHueSaturationAndBrightnessAsync(hue, sat, brightness, disableLogging : true); } else { await _nanoleafClient.StateEndpoint.SetHueAndSaturationAsync(hue, sat, disableLogging : true); } }