using System; using System.Collections.Generic; using System.Drawing; using System.Linq; namespace HeuristicLab.Analysis.FitnessLandscape { public class ColorGradient { private SortedList colors; public ColorGradient() { Clear(); } public void Clear() { colors = new SortedList(); } public Color this[double index] { get { if (colors.Count == 0) throw new KeyNotFoundException(); KeyValuePair lower = colors.First(); if (index < lower.Key) return lower.Value; foreach (var upper in colors) { if (upper.Key == index) return upper.Value; if (upper.Key > index) { double alpha = (index - lower.Key) / (upper.Key - lower.Key); return Color.FromArgb( (int)Math.Round(upper.Value.A * alpha + lower.Value.A * (1 - alpha)), (int)Math.Round(upper.Value.R * alpha + lower.Value.R * (1 - alpha)), (int)Math.Round(upper.Value.G * alpha + lower.Value.G * (1 - alpha)), (int)Math.Round(upper.Value.B * alpha + lower.Value.B * (1 - alpha))); } lower = upper; } return colors.Last().Value; } set { colors[index] = value; } } } }