static HslColor()
 {
   Empty = new HslColor
   {
     IsEmpty = true
   };
 }
    public static ColorCollection ScaledPalette(IEnumerable<Color> topRow)
    {
      ColorCollection results;

      results = new ColorCollection();

      topRow = topRow.ToArray();
      results.AddRange(topRow);

      for (int i = 5; i >= 0; i--)
      {
        foreach (Color color in topRow)
        {
          HslColor hsl;

          hsl = new HslColor(color);
          hsl.L = (5 + i + (16 * i)) / 100D;

          results.Add(hsl.ToRgbColor());
        }
      }

      return results;
    }
        protected virtual void PaintCell(PaintEventArgs e, int colorIndex, int cellIndex, Color color, Rectangle bounds)
        {
            if (color.A != 255)
              {
            this.PaintTransparentCell(e, bounds);
              }

              using (Brush brush = new SolidBrush(color))
              {
            e.Graphics.FillRectangle(brush, bounds);
              }

              switch (this.CellBorderStyle)
              {
            case ColorCellBorderStyle.FixedSingle:
              using (Pen pen = new Pen(this.CellBorderColor))
              {
            e.Graphics.DrawRectangle(pen, bounds.Left, bounds.Top, bounds.Width - 1, bounds.Height - 1);
              }
              break;
            case ColorCellBorderStyle.DoubleSoft:
              HslColor shadedOuter;
              HslColor shadedInner;

              shadedOuter = new HslColor(color);
              shadedOuter.L -= 0.50;

              shadedInner = new HslColor(color);
              shadedInner.L -= 0.20;

              using (Pen pen = new Pen(this.CellBorderColor))
              {
            e.Graphics.DrawRectangle(pen, bounds.Left, bounds.Top, bounds.Width - 1, bounds.Height - 1);
              }
              e.Graphics.DrawRectangle(Pens.White, bounds.Left + 1, bounds.Top + 1, bounds.Width - 3, bounds.Height - 3);
              using (Pen pen = new Pen(Color.FromArgb(32, shadedOuter.ToRgbColor())))
              {
            e.Graphics.DrawRectangle(pen, bounds.Left + 2, bounds.Top + 2, bounds.Width - 5, bounds.Height - 5);
              }
              using (Pen pen = new Pen(Color.FromArgb(32, shadedInner.ToRgbColor())))
              {
            e.Graphics.DrawRectangle(pen, bounds.Left + 3, bounds.Top + 3, bounds.Width - 7, bounds.Height - 7);
              }
              break;
              }

              if (this.HotIndex != InvalidIndex && this.HotIndex == cellIndex)
              {
            e.Graphics.DrawRectangle(Pens.Black, bounds.Left, bounds.Top, bounds.Width - 1, bounds.Height - 1);
            e.Graphics.DrawRectangle(Pens.White, bounds.Left + 1, bounds.Top + 1, bounds.Width - 3, bounds.Height - 3);
              }
        }
        protected virtual void CreateScale()
        {
            HslColor color;

            color = new HslColor(Color);

            color.L = 0;
            Color1 = color.ToRgbColor();

            color.L = 1;
            Color2 = color.ToRgbColor();
        }
    protected virtual void CreateScale()
    {
      HslColor color;

      color = new HslColor(this.Color);

      color.S = 0;
      this.Color1 = color.ToRgbColor();

      color.S = 1;
      this.Color2 = color.ToRgbColor();
    }
    /// <summary>
    /// Raises the <see cref="ColorSlider.ValueChanged" /> event.
    /// </summary>
    /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
    protected override void OnValueChanged(EventArgs e)
    {
      if (!this.LockUpdates)
      {
        HslColor color;

        this.LockUpdates = true;
        color = new HslColor(this.Color);
        color.L = this.Value / 100D;
        _color = color.ToRgbColor();
        this.OnColorChanged(e);
        this.LockUpdates = false;
      }

      base.OnValueChanged(e);
    }
 protected void PaintColor(PaintEventArgs e, HslColor color)
 {
   this.PaintColor(e, color, false);
 }
    protected virtual void PaintColor(PaintEventArgs e, HslColor color, bool includeFocus)
    {
      PointF location;

      location = this.GetColorLocation(color);

      if (!float.IsNaN(location.X) && !float.IsNaN(location.Y))
      {
        int x;
        int y;

        x = (int)location.X - (this.SelectionSize / 2);
        y = (int)location.Y - (this.SelectionSize / 2);

        if (this.SelectionGlyph == null)
        {
          e.Graphics.DrawRectangle(Pens.Black, x, y, this.SelectionSize, this.SelectionSize);
        }
        else
        {
          e.Graphics.DrawImage(this.SelectionGlyph, x, y);
        }

        if (this.Focused && includeFocus)
        {
          ControlPaint.DrawFocusRectangle(e.Graphics, new Rectangle(x - 1, y - 1, this.SelectionSize + 2, this.SelectionSize + 2));
        }
      }
    }
        protected virtual void SetColor(Point point)
        {
            double radius;
            double dx;
            double dy;
            double angle;
            double distance;
            double saturation;

            radius = GetRadius(_centerPoint);
            dx = Math.Abs(point.X - _centerPoint.X - Padding.Left);
            dy = Math.Abs(point.Y - _centerPoint.Y - Padding.Top);
            angle = Math.Atan(dy/dx)/Math.PI*180;
            distance = Math.Pow((Math.Pow(dx, 2) + (Math.Pow(dy, 2))), 0.5);
            saturation = distance/radius;

            if (distance < 6)
                saturation = 0; // snap to center

            if (point.X < _centerPoint.X)
                angle = 180 - angle;
            if (point.Y > _centerPoint.Y)
                angle = 360 - angle;

            LockUpdates = true;
            HslColor = new HslColor(angle, saturation, 0.5);
            Color = HslColor.ToRgbColor();
            LockUpdates = false;
        }
    /// <summary>
    /// Gets the point within the wheel representing the source color.
    /// </summary>
    /// <param name="color">The color.</param>
    protected virtual PointF GetColorLocation(HslColor color)
    {
      double angle;
      double radius;

      angle = color.H * Math.PI / 180;
      radius = _radius * color.S;

      return this.GetColorLocation(angle, radius);
    }
        /// <summary>
        ///     Raises the <see cref="ColorChanged" /> event.
        /// </summary>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected virtual void OnColorChanged(EventArgs e)
        {
            EventHandler handler;

            if (!LockUpdates)
                HslColor = new HslColor(Color);
            Refresh();

            handler = ColorChanged;

            if (handler != null)
                handler(this, e);
        }
        protected virtual PointF GetColorLocation(HslColor color)
        {
            double angleR = color.H*Math.PI/180;
            double radius = GetRadius(_centerPoint);
            radius *= color.S;

            return GetColorLocation(angleR, radius);
        }
        /// <summary>
        /// Change handler for editing components.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void ValueChangedHandler(object sender, EventArgs e)
        {
            if (!this.LockUpdates) {
                bool useHsl;
                bool useRgb;
                bool useNamed;

                useHsl = false;
                useRgb = false;
                useNamed = false;

                this.LockUpdates = true;

                if (sender == hexTextBox) {
                    string text;

                    text = hexTextBox.Text;
                    if (text.StartsWith("#")) {
                        text = text.Substring(1);
                    }

                    if (text.Length == 6 || text.Length == 8) {
                        try {
                            Color color;

                            color = ColorTranslator.FromHtml("#" + text);
                            aNumericUpDown.Value = color.A;
                            rNumericUpDown.Value = color.R;
                            bNumericUpDown.Value = color.B;
                            gNumericUpDown.Value = color.G;

                            useRgb = true;
                        }
              // ReSharper disable EmptyGeneralCatchClause
            catch {
                        }
                        // ReSharper restore EmptyGeneralCatchClause
                    } else {
                        useNamed = true;
                    }
                } else if (sender == aColorBar || sender == rColorBar || sender == gColorBar || sender == bColorBar) {
                    aNumericUpDown.Value = (int)aColorBar.Value;
                    rNumericUpDown.Value = (int)rColorBar.Value;
                    gNumericUpDown.Value = (int)gColorBar.Value;
                    bNumericUpDown.Value = (int)bColorBar.Value;

                    useRgb = true;
                } else if (sender == aNumericUpDown || sender == rNumericUpDown || sender == gNumericUpDown || sender == bNumericUpDown) {
                    useRgb = true;
                } else if (sender == hColorBar || sender == lColorBar || sender == sColorBar) {
                    hNumericUpDown.Value = (int)hColorBar.Value;
                    sNumericUpDown.Value = (int)sColorBar.Value;
                    lNumericUpDown.Value = (int)lColorBar.Value;

                    useHsl = true;
                } else if (sender == hNumericUpDown || sender == sNumericUpDown || sender == lNumericUpDown) {
                    useHsl = true;
                }

                if (useRgb || useNamed) {
                    Color color;

                    color = useNamed ? Color.FromName(hexTextBox.Text) : Color.FromArgb((int)aNumericUpDown.Value, (int)rNumericUpDown.Value, (int)gNumericUpDown.Value, (int)bNumericUpDown.Value);

                    this.Color = color;
                    this.HslColor = new HslColor(color);
                } else if (useHsl) {
                    HslColor color;

                    color = new HslColor((int)aNumericUpDown.Value, (double)hNumericUpDown.Value, (double)sNumericUpDown.Value / 100, (double)lNumericUpDown.Value / 100);
                    this.HslColor = color;
                    this.Color = color.ToRgbColor();
                }

                this.LockUpdates = false;
                this.UpdateFields(true);
            }
        }
 protected void PaintColor(PaintEventArgs e, HslColor color)
 {
     this.PaintColor(e, color, false);
 }
        /// <summary>
        /// Change handler for editing components.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void ValueChangedHandler(object sender, EventArgs e)
        {
            if (!this.LockUpdates)
            {
                bool useHsl;
                bool useRgb;
                bool useNamed;

                useHsl   = false;
                useRgb   = false;
                useNamed = false;

                this.LockUpdates = true;

                if (sender == hexTextBox)
                {
                    string text;
                    int    namedIndex;

                    text = hexTextBox.Text;
                    if (text.StartsWith("#"))
                    {
                        text = text.Substring(1);
                    }

                    if (hexTextBox.Items.Count == 0)
                    {
                        this.FillNamedColors();
                    }

                    namedIndex = hexTextBox.FindStringExact(text);

                    if (namedIndex != -1 || text.Length == 6 || text.Length == 8)
                    {
                        try
                        {
                            Color color;

                            color = namedIndex != -1 ? Color.FromName(text) : ColorTranslator.FromHtml("#" + text);
                            aNumericUpDown.Value = color.A;
                            rNumericUpDown.Value = color.R;
                            bNumericUpDown.Value = color.B;
                            gNumericUpDown.Value = color.G;

                            useRgb = true;
                        }
                        // ReSharper disable EmptyGeneralCatchClause
                        catch
                        { }
                        // ReSharper restore EmptyGeneralCatchClause
                    }
                    else
                    {
                        useNamed = true;
                    }
                }
                else if (sender == aColorBar || sender == rColorBar || sender == gColorBar || sender == bColorBar)
                {
                    aNumericUpDown.Value = (int)aColorBar.Value;
                    rNumericUpDown.Value = (int)rColorBar.Value;
                    gNumericUpDown.Value = (int)gColorBar.Value;
                    bNumericUpDown.Value = (int)bColorBar.Value;

                    useRgb = true;
                }
                else if (sender == aNumericUpDown || sender == rNumericUpDown || sender == gNumericUpDown || sender == bNumericUpDown)
                {
                    useRgb = true;
                }
                else if (sender == hColorBar || sender == lColorBar || sender == sColorBar)
                {
                    hNumericUpDown.Value = (int)hColorBar.Value;
                    sNumericUpDown.Value = (int)sColorBar.Value;
                    lNumericUpDown.Value = (int)lColorBar.Value;

                    useHsl = true;
                }
                else if (sender == hNumericUpDown || sender == sNumericUpDown || sender == lNumericUpDown)
                {
                    useHsl = true;
                }

                if (useRgb || useNamed)
                {
                    Color color;

                    color = useNamed ? Color.FromName(hexTextBox.Text) : Color.FromArgb((int)aNumericUpDown.Value, (int)rNumericUpDown.Value, (int)gNumericUpDown.Value, (int)bNumericUpDown.Value);

                    this.Color    = color;
                    this.HslColor = new HslColor(color);
                }
                else if (useHsl)
                {
                    HslColor color;

                    color         = new HslColor((int)aNumericUpDown.Value, (double)hNumericUpDown.Value, (double)sNumericUpDown.Value / 100, (double)lNumericUpDown.Value / 100);
                    this.HslColor = color;
                    this.Color    = color.ToRgbColor();
                }

                this.LockUpdates = false;
                this.UpdateFields(true);
            }
        }