public ThemeSelectionViewModel()
        {
            Swatches = new SwatchesProvider().Swatches.Where(swatch => swatch.AccentHues.Any());
            paletteHelper = new PaletteHelper();

            ApplyPrimary(Swatches.FirstOrDefault(swatch => swatch.Name.Equals(Settings.Default.AccentTheme.ToLower())));
            ApplyBase(Settings.Default.IsDarkTheme);
        }
        public void ReplaceAccentColor(string name)
        {
            if (name == null) throw new ArgumentNullException(nameof(name));

            var swatch = new SwatchesProvider().Swatches.FirstOrDefault(
                s => string.Compare(s.Name, name, StringComparison.InvariantCultureIgnoreCase) == 0 && s.IsAccented);

            if (swatch == null)
                throw new ArgumentException($"No such accented swatch '{name}'", nameof(name));

            ReplaceAccentColor(swatch);
        }
        public void ReplacePrimaryColor(string name, bool mahapps = false)
        {
            if (name == null) throw new ArgumentNullException(nameof(name));

            var swatch = new SwatchesProvider().Swatches.FirstOrDefault(
                s => string.Compare(s.Name, name, StringComparison.InvariantCultureIgnoreCase) == 0);

            if (swatch == null)
                throw new ArgumentException($"No such swatch '{name}'", nameof(name));

            ReplacePrimaryColor(swatch, mahapps);
        }
        public ColourProvider()
        {
            var swatches = new SwatchesProvider().Swatches.AsArray();

            Swatches = swatches.ToDictionary(s => s.Name);

            var accents = swatches.Where(s => s.IsAccented).ToArray();
            var orders = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);

            ThemeConstants.Themes.Select((str,idx)=>new {str,idx})
                .ForEach(x=> orders[x.str] = x.idx);

            var greys = swatches//.Where(s => !s.IsAccented)
                .Where(s => s.Name == "bluegrey")
                .SelectMany(swatch =>
                {
                    var hues = swatch.PrimaryHues
                            .Where(hue=> hue.Name=="Primary200"
                            || hue.Name == "Primary300"
                            || hue.Name == "Primary400"
                            || hue.Name == "Primary500")
                            .Select(hue =>  new Hue(swatch.Name, hue.Name, hue.Foreground, hue.Color));

                    var withNumber= hues.Select(h =>
                    {
                        var num = GetNumber(h.Name);
                        return new {hue=h, Num = num};
                    }).OrderBy(h=>h.Num)
                    .Take(8)
                    .Select(x=>x.hue);

                    return withNumber;
                });
            Hues = accents
                        .OrderBy(x => orders.Lookup(x.Name).ValueOr(() => 100))
                        .SelectMany(swatch =>
                        {
                            return swatch.AccentHues.Select(hue => new Hue(swatch.Name, hue.Name, hue.Foreground, hue.Color));
                        })
                        .Union(greys)
                        .ToArray();

            HueCache = Hues.ToDictionary(h => h.Key);
        }
        public SearchOptionsViewModel(ISearchMetadataCollection metadataCollection, 
            ISchedulerProvider schedulerProvider,
            SearchHints searchHints)
        {
            SearchHints = searchHints;
            //TODO: options for colour

            var swatches = new SwatchesProvider().Swatches;

            bool binding = false;

            var orderChanged = Observable.FromEventPattern<OrderChangedEventArgs>(
                                            h => PositionMonitor.OrderChanged += h,
                                            h => PositionMonitor.OrderChanged -= h)
                                    .Throttle(TimeSpan.FromMilliseconds(125))
                                    .Select(evt => evt.EventArgs)
                                    .Where(args=>args.PreviousOrder!=null && args.NewOrder.Length == args.PreviousOrder.Length)
                                    .Select(positionChangedArgs =>
                                    {
                                            //reprioritise filters and highlights
                                            return positionChangedArgs.NewOrder
                                            .OfType<SearchOptionsProxy>()
                                            .Select((item, index) => new {Meta=(SearchMetadata)item, index})
                                            //.Where(x => x.index != x.Meta.Position)
                                            .Select(x => new SearchMetadata(x.Meta, x.index))
                                            .ToArray();
                                    })
                                    .Subscribe(positionChangedArgs =>
                                    {
                                        positionChangedArgs.ForEach(metadataCollection.AddorUpdate);
                                    });

            ReadOnlyObservableCollection<SearchOptionsProxy> data;

            var userOptions = metadataCollection.Metadata.Connect()
                .WhereReasonsAre(ChangeReason.Add, ChangeReason.Remove) //ignore updates because we update from here
                .Transform(meta => new SearchOptionsProxy(meta, swatches, m => metadataCollection.Remove(m.SearchText)))
                .SubscribeMany(so =>
                {
                    //when a value changes, write the original value back to the cache
                    return so.WhenAnyPropertyChanged()
                    .Select(_=> new SearchMetadata(so.Position,so.Text, so.Filter, so.Highlight, so.UseRegex, so.IgnoreCase))
                    .Subscribe(metadataCollection.AddorUpdate);
                })
                .Sort(SortExpressionComparer<SearchOptionsProxy>.Ascending(proxy => proxy.Position))

                .ObserveOn(schedulerProvider.MainThread)
                .Bind(out data)
                .Subscribe();

            Data = data;

            //command to add the current search to the tail collection
            var searchInvoker = SearchHints.SearchRequested.Subscribe(request =>
            {
                schedulerProvider.Background.Schedule(() =>
                {
                    metadataCollection.AddorUpdate(new SearchMetadata(metadataCollection.NextIndex(), request.Text, false, true, request.UseRegEx, true));
                });
            });

            _cleanUp = new CompositeDisposable(searchInvoker,
                userOptions,
                searchInvoker,
                orderChanged);
        }
 public SettingsFlyoutViewModel()
 {
     Swatches = new SwatchesProvider().Swatches;
     MainVm = ((MainViewModel)Application.Current.MainWindow.DataContext);
 }
 public PaletteSelectorViewModel()
 {
     Swatches = new SwatchesProvider().Swatches;
 }
        /// <summary>
        /// Attempts to query the current palette configured in the application's resources.
        /// </summary>
        /// <returns></returns>
        /// <exception cref="InvalidOperationException">Thrown if there is any ambiguouty regarding the palette. Provided
        /// standard guidleines have been followed for palette configureation, this should not happen.</exception>
        public Palette QueryPalette()
        {
            //it's not safe to to query for the included swatches, so we find the mid (or accent) colour, 
            //& cross match it with the entirety of all available hues to find the owning swatch.

            //TODO could cache this statically
            var swatchesProvider = new SwatchesProvider();
            var swatchByPrimaryHueIndex = swatchesProvider
                .Swatches
                .SelectMany(s => s.PrimaryHues.Select(h => new {s, h}))
                .ToDictionary(a => a.h.Color, a => a.s);
            var swatchByAccentHueIndex = swatchesProvider
                .Swatches
                .Where(s => s.IsAccented)
                .SelectMany(s => s.AccentHues.Select(h => new { s, h }))
                .ToDictionary(a => a.h.Color, a => a.s);

            var primaryMidBrush = GetBrush("PrimaryHueMidBrush");
            var accentBrush = GetBrush("SecondaryAccentBrush");

            Swatch primarySwatch;
            if (!swatchByPrimaryHueIndex.TryGetValue(primaryMidBrush.Color, out primarySwatch))
                throw new InvalidOperationException("PrimaryHueMidBrush is not from standard swatches");
            Swatch accentSwatch;
            if (!swatchByAccentHueIndex.TryGetValue(accentBrush.Color, out accentSwatch))
                throw new InvalidOperationException("SecondaryAccentBrush is not from standard swatches");

            var primaryLightBrush = GetBrush("PrimaryHueLightBrush");
            var primaryDarkBrush = GetBrush("PrimaryHueDarkBrush");

            var primaryLightHueIndex = GetHueIndex(primarySwatch, primaryLightBrush.Color, false);
            var primaryMidHueIndex = GetHueIndex(primarySwatch, primaryMidBrush.Color, false);
            var primaryDarkHueIndex = GetHueIndex(primarySwatch, primaryDarkBrush.Color, false);
            var accentHueIndex = GetHueIndex(accentSwatch, accentBrush.Color, true);

            return new Palette(primarySwatch, accentSwatch, primaryLightHueIndex, primaryMidHueIndex, primaryDarkHueIndex, accentHueIndex);
        }