Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape/BoxChart/ColorValue.cs @ 16591

Last change on this file since 16591 was 16573, checked in by gkronber, 6 years ago

#2520: changed HeuristicLab.FLA addon to compile with new HL.Persistence

File size: 6.3 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Drawing;
4using System.Linq;
5using System.Text;
6using HeuristicLab.Common;
7using HeuristicLab.Core;
8using HeuristicLab.Data;
9using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
10using HeuristicLab.Common.Resources;
11using HEAL.Attic;
12
13namespace HeuristicLab.Analysis.FitnessLandscape.BoxChart {
14
15  [Item("Color", "A color")]
16  [StorableType("E4C70605-7105-4ADC-90BC-A0C3994B061B")]
17  public class ColorValue : NamedItem {
18
19    #region Constants & Initializers
20    public static readonly Dictionary<int, string> KnownColors = new Dictionary<int, string>();
21
22    static ColorValue() {
23      foreach (var name in Enum.GetNames(typeof(KnownColor))) {
24        var kc = Color.FromName(name);
25        if (!kc.IsSystemColor)
26          KnownColors[kc.ToArgb()] =  name;
27      }
28    }
29    #endregion
30
31    #region Fields
32    private int r, g, b;
33
34    [Storable]
35    private double h;
36    [Storable]
37    private double s;
38    [Storable]
39    private double v;
40
41    private Color? color;
42
43    private Image icon;
44    #endregion
45
46    #region Properties
47    public override bool CanChangeName { get { return false; } }
48    public override bool CanChangeDescription { get { return false; } }
49    public static new Image StaticItemImage { get { return VSImageLibrary.DisplayInColorVertical; } }
50    public override Image ItemImage {
51      get {
52        if (icon != null)
53          return icon;
54        icon = new Bitmap(16, 16);
55        using (var g = Graphics.FromImage(icon)) {
56          using (var b = new SolidBrush(Color)) {
57            g.FillRectangle(b, 0, 0, 16, 16);
58          }
59        }
60        return icon;
61      }
62    }
63    public int R {
64      get { return r;  }
65      set { if (value == r) return; r = value; OnRGBChanged(); }
66    }
67    public int G {
68      get { return g;  }
69      set { if (value == g) return; g = value; OnRGBChanged(); }
70    }
71    public int B {
72      get { return b;  }
73      set { if (value == b) return; b = value; OnRGBChanged(); }
74    }
75
76    public double H {
77      get { return h; }
78      set { if (value == h) return; h = value; OnHSVChanged(); }
79    }
80
81    public double S {
82      get { return s; }
83      set { if (value == s) return; s = value; OnHSVChanged(); }
84    }
85
86    public double V {
87      get { return v; }
88      set { if (value == v) return; v = value; OnHSVChanged(); }
89    }
90
91    public Color Color {
92      get {
93        if (color.HasValue) return color.Value;
94        color = Color.FromArgb(R, G, B);
95        return color.Value;
96      }
97      set {
98        if (value == Color)
99          return;
100        color = value;
101        r = value.R;
102        g = value.G;
103        b = value.B;
104        OnRGBChanged();
105      }
106    }
107    public bool IsNamedColor { get { return KnownColors.ContainsKey(Color.ToArgb()); } }
108    #endregion
109
110    #region Events
111    protected virtual void OnRGBChanged() {
112      RGBToHSV(r, g, b, out h, out s, out v);
113      OnColorChanged();
114    }
115
116    protected virtual void OnHSVChanged() {
117      HSVtoRGB(h, s, v, out r, out g, out b);
118      OnColorChanged();
119    }
120
121    public event EventHandler ColorChanged;
122
123    protected virtual void OnColorChanged() {
124      color = null;
125      icon = null;
126      name = GetColorName(Color);
127      EventHandler handler = ColorChanged;
128      if (handler != null)
129        handler(this, EventArgs.Empty);
130      OnToStringChanged();
131      OnItemImageChanged();
132      OnNameChanged();
133    }
134    #endregion
135
136    #region Construction & Cloning
137    [StorableConstructor]
138    protected ColorValue(StorableConstructorFlag _) : base(_) { }
139    protected ColorValue(ColorValue original, Cloner cloner) : base (original, cloner) {
140      h = original.h;
141      s = original.s;
142      v = original.v;
143      HSVtoRGB(h, s, v, out r, out g, out b);
144      name = GetColorName(Color);
145    }
146    public ColorValue() : this(Color.White) {}
147    public ColorValue(Color color) {
148      this.color = color;
149      r = color.R;
150      g = color.G;
151      b = color.B;
152      RGBToHSV(r, g, b, out h, out s, out v);
153      name = GetColorName(Color);
154    }
155    public ColorValue(double hue, double saturation, double value) {
156      h = hue;
157      s = saturation;
158      v = value;
159      HSVtoRGB(h, s, v, out r, out g, out b);
160      name = GetColorName(Color);
161    }
162    public ColorValue(int red, int green, int blue) {
163      r = red;
164      g = green;
165      b = blue;
166      RGBToHSV(r, g, b, out h, out s, out v);
167      name = GetColorName(Color);
168    }
169    [StorableHook(HookType.AfterDeserialization)]
170    private void AfterDeserialization() {
171      HSVtoRGB(h, s, v, out r, out g, out b);
172      name = GetColorName(Color);
173    }
174    public override IDeepCloneable Clone(Cloner cloner) {
175      return new ColorValue(this, cloner);
176    }
177    #endregion
178
179
180    public static string GetColorName(Color color) {
181      int argb = color.ToArgb();
182      string colorName;
183      if (KnownColors.TryGetValue(argb, out colorName))
184        return colorName;
185      return string.Format("#{0:x2}{1:x2}{2:x2}", color.R, color.G, color.B);
186    }
187
188    public override string ToString() {
189      return GetColorName(Color);
190    }
191
192    public static void RGBToHSV(int r, int g , int b, out double hue, out double saturation, out double value) {
193      int max = Math.Max(r, Math.Max(g, b));
194      int min = Math.Min(r, Math.Min(g, b));
195
196      hue = Color.FromArgb(r, g, b).GetHue();
197      saturation = (max == 0) ? 0 : 1d - (1d * min / max);
198      value = max / 255d;
199    }
200
201    public static void HSVtoRGB(double hue, double saturation, double value, out int r, out int g, out int b) {
202      int hi = Convert.ToInt32(Math.Floor(hue / 60)) % 6;
203      double f = hue / 60 - Math.Floor(hue / 60);
204
205      value = value * 255;
206      int v = Convert.ToInt32(value);
207      int p = Convert.ToInt32(value * (1 - saturation));
208      int q = Convert.ToInt32(value * (1 - f * saturation));
209      int t = Convert.ToInt32(value * (1 - (1 - f) * saturation));
210
211      switch (hi) {
212        case 0:  r=v; g=t; b=p; break;
213        case 1:  r=q; g=v; b=p; break;
214        case 2:  r=p; g=v; b=t; break;
215        case 3:  r=p; g=q; b=v; break;
216        case 4:  r=t; g=p; b=v; break;
217        default: r=v; g=p; b=q; break;
218      }
219    }
220
221  }
222}
Note: See TracBrowser for help on using the repository browser.