Beispiel #1
0
        public void UpdateColors()
        {
            if (UserSettings.Settings.ActiveDevice == null || _triangles == null)
            {
                return;
            }

            var client = NanoleafClient.GetClientForDevice(UserSettings.Settings.ActiveDevice);

            //Get colors of current effect
            var effect = client.EffectsEndpoint.GetEffectDetails(OrchestratorCollection.GetOrchestratorForDevice(UserSettings.Settings.ActiveDevice).GetActiveEffectName());

            Dispatcher.Invoke(new Action(() =>
            {
                if (effect == null)
                {
                    foreach (var triangle in _triangles)
                    {
                        triangle.Polygon.Fill = Brushes.LightSlateGray;
                    }
                }
                else
                {
                    var colors = effect.Palette.Select(hsb =>
                                                       new SolidColorBrush(
                                                           HsbToRgbConverter.ConvertToMediaColor(hsb.Hue, hsb.Saturation, hsb.Brightness)
                                                           )).ToList();

                    foreach (var triangle in _triangles)
                    {
                        triangle.Polygon.Fill = colors[_random.Next(colors.Count)];
                    }
                }
            }));
        }
        public void UpdateColors()
        {
            if (UserSettings.Settings.ActiveDevice == null || _triangles == null)
            {
                return;
            }

            //Run code on main thread since we update the UI
            Dispatcher.Invoke(new Action(() =>
            {
                var client       = NanoleafClient.GetClientForDevice(UserSettings.Settings.ActiveDevice);
                var orchestrator = OrchestratorCollection.GetOrchestratorForDevice(UserSettings.Settings.ActiveDevice);

                //Get colors of current effect, we can display colors for nanoleaf effects or custom color effects
                var effectName   = orchestrator.GetActiveEffectName();
                var customEffect = orchestrator.GetCustomEffectFromName(effectName);

                List <SolidColorBrush> colors = null;

                if (customEffect != null)
                {
                    if (customEffect is CustomColorEffect customColorEffect)
                    {
                        colors = new List <SolidColorBrush>()
                        {
                            new SolidColorBrush(Color.FromArgb(customColorEffect.Color.A, customColorEffect.Color.R, customColorEffect.Color.G, customColorEffect.Color.B))
                        };
                    }
                }
                else
                {
                    var effect = client.EffectsEndpoint.GetEffectDetails(OrchestratorCollection.GetOrchestratorForDevice(UserSettings.Settings.ActiveDevice).GetActiveEffectName());

                    if (effect != null)
                    {
                        colors = effect.Palette.Select(hsb =>
                                                       new SolidColorBrush(
                                                           HsbToRgbConverter.ConvertToMediaColor(hsb.Hue, hsb.Saturation, hsb.Brightness)
                                                           )).ToList();
                    }
                }

                if (colors == null)
                {
                    foreach (var triangle in _triangles)
                    {
                        triangle.Polygon.Fill = Brushes.LightSlateGray;
                    }
                }
                else
                {
                    foreach (var triangle in _triangles)
                    {
                        triangle.Polygon.Fill = colors[_random.Next(colors.Count)];
                    }
                }
            }));
        }
        public void UpdateColors()
        {
            if (UserSettings.Settings.ActiveDevice == null || _polygons == null)
            {
                return;
            }

            //Run code on main thread since we update the UI
            Dispatcher.Invoke(new Action(() =>
            {
                var orchestrator = OrchestratorCollection.GetOrchestratorForDevice(UserSettings.Settings.ActiveDevice);

                //Get colors of current effect, we can display colors for nanoleaf effects or custom color effects
                var effectName = orchestrator.GetActiveEffectName();

                ICustomEffect customEffect = null;

                if (effectName != null)
                {
                    customEffect = orchestrator.GetCustomEffectFromName(effectName);
                }

                List <SolidColorBrush> colors = null;

                if (customEffect != null)
                {
                    if (customEffect is CustomColorEffect customColorEffect)
                    {
                        colors = new List <SolidColorBrush>()
                        {
                            new SolidColorBrush(Color.FromArgb(customColorEffect.Color.A, customColorEffect.Color.R, customColorEffect.Color.G, customColorEffect.Color.B))
                        };
                    }
                }
                else
                {
                    var effect = UserSettings.Settings.ActiveDevice.Effects.FirstOrDefault(effect => effect.Name == effectName);

                    //Only retrieve palette if it is not known yet
                    if (effect?.Palette == null)
                    {
                        var client = NanoleafClient.GetClientForDevice(UserSettings.Settings.ActiveDevice);
                        effect     = client.EffectsEndpoint.GetEffectDetails(effectName);

                        if (effect != null)
                        {
                            //Update the effect such that the palette is known in the future
                            UserSettings.Settings.ActiveDevice.UpdateEffect(effect);
                        }
                    }

                    if (effect != null)
                    {
                        colors = effect.Palette.Select(hsb =>
                                                       new SolidColorBrush(
                                                           HsbToRgbConverter.ConvertToMediaColor(hsb.Hue, hsb.Saturation, hsb.Brightness)
                                                           )).ToList();
                    }
                }

                if (colors == null)
                {
                    foreach (var polygon in _polygons)
                    {
                        polygon.Polygon.Fill = Brushes.LightSlateGray;
                    }
                }
                else
                {
                    foreach (var polygon in _polygons)
                    {
                        polygon.Polygon.Fill = colors[_random.Next(colors.Count)];
                    }
                }
            }));
        }
