Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/DynamicDataDisplay/Common/Palettes/LinearPalette.cs @ 12503

Last change on this file since 12503 was 12503, checked in by aballeit, 9 years ago

#2283 added GUI and charts; fixed MCTS

File size: 3.1 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Windows.Media;
6using System.Diagnostics;
7using System.ComponentModel;
8using System.Collections.ObjectModel;
9using System.Windows.Markup;
10
11namespace Microsoft.Research.DynamicDataDisplay.Common.Palettes
12{
13  /// <summary>
14  /// Represents a palette with start and stop colors and intermediate colors with their custom offsets.
15  /// </summary>
16  [ContentProperty("Steps")]
17  public class LinearPalette : PaletteBase, ISupportInitialize
18  {
19    private readonly ObservableCollection<LinearPaletteColorStep> steps = new ObservableCollection<LinearPaletteColorStep>();
20    public ObservableCollection<LinearPaletteColorStep> Steps
21    {
22      get { return steps; }
23    }
24
25    private Color startColor = Colors.White;
26    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
27    public Color StartColor
28    {
29      get { return startColor; }
30      set { startColor = value; }
31    }
32
33    private Color endColor = Colors.Black;
34    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
35    public Color EndColor
36    {
37      get { return endColor; }
38      set { endColor = value; }
39    }
40
41    /// <summary>
42    /// Initializes a new instance of the <see cref="LinearPalette"/> class.
43    /// </summary>
44    public LinearPalette() { }
45
46    /// <summary>
47    /// Initializes a new instance of the <see cref="LinearPalette"/> class.
48    /// </summary>
49    /// <param name="startColor">The start color.</param>
50    /// <param name="endColor">The end color.</param>
51    /// <param name="steps">The steps.</param>
52    public LinearPalette(Color startColor, Color endColor, params LinearPaletteColorStep[] steps)
53    {
54      this.steps.Add(new LinearPaletteColorStep(startColor, 0));
55      if (steps != null)
56        this.steps.AddMany(steps);
57      this.steps.Add(new LinearPaletteColorStep(endColor, 1));
58    }
59
60    #region IPalette Members
61
62    /// <summary>
63    /// Gets the color by interpolation coefficient.
64    /// </summary>
65    /// <param name="t">Interpolation coefficient, should belong to [0..1].</param>
66    /// <returns>Color.</returns>
67    public override Color GetColor(double t)
68    {
69      if (t < 0) return steps[0].Color;
70      if (t > 1) return steps[steps.Count - 1].Color;
71
72      int i = 0;
73      double x = 0;
74      while (x <= t)
75      {
76        x = steps[i + 1].Offset;
77        i++;
78      }
79
80      double ratio = (t - steps[i - 1].Offset) / (steps[i].Offset - steps[i - 1].Offset);
81
82      Color c0 = steps[i - 1].Color;
83      Color c1 = steps[i].Color;
84
85      Color result = Color.FromRgb(
86        (byte)((1 - ratio) * c0.R + ratio * c1.R),
87        (byte)((1 - ratio) * c0.G + ratio * c1.G),
88        (byte)((1 - ratio) * c0.B + ratio * c1.B));
89      return result;
90    }
91
92    #endregion
93
94    #region ISupportInitialize Members
95
96    void ISupportInitialize.BeginInit()
97    {
98    }
99
100    void ISupportInitialize.EndInit()
101    {
102      if (steps.Count == 0 || steps[0].Offset > 0)
103        this.steps.Insert(0, new LinearPaletteColorStep(startColor, 0));
104      if (steps.Count == 0 || steps[steps.Count - 1].Offset < 1)
105        this.steps.Add(new LinearPaletteColorStep(endColor, 1));
106    }
107
108    #endregion
109  }
110}
Note: See TracBrowser for help on using the repository browser.