colorchoosecontrol.cs
来自「Fireball.CodeEditor is an source code ed」· CS 代码 · 共 1,133 行 · 第 1/3 页
CS
1,133 行
lsView.BaseColor = _currentColor;
hueView.BaseColor = _currentColor;
saturationView.BaseColor = _currentColor;
luminanceView.BaseColor = _currentColor;
hueView.ColorMode = ColorSliderMode.Hue;
saturationView.ColorMode = ColorSliderMode.Saturation;
luminanceView.ColorMode = ColorSliderMode.Brightness;
hueView.Value = 0;
saturationView.Value = 1;
luminanceView.Value = 1;
lsView.ColorChanged += new ColorChangedDelegate(lsView_ColorChanged);
hueView.ColorChanged += new ColorChangedDelegate(hueView_ColorChanged);
saturationView.ColorChanged += new ColorChangedDelegate(saturationView_ColorChanged);
luminanceView.ColorChanged += new ColorChangedDelegate(luminanceView_ColorChanged);
lsView.Invalidate();
}
void luminanceView_ColorChanged(object sender, ColorChooseControl.ColorChangedEventArgs e)
{
numLum.Value = new decimal(luminanceView.Value * 100);
}
void saturationView_ColorChanged(object sender, ColorChooseControl.ColorChangedEventArgs e)
{
numSat.Value = new decimal(saturationView.Value * 100);
}
void hueView_ColorChanged(object sender, ColorChooseControl.ColorChangedEventArgs e)
{
numHue.Value = new decimal(hueView.Value);
luminanceView.BaseColor = e.NewColor;
}
void lsView_ColorChanged(object sender, ColorChooseControl.ColorChangedEventArgs e)
{
_currentColor = lsView.CurrentColor;
numR.Value = _currentColor.Red;
numG.Value = _currentColor.Green;
numB.Value = _currentColor.Blue;
numHue.Value = new decimal(lsView.Hue);
numSat.Value = new decimal(lsView.Saturation * 100);
numLum.Value = new decimal(lsView.Luminance * 100);
}
private void numR_ValueChanged(object sender, EventArgs e)
{
if (!lsView.IsFocused)
{
ColorHLS newColor = lsView.CurrentColor.Clone();
newColor.Red = (byte)numR.Value;
hueView.Value = newColor.Hue;
lsView.SetHue(newColor.Hue);
}
}
private void numG_ValueChanged(object sender, EventArgs e)
{
if (!lsView.IsFocused)
{
ColorHLS newColor = lsView.CurrentColor.Clone();
newColor.Green = (byte)numG.Value;
hueView.Value = newColor.Hue;
lsView.SetHue(newColor.Hue);
}
}
private void numB_ValueChanged(object sender, EventArgs e)
{
if (!lsView.IsFocused)
{
ColorHLS newColor = lsView.CurrentColor.Clone();
newColor.Blue = (byte)numR.Value;
hueView.Value = newColor.Hue;
lsView.SetHue(newColor.Hue);
}
}
private void numHue_ValueChanged(object sender, EventArgs e)
{
lsView.SetHue((float)numHue.Value);
hueView.Value = (float)numHue.Value;
ColorHLS baseColor = new ColorHLS((float)numHue.Value, 0.5f, 1);
saturationView.BaseColor = baseColor;
luminanceView.BaseColor = baseColor;
this.panelColor2.BackColor = lsView.CurrentColor.Color;
}
private void numSat_ValueChanged(object sender, EventArgs e)
{
lsView.SetSaturation((float)(numSat.Value / 100));
saturationView.Value = (float)(numSat.Value / 100);
this.panelColor2.BackColor = lsView.CurrentColor.Color;
}
private void numLum_ValueChanged(object sender, EventArgs e)
{
lsView.SetLuminance((float)(numLum.Value / 100));
luminanceView.Value = (float)(numLum.Value / 100);
this.panelColor2.BackColor = lsView.CurrentColor.Color;
}
public ColorHLS CurrentColor
{
get
{
return _currentColor;
}
set
{
_currentColor = value;
lsView.SetHue(value.Hue);
lsView.SetSaturation(value.Saturation);
lsView.SetLuminance(value.Luminance);
hueView.BaseColor = value;
}
}
#region Nested Types
private delegate void ColorChangedDelegate(object sender, ColorChangedEventArgs e);
private enum ColorSliderMode
{
Hue=0,Saturation=1,Brightness=2
}
private class ColorChangedEventArgs
{
public ColorHLS OldColor;
public ColorHLS NewColor;
public ColorChangedEventArgs(ColorHLS oldColor, ColorHLS newColor)
{
OldColor = oldColor;
NewColor = newColor;
}
}
private class ColorPicker : Control
{
private ColorHLS _baseColor;
private ColorHLS _currentColor;
private Rectangle _cursorRect;
private bool _onDrag;
private ColorHLS _previusColor;
private PixelBuffer _pixBuffer;
internal event ColorChangedDelegate ColorChanged;
public ColorPicker()
{
_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(256, 256);
_cursorRect = new Rectangle(252, -3, 9, 9);
_onDrag = false;
_pixBuffer = new PixelBuffer(256, 256);
FillBuffer();
MouseEvent(new MouseEventArgs(MouseButtons.Left, 1, _cursorRect.X + 4, _cursorRect.Y + 4, 0));
}
public bool IsFocused
{
get
{
return _onDrag;
}
}
private void FillBuffer()
{
ColorHLS startColor=new ColorHLS(255, 255, 255, 255);
ColorHLS endColor = _baseColor.Clone();
float[,] colorsStepStart = ColorUtils.GetGradientColorSteps(startColor, new ColorHLS(255,0,0,0), 256);
float[,] colorsStepEnd = ColorUtils.GetGradientColorSteps(endColor, new ColorHLS(255, 0, 0, 0), 256);
for (int i = 0; i < 256; i++)
{
startColor.SetRGB((byte)colorsStepStart[i, 0], (byte)colorsStepStart[i, 1], (byte)colorsStepStart[i, 2]);
endColor.SetRGB((byte)colorsStepEnd[i,0],(byte)colorsStepEnd[i,1],(byte)colorsStepEnd[i,2]);
_pixBuffer.DrawLine(0, i, 256, i, startColor, endColor);
}
}
protected override void OnSizeChanged(EventArgs e)
{
this.Size = new System.Drawing.Size(256, 256);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
DrawBackground(e);
DrawCursor(e);
}
protected override void OnMouseDown(MouseEventArgs e)
{
_previusColor = _currentColor.Clone();
MouseEvent(e);
_onDrag = true;
}
protected override void OnMouseMove(MouseEventArgs e)
{
if (_onDrag)
{
MouseEvent(e);
}
}
protected override void OnMouseUp(MouseEventArgs e)
{
_onDrag = false;
MouseEvent(e);
}
public ColorHLS BaseColor
{
get
{
return _baseColor;
}
set
{
ColorHLS oldColor = _baseColor.Clone();
_baseColor = value;
FillBuffer();
this.Invalidate();
_currentColor = this.GetColorAt(
_cursorRect.Left + 4,
_cursorRect.Top + 4
);
if (this.ColorChanged != null)
this.ColorChanged(
this,
new ColorChangedEventArgs(
oldColor,
_currentColor
)
);
}
}
public ColorHLS CurrentColor
{
get
{
return _currentColor;
}
}
public float Hue
{
get
{
return _baseColor.Hue;
}
set
{
ColorHLS oldColor = _currentColor.Clone();
_baseColor = new ColorHLS(value, 0.5f, 1);
FillBuffer();
_currentColor = this.GetColorAt(
_cursorRect.Left + 4,
_cursorRect.Top + 4
);
this.Invalidate();
if (this.ColorChanged != null)
this.ColorChanged(
this,
new ColorChangedEventArgs(
oldColor,
_currentColor
)
);
}
}
public float Saturation
{
get
{
float x = _cursorRect.X + 4;
return (x / 255);
}
set
{
ColorHLS oldColor = _currentColor.Clone();
_cursorRect.X = (int)(255 * value)-4;
this.Invalidate();
_currentColor = this.GetColorAt(
_cursorRect.Left + 4,
_cursorRect.Top + 4
);
if (this.ColorChanged != null)
this.ColorChanged(
this,
new ColorChangedEventArgs(
oldColor,
_currentColor
)
);
}
}
public float Luminance
{
get
{
float y = _cursorRect.Y + 4;
return 1 - ( y / 255);
//return _currentColor.Luminance;
}
set
{
ColorHLS oldColor = _currentColor.Clone();
_cursorRect.Y = 255 - (int)(255 * value) - 4;
this.Invalidate();
_currentColor = this.GetColorAt(
_cursorRect.Left + 4,
_cursorRect.Top + 4
);
if (this.ColorChanged != null)
this.ColorChanged(
this,
new ColorChangedEventArgs(
oldColor,
_currentColor
)
);
}
}
public void SetHue(float hue)
{
_baseColor = new ColorHLS(hue, 0.5f, 1);
FillBuffer();
this.Invalidate();
_currentColor = this.GetColorAt(
_cursorRect.Left + 4,
_cursorRect.Top + 4
);
}
public void SetSaturation(float saturation)
{
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?