Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 18068 was 16995, checked in by gkronber, 6 years ago

#2520 Update plugin dependencies and references for HL.FLA for new persistence

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