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