// 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 F_ResizeEnd(object sender, EventArgs e)
        {
            if (MouseButtons == MouseButtons.Left)
            {
                return;
            }

            SuspendUI            = true;
            Settings.TopLeft     = new Point(_f.Location.X + 7, _f.Location.Y);
            Settings.BottomRight = new Point(_f.Location.X + _f.Size.Width - 6, _f.Location.Y + _f.Size.Height - 6);
            SetPositionTextBoxesFromSettings();
            SuspendUI = false;
            if (Settings.MultiColourZones.Any())
            {
                // send colour to first zone so can match to an area
                foreach (var label in Settings.SelectedLabels)
                {
                    var zones    = Settings.LabelsAndLocations.Single(x => x.Label == label).Zones;
                    var location = Settings.LabelsAndLocations.Single(x => x.Label == label).ScreenLocation;
                    if (zones > 1 && location == ScreenLocation.None)
                    {
                        // set first zone to green
                        var payload = new SetColourZonesPayload
                        {
                            start_index = new byte[1] {
                                0
                            },
                            end_index = new byte[1] {
                                0
                            },
                            Kelvin             = 3500,
                            TransitionDuration = 150,
                            Hue        = (int)Color.FromArgb(255, 0, 200, 0).GetHue(),
                            Saturation = 65535,
                            Brightness = 65535,
                            // don't apply yet
                            apply = new byte[1] {
                                0
                            }
                        };
                        BulbController.SetColour(label, payload, false);

                        // set rest of zones to off
                        payload = new SetColourZonesPayload
                        {
                            start_index = new byte[1] {
                                1
                            },
                            end_index = new byte[1] {
                                (byte)(zones - 1)
                            },
                            Kelvin             = 3500,
                            TransitionDuration = 150,
                            Hue        = 0,
                            Saturation = 0,
                            Brightness = 0,
                            // apply
                            apply = new byte[1] {
                                1
                            }
                        };
                        BulbController.SetColour(label, payload, false);
                    }
                }
            }
            ProcessorBase.SaveSettings(Settings, null);
        }