• Home
  • Products
  • Technology
  • Experience
  • Services
  • Our Company
  • Blog

Archives

  • February 2009
  • May 2008
  • April 2008
  • March 2008
  • December 2007
  • November 2007
  • October 2007
  • August 2007

Categories

  • Adobe AIR
  • Adobe Flex
  • Cairngorm
  • Encryption
  • General
  • Printing
  • UndoRedo Framework
  • Bookmarks

    • Adobe Labs
    • Planet Ubuntu

Log in
Using Adobe Swatch Exchange Files in AS3

When Adobe released Creative Suite 2 they included a new color palette format. ASE or Adobe Swatch Exchange was designed so that colors could be accurately moved between the variety of applications in the suite. Since it was specifically designed to make sharing colors easy it was an obvious target for color sharing websites (ColourLovers and Kuler both use it for downloads, for example). So ASE files with interesting and useful sets of colors are easy to get ahold of, and easy to use in Adobe programs. But what if you wanted to use them in a Flex or Flash application? Well, now you can use my AS3ASE library (generic name, I know). Note that it makes use of Yahoo!’s Astra libraries, since they already had some great classes for RGB and CMYK colors.

Ok, I’ll admit, the desire to use ASE file in your application probably isn’t keeping you up at night. Truthfully I came across the spec and thought it would be a good exercise, and I did have a few thoughts for little utilities I might use it for. A post on the Adobe forums brought me to this web page that had a great description of the workings of the binary file format. So I went to work.

This was also a great learning experience in working with binary files. Sorting out how to handle variable-length blocks, and what formats would give me the values correctly. I’m pretty sure the library isn’t bug free yet, but I’m pretty happy with it.

Here’s a quick little demo of it in action. Pressing the buttons loads an embedded ByteArray for each color set. All credit for the palettes goes to ColourLovers.com.

This movie requires Flash Player 9.0.28

There are really two simple things making this happen (aside from the embedded files). A repeater to display the colors:

<mx:HBox>
     <mx:Repeater id="colorDisplay" dataProvider="{activePalette.colors}">
          <mx:Canvas width="100%" height="100%" backgroundColor="{colorDisplay.currentItem.touint()}"/>
     </mx:Repeater>
</mx:HBox>

And a function to read in the ByteArray and assign the output to the palette variable I bound to above:

private function setColors(inBytes:ByteArrayAsset):void {
     inBytes.position = 0;  //Reset the ByteArray
     activePalette = ASEUtil.readASE(inBytes);
}

I’ve tried to keep the library simple, one static function to read in an ASE file as a ByteArray, and one to write one out the same way. It has it’s limitations currently, only CMYK and RGB colors are working now, color groups are only somewhat supported, etc. But it does work, and handles most files of the type that I could get my hands on. To get an ASE formatted ByteArray (that you can easily write out to a file), just call writeASE, and pass in a string and an array of RGBColor or CMYKColor objects. To read in an ASE file, drop it into a ByteArray and pass it to readASE, and you’ll get back an object that has the name of the palette group if it’s avaiable, and an ArrayCollection of RGBColor or CMYKColor objects that you can use. No more complicated than that.

If you’ve got any suggestions, bug-fixes, issues, or whatever, please comment. I’ll be continuing to work on this project, so feedback will be a huge help.

Posted by Aaron Leavitt on May 8, 2008 at 11:00 am
No Comments or LinksAdobe AIR, Adobe Flex

Trackback this post | Subscribe to the comments via RSS Feed