Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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...
commit 0c72c307abff204f52c0025e93fcf36bbb1db0be 1 parent c180e6b
Marcos Ojeda authored December 27, 2012
44  README.md
Source Rendered
@@ -14,8 +14,9 @@ palettes. colors are simple dicts of the form
14 14
 
15 15
     {
16 16
         'name': u'color name',
  17
+        'type': u'Process',
17 18
         'data': {
18  
-            'mode': 'RGB',
  19
+            'mode': u'RGB',
19 20
             'values': [1.0, 1.0, 1.0]
20 21
         }
21 22
     }
@@ -23,18 +24,20 @@ palettes. colors are simple dicts of the form
23 24
 the values provided vary between color mode. For all color modes, the
24 25
 value is always a list of floats.
25 26
 
26  
-RGB: three floats between [0,1]  corresponding to RGB
27  
-CMYK: four floats between [0,1] inclusive, corresponding to CMYK
28  
-Gray: one float between [0,1] with 1 being white, 0 being black
  27
+RGB: three floats between [0,1]  corresponding to RGB.
  28
+CMYK: four floats between [0,1] inclusive, corresponding to CMYK.
  29
+Gray: one float between [0,1] with 1 being white, 0 being black.
29 30
 LAB: three floats. The first L, is ranged from 0,1. Both A and B are
30 31
 floats ranging from [-128.0,127.0]. I believe illustrator just crops
31 32
 these to whole values, though.
32 33
 
33  
-Palettes are also dicts, but they have an attribute named swatches which
34  
-contains a list of colors contained within the palette.
  34
+Palettes (née Color Groups in Adobe Parlance) are also dicts, but they have an
  35
+attribute named `swatches` which contains a list of colors contained within
  36
+the palette.
35 37
 
