Free cookie consent management tool by TermsFeed Policy Generator

source: branches/FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape/ColorGradient.cs @ 11225

Last change on this file since 11225 was 7128, checked in by epitzer, 13 years ago

#1696 Integrate fitness landscape analysis plugins from Heureka! repository.

File size: 1.4 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Drawing;
4using System.Linq;
5
6namespace HeuristicLab.Analysis.FitnessLandscape {
7
8  public class ColorGradient {
9
10    private SortedList<double, Color> colors;
11
12    public ColorGradient() {
13      Clear();
14    }
15
16    public void Clear() {
17      colors = new SortedList<double, Color>();
18    }
19
20    public Color this[double index] {
21      get {
22        if (colors.Count == 0)
23          throw new KeyNotFoundException();
24        KeyValuePair<double, Color> lower = colors.First();
25        if (index < lower.Key)
26          return lower.Value;
27        foreach (var upper in colors) {
28          if (upper.Key == index)
29            return upper.Value;
30          if (upper.Key > index) {
31            double alpha = (index - lower.Key) / (upper.Key - lower.Key);
32            return Color.FromArgb(
33              (int)Math.Round(upper.Value.A * alpha + lower.Value.A * (1 - alpha)),
34              (int)Math.Round(upper.Value.R * alpha + lower.Value.R * (1 - alpha)),
35              (int)Math.Round(upper.Value.G * alpha + lower.Value.G * (1 - alpha)),
36              (int)Math.Round(upper.Value.B * alpha + lower.Value.B * (1 - alpha)));
37          }
38          lower = upper;
39        }
40        return colors.Last().Value;
41      }
42      set {
43        colors[index] = value;
44      }
45    }
46  }
47}
Note: See TracBrowser for help on using the repository browser.