Skip to content

Instantly share code, notes, and snippets.

@est77
Created September 19, 2013 07:49
Show Gist options
  • Save est77/6620265 to your computer and use it in GitHub Desktop.
Save est77/6620265 to your computer and use it in GitHub Desktop.
Parses Adobe swatch exchange files (color palettes).
#!/usr/bin/env python
# coding: utf-8
# Based on SwatchBooker code. Original license follows
#
# Copyright 2008 Olivier Berten <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
import sys
import struct
class ase_reader( object):
def __init__( self, callbacks):
self._cb = callbacks
def read( self, file):
data = file.read(4)
if struct.unpack( '4s', data)[0] != 'ASEF':
self._cb.bad_header_id()
return
version = struct.unpack( '>2H',file.read(4))
self._cb.set_version( version)
nbblocks = struct.unpack( '>L',file.read(4))[0]
self._cb.set_num_blocks( nbblocks)
for i in range( nbblocks):
name = False
block_type, block_size = struct.unpack( '>HL',file.read(6))
if block_size > 0:
length = struct.unpack( '>H',file.read(2))[0]
if length > 0:
name = unicode( struct.unpack( str( length * 2) + 's', file.read( length * 2))[0], 'utf_16_be').split( '\x00', 1)[0]
if block_type == 0xc001:
self._cb.begin_group( name)
elif block_type == 0xc002:
self._cb.end_group()
elif block_type == 0x0001:
model = struct.unpack('4s',file.read(4))[0]
color = { 'name': name}
if model == "CMYK":
color['value'] = list( struct.unpack( '>4f', file.read(16)))
color['model'] = model
elif model == "RGB ":
color['value'] = list(struct.unpack( '>3f',file.read(12)))
color['model'] = 'RGB'
elif model == "LAB ":
L,a,b = struct.unpack( '>3f',file.read(12))
color['value'] = [L*100,a,b]
color['model'] = 'LAB'
elif model == "Gray":
color['value'] = [1-struct.unpack( '>f',file.read(4))[0],]
color['model'] = 'Gray'
type = struct.unpack( '>H',file.read(2))[0]
'''
if type == 0:
color['usage'] = 'global'
elif type == 1:
color['usage'] = 'spot'
'''
self._cb.add_color( color)
if __name__ == "__main__":
class ase_reader_callbacks( object):
def bad_header_id( self):
print "bad header"
def set_version( self, version):
print "version = ", version
def set_num_blocks( self, nblocks):
print "nblocks = ", nblocks
def begin_group( self, name):
print "group ", name
def end_group( self):
print "group end"
def add_color( self, col):
print col['name'], col['value']
f = open( "ps_swatches.ase", "rb")
cb = ase_reader_callbacks()
r = ase_reader( cb)
r.read( f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment