Download SliceRectangleSample.zip, last updated 10/02/2013 (23.18 KB)

Download
  • md5: 1e8ccdf10305cc43789e4fa28c96006b
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;

namespace Cyotek.Drawing
{
  /// <summary>
  ///   A collection for cycling colors, with optional support for color swatches and brushes.
  /// </summary>
  public class ColorSwatchCollection : Collection<Color>, IDisposable
  {
    #region Instance Fields

    private IDictionary<int, Brush> _swatchBrushes;

    private IDictionary<int, Image> _swatchImages;

    #endregion

    #region Constructors

    /// <summary>
    ///   Initializes a new instance of the <see cref="ColorSwatchCollection" /> class.
    /// </summary>
    public ColorSwatchCollection()
    {
      this.CurrentIndex = -1;
    }

    /// <summary>
    ///   Initializes a new instance of the <see cref="ColorSwatchCollection" /> class.
    /// </summary>
    /// <param name="colors">The colors.</param>
    public ColorSwatchCollection(IList<Color> colors)
      : base(colors)
    {
      this.CurrentIndex = -1;
    }

    #endregion

    #region Class Properties

    /// <summary>
    ///   Returns a standard 16 color palette.
    /// </summary>
    public static Color[] Basic
    {
      get { return new[] { Color.FromArgb(0, 0, 0), Color.FromArgb(128, 0, 0), Color.FromArgb(0, 128, 0), Color.FromArgb(128, 128, 0), Color.FromArgb(0, 0, 128), Color.FromArgb(128, 0, 128), Color.FromArgb(0, 128, 128), Color.FromArgb(192, 192, 192), Color.FromArgb(128, 128, 128), Color.FromArgb(255, 0, 0), Color.FromArgb(0, 255, 0), Color.FromArgb(255, 255, 0), Color.FromArgb(0, 0, 255), Color.FromArgb(255, 0, 255), Color.FromArgb(0, 255, 255), Color.FromArgb(255, 255, 255) }; }
    }

    /// <summary>
    ///   Returns a standard 16 color palette with 25% alpha.
    /// </summary>
    public static Color[] Basic25
    {
      get { return Basic.Select(c => Color.FromArgb(63, c)).ToArray(); }
    }

    /// <summary>
    ///   Returns a standard 16 color palette with 50% alpha.
    /// </summary>
    public static Color[] Basic50
    {
      get { return Basic.Select(c => Color.FromArgb(128, c)).ToArray(); }
    }

    /// <summary>
    ///   Returns a standard 16 color palette with 75% alpha.
    /// </summary>
    public static Color[] Basic75
    {
      get { return Basic.Select(c => Color.FromArgb(191, c)).ToArray(); }
    }

    /// <summary>
    ///   Returns a subtle pastel color palette.
    /// </summary>
    public static Color[] Standard
    {
      get { return new[] { Color.FromArgb(165, 240, 190), Color.FromArgb(165, 214, 240), Color.FromArgb(165, 183, 240), Color.FromArgb(189, 165, 240), Color.FromArgb(234, 165, 240), Color.FromArgb(246, 169, 195), Color.FromArgb(255, 185, 175), Color.FromArgb(255, 212, 175), Color.FromArgb(255, 228, 175), Color.FromArgb(255, 244, 174), Color.FromArgb(248, 253, 173), Color.FromArgb(206, 245, 167) }; }
    }

    /// <summary>
    ///   Returns a subtle pastel color palette with 25% alpha
    /// </summary>
    public static Color[] Standard25
    {
      get { return Standard.Select(c => Color.FromArgb(63, c)).ToArray(); }
    }

    /// <summary>
    ///   Returns a subtle pastel color palette with 50% alpha
    /// </summary>
    public static Color[] Standard50
    {
      get { return Standard.Select(c => Color.FromArgb(128, c)).ToArray(); }
    }

    /// <summary>
    ///   Returns a subtle pastel color palette with 75% alpha
    /// </summary>
    public static Color[] Standard75
    {
      get { return Standard.Select(c => Color.FromArgb(191, c)).ToArray(); }
    }

    #endregion

    #region Properties

    /// <summary>
    ///   Gets the current color.
    /// </summary>
    public Color Current
    {
      get { return this[this.CurrentIndex]; }
    }

