Download RiffPaletteWriter.zip version 1.0.0.0, last updated 04/03/2017 (14.94 KB)

Download
  • md5: 2864394d3c1cc948887ced13c5d5bedf
  • sha1: dc44b4f4d8d6318030d6460a5f88f43aa345ce2f
  • sha256: 8781446c207bc5faf4021226bdbd3789c6acdd21efbaeaa101dbcca0a2749abf
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using PhotoshopColorSwatchWriter;

// Loading Microsoft RIFF Palette (pal) files with C#
// http://cyotek.com/blog/loading-microsoft-riff-palette-pal-files-with-csharp

// Writing Microsoft RIFF Palette (pal) files with C#
// http://cyotek.com/blog/writing-microsoft-riff-palette-pal-files-with-csharp

namespace Cyotek.Demonstrations.RiffPaletteWriter
{
  internal sealed partial class GeneratorWindow : Form
  {
    #region Fields

    private RiffPalette _comparePalette;

    private RiffPalette _originalPalette;

    private Random _random;

    private string _tempFileName;

    #endregion

    #region Constructors

    public GeneratorWindow()
    {
      this.InitializeComponent();
    }

    #endregion

    #region Methods

    /// <summary>
    /// Raises the <see cref="E:System.Windows.Forms.Form.Load"/> event.
    /// </summary>
    /// <param name="e">An <see cref="T:System.EventArgs"/> that contains the event data. </param>
    protected override void OnLoad(EventArgs e)
    {
      base.OnLoad(e);

      _random = new Random();

      _tempFileName = Path.GetTempFileName();

      this.Text = Application.ProductName;
      this.GenerateAndSavePalette();
    }

    private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
    {
      using (Form dialog = new AboutDialog())
        dialog.ShowDialog(this);
    }

    private void exitToolStripMenuItem_Click(object sender, EventArgs e)
    {
      this.Close();
    }

    private void GenerateAndSavePalette()
    {
      // generate our palette
      _originalPalette = new RiffPalette(this.GeneratePalette());

      // show a preview in the first pane
      originalPalettePanel.Colors = _originalPalette;

      // save and update the compare preview
      this.SaveAndComparePalette();
    }

    private Color[] GeneratePalette()
    {
      List<Color> palette;

      palette = new List<Color>();
      for (int i = 0; i < 255; i++)
      {
        int r;
        int g;
        int b;

        r = _random.Next(0, 255);
        g = _random.Next(0, 255);
        b = _random.Next(0, 255);

        palette.Add(Color.FromArgb(r, g, b));
      }

      return palette.ToArray();
    }

    private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
    {
      this.GenerateAndSavePalette();
    }

    private void SaveAndComparePalette()
    {
      // write the palette to a swatch file
      _originalPalette.Save(_tempFileName);

      // load back the swatch file and display this in the second pane
      _comparePalette = new RiffPalette();
      _comparePalette.Load(_tempFileName);
      comparePalettePanel.Colors = _comparePalette;
    }

    #endregion
  }
}

Donate

Donate