Download WestwoodPaletteLoader.zip, last updated 26/12/2017 (27.76 KB)

Download
  • md5: da29ba80301e3fa413c90a973afdaec9
  • sha1: 362ce84dba11d8047dc3c1dc3e44ee2f0edee602
  • sha256: 2e31cc54ea412623684856d331851c8b213bc6369629c620a2fc2d44aedae2bd
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

// Reading and writing 18-bit RGB VGA Palette (pal) files with C#
// http://www.cyotek.com/blog/reading-and-writing-18-bit-rgb-vga-palette-pal-files-with-csharp

namespace Cyotek.Demonstrations.Rgb18BitPalette
{
  internal partial class WriteTestDialog : Form
  {
    #region Constants

    private readonly List<FileInfo> _files;

    #endregion

    #region Constructors

    public WriteTestDialog()
    {
      this.InitializeComponent();

      _files = new List<FileInfo>();
    }

    public WriteTestDialog(List<FileInfo> files)
      : this()
    {
      _files = files;
    }

    #endregion

    #region Methods

    protected override void OnShown(EventArgs e)
    {
      base.OnShown(e);

      if (_files.Count != 0)
      {
        for (int i = 0; i < _files.Count; i++)
        {
          paletteComboBox.Items.Insert(i, _files[i]);
        }

        paletteComboBox.SelectedIndex = 0;
      }
      else
      {
        paletteComboBox.SelectedIndex = paletteComboBox.FindStringExact("Windows (256)");
      }
    }

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

    private void Compare(WestwoodPalette source)
    {
      using (Stream output = new MemoryStream())
      {
        PaletteCompareResult compareResult;
        Color[] src;
        Color[] dst;

        source.Save(output);
        output.Position = 0;

        compareResult = PaletteComparer.Compare(source, output);

        if (compareResult != PaletteCompareResult.Mismatch)
        {
          resultLabel.Text = "Files are identical";
          resultLabel.BackColor = Color.Green;
        }
        else
        {
          resultLabel.Text = "Files do not match.";
          resultLabel.BackColor = Color.Red;
        }

        src = source.ToArray();
        output.Position = 0;
        dst = WestwoodPalette.LoadFrom(output).
                              ToArray();

        sourceView.Palette = src;
        destinationView.ComparePalette = src;
        destinationView.Palette = dst;
      }
    }

    private void paletteComboBox_SelectedIndexChanged(object sender, EventArgs e)
    {
      WestwoodPalette palette;
      int index;

      index = paletteComboBox.SelectedIndex;

      if (index < _files.Count)
      {
        palette = WestwoodPalette.LoadFrom(_files[index].
                                             FullPath);
      }
      else
      {
        Color[] colors;

        switch (index - _files.Count)
        {
          case 0:
            colors = ColorPalettes.HexagonPalette;
            break;
          case 1:
            colors = ColorPalettes.NamedColors;
            break;
          case 2:
            colors = ColorPalettes.PaintPalette;
            break;
          case 3:
            colors = ColorPalettes.WebSafe;
            break;
          case 4:
            colors = ColorPalettes.QbColors;
            break;
          case 5:
            colors = ColorPalettes.StandardPalette;
            break;
          case 6:
            colors = ColorPalettes.RedScale;
            break;
          case 7:
            colors = ColorPalettes.GreenScale;
            break;
          case 8:
            colors = ColorPalettes.BlueScale;
            break;
          case 9:
            colors = ColorPalettes.GreyScale;
            break;
          default:
            colors = null;
            break;
        }

        if (colors != null && colors.Length != 0)
        {
          palette = new WestwoodPalette(colors);
        }
        else
        {
          palette = null;
        }
      }

      if (palette != null && palette.Count != 0)
      {
        this.Compare(palette);
      }
      else
      {
        sourceView.Palette = null;
        destinationView.Palette = null;
      }
    }

    #endregion
  }
}

Donate

Donate