-
Notifications
You must be signed in to change notification settings - Fork 6
/
generate_palette.py
221 lines (162 loc) · 5.53 KB
/
generate_palette.py
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""
Inkscape extension to generate color palettes from selected objects' color properties
"""
import os
import sys
import inkex
def log(text):
inkex.utils.debug(text)
def abort(text):
inkex.errormsg(_(text))
exit()
class GeneratePalette(inkex.Effect):
def __init__(self):
inkex.Effect.__init__(self)
self.arg_parser.add_argument(
'-n', '--name',
type=str,
dest='name',
help='Palette name'
)
self.arg_parser.add_argument(
'-p', '--property',
type=str,
dest='property',
help='Color property'
)
self.arg_parser.add_argument(
'-d', '--default',
type=inkex.Boolean,
dest='default',
help='Default grays'
)
self.arg_parser.add_argument(
'-s', '--sort',
type=str,
dest='sort',
help='Sort type'
)
self.arg_parser.add_argument(
'-r', '--replace',
type=inkex.Boolean,
dest='replace',
help='Replace existing'
)
def get_palettes_path(self):
if sys.platform.startswith('win'):
path = os.path.join(os.environ['APPDATA'], 'inkscape', 'palettes')
else:
path = os.environ.get('XDG_CONFIG_HOME', '~/.config')
path = os.path.join(path, 'inkscape', 'palettes')
return os.path.expanduser(path)
def get_file_path(self):
name = str(self.options.name).replace(' ', '-')
return "%s/%s.gpl" % (self.palettes_path, name)
def get_default_colors(self):
colors = [
" 0 0 0 Black",
" 26 26 26 90% Gray",
" 51 51 51 80% Gray",
" 77 77 77 70% Gray",
"102 102 102 60% Gray",
"128 128 128 50% Gray",
"153 153 153 40% Gray",
"179 179 179 30% Gray",
"204 204 204 20% Gray",
"230 230 230 10% Gray",
"236 236 236 7.5% Gray",
"242 242 242 5% Gray",
"249 249 249 2.5% Gray",
"255 255 255 White"
]
return colors if self.options.default else []
def get_node_prop(self, node, property):
attr = node.attrib.get('style')
style = dict(inkex.Style.parse_str(attr))
return style.get(property, 'none')
def get_node_index(self, item):
node = item[1]
id = node.attrib.get('id')
return self.options.ids.index(id)
def get_node_yx(self, item):
node_bbox = item[1].bounding_box()
x = node_bbox.center_x - self.selected_bbox.left
return [ self.round_to(x, node_bbox.width), node_bbox.center_y ]
def get_node_xy(self, item):
node_bbox = item[1].bounding_box()
y = node_bbox.center_y - self.selected_bbox.top
return [ self.round_to(y, node_bbox.height), node_bbox.center_x ]
@staticmethod
def round_to(val, unit):
return val - (val % unit)
def get_formatted_color(self, color):
rgb = inkex.Color(color).to_rgb()
if self.options.sort == 'hsl':
key = inkex.Color(color).to_hsl()
key = "{:03d}{:03d}{:03d}".format(*key)
else:
key = "{:03d}{:03d}{:03d}".format(*rgb)
rgb = "{:3d} {:3d} {:3d}".format(*rgb)
color = str(color).upper()
name = str(inkex.Color(color).to_named()).upper()
if name != color:
name = "%s (%s)" % (name.capitalize(), color)
return "%s %s %s" % (key, rgb, name)
def get_selected_colors(self):
colors = []
selected = list(self.svg.selected.items())
if self.options.sort == 'selection':
selected.sort(key=self.get_node_index)
elif self.options.sort == 'xy_location':
self.selected_bbox = self.svg.selection.bounding_box()
selected.sort(key=self.get_node_xy)
elif self.options.sort == 'yx_location':
self.selected_bbox = self.svg.selection.bounding_box()
selected.sort(key=self.get_node_yx)
for id, node in selected:
if self.options.property in ['fill', 'both']:
fill = self.get_node_prop(node, 'fill')
if inkex.colors.is_color(fill):
if fill != 'none' and fill not in colors:
colors.append(fill)
if self.options.property in ['stroke', 'both']:
stroke = self.get_node_prop(node, 'stroke')
if inkex.colors.is_color(stroke):
if stroke != 'none' and stroke not in colors:
colors.append(stroke)
colors = list(map(self.get_formatted_color, colors))
if self.options.sort == 'hsl' or self.options.sort == 'rgb':
colors.sort()
return list(map(lambda x : x[11:], colors))
def write_palette(self):
file = open(self.file_path, 'w')
try:
file.write("GIMP Palette\n")
file.write("Name: %s\n" % self.options.name)
file.write("#\n# Generated with Inkscape Generate Palette\n#\n")
for color in self.default_colors:
file.write("%s\n" % color)
for color in self.selected_colors:
if color[:11] not in list(map(lambda x : x[:11], self.default_colors)):
file.write("%s\n" % color)
finally:
file.close()
def effect(self):
self.palettes_path = self.get_palettes_path()
self.file_path = self.get_file_path()
self.default_colors = self.get_default_colors()
self.selected_colors = self.get_selected_colors()
if not self.options.replace and os.path.exists(self.file_path):
abort('Palette already exists!')
if not self.options.name:
abort('Please enter a palette name.')
if len(self.options.ids) < 2:
abort('Please select at least 2 objects.')
if not len(self.selected_colors):
abort('No colors found in selected objects!')
self.write_palette()
if __name__ == '__main__':
palette = GeneratePalette()
palette.run()