private void DoMainLoop(MaxLifxBulbController bulbController, ref int frames, DateTime start, ref SetColourPayload payload, Bitmap screenPixel, Graphics gdest, Graphics gsrc) { if (ShowUI) { var t = new Thread(() => { var form2 = new ScreenColourUI(SettingsCast); /* (SettingsCast, bulbController.Bulbs);*/ form2.ShowDialog(); }); t.Start(); ShowUI = false; } frames++; var screenColourSet = GetScreenColours(SettingsCast.TopLeft, SettingsCast.BottomRight, screenPixel, gdest, gsrc); if (screenColourSet != null) { foreach (var label in SettingsCast.SelectedLabels) { var location = SettingsCast.LabelsAndLocations.Single(x => x.Label == label).ScreenLocation; var avgColour = screenColourSet.all; switch (location) { case ScreenLocation.Top: avgColour = screenColourSet.top; break; case ScreenLocation.Bottom: avgColour = screenColourSet.bottom; break; case ScreenLocation.Left: avgColour = screenColourSet.left; break; case ScreenLocation.Right: avgColour = screenColourSet.right; break; case ScreenLocation.TopLeft: avgColour = screenColourSet.topleft; break; case ScreenLocation.TopRight: avgColour = screenColourSet.topright; break; case ScreenLocation.BottomLeft: avgColour = screenColourSet.bottomleft; break; case ScreenLocation.BottomRight: avgColour = screenColourSet.bottomright; break; } var hue = (int) (avgColour.GetHue()); var saturation = (ushort) (avgColour.GetSaturation()*(SettingsCast.Saturation-SettingsCast.MinSaturation) + SettingsCast.MinSaturation); var brightness = (ushort) (avgColour.GetBrightness()*(SettingsCast.Brightness-SettingsCast.MinBrightness) + SettingsCast.MinBrightness); payload = new SetColourPayload { Kelvin = 3500, TransitionDuration = (uint) (SettingsCast.Fade), Hue = hue, Saturation = saturation, Brightness = brightness }; bulbController.SetColour(label, payload); bulbController.SetColour(label, payload); } } Thread.Sleep(SettingsCast.Delay); }
// this is where the actual colours are determined and set for the bulbs private void DoMainLoop(MaxLifxBulbController bulbController, ref int frames, DateTime start) { if (ShowUI) { var t = new Thread(() => { var form2 = new ScreenColourUI(SettingsCast, bulbController); /* (SettingsCast, bulbController.Bulbs);*/ form2.ShowDialog(); }); t.Start(); ShowUI = false; } frames++; // determine colours on screen // var screenColourSet = GetScreenColours(SettingsCast.TopLeft, SettingsCast.BottomRight, screenPixel, gdest, gsrc); Color? avgColour = null; ScreenColorSet screenColourSet = null; foreach (var label in SettingsCast.SelectedLabels) { var multiFlag = false; var labelsAndLocations = SettingsCast.LabelsAndLocations.Single(x => x.Label == label); var location = labelsAndLocations.ScreenLocation; var zones = labelsAndLocations.Zones; // skip if set to None if (location == ScreenLocation.None) { continue; } if (zones > 1) { List <Rectangle> rectList = null; bool found = false; // try and grab the zones switch (location) { case ScreenLocation.SurroundCounterClockwise: found = ZoneAreas.TryGetValue(zones.ToString() + "ccw", out rectList); break; case ScreenLocation.SurroundClockwise: found = ZoneAreas.TryGetValue(zones.ToString() + "cw", out rectList); break; // for all other ScreenLocations we skip } if (found) { // our average colour pixel var screenPixel = new Bitmap(1, 1, PixelFormat.Format32bppArgb); var gdest = Graphics.FromImage(screenPixel); var gsrc = Graphics.FromHwnd(IntPtr.Zero); var srcData = screenPixel.LockBits( new Rectangle(0, 0, screenPixel.Width, screenPixel.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); screenPixel.UnlockBits(srcData); for (int i = 0; i < zones; i++) { var areaColour = GetScreenColourZones(rectList[i], srcData, gdest, gsrc); // code for using most dominant colour; doesn't look very good tbh /*var bmpScreenshot = new Bitmap(rectList[i].Width, rectList[i].Height, PixelFormat.Format32bppArgb); * var gfxScreenshot = Graphics.FromImage(bmpScreenshot); * gfxScreenshot.CopyFromScreen(rectList[i].X, rectList[i].Y, 0, 0, rectList[i].Size, CopyPixelOperation.SourceCopy); * Bitmap resized = new Bitmap(bmpScreenshot, new Size(bmpScreenshot.Width / 3, bmpScreenshot.Height / 3)); * var areaColour = dominantColour(resized);*/ if (areaColour != null) { Color avgZoneColour = (Color)areaColour; // Color isn't HSV, so need to convert double hue = 0; double saturation = 0; double brightness = 0; Utils.ColorToHSV((Color)avgZoneColour, out hue, out saturation, out brightness); brightness = (brightness * (SettingsCast.Brightness - SettingsCast.MinBrightness) + SettingsCast.MinBrightness); saturation = (saturation * (SettingsCast.Saturation - SettingsCast.MinSaturation) + SettingsCast.MinSaturation); var zonePayload = new SetColourZonesPayload { start_index = new byte[1] { (byte)(i) }, end_index = new byte[1] { (byte)(i) }, Kelvin = (ushort)SettingsCast.Kelvin, TransitionDuration = (uint)(SettingsCast.Fade), Hue = (int)hue, Saturation = (ushort)saturation, Brightness = (ushort)brightness, // 0 for delayed apply; 1 for immediate (don't wait for other zones); apply = new byte[1] { 1 } }; // send bulbController.SetColour(label, zonePayload, false); } } multiFlag = true; } } // only do this calc if necessary if (!multiFlag) { // set once var screenPixel = new Bitmap(2, 2, PixelFormat.Format32bppArgb); var gdest = Graphics.FromImage(screenPixel); var gsrc = Graphics.FromHwnd(IntPtr.Zero); screenColourSet = GetScreenColours(SettingsCast.TopLeft, SettingsCast.BottomRight, screenPixel, gdest, gsrc); // default is all avgColour = screenColourSet.all; switch (location) { case ScreenLocation.Top: avgColour = screenColourSet.top; break; case ScreenLocation.Bottom: avgColour = screenColourSet.bottom; break; case ScreenLocation.Left: avgColour = screenColourSet.left; break; case ScreenLocation.Right: avgColour = screenColourSet.right; break; case ScreenLocation.TopLeft: avgColour = screenColourSet.topleft; break; case ScreenLocation.TopRight: avgColour = screenColourSet.topright; break; case ScreenLocation.BottomLeft: avgColour = screenColourSet.bottomleft; break; case ScreenLocation.BottomRight: avgColour = screenColourSet.bottomright; break; } // Color isn't HSV, so need to convert double hue = 0; double saturation = 0; double brightness = 0; Utils.ColorToHSV((Color)avgColour, out hue, out saturation, out brightness); brightness = (brightness * (SettingsCast.Brightness - SettingsCast.MinBrightness) + SettingsCast.MinBrightness); saturation = (saturation * (SettingsCast.Saturation - SettingsCast.MinSaturation) + SettingsCast.MinSaturation); var payload = new SetColourPayload { Kelvin = (ushort)SettingsCast.Kelvin, TransitionDuration = (uint)(SettingsCast.Fade), Hue = (int)hue, Saturation = (ushort)saturation, Brightness = (ushort)brightness }; bulbController.SetColour(label, payload, true); } // this is for when multizone apply is set to 0: need to send another packet to apply everything /*else { * var payload = new SetColourZonesPayload * { * start_index = new byte[1] { 0 }, * end_index = new byte[1] { 0 }, * Kelvin = 3500, * TransitionDuration = 0, * Hue = 0, * Saturation = 0, * Brightness = 0, * // ignore this payload and apply previous ones * apply = new byte[1] { 2 } * }; * bulbController.SetColourZoneFinder(label, payload); * //bulbController.SetColour(label, payload); * }*/ // } } Thread.Sleep(SettingsCast.Delay); }
private void DoMainLoop(MaxLifxBulbController bulbController, ref int frames, DateTime start, ref SetColourPayload payload, Bitmap screenPixel, Graphics gdest, Graphics gsrc) { if (ShowUI) { var t = new Thread(() => { var form2 = new ScreenColourUI(SettingsCast); /* (SettingsCast, bulbController.Bulbs);*/ form2.ShowDialog(); }); t.Start(); ShowUI = false; } frames++; var screenColourSet = GetScreenColours(SettingsCast.TopLeft, SettingsCast.BottomRight, screenPixel, gdest, gsrc); if (screenColourSet != null) { foreach (var label in SettingsCast.SelectedLabels) { var location = SettingsCast.LabelsAndLocations.FirstOrDefault(x => x.Label == label); if (location == null) { continue; } int r = 0, g = 0, b = 0, ctr = 0; foreach (var pixel in location.SelectedPixels) { r += screenColourSet.colours[pixel].R; g += screenColourSet.colours[pixel].G; b += screenColourSet.colours[pixel].B; ctr++; } var avgColour = ctr == 0 ? Color.Black : Color.FromArgb(255, r / ctr, g / ctr, b / ctr); var HSV = RGBToHSV(avgColour); var hue = (int)(HSV[0]); var saturation = (ushort)(HSV[1] * (SettingsCast.Saturation - SettingsCast.MinSaturation) + SettingsCast.MinSaturation); var brightness = (ushort)(HSV[2] * (SettingsCast.Brightness - SettingsCast.MinBrightness) + SettingsCast.MinBrightness); payload = new SetColourPayload { Kelvin = 3500, TransitionDuration = (uint)(SettingsCast.Fade), Hue = hue, Saturation = saturation, Brightness = brightness, RGBColour = avgColour }; payload.PayloadType = bulbController.Bulbs.FirstOrDefault(x => x.Label == label).BulbType; bulbController.SetColour(label, payload); } } Thread.Sleep(SettingsCast.Delay); }