    /// <summary>
    ///   Gets the current brush.
    /// </summary>
    public Brush CurrentBrush
    {
      get { return _swatchBrushes[this.Current.ToArgb()]; }
    }

    /// <summary>
    ///   Gets or sets the index of the current color.
    /// </summary>
    public int CurrentIndex { get; set; }

    #endregion

    #region Members

    /// <summary>
    ///   Creates brushes for each color in the collection.
    /// </summary>
    public void CreateBrushes()
    {
      _swatchBrushes = new Dictionary<int, Brush>();

      foreach (Color color in this)
      {
        int key;

        key = color.ToArgb();
        if (!_swatchBrushes.ContainsKey(key))
          _swatchBrushes.Add(color.ToArgb(), new SolidBrush(color));
      }
    }

    /// <summary>
    ///   Creates swatch images for each color in the collection using a predefined size.
    /// </summary>
    public void CreateSwatches()
    {
      this.CreateSwatches(new Size(16, 16), 3);
    }

    /// <summary>
    ///   Creates swatch images for each color in the collection.
    /// </summary>
    /// <param name="size">The size of the swatch image.</param>
    /// <param name="margin">The margin around the borders of the swatch image.</param>
    public void CreateSwatches(Size size, int margin)
    {
      Rectangle rectangle;

      _swatchImages = new Dictionary<int, Image>();

      rectangle = new Rectangle(margin, margin, size.Width - (margin * 2), size.Height - (margin * 2));

      foreach (Color color in this)
      {
        int key;

        key = color.ToArgb();
        if (!_swatchImages.ContainsKey(key))
        {
          Image bitmap;

          bitmap = new Bitmap(size.Width, size.Height, PixelFormat.Format32bppArgb);
          using (Graphics g = Graphics.FromImage(bitmap))
          {
            using (SolidBrush brush = new SolidBrush(color))
              g.FillRectangle(brush, rectangle);
            g.DrawRectangle(Pens.Gray, rectangle);
          }

          _swatchImages.Add(color.ToArgb(), bitmap);
        }
      }
    }

    /// <summary>
    ///   Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
    /// </summary>
    public void Dispose()
    {
      if (_swatchBrushes != null)
      {
        foreach (KeyValuePair<int, Brush> pair in _swatchBrushes)
          pair.Value.Dispose();
      }

      if (_swatchImages != null)
      {
        foreach (KeyValuePair<int, Image> pair in _swatchImages)
          pair.Value.Dispose();
      }
    }

    /// <summary>
    ///   Gets the brush for a given color
    /// </summary>
    /// <param name="color">The color of the brush to return.</param>
    /// <returns>
    ///   A brush for the given color on success, otherwise <c>null</c>.
    /// </returns>
    public Brush GetBrush(Color color)
    {
      Brush result;

      _swatchBrushes.TryGetValue(color.ToArgb(), out result);

      return result;
    }

    /// <summary>
    ///   Gets the swatch image for a given color
    /// </summary>
    /// <param name="color">The color of the swatch to return.</param>
    /// <returns>
    ///   An image containing a swatch for the given color on success, otherwise <c>null</c>.
    /// </returns>
    public Image GetSwatch(Color color)
    {
      Image result;

      _swatchImages.TryGetValue(color.ToArgb(), out result);

      return result;
    }

    /// <summary>
    ///   Returns the next color in the collection.
    /// </summary>
    /// <remarks>This method will automatically move to the start of the collection once the end is reached.</remarks>
    public Color Next()
    {
      this.CurrentIndex++;
      if (this.CurrentIndex > this.Count - 1)
        this.CurrentIndex = 0;

      return this[this.CurrentIndex];
    }

    /// <summary>
    ///   Returns the next brush in the collection.
    /// </summary>
    /// <remarks>This method will automatically move to the start of the collection once the end is reached.</remarks>
    public Brush NextBrush()
    {
      return _swatchBrushes[this.Next().ToArgb()];
    }

    /// <summary>
    ///   Resets the collection cycle back to the start.
    /// </summary>
    public void Reset()
    {
      this.CurrentIndex = 0;
    }

    #endregion
  }
}

Donate

Donate