public virtual void ReplacePrimaryColor(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); if (swatch == null) { throw new ArgumentException($"No such swatch '{name}'", nameof(name)); } ReplacePrimaryColor(swatch); }
/// <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)); }
public PaletteSelectorViewModel() { Swatches = new SwatchesProvider().Swatches; }