Download AdobeSwatchExchangeLoader-v2.zip version 2.0.0.0, last updated 21/10/2015 (72.79 KB)

Download
  • md5: 3591debc1f6749c23ad9bbd113dd760a
  • sha1: 9b8b0e53179b0b8d04c14324125ae5452df58305
  • sha256: 152c5051abec694120031b4b0ed923608627b0081994f99cf6d043cc1663ed20
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;

// Reading Adobe Swatch Exchange (ase) files using C#
// http://www.cyotek.com/blog/reading-adobe-swatch-exchange-ase-files-using-csharp

// Writing Adobe Swatch Exchange (ase) files using C#
// http://www.cyotek.com/blog/writing-adobe-swatch-exchange-ase-files-using-csharp

// Sample palettes used in this example program from:
// http://www.sherwin-williams.com/architects-specifiers-designers/color/color-tools/downloadable-color-palettes/
// http://www.colourlovers.com/palette/3899568/Blue_Lace
// http://www.colourlovers.com/palette/92095/Giant_Goldfish
// http://www.pixeljoint.com/forum/forum_posts.asp?TID=12795
// http://www.pixeljoint.com/forum/forum_posts.asp?TID=16247
// http://androidarts.com/palette/16pal.htm

namespace AdobeSwatchExchangeLoader
{
  internal sealed partial class MainForm : Form
  {
    #region Fields

    private AseDocument _paletteData;

    #endregion

    #region Constructors

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

    #endregion

    #region Properties

    private string DataPath
    {
      get { return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "data"); }
    }

    #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);

      this.ReloadFileList();

      this.LoadFile(Path.Combine(this.DataPath, "demo.ase"));
    }

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

    private void AddFiles(string extension)
    {
      string path;

      path = this.DataPath;
      foreach (string fileName in Directory.GetFiles(path, "*." + extension))
      {
        filesListBox.Items.Add(new FileInfo(fileName));
      }
    }

    private string BrowseForAseFile()
    {
      string result;

      openFileDialog.InitialDirectory = this.DataPath;

      result = openFileDialog.ShowDialog(this) == DialogResult.OK ? openFileDialog.FileName : null;

      return result;
    }

    private void compareTestToolStripMenuItem_Click(object sender, EventArgs e)
    {
      string sourceFileName;

      sourceFileName = this.BrowseForAseFile();

      if (!string.IsNullOrEmpty(sourceFileName))
      {
        using (CompareResultsDialog dialog = new CompareResultsDialog(sourceFileName))
        {
          dialog.ShowDialog(this);
        }
      }
    }

    private void createSampleFileToolStripMenuItem_Click(object sender, EventArgs e)
    {
      string fileName;

      fileName = this.GetUniqueFileName();

      SampleDocuments.MakeCombinedSample(fileName);

      this.ReloadFileList();
    }

    private void DefinePreviewColors()
    {
      List<Color> colors;

      colors = new List<Color>();

      if (_paletteData != null)
      {
        if (_paletteData.Groups != null)
        {
          colors.AddRange(_paletteData.Groups.Where(group => group.Colors != null).
                                       SelectMany(group => group.Colors).
                                       Select(colorEntry => colorEntry.ToColor()));
        }

        if (_paletteData.Colors != null)
        {
          colors.AddRange(_paletteData.Colors.Select(colorEntry => colorEntry.ToColor()));
        }
      }

      simpleColorGrid.Colors = colors.ToArray();
    }

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

    private void filesListBox_SelectedIndexChanged(object sender, EventArgs e)
    {
      FileInfo selectedFile;

      selectedFile = filesListBox.SelectedItem as FileInfo;

      this.LoadFile(selectedFile?.FullPath);
    }

    private string GetUniqueFileName()
    {
      string path;
      string fileName;
      string baseName;

      path = this.DataPath;
      baseName = DateTime.Now.ToString("yyyyMMddHHmm");
      fileName = Path.Combine(path, baseName + ".ase");

      if (File.Exists(fileName))
      {
        int counter;

        counter = 1;

        do
        {
          fileName = Path.Combine(path, $"{baseName} ({counter}).ase");
          counter++;
        } while (File.Exists(fileName));
      }

      return fileName;
    }

    private void LoadFile(string fileName)
    {
      _paletteData = null;
      this.Text = Application.ProductName;

      if (!string.IsNullOrEmpty(fileName))
      {
        _paletteData = new AseDocument();

        try
        {
          int index;

          _paletteData.Load(fileName);
          aseHexViewer.FileName = fileName;
          this.Text = $"{Path.GetFileName(fileName)} - {Application.ProductName}";

          index = filesListBox.FindStringExact(Path.GetFileName(fileName));
          if (index != -1)
          {
            filesListBox.SelectedIndex = index;
          }
        }
        catch (Exception ex)
        {
          MessageBox.Show(ex.GetBaseException().
                             Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
      }

      simpleColorGrid.Invalidate();
      aseExplorerTreeView.Data = _paletteData;
      this.DefinePreviewColors();
    }

    private void makeSamplesToolStripMenuItem_Click(object sender, EventArgs e)
    {
      string path;

      path = this.DataPath;

      SampleDocuments.MakeDb16Sample(Path.Combine(path, "db16.ase"));
      SampleDocuments.MakeDb32Sample(Path.Combine(path, "db32.ase"));
      SampleDocuments.MakeArne16Sample(Path.Combine(path, "arne16.ase"));
      SampleDocuments.MakeDb16GlobalSample(Path.Combine(path, "db16pal.ase"));
      SampleDocuments.MakeDb32GlobalSample(Path.Combine(path, "db32pal.ase"));
      SampleDocuments.MakeArne16GlobalSample(Path.Combine(path, "arne16pal.ase"));
      SampleDocuments.MakeDbSample(Path.Combine(path, "db.ase"));
      SampleDocuments.MakeCombinedSample(Path.Combine(path, "demo.ase"));

      this.ReloadFileList();
    }

    private void ReloadFileList()
    {
      filesListBox.Items.Clear();
      this.AddFiles("ase");
    }

    #endregion
  }
}

Donate

Donate