colorchoosecontrol.cs
来自「Fireball.CodeEditor is an source code ed」· CS 代码 · 共 1,133 行 · 第 1/3 页
CS
1,133 行
_cursorRect.X = (int)(255 * saturation) - 4;
this.Invalidate();
_currentColor = this.GetColorAt(
_cursorRect.Left + 4,
_cursorRect.Top + 4
);
}
public void SetLuminance(float luminance)
{
_cursorRect.Y = 255 - (int)(255 * luminance) - 4;
this.Invalidate();
_currentColor = this.GetColorAt(
_cursorRect.Left + 4,
_cursorRect.Top + 4
);
}
private void MouseEvent(MouseEventArgs e)
{
Point newPos = new Point(e.X - 4, e.Y - 4);
if (newPos.X < -4) newPos.X = -4;
if (newPos.Y < -4) newPos.Y = -4;
if (newPos.X > (256 - 5)) newPos.X = (256 - 5);
if (newPos.Y > (256 - 5)) newPos.Y = (256 - 5);
_cursorRect.Location = newPos;
_currentColor = GetColorAt(newPos.X + 4, newPos.Y + 4);
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)
{
SmoothingMode smooth = e.Graphics.SmoothingMode;
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
Pen cursorPen = new Pen(_currentColor.Complementary);
cursorPen.Width = 2f;
e.Graphics.DrawEllipse(cursorPen, _cursorRect);
e.Graphics.SmoothingMode = smooth;
}
private ColorHLS GetColorAt(int x,int y)
{
ColorHLS color;
_pixBuffer.GetPixel(x, y, out color);
return color;
}
public void UpdateView()
{
}
}
private class ColorSlider : Control
{
private ColorHLS _baseColor;
private ColorHLS _currentColor;
private ColorHLS _previusColor;
private ColorSliderMode _colorMode;
private Rectangle _cursorRect;
private bool _onDrag;
private float _saturation;
private float _luminance;
private float _hue;
private PixelBuffer _pixBuffer;
internal event ColorChangedDelegate ColorChanged;
public ColorSlider()
:this(ColorSliderMode.Hue)
{
}
public ColorSlider(ColorSliderMode colorMode)
{
_colorMode = colorMode;
_baseColor = new ColorHLS(255, 255, 0, 0);
_currentColor = _baseColor.Clone();
_previusColor = _baseColor.Clone();
this.SetStyle(
ControlStyles.AllPaintingInWmPaint |
ControlStyles.FixedHeight |
ControlStyles.FixedWidth |
ControlStyles.OptimizedDoubleBuffer |
ControlStyles.UserPaint,
true);
this.Size = new System.Drawing.Size(20, 256);
_cursorRect = new Rectangle(0, 256-4, 20, 7);
_onDrag = false;
_pixBuffer = new PixelBuffer(20, 256);
FillBuffer();
this.Invalidate();
}
public ColorSliderMode ColorMode
{
get
{
return _colorMode;
}
set
{
_colorMode = value;
FillBuffer();
this.Invalidate();
}
}
public ColorHLS BaseColor
{
get
{
return _baseColor;
}
set
{
_baseColor = value;
ChangeBaseColor(_baseColor);
}
}
/*public ColorHLS CurrentColor
{
get
{
return _currentColor;
}
set
{
_currentColor = value;
}
}*/
/*public float Saturation
{
get
{
return _saturation;
}
}
public float Brightness
{
get
{
return _brightness;
}
}
public float Hue
{
get
{
return _hue; ;
}
}*/
public float Value
{
get
{
switch (_colorMode)
{
default:
case ColorSliderMode.Hue:
return _hue;
case ColorSliderMode.Saturation:
return _saturation;
case ColorSliderMode.Brightness:
return _luminance;
}
}
set
{
SetValue(value);
}
}
private void SetValue(float value)
{
float y = -3;
switch (_colorMode)
{
default:
case ColorSliderMode.Hue:
value %= 360;
y = 255- (((value / 359) * 255)+3);
_hue = value;
break;
case ColorSliderMode.Saturation:
if (value < 0 || value > 1)
{
throw new ArgumentOutOfRangeException("value");
}
y = (((1 - value) * 255))-3;
_saturation = value;
break;
case ColorSliderMode.Brightness:
if (value < 0 || value > 1)
{
throw new ArgumentOutOfRangeException("value");
}
y = ((1 - value) * 255)-3;
_luminance = value;
break;
}
_cursorRect.Y = (int)y;
_currentColor = GetColorAt(1, _cursorRect.Y + 3);
this.Invalidate();
}
private void ChangeBaseColor(ColorHLS color)
{
FillBuffer();
this.Invalidate();
}
private void ChangeCurrentColor(ColorHLS color)
{
throw new NotImplementedException();
}
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)
{
this.Size = new System.Drawing.Size(20, 256);
}
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.Brightness:
colors = ColorUtils.CreateGradientColorArray(
new ColorHLS[]
{
new ColorHLS(255, 0, 0, 0),
_baseColor
},
256
);
break;
}
//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]);
}
}
private void MouseEvent(MouseEventArgs e)
{
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.Brightness:
_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;
}
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;
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);
}
private ColorHLS GetColorAt(int x, int y)
{
ColorHLS color;
_pixBuffer.GetPixel(x, y, out color);
return color;
}
}
#endregion
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?