Free cookie consent management tool by TermsFeed Policy Generator

source: branches/FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape.Views/BoxChart/ColorValueView.cs @ 13365

Last change on this file since 13365 was 7128, checked in by epitzer, 13 years ago

#1696 Integrate fitness landscape analysis plugins from Heureka! repository.

File size: 10.2 KB
Line 
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.ComponentModel;
5using System.Drawing;
6using System.Data;
7using System.Drawing.Drawing2D;
8using System.Linq;
9using System.Reflection;
10using System.Text;
11using System.Windows.Forms;
12using HeuristicLab.Core.Views;
13using HeuristicLab.MainForm;
14
15namespace HeuristicLab.Analysis.FitnessLandscape.BoxChart {
16
17  [View("ColorValue View")]
18  [Content(typeof(ColorValue), IsDefaultView = true)]
19  public sealed partial class ColorValueView : NamedItemView {
20
21    private Bitmap colorWheel;
22    private const int RADIUS = 128;
23    private const int COLOR_COUNT = 128;
24
25    public new ColorValue Content {
26      get { return (ColorValue)base.Content; }
27      set { base.Content = value; }
28    }
29
30    public ColorValueView() {
31      InitializeComponent();
32      InitializeColorWheel();
33    }
34
35    protected override void DeregisterContentEvents() {
36      Content.ColorChanged -= Content_ColorChanged;
37      base.DeregisterContentEvents();
38    }
39
40
41    protected override void RegisterContentEvents() {
42      base.RegisterContentEvents();
43      Content.ColorChanged += Content_ColorChanged;
44    }
45
46    #region Event Handlers (Content)
47    private void Content_ColorChanged(object sender, EventArgs e) {
48      if (InvokeRequired) {
49        Invoke(new EventHandler(Content_ColorChanged), sender, e);
50      } else {
51        UpdateControls();
52        colorWheelPanel.Invalidate();
53        colorPanel.Invalidate();
54        brightnessPanel.Invalidate();
55      }
56    }
57    #endregion
58
59    protected override void OnContentChanged() {
60      base.OnContentChanged();
61      Update(() => {
62        UpdateControls();
63        colorWheelPanel.Invalidate();
64        colorPanel.Invalidate();
65        brightnessPanel.Invalidate();
66      });
67    }
68
69    private void UpdateControls() {
70      if (Content == null) {
71        hueScrollBar.Value = 0;
72        saturationScrollBar.Value = 0;
73        brightnessScrollBar.Value = 0;
74        hueUpDown.Value = 0;
75        saturationUpDown.Value = 0;
76        brightnessUpDown.Value = 0;
77        redScrollBar.Value = 0;
78        greenScrollBar.Value = 0;
79        blueScrollBar.Value = 0;
80        redUpDown.Value = 0;
81        greenUpDown.Value = 0;
82        blueUpDown.Value = 0;
83      }
84      else {
85        hueScrollBar.Value = (int)Math.Round(Content.H);
86        saturationScrollBar.Value = (int)Math.Round(Content.S*100);
87        brightnessScrollBar.Value = (int)Math.Round(Content.V*100);
88        hueUpDown.Value = (decimal)Math.Round(Content.H);
89        saturationUpDown.Value = (decimal)Math.Round(Content.S*100);
90        brightnessUpDown.Value = (decimal)Math.Round(Content.V*100);
91        redScrollBar.Value = Content.R;
92        greenScrollBar.Value = Content.G;
93        blueScrollBar.Value = Content.B;
94        redUpDown.Value = Content.R;
95        greenUpDown.Value = Content.G;
96        blueUpDown.Value = Content.B;
97      }
98    }
99
100    protected override void SetEnabledStateOfControls() {
101      base.SetEnabledStateOfControls();
102      hueScrollBar.Enabled =
103        saturationScrollBar.Enabled =
104        brightnessScrollBar.Enabled =
105        redScrollBar.Enabled =
106        greenScrollBar.Enabled =
107        blueScrollBar.Enabled =
108        hueUpDown.Enabled =
109        saturationUpDown.Enabled =
110        brightnessUpDown.Enabled =
111        redUpDown.Enabled =
112        greenUpDown.Enabled =
113        blueUpDown.Enabled =
114          Content != null;
115    }
116
117    private static Color GetFullColor(ColorValue c) {
118      return new ColorValue(c.H, c.S, 1).Color;
119    }
120
121    private static Point GetColorPoint(ColorValue c, int size) {
122      return GetPoint(c.H, c.S*size/2, new Point(size/2, size/2));
123    }
124
125    #region Event Handlers (child controls)
126    private void colorWheelPanel_Paint(object sender, PaintEventArgs e) {
127      e.Graphics.DrawImage(colorWheel, 0, 0, colorWheelPanel.Width, colorWheelPanel.Height);
128      if (Content != null)
129        DrawColorPointer(e.Graphics,
130          GetColorPoint(Content, Math.Min(colorWheelPanel.Width, colorWheelPanel.Height)));
131    }
132
133    private void brightnessPanel_Paint(object sender, PaintEventArgs e) {
134      if (Content != null) {
135        var bar = brightnessPanel.ClientRectangle;
136        bar.Width -= 10;
137        using (var lgb = new LinearGradientBrush(bar, GetFullColor(Content), Color.Black, LinearGradientMode.Vertical)) {
138          e.Graphics.FillRectangle(lgb, bar);
139        }
140        DrawBrighnessPointer(e.Graphics, new Point(bar.Width-9, (int)Math.Round((1.0-Content.V)*bar.Height)));
141      }
142    }
143
144    private void colorPanel_Paint(object sender, PaintEventArgs e) {
145      if (Content != null) {
146        using (var b = new SolidBrush(Content.Color)) {
147          e.Graphics.FillRectangle(b, 0, 0, colorPanel.Width, colorPanel.Height);
148        }
149      }
150    }
151
152    private bool updateInProgress = false;
153    private void Update(Action thunk) {
154      if (!updateInProgress) {
155        updateInProgress = true;
156        thunk();
157        updateInProgress = false;
158      }
159    }
160
161    private void hueScrollBar_ValueChanged(object sender, EventArgs e) {
162      Update(() => Content.H = hueScrollBar.Value);
163    }
164
165    private void saturationScrollBar_ValueChanged(object sender, EventArgs e) {
166      Update(() => Content.S = saturationScrollBar.Value/100.0);
167    }
168
169    private void brightnessScrollBar_ValueChanged(object sender, EventArgs e) {
170      Update(() => Content.V = brightnessScrollBar.Value/100.0);
171    }
172
173    private void redScrollBar_ValueChanged(object sender, EventArgs e) {
174      Update(() => Content.R = redScrollBar.Value);
175    }
176
177    private void greenScrollBar_ValueChanged(object sender, EventArgs e) {
178      Update(() => Content.G = greenScrollBar.Value);
179    }
180
181    private void blueScrollBar_ValueChanged(object sender, EventArgs e) {
182      Update(() => Content.B = blueScrollBar.Value);
183    }
184
185    private void hueUpDown_ValueChanged(object sender, EventArgs e) {
186      Update(() => Content.H = (double)hueUpDown.Value);
187    }
188
189    private void saturationUpDown_ValueChanged(object sender, EventArgs e) {
190      Update(() => Content.S = (double)saturationUpDown.Value/100.0);
191    }
192
193    private void brightnessUpDown_ValueChanged(object sender, EventArgs e) {
194      Update(() => Content.V = (double)brightnessUpDown.Value/100.0);
195    }
196
197    private void redUpDown_ValueChanged(object sender, EventArgs e) {
198      Update(() => Content.R = (int)redUpDown.Value);
199    }
200
201    private void greenUpDown_ValueChanged(object sender, EventArgs e) {
202      Update(() => Content.G = (int)greenUpDown.Value);
203    }
204
205    private void blueUpDown_ValueChanged(object sender, EventArgs e) {
206      Update(() => Content.B = (int)blueUpDown.Value);
207    }
208
209    private void colorWheelPanel_MouseDown(object sender, MouseEventArgs e) {
210      Update(() => SetColorWheel(e.Location));
211    }
212
213    private void colorWheelPanel_MouseMove(object sender, MouseEventArgs e) {
214      if (e.Button == MouseButtons.Left)
215        Update(() => SetColorWheel(e.Location));
216    }
217
218    private void brightnessPanel_MouseDown(object sender, MouseEventArgs e) {
219      Update(() => SetBrightness(e.Location));
220    }
221
222    private void brightnessPanel_MouseMove(object sender, MouseEventArgs e) {
223      if (e.Button == MouseButtons.Left) {
224        Update(() => SetBrightness(e.Location));
225      }
226    }
227
228    private void colorPanel_DoubleClick(object sender, EventArgs e) {
229      colorDialog.Color = Content.Color;
230      if (colorDialog.ShowDialog() == DialogResult.OK) {
231        Update(() => Content.Color = colorDialog.Color);
232      }
233    }
234    private void defaultColorPanel_MouseClick(object sender, MouseEventArgs e) {
235      if (Content != null)
236        Update(() => Content.Color = ((Panel)sender).BackColor);
237    }
238    #endregion
239
240    #region Auxiliary Functions
241    private void DrawColorPointer(Graphics g, Point p) {
242      g.DrawRectangle(Pens.Black, p.X - 3, p.Y - 3, 6, 6);
243    }
244
245    private void DrawBrighnessPointer(Graphics g, Point p) {
246      g.FillPolygon(Brushes.Black, new[] {
247        p,
248        new Point(p.X + 10, p.Y + 5),
249        new Point(p.X + 10, p.Y - 5)});
250    }
251
252    private void InitializeColorWheel() {
253      colorWheel = new Bitmap(256, 256);
254      using (var brush = new PathGradientBrush(GetPoints(RADIUS, new Point(RADIUS, RADIUS))) {
255        CenterColor = Color.White,
256        CenterPoint = new PointF(RADIUS, RADIUS),
257        SurroundColors = GetColors()
258      }) {
259        using (var g = Graphics.FromImage(colorWheel)) {
260          g.FillEllipse(brush, 0, 0, colorWheelPanel.Width, colorWheel.Height);
261        }
262      }
263      SetStyle(ControlStyles.AllPaintingInWmPaint, true);
264      SetStyle(ControlStyles.UserPaint, true);
265      SetStyle(ControlStyles.DoubleBuffer, true);
266    }
267
268    private static Point[] GetPoints(double radius, Point center) {
269      var points = new Point[COLOR_COUNT];
270      for (int i = 0; i<COLOR_COUNT; i++) {
271        points[i] = GetPoint(i*360.0/COLOR_COUNT, radius, center);
272      }
273      return points;
274    }
275
276    private static Point GetPoint(double degrees, double radius, Point center) {
277      var rad = degrees*Math.PI/180;
278      return new Point(
279        (int)Math.Round(center.X + radius * Math.Cos(rad)),
280        (int)Math.Round(center.Y + radius * Math.Sin(rad)));
281    }
282
283    private Color[] GetColors() {
284      var colors = new Color[COLOR_COUNT];
285      for (int i = 0; i<COLOR_COUNT; i++) {
286        colors[i] = new ColorValue(i*360.0/COLOR_COUNT, 1d, 1d).Color;
287      }
288      return colors;
289    }
290
291    private void SetColorWheel(Point p) {
292      if (Content == null) return;
293      var x = p.X - colorWheelPanel.Width/2f;
294      var y = p.Y - colorWheelPanel.Height/2f;
295      Content.S = Math.Min(1, Math.Max(0, Math.Sqrt(sqr(x) + sqr(y))/(colorWheelPanel.Width/2f)));
296      var angle = Math.Atan2(y, x)*180/Math.PI;
297      if (angle < 0) angle += 360;
298      Content.H = Math.Min(360, Math.Max(0, angle));
299    }
300
301    private void SetBrightness(Point p) {
302      if (Content != null)
303        Content.V = Math.Max(0, Math.Min(1, 1 - 1.0*p.Y/brightnessPanel.Height));
304    }
305
306    private static double sqr(double x) { return x*x; }
307    #endregion
308
309
310
311  }
312}
Note: See TracBrowser for help on using the repository browser.