|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.ComponentModel; |
| 4 | +using System.Drawing; |
| 5 | +using System.Drawing.Drawing2D; |
| 6 | +using System.Data; |
| 7 | +using System.Text; |
| 8 | +using System.Windows.Forms; |
| 9 | + |
| 10 | + |
| 11 | +namespace CommonTools |
| 12 | +{ |
| 13 | + public class ColorSlider : LabelRotate |
| 14 | + { |
| 15 | + public event EventHandler SelectedValueChanged; |
| 16 | + Orientation m_orientation = Orientation.Vertical; |
| 17 | + public Orientation Orientation |
| 18 | + { |
| 19 | + get { return m_orientation; } |
| 20 | + set { m_orientation = value; } |
| 21 | + } |
| 22 | + |
| 23 | + public enum eNumberOfColors |
| 24 | + { |
| 25 | + Use2Colors, |
| 26 | + Use3Colors, |
| 27 | + } |
| 28 | + eNumberOfColors m_numberOfColors = eNumberOfColors.Use3Colors; |
| 29 | + public eNumberOfColors NumberOfColors |
| 30 | + { |
| 31 | + get { return m_numberOfColors; } |
| 32 | + set { m_numberOfColors = value; } |
| 33 | + } |
| 34 | + public enum eValueOrientation |
| 35 | + { |
| 36 | + MinToMax, |
| 37 | + MaxToMin, |
| 38 | + } |
| 39 | + eValueOrientation m_valueOrientation = eValueOrientation.MinToMax; |
| 40 | + public eValueOrientation ValueOrientation |
| 41 | + { |
| 42 | + get { return m_valueOrientation; } |
| 43 | + set { m_valueOrientation = value; } |
| 44 | + } |
| 45 | + float m_percent = 0; |
| 46 | + public float Percent |
| 47 | + { |
| 48 | + get { return m_percent; } |
| 49 | + set |
| 50 | + { |
| 51 | + // ok so it is not really percent, but a value between 0 - 1. |
| 52 | + if (value < 0) value = 0; |
| 53 | + if (value > 1) value = 1; |
| 54 | + if (value != m_percent) |
| 55 | + { |
| 56 | + m_percent = value; |
| 57 | + if (SelectedValueChanged != null) |
| 58 | + SelectedValueChanged(this, null); |
| 59 | + Invalidate(); |
| 60 | + } |
| 61 | + |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + Color m_color1 = Color.Black; |
| 66 | + Color m_color2 = Color.FromArgb(255,127,127,127); |
| 67 | + Color m_color3 = Color.White; |
| 68 | + public Color Color1 |
| 69 | + { |
| 70 | + get { return m_color1; } |
| 71 | + set { m_color1 = value; } |
| 72 | + } |
| 73 | + public Color Color2 |
| 74 | + { |
| 75 | + get { return m_color2; } |
| 76 | + set { m_color2 = value; } |
| 77 | + } |
| 78 | + public Color Color3 |
| 79 | + { |
| 80 | + get { return m_color3; } |
| 81 | + set { m_color3 = value; } |
| 82 | + } |
| 83 | + |
| 84 | + Padding m_barPadding = new Padding(12,5, 24, 10); |
| 85 | + public Padding BarPadding |
| 86 | + { |
| 87 | + get { return m_barPadding; } |
| 88 | + set |
| 89 | + { |
| 90 | + m_barPadding = value; |
| 91 | + Invalidate(); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + public ColorSlider() |
| 96 | + { |
| 97 | + } |
| 98 | + |
| 99 | + protected override void OnGotFocus(EventArgs e) |
| 100 | + { |
| 101 | + base.OnGotFocus(e); |
| 102 | + Invalidate(); |
| 103 | + } |
| 104 | + protected override void OnLostFocus(EventArgs e) |
| 105 | + { |
| 106 | + base.OnLostFocus(e); |
| 107 | + Invalidate(); |
| 108 | + } |
| 109 | + protected override void OnPaint(PaintEventArgs e) |
| 110 | + { |
| 111 | + base.OnPaint(e); |
| 112 | + DrawColorBar(e.Graphics); |
| 113 | + |
| 114 | + if (Focused) |
| 115 | + { |
| 116 | + RectangleF lr = ClientRectangleF; |
| 117 | + lr.Inflate(-2,-2); |
| 118 | + ControlPaint.DrawFocusRectangle(e.Graphics, Util.Rect(lr)); |
| 119 | + } |
| 120 | + } |
| 121 | + protected override void OnMouseMove(MouseEventArgs e) |
| 122 | + { |
| 123 | + base.OnMouseMove(e); |
| 124 | + PointF mousepoint = new PointF(e.X, e.Y); |
| 125 | + if (e.Button == MouseButtons.Left) |
| 126 | + SetPercent(mousepoint); |
| 127 | + } |
| 128 | + protected override void OnMouseDown(MouseEventArgs e) |
| 129 | + { |
| 130 | + base.OnMouseDown(e); |
| 131 | + Focus(); |
| 132 | + PointF mousepoint = new PointF(e.X, e.Y); |
| 133 | + if (e.Button == MouseButtons.Left) |
| 134 | + SetPercent(mousepoint); |
| 135 | + } |
| 136 | + protected override bool ProcessDialogKey(Keys keyData) |
| 137 | + { |
| 138 | + float percent = Percent * 100; |
| 139 | + int step = 0; |
| 140 | + if ((keyData & Keys.Up) == Keys.Up) |
| 141 | + step = 1; |
| 142 | + if ((keyData & Keys.Down) == Keys.Down) |
| 143 | + step = -1; |
| 144 | + if ((keyData & Keys.Control) == Keys.Control) |
| 145 | + step *= 5; |
| 146 | + if (step != 0) |
| 147 | + { |
| 148 | + SetPercent((float)Math.Round(percent + step)); |
| 149 | + return true; |
| 150 | + } |
| 151 | + return base.ProcessDialogKey(keyData); |
| 152 | + } |
| 153 | + protected virtual void SetPercent(float percent) |
| 154 | + { |
| 155 | + Percent = percent / 100; |
| 156 | + } |
| 157 | + protected virtual void SetPercent(PointF mousepoint) |
| 158 | + { |
| 159 | + RectangleF cr = ClientRectangleF; |
| 160 | + RectangleF br = BarRectangle; |
| 161 | + mousepoint.X += cr.X - br.X; |
| 162 | + mousepoint.Y += cr.Y - br.Y; |
| 163 | + Percent = GetPercentSet(BarRectangle, Orientation, mousepoint); |
| 164 | + Refresh(); |
| 165 | + } |
| 166 | + protected RectangleF BarRectangle |
| 167 | + { |
| 168 | + get |
| 169 | + { |
| 170 | + RectangleF r = ClientRectangle; |
| 171 | + r.X += BarPadding.Left; |
| 172 | + r.Width -= BarPadding.Right; |
| 173 | + r.Y += BarPadding.Top; |
| 174 | + r.Height -= BarPadding.Bottom; |
| 175 | + return r; |
| 176 | + } |
| 177 | + } |
| 178 | + protected float GetPercentSet(RectangleF r, Orientation orientation, PointF mousepoint) |
| 179 | + { |
| 180 | + float percentSet = 0; |
| 181 | + if (orientation == Orientation.Vertical) |
| 182 | + { |
| 183 | + if (m_valueOrientation == eValueOrientation.MaxToMin) |
| 184 | + percentSet = 1 - ((mousepoint.Y - r.Y / r.Height) / r.Height); |
| 185 | + else |
| 186 | + percentSet = mousepoint.Y / r.Height; |
| 187 | + } |
| 188 | + if (orientation == Orientation.Horizontal) |
| 189 | + if (m_valueOrientation == eValueOrientation.MaxToMin) |
| 190 | + percentSet = 1 - ((mousepoint.X - r.X / r.Width) / r.Width); |
| 191 | + else |
| 192 | + percentSet = (mousepoint.X / r.Width); |
| 193 | + if (percentSet < 0) |
| 194 | + percentSet = 0; |
| 195 | + if (percentSet > 100) |
| 196 | + percentSet = 100; |
| 197 | + return percentSet; |
| 198 | + } |
| 199 | + protected void DrawSelector(Graphics dc, RectangleF r, Orientation orientation, float percentSet) |
| 200 | + { |
| 201 | + Pen pen = new Pen(Color.CadetBlue); |
| 202 | + percentSet = Math.Max(0, percentSet); |
| 203 | + percentSet = Math.Min(1, percentSet); |
| 204 | + if (orientation == Orientation.Vertical) |
| 205 | + { |
| 206 | + float selectorY = (float)Math.Floor(r.Top + (r.Height - (r.Height * percentSet))); |
| 207 | + if (m_valueOrientation == eValueOrientation.MaxToMin) |
| 208 | + selectorY = (float)Math.Floor(r.Top + (r.Height - (r.Height * percentSet))); |
| 209 | + else |
| 210 | + selectorY = (float)Math.Floor(r.Top + (r.Height * percentSet)); |
| 211 | + |
| 212 | + dc.DrawLine(pen, r.X, selectorY, r.Right, selectorY); |
| 213 | + |
| 214 | + Image image = SelectorImages.Image(SelectorImages.eIndexes.Right); |
| 215 | + float xpos = r.Right; |
| 216 | + float ypos = selectorY - image.Height/2; |
| 217 | + dc.DrawImageUnscaled(image, (int)xpos, (int)ypos); |
| 218 | + |
| 219 | + image = SelectorImages.Image(SelectorImages.eIndexes.Left); |
| 220 | + xpos = r.Left - image.Width; |
| 221 | + dc.DrawImageUnscaled(image, (int)xpos, (int)ypos); |
| 222 | + } |
| 223 | + if (orientation == Orientation.Horizontal) |
| 224 | + { |
| 225 | + float selectorX = 0; |
| 226 | + if (m_valueOrientation == eValueOrientation.MaxToMin) |
| 227 | + selectorX = (float)Math.Floor(r.Left + (r.Width - (r.Width * percentSet))); |
| 228 | + else |
| 229 | + selectorX = (float)Math.Floor(r.Left + (r.Width * percentSet)); |
| 230 | + |
| 231 | + dc.DrawLine(pen, selectorX, r.Top, selectorX, r.Bottom); |
| 232 | + |
| 233 | + Image image = SelectorImages.Image(SelectorImages.eIndexes.Up); |
| 234 | + float xpos = selectorX - image.Width/2; |
| 235 | + float ypos = r.Bottom; |
| 236 | + dc.DrawImageUnscaled(image, (int)xpos, (int)ypos); |
| 237 | + |
| 238 | + image = SelectorImages.Image(SelectorImages.eIndexes.Down); |
| 239 | + ypos = r.Top - image.Height; |
| 240 | + dc.DrawImageUnscaled(image, (int)xpos, (int)ypos); |
| 241 | + } |
| 242 | + } |
| 243 | + protected void DrawColorBar(Graphics dc) |
| 244 | + { |
| 245 | + RectangleF lr = BarRectangle; |
| 246 | + if (m_numberOfColors == eNumberOfColors.Use2Colors) |
| 247 | + Util.Draw2ColorBar(dc, lr, Orientation, m_color1, m_color2); |
| 248 | + else |
| 249 | + Util.Draw3ColorBar(dc, lr, Orientation, m_color1, m_color2, m_color3); |
| 250 | + DrawSelector(dc, lr, Orientation, (float)Percent); |
| 251 | + } |
| 252 | + } |
| 253 | + |
| 254 | + public class HSLColorSlider : ColorSlider |
| 255 | + { |
| 256 | + HSLColor m_selectedColor = new HSLColor(); |
| 257 | + public HSLColor SelectedHSLColor |
| 258 | + { |
| 259 | + get { return m_selectedColor; } |
| 260 | + set |
| 261 | + { |
| 262 | + if (m_selectedColor == value) |
| 263 | + return; |
| 264 | + m_selectedColor = value; |
| 265 | + value.Lightness = 0.5; |
| 266 | + Color2 = Color.FromArgb(255, value.Color); |
| 267 | + Percent = (float)m_selectedColor.Lightness; |
| 268 | + Refresh();//Invalidate(Util.Rect(BarRectangle)); |
| 269 | + } |
| 270 | + } |
| 271 | + |
| 272 | + protected override void SetPercent(PointF mousepoint) |
| 273 | + { |
| 274 | + base.SetPercent(mousepoint); |
| 275 | + m_selectedColor.Lightness = Percent; |
| 276 | + Refresh(); |
| 277 | + } |
| 278 | + protected override void SetPercent(float percent) |
| 279 | + { |
| 280 | + base.SetPercent(percent); |
| 281 | + m_selectedColor.Lightness = percent / 100; |
| 282 | + SelectedHSLColor = m_selectedColor; |
| 283 | + } |
| 284 | + } |
| 285 | +} |
0 commit comments