Permalink
Browse files

add 'type' field to all objects

type may be ["Color Group"|"Spot"|"Global"|"Process"]. Most swatches will be process colors. Spot and Global follow the definitions in the illustrator docs

all text in the dict is wrapped in unicode()
  • Loading branch information...
1 parent c180e6b commit 0c72c307abff204f52c0025e93fcf36bbb1db0be @nsfmc committed Dec 27, 2012
Showing with 107 additions and 43 deletions.
  1. +34 −10 README.md
  2. +1 −0 setup.py
  3. +56 −28 swatch/__init__.py
  4. +16 −5 swatch/parser.py
View
@@ -14,38 +14,62 @@ palettes. colors are simple dicts of the form
{
'name': u'color name',
+ 'type': u'Process',
'data': {
- 'mode': 'RGB',
+ 'mode': u'RGB',
'values': [1.0, 1.0, 1.0]
}
}
the values provided vary between color mode. For all color modes, the
value is always a list of floats.
-RGB: three floats between [0,1] corresponding to RGB
-CMYK: four floats between [0,1] inclusive, corresponding to CMYK
-Gray: one float between [0,1] with 1 being white, 0 being black
+RGB: three floats between [0,1] corresponding to RGB.
+CMYK: four floats between [0,1] inclusive, corresponding to CMYK.
+Gray: one float between [0,1] with 1 being white, 0 being black.
LAB: three floats. The first L, is ranged from 0,1. Both A and B are
floats ranging from [-128.0,127.0]. I believe illustrator just crops
these to whole values, though.
-Palettes are also dicts, but they have an attribute named swatches which
-contains a list of colors contained within the palette.
+Palettes (née Color Groups in Adobe Parlance) are also dicts, but they have an
+attribute named `swatches` which contains a list of colors contained within
+the palette.
{
'name': u'accent colors',
+ 'type': u'Color Group',
'swatches': [
{color}, {color}, ..., {color}
]
}
Because Adobe Illustrator lets swatches exist either inside and outside
of palettes, the output of swatch.parse is a list that may contain
-swatches and palettes, i.e. [ [swatch|palette]* ]
+swatches and palettes, i.e. [ swatch* palette* ]
-Here's an example:
+Here's an example with a light grey swatch followed by a color group containing three
>>> import swatch
- >>> swatch.parse("simple palette.ase")
- [{'data': {'mode': 'RGB', 'values': [1.0, 1.0, 1.0]}, 'name': u'Bright White'}]
+ >>> swatch.parse("example.ase")
+ [{'data': {'mode': u'Gray', 'values': [0.75]},
+ 'name': u'Light Grey',
+ 'type': u'Process'},
+ {'name': u'Accent Colors',
+ 'swatches': [{'data': {'mode': u'CMYK',
+ 'values': [0.5279774069786072,
+ 0.24386966228485107,
+ 1.0,
+ 0.04303044080734253]},
+ 'name': u'Green',
+ 'type': u'Process'},
+ {'data': {'mode': u'CMYK',
+ 'values': [0.6261844635009766,
+ 0.5890134572982788,
+ 3.051804378628731e-05,
+ 3.051804378628731e-05]},
+ 'name': u'Violet Process Global',
+ 'type': u'Global'},
+ {'data': {'mode': u'LAB', 'values': [0.6000000238418579, -35.0, -5.0]},
+ 'name': u'Cyan Spot (global)',
+ 'type': u'Spot'}],
+ 'type': u'Color Group'}]
View
@@ -2,6 +2,7 @@
import codecs
from swatch import __version__ as VERSION
+
README = codecs.open('README.md', encoding='utf-8').read()
LICENSE = codecs.open('LICENSE', encoding='utf-8').read()
View
@@ -13,7 +13,7 @@
__title__ = 'swatch'
-__version__ = '0.1.0'
+__version__ = '0.2.0'
__author__ = 'Marcos Ojeda'
__license__ = 'MIT'
__copyright__ = 'Copyright 2012 Marcos A Ojeda'
@@ -24,50 +24,78 @@
def parse(filename):
- """parses a .ase file and returns a list of colors and palettes
+ """parses a .ase file and returns a list of colors and color groups
- swatch.parse reads in an ase file and converts it to a list of colors and
+ `swatch.parse` reads in an ase file and converts it to a list of colors and
palettes. colors are simple dicts of the form
- {
- 'name': u'color name',
- 'data': {
- 'mode': 'RGB',
- 'values': [1.0, 1.0, 1.0]
- }
+ ```json
+ {
+ 'name': u'color name',
+ 'type': u'Process',
+ 'data': {
+ 'mode': u'RGB',
+ 'values': [1.0, 1.0, 1.0]
}
+ }
+ ```
the values provided vary between color mode. For all color modes, the
value is always a list of floats.
- RGB: three floats between [0,1] corresponding to RGB
- CMYK: four floats between [0,1] inclusive, corresponding to CMYK
- Gray: one float between [0,1] with 1 being white, 0 being black
+ RGB: three floats between [0,1] corresponding to RGB.
+ CMYK: four floats between [0,1] inclusive, corresponding to CMYK.
+ Gray: one float between [0,1] with 1 being white, 0 being black.
LAB: three floats. The first L, is ranged from 0,1. Both A and B are
floats ranging from [-128.0,127.0]. I believe illustrator just crops
these to whole values, though.
- Palettes are also dicts, but they have an attribute named swatches which
- contains a list of colors contained within the palette.
+ Palettes (née Color Groups in Adobe Parlance) are also dicts, but they have an
+ attribute named `swatches` which contains a list of colors contained within
+ the palette.
- {
- 'name': u'accent colors',
- 'swatches': [
- {color}, {color}, ..., {color}
- ]
- }
+ ```json
+ {
+ 'name': u'accent colors',
+ 'type': u'Color Group',
+ 'swatches': [
+ {color}, {color}, ..., {color}
+ ]
+ }
+ ```
Because Adobe Illustrator lets swatches exist either inside and outside
of palettes, the output of swatch.parse is a list that may contain
- swatches and palettes, i.e. [ [swatch|palette]* ]
-
- Here's an example:
-
- >>> import swatch
- >>> swatch.parse("simple palette.ase")
- [{'data': {'mode': 'RGB', 'values': [1.0, 1.0, 1.0]}, 'name': u'Bright White'}]
-
+ swatches and palettes, i.e. [ swatch* palette* ]
+
+ Here's an example with a light grey swatch followed by a color group containing three
+
+ >>> import swatch
+ >>> swatch.parse("example.ase")
+ [{'data': {'mode': u'Gray', 'values': [0.75]},
+ 'name': u'Light Grey',
+ 'type': u'Process'},
+ {'name': u'Accent Colors',
+ 'swatches': [{'data': {'mode': u'CMYK',
+ 'values': [0.5279774069786072,
+ 0.24386966228485107,
+ 1.0,
+ 0.04303044080734253]},
+ 'name': u'Green',
+ 'type': u'Process'},
+ {'data': {'mode': u'CMYK',
+ 'values': [0.6261844635009766,
+ 0.5890134572982788,
+ 3.051804378628731e-05,
+ 3.051804378628731e-05]},
+ 'name': u'Violet Process Global',
+ 'type': u'Global'},
+ {'data': {'mode': u'LAB', 'values': [0.6000000238418579, -35.0, -5.0]},
+ 'name': u'Cyan Spot (global)',
+ 'type': u'Spot'}],
+ 'type': u'Color Group'}]
"""
+
with open(filename, "rb") as data:
header, version_major, version_minor, chunk_count = struct.unpack("!4sHHI", data.read(12))
View
@@ -60,14 +60,25 @@ def dict_for_chunk(fd):
color_data = data[2 + title_length:]
output = {
- 'name': title,
+ 'name': unicode(title),
+ 'type': u'Color Group' # default to color group
}
if color_data:
fmt = {'RGB': '!fff', 'Gray': '!f', 'CMYK': '!ffff', 'LAB': '!fff'}
- color_type = struct.unpack("!4s", color_data[:4])[0].strip()
- color_values = list(struct.unpack(fmt[color_type], color_data[4:-2]))
- data = {'mode': color_type, 'values': color_values}
- output.update(data=data)
+ color_mode = struct.unpack("!4s", color_data[:4])[0].strip()
+ color_values = list(struct.unpack(fmt[color_mode], color_data[4:-2]))
+
+ color_types = ['Global', 'Spot', 'Process']
+ swatch_type_index = struct.unpack(">h", color_data[-2:])[0]
+ swatch_type = color_types[swatch_type_index]
+
+ output.update({
+ 'data': {
+ 'mode': unicode(color_mode),
+ 'values': color_values
+ },
+ 'type': unicode(swatch_type)
+ })
return output

0 comments on commit 0c72c30

Please sign in to comment.