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.

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

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.

Get the most popular Phaser 3 book

Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

Get the book

214 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 100 rounds
// 10000000
// 2 Cars
// 2048
// A Blocky Christmas
// A Jumping Block
// A Life of Logic
// Angry Birds
// Angry Birds Space
// Artillery
// Astro-PANIC!
// Avoider
// Back to Square One
// Ball Game
// Ball vs Ball
// Ball: Revamped
// Balloon Invasion
// BallPusher
// Ballz
// Bar Balance
// Bejeweled
// Biggification
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Boom Dots
// Bouncing Ball
// Bouncing Ball 2
// Bouncy Light
// BoxHead
// Breakout
// Bricks
// Bubble Chaos
// Bubbles 2
// Card Game
// Castle Ramble
// Chronotron
// Circle Chain
// Circle Path
// Circle Race
// Circular endless runner
// Cirplosion
// CLOCKS - The Game
// Color Hit
// Color Jump
// ColorFill
// Columns
// Concentration
// Crossy Road
// Crush the Castle
// Cube Jump
// CubesOut
// Dash N Blast
// Dashy Panda
// Deflection
// Diamond Digger Saga
// Don't touch the spikes
// Dots
// Down The Mountain
// Drag and Match
// Draw Game
// Drop Wizard
// DROP'd
// Dudeski
// Dungeon Raid
// Educational Game
// Elasticity
// Endless Runner
// Erase Box
// Eskiv
// Farm Heroes Saga
// Filler
// Flappy Bird
// Fling
// Flipping Legend
// Floaty Light
// Fuse Ballz
// GearTaker
// Gem Sweeper
// Globe
// Goat Rider
// Gold Miner
// Grindstone
// GuessNext
// Helicopter
// Hero Emblems
// Hero Slide
// Hexagonal Tiles
// HookPod
// Hop Hop Hop Underwater
// Horizontal Endless Runner
// Hundreds
// Hungry Hero
// Hurry it's Christmas
// InkTd
// Iromeku
// Jet Set Willy
// Jigsaw Game
// Knife Hit
// Knightfall
// Legends of Runeterra
// Lep's World
// Line Rider
// Lumines
// Magick
// MagOrMin
// Mass Attack
// Math Game
// Maze
// Meeblings
// Memdot
// Metro Siberia Underground
// Mike Dangers
// Mikey Hooks
// Nano War
// Nodes
// o:anquan
// One Button Game
// One Tap RPG
// Ononmin
// Pacco
// Perfect Square!
// Perfectionism
// Phyballs
// Pixel Purge
// PixelField
// Planet Revenge
// Plants Vs Zombies
// Platform
// Platform game
// Plus+Plus
// Pocket Snap
// Poker
// Pool
// Pop the Lock
// Pop to Save
// Poux
// Pudi
// Pumpkin Story
// Puppet Bird
// Pyramids of Ra
// qomp
// Quick Switch
// Racing
// Radical
// Rebuild Chile
// Renju
// Rise Above
// Risky Road
// Roguelike
// Roly Poly
// Run Around
// Rush Hour
// SameGame
// SamePhysics
// Save the Totem
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// Stick Hero
// String Avoider
// Stringy
// Sudoku
// Super Mario Bros
// Surfingers
// Survival Horror
// Talesworth Adventure
// Tetris
// The Impossible Line
// The Moops - Combos of Joy
// The Next Arrow
// Threes
// Tic Tac Toe
// Timberman
// Tiny Wings
// Tipsy Tower
// Toony
// Totem Destroyer
// Tower Defense
// Trick Shot
// Tunnelball
// Turn
// Turnellio
// TwinSpin
// vvvvvv
// Warp Shift
// Way of an Idea
// Whack a Creep
// Wheel of Fortune
// Where's my Water
// Wish Upon a Star
// Word Game
// Wordle
// Worms
// Yanga
// Yeah Bunny
// Zhed
// zNumbers