36 38
     {
37 39
         'name': u'accent colors',
  40
+        'type': u'Color Group',
38 41
         'swatches': [
39 42
             {color}, {color}, ..., {color}
40 43
         ]
@@ -42,10 +45,31 @@ contains a list of colors contained within the palette.
42 45
 
43 46
 Because Adobe Illustrator lets swatches exist either inside and outside
44 47
 of palettes, the output of swatch.parse is a list that may contain
45  
-swatches and palettes, i.e. [ [swatch|palette]* ]
  48
+swatches and palettes, i.e. [ swatch* palette* ]
46 49
 
47  
-Here's an example:
  50
+Here's an example with a light grey swatch followed by a color group containing three
48 51
 
49 52
     >>> import swatch
50  
-    >>> swatch.parse("simple palette.ase")
51  
-    [{'data': {'mode': 'RGB', 'values': [1.0, 1.0, 1.0]}, 'name': u'Bright White'}]
  53
+    >>> swatch.parse("example.ase")
  54
+    [{'data': {'mode': u'Gray', 'values': [0.75]},
  55
+      'name': u'Light Grey',
  56
+      'type': u'Process'},
  57
+     {'name': u'Accent Colors',
  58
+      'swatches': [{'data': {'mode': u'CMYK',
  59
+         'values': [0.5279774069786072,
  60
+          0.24386966228485107,
  61
+          1.0,
  62
+          0.04303044080734253]},
  63
+        'name': u'Green',
  64
+        'type': u'Process'},
  65
+       {'data': {'mode': u'CMYK',
  66
+         'values': [0.6261844635009766,
  67
+          0.5890134572982788,
  68
+          3.051804378628731e-05,
  69
+          3.051804378628731e-05]},
  70
+        'name': u'Violet Process Global',
  71
+        'type': u'Global'},
  72
+       {'data': {'mode': u'LAB', 'values': [0.6000000238418579, -35.0, -5.0]},
  73
+        'name': u'Cyan Spot (global)',
  74
+        'type': u'Spot'}],
  75
+      'type': u'Color Group'}]
1  setup.py
@@ -2,6 +2,7 @@
2 2
 import codecs
3 3
 from swatch import __version__ as VERSION
4 4
 
  5
+
5 6
 README = codecs.open('README.md', encoding='utf-8').read()
6 7
 LICENSE = codecs.open('LICENSE', encoding='utf-8').read()
7 8
 
84  swatch/__init__.py
@@ -13,7 +13,7 @@
13 13
 
14 14
 
15 15
 __title__ = 'swatch'
16  
-__version__ = '0.1.0'
  16
+__version__ = '0.2.0'
17 17
 __author__ = 'Marcos Ojeda'
18 18
 __license__ = 'MIT'
19 19
 __copyright__ = 'Copyright 2012 Marcos A Ojeda'
@@ -24,50 +24,78 @@
24 24
 
25 25
 
26 26
 def parse(filename):
27  
-    """parses a .ase file and returns a list of colors and palettes
  27
+    """parses a .ase file and returns a list of colors and color groups
28 28
 
29  
-    swatch.parse reads in an ase file and converts it to a list of colors and
  29
+    `swatch.parse` reads in an ase file and converts it to a list of colors and
30 30
     palettes. colors are simple dicts of the form
31 31
 
32  
-        {
33  
-            'name': u'color name',
34  
-            'data': {
35  
-                'mode': 'RGB',
36  
-                'values': [1.0, 1.0, 1.0]
37  
-            }
  32
+    ```json
  33
+    {
  34
+        'name': u'color name',
  35
+        'type': u'Process',
  36
+        'data': {
  37
+            'mode': u'RGB',
  38
+            'values': [1.0, 1.0, 1.0]
38 39
         }
  40
+    }
  41
+    ```
39 42
 
40 43
     the values provided vary between color mode. For all color modes, the
41 44
     value is always a list of floats.
42 45
 
43  
-    RGB: three floats between [0,1]  corresponding to RGB
44  
-    CMYK: four floats between [0,1] inclusive, corresponding to CMYK
45  
-    Gray: one float between [0,1] with 1 being white, 0 being black
  46
+    RGB: three floats between [0,1]  corresponding to RGB.
  47
+    CMYK: four floats between [0,1] inclusive, corresponding to CMYK.
  48
+    Gray: one float between [0,1] with 1 being white, 0 being black.
46 49
     LAB: three floats. The first L, is ranged from 0,1. Both A and B are
47 50
     floats ranging from [-128.0,127.0]. I believe illustrator just crops
48 51
     these to whole values, though.
49 52
 
50  
-    Palettes are also dicts, but they have an attribute named swatches which
51  
-    contains a list of colors contained within the palette.
  53
+    Palettes (née Color Groups in Adobe Parlance) are also dicts, but they have an
  54
+    attribute named `swatches` which contains a list of colors contained within
  55
+    the palette.
52 56
 
53  
-        {
54  
-            'name': u'accent colors',
55  
-            'swatches': [
56  
-                {color}, {color}, ..., {color}
57  
-            ]
58  
-        }
  57
+    ```json
  58
+    {
  59
+        'name': u'accent colors',
  60
+        'type': u'Color Group',
  61
+        'swatches': [
  62
+            {color}, {color}, ..., {color}
  63
+        ]
  64
+    }
  65
+    ```
59 66
 
60 67
     Because Adobe Illustrator lets swatches exist either inside and outside
61 68
     of palettes, the output of swatch.parse is a list that may contain
62  
-    swatches and palettes, i.e. [ [swatch|palette]* ]
63  
-
64  
-    Here's an example:
65  
-
66  
-    >>> import swatch
67  
-    >>> swatch.parse("simple palette.ase")
68  
-    [{'data': {'mode': 'RGB', 'values': [1.0, 1.0, 1.0]}, 'name': u'Bright White'}]
69  
-
  69
+    swatches and palettes, i.e. [ swatch* palette* ]
  70
+
  71
+    Here's an example with a light grey swatch followed by a color group containing three
  72
+
  73
+        >>> import swatch
  74
+        >>> swatch.parse("example.ase")
  75
+        [{'data': {'mode': u'Gray', 'values': [0.75]},
  76
+          'name': u'Light Grey',
  77
+          'type': u'Process'},
  78
+         {'name': u'Accent Colors',
  79
+          'swatches': [{'data': {'mode': u'CMYK',
  80
+             'values': [0.5279774069786072,
  81
+              0.24386966228485107,
  82
+              1.0,
  83
+              0.04303044080734253]},
  84
+            'name': u'Green',
  85
+            'type': u'Process'},
  86
+           {'data': {'mode': u'CMYK',
  87
+             'values': [0.6261844635009766,
  88
+              0.5890134572982788,
  89
+              3.051804378628731e-05,
  90
+              3.051804378628731e-05]},
  91
+            'name': u'Violet Process Global',
  92
+            'type': u'Global'},
  93
+           {'data': {'mode': u'LAB', 'values': [0.6000000238418579, -35.0, -5.0]},
  94
+            'name': u'Cyan Spot (global)',
  95
+            'type': u'Spot'}],
  96
+          'type': u'Color Group'}]
70 97
     """
  98
+
71 99
     with open(filename, "rb") as data:
72 100
         header, version_major, version_minor, chunk_count = struct.unpack("!4sHHI", data.read(12))
73 101
 
21  swatch/parser.py
@@ -60,14 +60,25 @@ def dict_for_chunk(fd):
60 60
     color_data = data[2 + title_length:]
61 61
 
62 62
     output = {
63  
-        'name': title,
  63
+        'name': unicode(title),
  64
+        'type': u'Color Group'  # default to color group
64 65
     }
65 66
 
66 67
     if color_data:
67 68
         fmt = {'RGB': '!fff', 'Gray': '!f', 'CMYK': '!ffff', 'LAB': '!fff'}
68  
-        color_type = struct.unpack("!4s", color_data[:4])[0].strip()
69  
-        color_values = list(struct.unpack(fmt[color_type], color_data[4:-2]))
70  
-        data = {'mode': color_type, 'values': color_values}
71  
-        output.update(data=data)
  69
+        color_mode = struct.unpack("!4s", color_data[:4])[0].strip()
  70
+        color_values = list(struct.unpack(fmt[color_mode], color_data[4:-2]))
  71
+
  72
+        color_types = ['Global', 'Spot', 'Process']
  73
+        swatch_type_index = struct.unpack(">h", color_data[-2:])[0]
  74
+        swatch_type = color_types[swatch_type_index]
  75
+
  76
+        output.update({
  77
+            'data': {
  78
+                'mode': unicode(color_mode),
  79
+                'values': color_values
  80
+            },
  81
+            'type': unicode(swatch_type)
  82
+        })
72 83
 
73 84
     return output

0 notes on commit 0c72c30

Please sign in to comment.
Something went wrong with that request. Please try again.