-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValueSlider.cs
184 lines (164 loc) · 6.07 KB
/
ValueSlider.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace ColourPickerComponents
{
public partial class ValueSlider : BaseColourControl
{
// selectedColor is the actual value selected
// by the user. fullColor is the same color,
// with its brightness set to 255.
private Color SelectedColour = Color.White;
private Color fullcolour = Color.White;
public Color colourBeforeClick;
private Rectangle paintArea;
private Point brightnessPoint;
private Bitmap gradientImage;
public ValueSlider()
{
InitializeComponent();
this.DoubleBuffered = true;
}
private void BrightnessSlider_Load(object sender, EventArgs e)
{
this.CalcPositions();
this.UpdateBitmap();
this.Refresh();
}
private void UpdateBitmap()
{
Rectangle r = new Rectangle(0, 0, 50, 200);
using (LinearGradientBrush lgb = new LinearGradientBrush(r,this.fullcolour,Color.Black,LinearGradientMode.Vertical))
{
// Create a new bitmap containing
// the color wheel gradient, so the
// code only needs to do all this
// work once. Later code uses the bitmap
// rather than recreating the gradient.
gradientImage = new Bitmap(50, 200, PixelFormat.Format32bppArgb);
using (Graphics newGraphics = Graphics.FromImage(gradientImage))
{
newGraphics.FillRectangle(lgb, r);
}
}
}
private void CalcPositions()
{
Rectangle old = this.paintArea;
this.paintArea = new Rectangle(this.Left, this.Top, this.Width, this.Height);
if (old.Height == 0) // first time round
{
this.brightnessPoint = new Point(0, (int)((double)this.paintArea.Height * 0.2d));
}
else
{
double prop = (double)this.brightnessPoint.Y / (double)old.Height;
this.brightnessPoint.Y = (int)(prop * (double)this.paintArea.Height);
}
}
public void SetColour(Color colour)
{
this.fullcolour = colour;
this.UpdateBitmap();
this.Refresh();
this.UpdateColour(this.CalcColour());
}
private void ValueSlider_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawImage(this.gradientImage, new Rectangle(0, 0, paintArea.Width, paintArea.Height));
if (this.ShowMarker)
{
ColourHandler.HSV alt = ColourHandler.RGBtoHSV(new ColourHandler.RGB(this.SelectedColour.R, this.SelectedColour.G, this.SelectedColour.B));
alt.Saturation = 0;
alt.value = 255 - alt.value;
using (Pen pen = new Pen(ColourHandler.HSVtoColour(alt)))
{
pen.Width = this.MarkerPenWidth;
e.Graphics.DrawRectangle(pen, 0 - pen.Width, this.brightnessPoint.Y - pen.Width, this.paintArea.Width + (pen.Width * 2), this.MarkerWidth * 2);
}
}
}
private void ValueSlider_Resize(object sender, EventArgs e)
{
this.CalcPositions();
this.UpdateBitmap();
this.Refresh();
}
/////////////////////////////
//
// MOUSE EVENTS
//
/////////////////////////////
private void ValueSlider_MouseDown(object sender, MouseEventArgs e)
{
if (this.mouseInRegion)
{
this.HasBeenClicked = true;
this.mouseDown = true;
this.colourBeforeClick = this.SelectedColour;
this.UpdateBrightnessPoint(e.Location);
}
}
private void ValueSlider_MouseMove(object sender, MouseEventArgs e)
{
this.mouseInRegion = true;
if (this.mouseDown)
{
this.UpdateBrightnessPoint(e.Location);
}
}
private void ValueSlider_MouseLeave(object sender, EventArgs e)
{
this.mouseInRegion = false;
if (this.mouseDown)
{
this.ResetColorAfterDragout();
}
}
private void ValueSlider_MouseUp(object sender, MouseEventArgs e)
{
this.mouseDown = false;
}
private void ResetColorAfterDragout()
{
this.UpdateColour(this.colourBeforeClick);
}
private void UpdateBrightnessPoint(Point point)
{
if (point.Y < 0)
{
this.brightnessPoint.Y = 0;
}
else if (point.Y > (this.Height-this.MarkerWidth))
{
this.brightnessPoint.Y = this.Height - this.MarkerWidth;
}
else
{
this.brightnessPoint.Y = point.Y;
}
this.UpdateColour(this.CalcColour());
}
private Color CalcColour()
{
this.HSV = ColourHandler.RGBtoHSV(new ColourHandler.RGB(this.fullcolour.R, this.fullcolour.G,this.fullcolour.B));
double value = ((double)(this.brightnessPoint.Y) / (double)(this.Height-this.MarkerWidth)) * 255;
this.HSV.value = 255 - (int)value;
return ColourHandler.HSVtoColour(HSV);
}
private void UpdateColour(Color colour)
{
this.SelectedColour = colour;
ColourEventArgs args = new ColourEventArgs(colour);
OnNewColour(args);
this.Refresh();
}
}
}