I am making a custom color dialog. It will look like this Note at the red rectangle, I draw this hue slide on the mouse_move event of the rainbow at the bottom. My problem is the drawing speed. It is a bit slow. Is there another way to draw this panel? Here is my code:
// picColorRuler is the rainbow below
// picColorArea is the panel with red border in the attached image
// Change the target location.
private void picColorRuler_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
rulerColorTarget.Left = e.X;
if (rulerColorTarget.Left < 0) rulerColorTarget.Left = 0;
if (rulerColorTarget.Left > picColorRuler.ClientSize.Width - 1)
{
rulerColorTarget.Left = picColorRuler.ClientSize.Width - 1;
}
picColorRuler.Refresh();
}
}
// Painting the rainbow.
private void picColorRuler_Paint(object sender, PaintEventArgs e)
{
// Draw the target.
rulerColorTarget.Color = rulerImg.GetPixel(rulerColorTarget.Left, rulerColorTarget.Top);
rulerColorTarget.Draw(e.Graphics);
// Change the picColorArea's colors.
PaintPicColorArea();
}
private void PaintPicColorArea()
{
var width = picColorArea.ClientSize.Width;
var height = picColorArea.ClientSize.Height;
var satSeed = 1f / width;
var lightSeed = 1f / height;
var sat = 0f;
for (var x = 0; x < width; ++x)
{
if (x == width - 1) sat = 1;
var light = 0f;
for (var y = 0; y < height; ++y)
{
if (y == height - 1) light = 1;
var sc = Util.Hsl2Rgb(rulerColorTarget.Color.GetHue(), sat, light);
areaImg.SetPixel(x, y, sc);
light += lightSeed;
}
sat += satSeed;
}
picColorArea.BackgroundImage = null;
picColorArea.BackgroundImage = areaImg;
}