I have the following colors in my colorPalette.xaml file and I was curious to know…
Is its possible for me to loop through colors in my resource dictionary programmaticly and invert the color values?
Similar to taking an image in photoshop and adding the invert filter. I ask because I’d prefer to not make a duplicate xaml file where I manually invert the colors. I’d rather it be a more procedural solution.
<Color x:Key="Accent01">#1d7b87</Color>
<Color x:Key="Accent02">#28aabc</Color>
<Color x:Key="ColorWhite">White</Color>
<Color x:Key="ColorBlack">Black</Color>
<Color x:Key="Color01">#e0e0e0</Color>
<Color x:Key="Color02">#c3c5c7</Color>
<Color x:Key="Color03">#a6a9ad</Color>
<Color x:Key="Color04">#8b8f94</Color>
<Color x:Key="Color05">#71757a</Color>
<Color x:Key="Color06">#585c61</Color>
<Color x:Key="Color07">#404347</Color>
<Color x:Key="Color08">#292b2e</Color>
<Color x:Key="Color09">#1e1f21</Color>
<Color x:Key="Color10">#121314</Color>
2
Answers
after loading the directory you can loop on it’s items and change thier values by using the keys Property it should be something like this :
try to take a look at this thread it may help you getting your rescourcesdirectory
Ahmad’s solution is great if you don’t mind changing the values. But what if you want to keep the original values and have the inverted versions?
Let’s say you have a folder named Converters and in it you create the two following
IValueConverter
classes:The base class converting a
System.Windows.Media.Color
to aSystem.Windows.Media.SolidColorBrush
:An inheriting class to invert the color – using any of these methods – then convert it to a brush:
Note: Gray-scale color’s inversions are less dramatic, so for this example I added:
Then in xaml you add your reference:
Declare your keys:
And usage: