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