Understanding and creating on the fly Photoshop Color Swatches with php
This tutorial won’t cover basic Color Swatches information… I suggest you to follow Photoshop’s help if you don’t know what I am talking about.

The interesting value of the Swatches palette is the ability to load custom swatch collections, so you can quickly access to specific colors without having to remember any numeric color values.
For instance, you can create a custom swatch with the colors you use in your project logo, and later use this swatch when designing the website layout, so you will be sure colors will match (all in all, they are the same ones!).
When you save a Swatch from Photoshop, it will create an Adobe Color .ACO file.
Knowing how Photoshop writes this file will allow us to create our custom swatches with php or another language.
Anatomy of a .ACO file
ACO files are binary files made by 16-bit integers made of two sections. All values are hexadecimal
The first section of the file is made this way:
0001: starts with 1 because it’s section 1…
number of colors: this represents the number of colors in the swatch, such as 0001, 0002, 000f… probably the limit is really 65535 (FFFF) even if I don’t see the point in having so much colors.
Then for each color, we have:
0000: let’s call it a separator…
red component: you have to write it twice, so if you red component is 0A, you must write 0A0A
green component: same thing as the red one
blue component: guess what?
0000: another separator
And the first section is over. Next section starts with
0002: being the second one…
number of colors: same thing as for the first section
Then for each color we have:
0000: separator
red component: same as before
green component: same as before
blue component: same as before
0000: separator
0000: separator (yes, another)
color name length + 1: do you want to call the color “my favourite red”? Then the length is 16 so the final value will be 17 = 0011 in hexadecimal
the name itself: this is the name itself coded in UTF-16, so if your color name is “c1″ the name will be 0063 0031
0000: separator
Make a file following these rules, and you’ll have your .ACO file ready to be loaded in Photoshop.
Why should you do that? Well, in case you want to export color schemes you found somewhere in the web… and here it is a php function that starting with a string with one or more colors in hexadecimal format (such as 000000FFFFFF for black and white), returns the ACO format.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | function hex_to_aco($hex){ $number_of_colors = strlen($hex)/6; $n = sprintf('%04x',$number_of_colors); $string = "0001".$n; for($x=0;$x<$number_of_colors;$x++){ $string.= "0000"; $string.= substr($hex,$x*6,2).substr($hex,$x*6,2); $string.= substr($hex,$x*6+2,2).substr($hex,$x*6+2,2); $string.= substr($hex,$x*6+4,2).substr($hex,$x*6+4,2); $string.= "0000"; } $string.= "0002".$n; for($x=0;$x<$number_of_colors;$x++){ $string.= "0000"; $string.= substr($hex,$x*6,2).substr($hex,$x*6,2); $string.= substr($hex,$x*6+2,2).substr($hex,$x*6+2,2); $string.= substr($hex,$x*6+4,2).substr($hex,$x*6+4,2); $string.= "0000"; $string.= "0000"; $color_name = "pic2col $x"; $string.= sprintf('%04x',strlen($color_name)+1); for($y=0;$y<strlen($color_name);$y++){ $string.= sprintf('%04x',ord($color_name{$y})); } $string.= "0000"; } return($string); } |
You can find this function used in pic2col, at the end of the color sequence you will find a link to download it as a ACO file.
I tested it in CS4 on XP and Vista and it works. Waiting for feedback.
These templates can be easily edited so you are able to customize them to serve you particular needs.
3 Responses to “Understanding and creating on the fly Photoshop Color Swatches with php”
Leave a Reply
- Get up to $100,000 for your next Flash game with Mochi GAME Developer Fund
- Create a dynamic content animated footer ad for your site in just 9 jQuery lines – 17 lines version
- Sell sitelocked version of your Flash games and even .fla sources to Free Online Games
- Protect your work from ActionScript code theft with SWF Protector
- Create a dynamic content animated footer ad for your site in just 9 jQuery lines
- Understanding Box2D’s one-way platforms, aka CLOUDS
- Triqui MochiAds Arcade plugin for WordPress upgraded to 1.2
- Box2D Flash game creation tutorial – part 2
- 11 Flash isometric engines you can use in your games
- Monetize your Flash games with GamesChart
- Create a Lightbox effect only with CSS - no javascript needed
- Flash game creation tutorial - part 1
- Create a Flash Racing Game Tutorial
- Flash game creation tutorial - part 2
- Make a Flash game like Flash Element Tower Defense - Part 2
- Flash game creation tutorial - part 3
- Make a Flash game like Flash Element Tower Defense - Part 1
- Create a flash draw game like Line Rider or others - part 1
- Triqui MochiAds Arcade plugin for WordPress official page
- Create a flash artillery game - step 1
- Flash game creation tutorial – part 5.2 (4.87/5)
- Create a flash artillery game – step 1 (4.79/5)
- Create a Flash Racing Game Tutorial (4.76/5)
- Create a flash artillery game – step 2 (4.74/5)
- Create a survival horror game in Flash tutorial – part 1 (4.73/5)
- Creation of a Flash arcade site using WordPress – step 2 (4.73/5)
- Flash game creation tutorial – part 2 (4.71/5)
- Flash game creation tutorial – part 1 (4.70/5)
- Create a flash draw game like Line Rider or others – part 1 (4.69/5)
- Creation of a platform game with Flash – step 2 (4.68/5)

(2 votes, average: 4.00 out of 5)





Hi Emanuele, I’ve tried Pic2Col a couple of times and it doesn’t work for me. I put in a URL to an image from a google image search result, click the Get Colours button and… the page just reloads.
Image URL was http://lumajs.googlepages.com/ocean-temperature.jpg which is around 400px wide. I’m using Firefox 3.5.3 on Mac.
Image was too high, anyway I raised the limit to 800
Nice tutorial as usual! I really like your work! You ve learned me almost everything i know avout as3 already and this php stuff i also really good!