skip to Main Content

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


  1. 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 :

    foreach(object keyy in RescourcesDir.Keys)
    {
        //get the object and it's value
        object val = RescourcesDir[keyy];
        //change it's value ...
        RescourcesDir[keyy] = somevalue;
    }
    

    try to take a look at this thread it may help you getting your rescourcesdirectory

    Login or Signup to reply.
  2. 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:

    1. The base class converting a System.Windows.Media.Color to a System.Windows.Media.SolidColorBrush:

      using System;
      using System.Globalization;
      using System.Windows.Data;
      using System.Windows.Media;
      
      namespace WPFTest.Converters
      {
        public class ColorToBrush : IValueConverter
        {
          public virtual object Convert(object value, Type targetType, object parameter, CultureInfo culture)
          {
            return value is Color ? new SolidColorBrush((Color)value) : Brushes.Black;
          }
      
          public virtual object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
          {
            throw new NotImplementedException();
          }
        }
      }
      
    2. An inheriting class to invert the color – using any of these methods – then convert it to a brush:

      using System;
      using System.Globalization;
      using System.Windows.Media;
      
      namespace WPFTest.Converters
      {
        public class InvertColorToBrush : ColorToBrush
        {
          public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
          {
            if (value is Color)
            {
              Color color = (Color)value;
              int iCol = ((color.A << 24) | (color.R << 16) | (color.G << 8) | color.B) ^ 0xffffff;
              Color inverted = Color.FromArgb((byte)(iCol >> 24),
                                              (byte)(iCol >> 16),
                                              (byte)(iCol >> 8),
                                              (byte)(iCol));
      
              return base.Convert(inverted, targetType, parameter, culture);
            }
            else
            {
              return Brushes.Black;
            }
          }
      
          public override object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
          {
            throw new NotImplementedException();
          }
        }
      }
      

    Note: Gray-scale color’s inversions are less dramatic, so for this example I added:

    <Color x:Key="Blue">#0000ff</Color>
    <Color x:Key="Yellow">#ffff00</Color>
    

    Then in xaml you add your reference:

    xmlns:converters="clr-namespace:WPFTest.Converters"
    

    Declare your keys:

    <converters:ColorToBrush x:Key="BrushColor" />
    <converters:InvertColorToBrush x:Key="BrushInvertColor" />
    

    And usage:

    <Label
      Content="COLOR TEST"
      Background="{Binding Converter={StaticResource BrushColor}, Mode=OneWay, Source={StaticResource Blue}}"
      Foreground="{Binding Converter={StaticResource BrushColor}, Mode=OneWay, Source={StaticResource Yellow}}"/>
    
    <Label
      Content="COLOR INVERT TEST"
      Background="{Binding Converter={StaticResource BrushInvertColor}, Mode=OneWay, Source={StaticResource Blue}}"
      Foreground="{Binding Converter={StaticResource BrushInvertColor}, Mode=OneWay, Source={StaticResource Yellow}}"/>
    

    Example Inverted Labels

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search