title: Convert string to color for objects
body: <p>I am wondering how to convert string to a colour I have already set in c#? I have a class which sets a colour from a list and the colour variable name is set as a string. I have objects set as the colour but I am wondering how to set the colours using a colour converter for other objects, I realise this description is poor so hopefully my image will help explain more. Also hopefully someone can help me :)</p>
  
  <p>Image 1 is my current output</p>
  
  <p><strong>Image 1</strong>
  <a href="https://i.stack.imgur.com/jFGp9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jFGp9.png" alt="enter image description here"></a></p>
  
  <p>I want the colours under the "Zone legend" to be the same as the "Sequence Stratigraphy"</p>
  
  <p>My Code for the colour setting of the sequence straigraphy  is below along with my code for the zone legend</p>
  
  <p><strong>Sequence Stratigraphy Colours</strong></p>
  
  <pre><code>internal static class ZoneColorizer
  {
      public static void Colorize(ZoneDto[] zones)
      {
          Color[] colorsToChooseFrom = new Color[] { Colors.DarkSlateGray, Colors.DarkBlue, Colors.DarkRed, Colors.DarkCyan, Colors.DarkGoldenrod, Colors.DarkGreen, Colors.DarkMagenta };
          byte colorIndex = 0;
          foreach (var zone in zones)
          {
              var color = colorsToChooseFrom[colorIndex];
              var zoneColor = Color.FromArgb(128, color.R, color.G, color.B);
              zone.ColourCode = zoneColor.ToString();
              colorIndex++;
  
              byte subzoneNumber = 50;
              byte totalSubZones = (byte)zone.Subzones.LongLength;
  
              byte multipler = (byte)(200 / totalSubZones);
              foreach (var subzone in zone.Subzones)
              {
                  var subZoneColor = Color.FromArgb(subzoneNumber, color.R, color.G, color.B);
                  subzone.ColourCode = subZoneColor.ToString();
                  subzoneNumber+= multipler;
              }
          }
      }
  }
  </code></pre>
  
  <p><strong>Zone Legend code</strong></p>
  
  <pre><code>private List&lt;Color&gt; colours;
  
      public ObservableCollection&lt;string&gt; Zones { get; set; }
  
      public LegendControl()
      {
          colours = new List&lt;Color&gt;() { Colors.DarkSlateGray, Colors.DarkBlue, Colors.DarkRed, Colors.DarkCyan, Colors.DarkGoldenrod, Colors.DarkGreen, Colors.DarkMagenta };
  
          InitializeComponent();
          Zones = new ObservableCollection&lt;string&gt;();
          DataContext = Zones;
      }
  
      public void Bind(ZoneDto[] zones)
      {
          int zoneNumber = 0;
          int rowNumber = 0;
  
          byte subzoneNumber = 0;
          byte totalsubZones = 0;
          Zones.Clear();
          foreach (var zone in zones)
         {
              Color zoneColor = GetColour(zoneNumber);
              var zonerectangle = CreateZoneRectangle(zone, rowNumber, zoneColor);
              Grid1.Children.Add(zonerectangle);
              var zoneName = CreateZoneName(zone, rowNumber);
              Grid1.Children.Add(zoneName);
              zoneNumber++;
              foreach (var subzone in zone.Subzones)
              {
                  totalsubZones++;
                  Grid1.RowDefinitions.Add(new RowDefinition());
                  Color subzoneColor = GetGradeColour(subzoneNumber, totalsubZones, zoneColor);
                  var subzoneRectangle = CreateSubZoneRectangle(subzone, rowNumber, subzoneColor);
                  Grid1.Children.Add(subzoneRectangle);
                  var subzoneName = CreateSubZoneName(subzone, rowNumber);
                  Grid1.Children.Add(subzoneName);
                  subzoneNumber++;
                  rowNumber++;
              }
          }
      }
  
      private Rectangle CreateZoneRectangle(ZoneDto zone, int rowNumber, Color zoneColour)
      {
          var rectangle = new Rectangle
          {
              Fill = new SolidColorBrush(zoneColour),
              Margin = new Thickness(2)
          };
          int rowSpan = 0;
          foreach (var subzone in zone.Subzones)
          {
              rowSpan++;
          }
          rectangle.SetValue(Grid.RowProperty, rowNumber);
          rectangle.SetValue(Grid.ColumnProperty, 0);
          rectangle.SetValue(Grid.RowSpanProperty, rowSpan);
          return rectangle;
      }
  
      private TextBlock CreateZoneName(ZoneDto zone, int rowNumber)
      {
          Zones.Clear();
          var textblock = new TextBlock
          {
              Text = zone.Name,  
              RenderTransformOrigin = new Point(0.5, 0.5),
              LayoutTransform = new RotateTransform(270),
              VerticalAlignment = System.Windows.VerticalAlignment.Center,
              HorizontalAlignment = System.Windows.HorizontalAlignment.Center
          };
          int rowSpan = 0;
          foreach (var subzon in zone.Subzones)
          {
              rowSpan++;
          }
          textblock.SetValue(Grid.RowProperty, rowNumber);
          textblock.SetValue(Grid.ColumnProperty, 0);
          textblock.SetValue(Grid.RowSpanProperty, rowSpan);
          return textblock;
      }
  
      private Rectangle CreateSubZoneRectangle(ZoneDto subzone, int rowNumber, Color subzoneColor)
      {
          var rectangle = new Rectangle
          {
              Fill = new SolidColorBrush(subzoneColor),
              Margin = new Thickness(2)
          };
          rectangle.SetValue(Grid.RowProperty, rowNumber);
          rectangle.SetValue(Grid.ColumnProperty, 1);
          return rectangle;
      }
  
      private TextBlock CreateSubZoneName(ZoneDto subzone, int rowNumber)
      {
          var textblock = new TextBlock
          {
              Text = subzone.Name,
              VerticalAlignment = System.Windows.VerticalAlignment.Center,
              HorizontalAlignment = System.Windows.HorizontalAlignment.Center
          };
          textblock.SetValue(Grid.RowProperty, rowNumber);
          textblock.SetValue(Grid.ColumnProperty, 1);
          return textblock;
      }
  
      private Color GetColour(int zoneNumber)
      {
          var numOfColours = colours.Count;
  
          if (zoneNumber &gt;= numOfColours)
          {
              return colours[zoneNumber % numOfColours];
          }
          return colours[zoneNumber];
      }
  
  
      private Color GetGradeColour(byte subzoneNumber, byte totalsubZones, Color zoneColour)
      {
          byte multipler = (byte)(200 / totalsubZones);
  
          byte a = (byte)((subzoneNumber + 1) * multipler);
  
          return Color.FromArgb(a, zoneColour.R, zoneColour.G, zoneColour.B);
      }
  </code></pre>
  
  <p><strong>the code for the ZoneDto</strong></p>
  
  <pre><code>public class GridDataDto
  {
      public WellDto[] Wells { get; set; }
  
      public ZoneDto[] Zones { get; set; }
  
      public string[] Facies { get; set; }
  
      public CellDto MinLimits { get; set; }
  
      public CellDto MaxLimits { get; set; }
  }
  
  public class ZoneDto
  {
      public string Name { get; set; }
  
      public ZoneDto[] Subzones { get; set; }
  
      public int MinK { get; set; }
  
      public int MaxK { get; set; }
  
      public string ColourCode { get; set; }
  }
  
  public class WellDto
  {
      public string Name { get; set; }
      public double X { get; set; }
      public double Y { get; set; }
  }
  
  public class CellDto
  {
      public double X { get; set; }
      public double Y { get; set; }
      public double Z { get; set; }
      public int I { get; set; }
      public int J { get; set; }
      public int K { get; set; }
  }
  </code></pre>