Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/DynamicDataDisplay/Common/Palettes/UniformLinearPalette.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.ComponentModel;
7using System.Windows.Markup;
8using Microsoft.Research.DynamicDataDisplay.Common.Auxiliary;
9using System.Collections.ObjectModel;
10using System.Diagnostics.CodeAnalysis;
11
12namespace Microsoft.Research.DynamicDataDisplay.Common.Palettes
13{
14  [ContentProperty("ColorSteps")]
15  public sealed class UniformLinearPalette : IPalette, ISupportInitialize
16  {
17    private double[] points;
18
19    private ObservableCollection<Color> colors = new ObservableCollection<Color>();
20    public ObservableCollection<Color> Colors
21    {
22      get { return colors; }
23    }
24
25    private List<PaletteColorStep> colorSteps = new List<PaletteColorStep>();
26    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
27    [EditorBrowsable(EditorBrowsableState.Never)]
28    public List<PaletteColorStep> ColorSteps
29    {
30      get { return colorSteps; }
31    }
32
33    [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
34    private void RaiseChanged()
35    {
36      Changed.Raise(this);
37    }
38    public event EventHandler Changed;
39
40    public UniformLinearPalette() { }
41
42    public UniformLinearPalette(params Color[] colors)
43    {
44      if (colors == null) throw new ArgumentNullException("colors");
45      if (colors.Length < 2) throw new ArgumentException(Strings.Exceptions.PaletteTooFewColors);
46
47      this.colors = new ObservableCollection<Color>(colors);
48      FillPoints(colors.Length);
49    }
50
51    private void FillPoints(int size)
52    {
53      points = new double[size];
54      for (int i = 0; i < size; i++)
55      {
56        points[i] = i / (double)(size - 1);
57      }
58    }
59
60    private bool increaseBrightness = true;
61    [DefaultValue(true)]
62    public bool IncreaseBrightness
63    {
64      get { return increaseBrightness; }
65      set { increaseBrightness = value; }
66    }
67
68    public Color GetColor(double t)
69    {
70      Verify.AssertIsFinite(t);
71      Verify.IsTrue(0 <= t && t <= 1);
72
73      if (t <= 0)
74        return colors[0];
75      else if (t >= 1)
76        return colors[colors.Count - 1];
77      else
78      {
79        int i = 0;
80        while (points[i] < t)
81          i++;
82
83        double ratio = (points[i] - t) / (points[i] - points[i - 1]);
84
85        Verify.IsTrue(0 <= ratio && ratio <= 1);
86
87        Color c0 = colors[i - 1];
88        Color c1 = colors[i];
89        Color res = Color.FromRgb(
90          (byte)(c0.R * ratio + c1.R * (1 - ratio)),
91          (byte)(c0.G * ratio + c1.G * (1 - ratio)),
92          (byte)(c0.B * ratio + c1.B * (1 - ratio)));
93
94        // Increasing saturation and brightness
95        if (increaseBrightness)
96        {
97          HsbColor hsb = res.ToHsbColor();
98          //hsb.Saturation = 0.5 * (1 + hsb.Saturation);
99          hsb.Brightness = 0.5 * (1 + hsb.Brightness);
100          return hsb.ToArgbColor();
101        }
102        else
103        {
104          return res;
105        }
106      }
107    }
108
109    #region ISupportInitialize Members
110
111    bool beganInit = false;
112    public void BeginInit()
113    {
114      beganInit = true;
115    }
116
117    public void EndInit()
118    {
119      if (beganInit)
120      {
121        colors = new ObservableCollection<Color>(colorSteps.Select(step => step.Color));
122        FillPoints(colors.Count);
123      }
124    }
125
126    #endregion
127  }
128}
Note: See TracBrowser for help on using the repository browser.