Beispiel #4
0
        /// <summary>
        /// Builds the effects list from the given effects.
        /// Also updates retrieved effects for all given devices passed
        /// in <paramref name="orchestrators"/>.
        /// </summary>
        private void BuildEffects(IEnumerable <ICustomEffect> customEffects, IEnumerable <Effect> deviceEffects, IEnumerable <Orchestrator> orchestrators)
        {
            var effects = new List <EffectComboBoxItemViewModel>();

            //Take any client. Since the dropdown will only display effects shared accross all devices, it does not matter which lights we use to query the effects
            var nanoleafClient = NanoleafClient.GetClientForDevice(orchestrators.First().Device);

            foreach (var customEffect in customEffects)
            {
                effects.Add(new EffectComboBoxItemViewModel()
                {
                    EffectName = customEffect.GetName(),
                    Width      = (int)Width,
                    Colors     = customEffect.GetColors().Select(color => Color.FromArgb(color.A, color.R, color.G, color.B)),
                    EffectType = ICustomEffect.EffectType
                });
            }

            var requestFailed = false;

            foreach (var effect in deviceEffects)
            {
                if (requestFailed)
                {
                    effects.Add(new EffectComboBoxItemViewModel()
                    {
                        EffectName = effect.Name,
                        Width      = (int)Width,
                        Colors     = _defaultColors,
                        EffectType = EffectType.Unknown
                    });
                }

                try
                {
                    //This executes a request, if 1 fails, let all others revert to blank automatically
                    //We do this because we do not want to execute lots of requests and wait till failure for each request
                    var effectWithPalette = effect;

                    if (effect.Palette == null || !effect.Palette.Any())
                    {
                        effectWithPalette = nanoleafClient.EffectsEndpoint.GetEffectDetails(effect.Name);

                        //Replace the retrieved effect with the current effect such that we do not have to make this call in the future
                        //Do this for each device in the given orchestrators
                        foreach (var orchestrator in orchestrators)
                        {
                            orchestrator.Device.UpdateEffect(effectWithPalette);
                        }
                    }

                    effects.Add(new EffectComboBoxItemViewModel()
                    {
                        EffectName = effectWithPalette.Name,
                        Width      = (int)Width,
                        Colors     = effectWithPalette.Palette.Select(palette => HsbToRgbConverter.ConvertToMediaColor(palette.Hue, palette.Saturation, palette.Brightness)),
                        EffectType = effectWithPalette.EffectType
                    });
                }
                catch
                {
                    requestFailed = true;
                    effects.Add(new EffectComboBoxItemViewModel()
                    {
                        EffectName = effect.Name,
                        Width      = (int)Width,
                        Colors     = _defaultColors,
                        EffectType = EffectType.Unknown
                    });
                }
            }

            Effects = new ObservableCollection <EffectComboBoxItemViewModel>(effects);
            OnPropertyChanged(nameof(Effects));
        }