AS3 ASE Adobe Swatch Exchange Encoder Demo

The Adobe Swatch Exchange (ASE) is a useful file format for importing colour schemes into applications in the Creative Suite (Photoshop, Illustrator etc).

I’ve been optimising the BitmapData colour palette algorithm (coming shortly in another post) and for the demo thought that it would be nice to give you the option to save the extracted color palette (a feature that people have been requesting in the comments). I added an option for saving a TXT file containing the hexadecimal and RGB colour values, but thought it would be nice to offer the choice of downloading the palette in ASE format so you can quickly incorporate the colour scheme in your artwork or designs, one of the nice features of Kuler.

So the first step was to open up and existing ASE file (created by Photoshop) in a hex editor (I use 0xED which is an excellent and free editor for OS X). Looking at the bytes of the file showed that it was evidently a quite simple format and wouldn’t be difficult to reverse engineer. I also found the ASE file format specification and a PHP implementation over at Colour Lovers, both of which were quite helpful.

This AS3 code will take an Array of colours (adding Vectors to the <pre> tag seems to cause problems, though my local source code does use a Vector of uints) and encode the ByteArray of an ASE file, ready to save using FileReference of FileStream in AIR…

const FILE_SIGNATURE : String = "ASEF";

function encode ( pixels : Array ):ByteArray
{
	var swatch : ByteArray = new ByteArray();
	var ase : ByteArray = new ByteArray();
	var hex:String;
	var pix:uint;

	ase.writeUTFBytes (FILE_SIGNATURE);// header
	ase.writeInt (0x10000);// version
	ase.writeInt (pixels.length * 2);// blocks

	for (var i : int = 0; i < pixels.length; ++i)
	{
		pix = pixels[i];
		swatch.length = 0;

		// start of group

		ase.writeShort (0xC001);
		ase.writeInt (0);
		ase.writeShort (1);

		// swatch name

		hex = pix.toString(16);
		while (hex.length < 6) hex = "0" + hex;

		swatch.writeShort ((hex = "#" + hex).length + 1);
		for (var n : int = 0; n < hex.length; ++n) swatch.writeShort (hex.charCodeAt(n));
		swatch.writeShort (0);

		// colours

		swatch.writeUTFBytes ("RGB ");
		swatch.writeFloat ((pix >> 16 & 0xFF) / 255);
		swatch.writeFloat ((pix >> 8 & 0xFF) / 255);
		swatch.writeFloat ((pix & 0xFF) / 255);
		swatch.writeShort (2);

		// write swatch

		ase.writeInt (swatch.length);
		ase.writeBytes (swatch);
	}

	return ase;
}

I’ll put the full code in my repository along with the improved ColourUtils Class which prompted the ASEEncoder, though for now you can just copy and paste this snippet and play with the demo above to save the top colour schemes from Colour Lovers as ASE files, which you can then import into Adobe Creative Suite applications.

Here is an example of how to encode a palette and then save it to the file system:

var colours : Array = [0xFF0000, 0x00FF00, 0x0000FF];
var aseFile : ByteArray = encode( colours );
var fileRef : FileReference = new FileReference();
fileRef.save( aseFile, "swatch.ase" );

The AS3 ASEEncoder currently only encodes swatches as RGB, though saving CMYK should only be a matter of converting the colour space and then changing the swatch declaration.

Going in the other direction, it would be nice to provide a method to decode existing ASE files into a Vector of uints, something which I’ll look at once the new ColourUtils is online.

Posted on 30 Mar 2010
10 Comments
1 Trackbacks

Meta

ASE (Adobe Swatch Exchange) Encoder was posted on March 30th 2010 in the category Code / Actionscript 3.0, Flash, Open Source and tagged; , , , , , .

You can Leave a comment.


Warning: file_get_contents(http://search.twitter.com/search.atom?q=from:soulwire&rpp=1) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 401 Authorization Required in /home/soulwire/webapps/soulwire_blog/wp-content/themes/soulwire/functions.php on line 203

Discussion

9 Responses to ASE (Adobe Swatch Exchange) Encoder

Leave a Reply

  1. Aaron Leavitt 4 years ago

    I wrote something similar a while back for my (long neglected) Color Browser application. I did get it working both ways, I always got the feeling it was a little less than ideal though. Nice work, it’s nice to see some more movement and work in an area that seems to get ignored often.

    Reply to this comment

    1. Soulwire 4 years ago

      Hi Aaron.

      How did I not find your ASEUtil!? Great stuff and thanks for the link – I see that, yes, interpreting an existing ASE file is not too much extra leg work also. I do recognise the ‘less than ideal’ feeling you described. There are a few slightly strange aspects of ASE and I suppose that working backwards to understand / create a fairly undocumented format is going to leave doubts in the mind as to whether its been tackled in the best way possible.

      I like the look of your color browser application.

  2. barbgreenemann 4 years ago

    i am from swa so cho and cnnot read but i will attempt this game ful new color scheme as i wnat to paint my bathing sweet thank you mastergernius person

    Reply to this comment

    1. Soulwire 4 years ago

      Um, thanks. I like your illustrations.

  3. Dorian Fitzgerald 3 years ago

    Found your website through searching for help with ASE conversion/maintenance – you mention that converting your script to extract from existing ASE files was a possibility. Just wondering if you ever found the time or inclination – with my limited knowledge of programming/hex etc. I was about to try a brute force route using Autoit to extract swatch names and RGB info from my custom swatches. Obviously not ideal. Looked for Aaron’s solution but the project seemed pretty bare.

    Either way love your aesthetic – keep on keepin’ on, as it were.

    Reply to this comment

    1. Soulwire 3 years ago

      Hi Dorian. Aaron mentioned his ASEUtil library, which can read ASE format as well as write it. It’s open source; check it out at: http://code.google.com/p/as3ase/

  4. Wouter 3 years ago

    I love you website! Very clear, pleasing to the eye and great content. At the moment, I am working some color functions in my pattern tool. Your color palette generation and saving (for Adobe) are a great inspiration.

    In this post you are indicating you are/were working on a new version of your ColoursUtils. Can you tell me if you are planning to share it soon? Thanks!

    Reply to this comment

    1. Soulwire 3 years ago

      Thanks :) And I am indeed – it’s been sitting in my draft items for a very long time! I’ll email you the code and move the blog post up the todo list.

  5. mike_graves 2 years ago

    Just wonder if I can get a copy of that colorUtils class please :¬)
    This post is the bomb. I just found this blog a few days ago and I have spent alot of free reading your stuff.
    Thanks!

    Reply to this comment