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