colorslider.cs
来自「Fireball.CodeEditor is an source code ed」· CS 代码 · 共 453 行 · 第 1/2 页
CS
453 行
_currentColor = GetColorAt(_cursorRect.X+3, 1);
}
this.Invalidate();
}
private void ChangeBaseColor(ColorHLS color)
{
FillBuffer();
this.Invalidate();
}
private void ChangeCurrentColor(ColorHLS color)
{
throw new NotImplementedException();
}
private void UpdateControl()
{
if (_orientation == ColorSliderOrientation.Vertical)
{
_cursorRect = new Rectangle(0, 256 - 4, 20, 7);
}
else
{
_cursorRect = new Rectangle(-3, 0, 7, 20);
}
_pixBuffer = new PixelBuffer(this.Width, this.Height);
FillBuffer();
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
_previusColor = _currentColor.Clone();
MouseEvent(e);
_onDrag = true;
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (_onDrag)
{
MouseEvent(e);
}
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
_onDrag = false;
MouseEvent(e);
}
protected override void OnSizeChanged(EventArgs e)
{
if (_orientation == ColorSliderOrientation.Vertical)
{
this.Size = new System.Drawing.Size(20, 256);
}
else
{
this.Size = new System.Drawing.Size(256, 20);
}
UpdateControl();
this.Invalidate();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
DrawBackground(e);
DrawCursor(e);
}
private void FillBuffer()
{
ColorHLS[] colors = new ColorHLS[0];
switch (_colorMode)
{
default:
case ColorSliderMode.Hue:
colors = ColorUtils.CreateGradientColorArray(
new ColorHLS[]
{
new ColorHLS(255, 255, 0, 0),
new ColorHLS(255, 255, 255, 0),
new ColorHLS(255, 0, 255, 0),
new ColorHLS(255, 0, 255, 255),
new ColorHLS(255, 0, 0, 255),
new ColorHLS(255, 255, 0, 255),
new ColorHLS(255, 255, 0, 0)
},
256
);
break;
case ColorSliderMode.Saturation:
colors = ColorUtils.CreateGradientColorArray(
new ColorHLS[]
{
new ColorHLS(255, 255, 255, 255),
_baseColor
},
256
);
break;
case ColorSliderMode.Luminance:
colors = ColorUtils.CreateGradientColorArray(
new ColorHLS[]
{
new ColorHLS(255, 0, 0, 0),
_baseColor
},
256
);
break;
}
if (_orientation == ColorSliderOrientation.Vertical)
{
//ColorHLS[] colors = ColorUtils.CreateGradientColorArray(new ColorHLS(255,255, 0, 0), new ColorHLS(255,255, 255, 0), 256);
for (int y = 0; y < this.Height; y++)
{
_pixBuffer.DrawLine(0, this.Height - 1 - y, this.Width, this.Height - 1 - y, colors[y]);
}
}
else
{
for (int x = 0; x < this.Width; x++)
{
_pixBuffer.DrawLine(x, 0, x, this.Height, colors[x]);
}
}
}
private void MouseEvent(MouseEventArgs e)
{
if (_orientation == ColorSliderOrientation.Vertical)
{
Point newPos = new Point(0, e.Y - 3);
if (newPos.Y < -3) newPos.Y = -3;
if (newPos.Y > (256 - 4)) newPos.Y = (256 - 4);
_cursorRect.Location = newPos;
_currentColor = GetColorAt(1, newPos.Y + 3);
switch (_colorMode)
{
case ColorSliderMode.Saturation:
_saturation = 1 - ((float)(newPos.Y + 3) / 255);
break;
case ColorSliderMode.Luminance:
_luminance = 1 - ((float)(newPos.Y + 3) / 255); //_currentColor.Luminance * 2;
break;
case ColorSliderMode.Hue:
_hue = (float)Math.Round(((double)(255 - (newPos.Y + 3)) / 255) * 359, MidpointRounding.ToEven); //_currentColor.Hue;
break;
}
}
else
{
Point newPos = new Point(e.X-3, 0);
if (newPos.X < -3) newPos.X = -3;
if (newPos.X > (256 - 4)) newPos.X = (256 - 4);
_cursorRect.Location = newPos;
_currentColor = GetColorAt(newPos.X+3, 1);
switch (_colorMode)
{
case ColorSliderMode.Saturation:
_saturation = ((float)(newPos.X + 3) / 255);
break;
case ColorSliderMode.Luminance:
_luminance = ((float)(newPos.X + 3) / 255); //_currentColor.Luminance * 2;
break;
case ColorSliderMode.Hue:
_hue = (float)Math.Round(((double)((newPos.Y + 3)) / 255) * 359, MidpointRounding.ToEven); //_currentColor.Hue;
break;
}
}
this.Invalidate();
if (this.ColorChanged != null)
this.ColorChanged(this, new ColorChangedEventArgs(_previusColor, _currentColor));
}
private void DrawBackground(PaintEventArgs e)
{
_pixBuffer.Render(e.Graphics, 0, 0);
}
private void DrawCursor(PaintEventArgs e)
{
Pen cursorPen = new Pen(_currentColor.Complementary);
cursorPen.Width = 1f;
if (_orientation == ColorSliderOrientation.Vertical)
{
e.Graphics.DrawLine(cursorPen, _cursorRect.X, _cursorRect.Y, _cursorRect.Right, _cursorRect.Y);
e.Graphics.DrawLine(cursorPen, _cursorRect.X, _cursorRect.Y + 1, _cursorRect.Right, _cursorRect.Y + 1);
e.Graphics.DrawLine(cursorPen, _cursorRect.X, _cursorRect.Y + 2, _cursorRect.Right, _cursorRect.Y + 2);
e.Graphics.DrawLine(cursorPen, _cursorRect.X, _cursorRect.Y + 4, _cursorRect.Right, _cursorRect.Y + 4);
e.Graphics.DrawLine(cursorPen, _cursorRect.X, _cursorRect.Y + 5, _cursorRect.Right, _cursorRect.Y + 5);
e.Graphics.DrawLine(cursorPen, _cursorRect.X, _cursorRect.Y + 6, _cursorRect.Right, _cursorRect.Y + 6);
}
else
{
e.Graphics.DrawLine(cursorPen, _cursorRect.X, _cursorRect.Y, _cursorRect.X, _cursorRect.Bottom);
e.Graphics.DrawLine(cursorPen, _cursorRect.X + 1, _cursorRect.Y, _cursorRect.X + 1, _cursorRect.Bottom);
e.Graphics.DrawLine(cursorPen, _cursorRect.X + 2, _cursorRect.Y, _cursorRect.X + 2, _cursorRect.Bottom);
e.Graphics.DrawLine(cursorPen, _cursorRect.X + 4, _cursorRect.Y, _cursorRect.X + 4, _cursorRect.Bottom);
e.Graphics.DrawLine(cursorPen, _cursorRect.X + 5, _cursorRect.Y, _cursorRect.X + 5, _cursorRect.Bottom);
e.Graphics.DrawLine(cursorPen, _cursorRect.X + 6, _cursorRect.Y, _cursorRect.X + 6, _cursorRect.Bottom);
}
}
private ColorHLS GetColorAt(int x, int y)
{
ColorHLS color;
_pixBuffer.GetPixel(x, y, out color);
return color;
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?