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.IO;
using System.Linq;

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

// TODO: A lot of the compares here could be simplified by implementing IEquatable and IComparable to the source objects and collections

namespace AdobeSwatchExchangeLoader
{
  internal static class AseComparer
  {
    #region Static Methods

    public static AseCompareResult Compare(string sourceFileName, string destinationFileName)
    {
      AseCompareResult result;

      if (!AreFilesEqual(sourceFileName, destinationFileName))
      {
        if (!AreAseDocumentsEqual(sourceFileName, destinationFileName))
        {
          result = AseCompareResult.Mismatch;
        }
        else
        {
          result = AseCompareResult.ColorMatch;
        }
      }
      else
      {
        result = AseCompareResult.Match;
      }

      return result;
    }

    private static bool AreAseDocumentsEqual(string sourceFileName, string destinationFileName)
    {
      AseDocument source;
      AseDocument destination;
      bool result;

      source = new AseDocument();
      source.Load(sourceFileName);

      try
      {
        destination = new AseDocument();
        destination.Load(destinationFileName);
      }
      catch
      {
        destination = new AseDocument();
      }

      result = AreAseDocumentsEqual(source, destination);

      return result;
    }

    private static bool AreAseDocumentsEqual(AseDocument lhs, AseDocument rhs)
    {
      bool result;

      result = AreColorEntryCollectionsEqual(lhs.Colors, rhs.Colors) &&
               AreColorGroupCollectionsEqual(lhs.Groups, rhs.Groups);

      return result;
    }

    private static bool AreBlockBasesEqual(Block lhs, Block rhs)
    {
      return lhs.Name == rhs.Name &&
             (lhs.ExtraData == null && rhs.ExtraData == null ||
              lhs.ExtraData != null && rhs.ExtraData != null && !lhs.ExtraData.SequenceEqual(rhs.ExtraData));
    }

    private static bool AreColorEntryCollectionsEqual(ColorEntryCollection lhs, ColorEntryCollection rhs)
    {
      bool result;

      result = lhs.Count == rhs.Count;

      if (result)
      {
        for (int i = 0; i < lhs.Count; i++)
        {
          ColorEntry lhsColor;
          ColorEntry rhsColor;

          lhsColor = lhs[i];
          rhsColor = rhs[i];

          if (!AreBlockBasesEqual(lhsColor, rhsColor) || lhsColor.R != rhsColor.R || lhsColor.G != rhsColor.G ||
              lhsColor.B != rhsColor.B || lhsColor.Type != rhsColor.Type)
          {
            result = false;
            break;
          }
        }
      }

      return result;
    }

    private static bool AreColorGroupCollectionsEqual(ColorGroupCollection lhs, ColorGroupCollection rhs)
    {
      bool result;

      result = lhs.Count == rhs.Count;

      if (result)
      {
        for (int i = 0; i < lhs.Count; i++)
        {
          ColorGroup lhsGroup;
          ColorGroup rhsGroup;

          lhsGroup = lhs[i];
          rhsGroup = rhs[i];

          if (!AreBlockBasesEqual(lhsGroup, rhsGroup) ||
              !AreColorEntryCollectionsEqual(lhsGroup.Colors, rhsGroup.Colors))
          {
            result = false;
            break;
          }
        }
      }

      return result;
    }

    private static bool AreFilesEqual(string sourceFileName, string destinationFileName)
    {
      bool result;

      using (Stream source = File.OpenRead(sourceFileName))
      {
        using (Stream dest = File.OpenRead(destinationFileName))
        {
          result = source.Length == dest.Length;

          if (result)
          {
            byte[] sourceBuffer;
            byte[] destBuffer;
            int sourceRead;
            int destRead;
            int bufferSize;

            bufferSize = 4096;
            sourceBuffer = new byte[bufferSize];
            destBuffer = new byte[bufferSize];

            do
            {
              sourceRead = source.Read(sourceBuffer, 0, bufferSize);
              destRead = dest.Read(destBuffer, 0, bufferSize);

              if (sourceRead != destRead)
              {
                result = false;
              }
              else
              {
                for (int i = 0; i < sourceRead; i++)
                {
                  if (sourceBuffer[i] != destBuffer[i])
                  {
                    result = false;
                    break;
                  }
                }
              }
            } while ((sourceRead != 0 || destRead != 0) && result);
          }
        }
      }

      return result;
    }

    #endregion
  }
}

Donate

Donate