Rev 17163 | Rev 18123 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
12781 | subik | 1 | #!/usr/bin/python |
2 | # -*- coding: utf-8 -*- |
||
3 | |||
4 | """ |
||
5 | ABOUT THIS SCRIPT: |
||
6 | |||
13058 | subik | 7 | ColorChart.py allows a user to create color charts with all |
8 | the colors of a given scribus document. |
||
9 | It generates a color field for each color and a description |
||
10 | of the color, containing the color name, the CMYK values and |
||
11 | the RGB values. |
||
12781 | subik | 12 | |
13058 | subik | 13 | If there is a document opened in scribus, ColorChart uses this |
14 | document as color source and creates a new document with the |
||
15 | color chart. |
||
16 | If there is no document opened in scribus, ColorChart displays |
||
17 | a file open dialog to allow the user to chose a scribus file |
||
18 | to generate a colorchart of. |
||
19 | You will be asked to give a name for the color chart. This name |
||
20 | will be displayed in the pages headlines. |
||
12781 | subik | 21 | ############################ |
22 | |||
23 | LICENSE: |
||
24 | |||
25 | This program is free software; you can redistribute it and/or modify |
||
26 | it under the terms of the GNU General Public License as published by |
||
27 | the Free Software Foundation; either version 2 of the License, or |
||
28 | (at your option) any later version. |
||
29 | |||
30 | This program is distributed in the hope that it will be useful, |
||
31 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
32 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
33 | GNU General Public License for more details. |
||
34 | |||
35 | You should have received a copy of the GNU General Public License |
||
36 | along with this program; if not, write to the Free Software |
||
37 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
||
38 | |||
39 | Author: Sebastian Stetter |
||
40 | please report bugs to: scribusscript@sebastianstetter.de |
||
41 | """ |
||
42 | from __future__ import division |
||
43 | import sys |
||
44 | __version__=1.1 |
||
45 | try: |
||
46 | # Please do not use 'from scribus import *' . If you must use a 'from import', |
||
47 | # Do so _after_ the 'import scribus' and only import the names you need, such |
||
48 | # as commonly used constants. |
||
49 | import scribus |
||
50 | except ImportError,err: |
||
51 | print "This Python script is written for the Scribus scripting interface." |
||
52 | print "It can only be run from within Scribus." |
||
53 | sys.exit(1) |
||
54 | |||
55 | #################### |
||
56 | # IMPORTS GO HERE # |
||
57 | #################### |
||
58 | |||
59 | COLOR_FIELD_HEIGHT=25 |
||
60 | #space between colorfields |
||
61 | HSPACE=5 |
||
62 | VSPACE=4 |
||
63 | #space for header and footer |
||
64 | HEADERSIZE = 10 |
||
65 | FOOTERSIZE = 5 |
||
66 | TEXT_BOX_WIDTH = 50 |
||
67 | global pageTitle |
||
68 | pageTitle="COLOR CHART" |
||
69 | |||
70 | def drawHeaderFooter(pagetitle): |
||
71 | """draw some info on the pages""" |
||
72 | # get page size |
||
73 | pageSize=scribus.getPageSize() |
||
74 | pageWidth=pageSize[0] |
||
75 | pageHeight=pageSize[1] |
||
76 | #pageMargins |
||
77 | pageMargins=scribus.getPageMargins() |
||
78 | topMargin=pageMargins[0] |
||
79 | leftMargin=pageMargins[1] |
||
80 | rightMargin=pageMargins[2] |
||
81 | bottomMargin=pageMargins[3] |
||
13058 | subik | 82 | |
12781 | subik | 83 | #create textbox and insert text for header |
84 | textbox=scribus.createText(leftMargin, topMargin, pageWidth-leftMargin-rightMargin, HEADERSIZE) |
||
85 | #set proper font size and alignment |
||
86 | scribus.setFontSize(18, textbox) |
||
87 | scribus.setTextAlignment(scribus.ALIGN_CENTERED, textbox) |
||
88 | #load the string into the textbox |
||
89 | headerstring=pagetitle |
||
90 | scribus.insertText(headerstring, 0, textbox) |
||
13058 | subik | 91 | |
12781 | subik | 92 | #create textbox and insert text for footer |
93 | textbox=scribus.createText(leftMargin, pageHeight-bottomMargin-FOOTERSIZE, pageWidth-leftMargin-rightMargin, FOOTERSIZE) |
||
94 | #set proper font size and alignment |
||
95 | scribus.setFontSize(9, textbox) |
||
96 | scribus.setTextAlignment(scribus.ALIGN_LEFT, textbox) |
||
97 | #load the string into the textbox |
||
98 | footerstring="Created using ColorChart.py V %s script for Scribus by Sebastian Stetter - http://www.sebastianstetter.de" % str(__version__) |
||
99 | scribus.insertText(footerstring, 0, textbox) |
||
100 | |||
13058 | subik | 101 | |
102 | def getSpotColors(): |
||
103 | """ Get spot colors from an original document. |
||
104 | Must be called after getColorsFromDocument(). |
||
105 | """ |
||
106 | ret = {} |
||
107 | for color in scribus.getColorNames(): |
||
108 | ret[color] = scribus.isSpotColor(color) |
||
109 | return ret |
||
110 | |||
111 | |||
12781 | subik | 112 | def getColorsFromDocument(): |
113 | """gets colors from opend document. if there is no document, display dialog to chose a file. returns a list[name,c,m,y,k]""" |
||
114 | def getColors(): |
||
115 | """gets the colors and returns a list[name,c,m,y,k]""" |
||
116 | colorNames=scribus.getColorNames() |
||
117 | list=[] |
||
118 | scribus.statusMessage("Reading Colors...") |
||
119 | stepsTotal=len(colorNames) |
||
120 | scribus.progressTotal(stepsTotal) |
||
121 | steps=0 |
||
122 | for name in colorNames: |
||
123 | color=scribus.getColor(name) |
||
124 | listitem=[name, color[0], color[1], color[2], color[3]] |
||
125 | list.append(listitem) |
||
126 | #update progress bar |
||
127 | steps=steps+1 |
||
128 | scribus.progressSet(steps) |
||
129 | return list |
||
130 | |||
131 | #check if we have a document - otherwise display open file dialog |
||
17395 | fschmid | 132 | if scribus.haveDoc() > 0: |
12781 | subik | 133 | pass |
134 | list=getColors() |
||
135 | return list |
||
136 | else: |
||
137 | pass |
||
138 | #display file open dialog |
||
139 | file=scribus.fileDialog("ColorChart by Sebastian Stetter", 'Scribus files(*.sla *.SLA *.sla.gz *.SLA.GZ)') |
||
140 | #open file |
||
141 | try: |
||
142 | scribus.openDoc(file) |
||
143 | except: |
||
144 | scribus.messageBox("ColorChart by Sebastian Stetter", "could not open file") |
||
145 | sys.exit() |
||
146 | list=getColors() |
||
147 | return list |
||
148 | |||
149 | def prepareDocument(): |
||
150 | """creates the new document, sets up colors """ |
||
151 | colorList = getColorsFromDocument() |
||
13058 | subik | 152 | spotDict = getSpotColors() |
12781 | subik | 153 | scribus.statusMessage("Preparing new document...") |
154 | scribus.newDocument(scribus.PAPER_A4, (15,15, 20, 20), scribus.PORTRAIT, 1, scribus.UNIT_POINTS, scribus.PAGE_1, 0, 1) |
||
155 | scribus.setUnit(scribus.UNIT_MILLIMETERS) |
||
156 | #delete existing colors |
||
157 | cols = scribus.getColorNames() |
||
158 | for col in cols: |
||
159 | scribus.deleteColor(col, "None") |
||
13058 | subik | 160 | |
12781 | subik | 161 | #create our new colors |
162 | for color in colorList: |
||
163 | cname=color[0] |
||
164 | c = int(color[1]) |
||
165 | m = int(color[2]) |
||
166 | y = int(color[3]) |
||
167 | k = int(color[4]) |
||
168 | scribus.defineColor(cname, c, m, y, k ) |
||
13058 | subik | 169 | if spotDict.has_key(cname): |
170 | scribus.setSpotColor(cname, spotDict[cname]) |
||
171 | |||
12781 | subik | 172 | #get the pageTitle form user and store it in PageTitle |
173 | global pageTitle |
||
174 | pageTitle=scribus.valueDialog("ColorChart by Sebastian Stetter", "Please enter document title", "Scribus COLOR CHART") |
||
175 | drawHeaderFooter(pageTitle) |
||
176 | |||
177 | def createPage(): |
||
178 | """appends a new page""" |
||
179 | scribus.newPage(-1) #append new page |
||
180 | #new page - new header and footer |
||
181 | drawHeaderFooter(pageTitle) |
||
182 | |||
183 | |||
184 | def rgbhex(r,g,b): |
||
185 | '''convert rgb values in 0-255 style to hex string in #000000 to #ffffff style''' |
||
186 | hr=hex(r) |
||
187 | hr = hr.replace("0x", "") |
||
188 | if len(hr)== 0: |
||
189 | hr = "00" |
||
190 | elif len(hr)==1: |
||
191 | hr = "0"+hr |
||
192 | else: |
||
193 | pass |
||
194 | hg=hex(g) |
||
195 | hg = hg.replace("0x", "") |
||
196 | if len(hg)== 0: |
||
197 | hg = "00" |
||
198 | elif len(hg)==1: |
||
199 | hg = "0"+hg |
||
200 | else: |
||
201 | pass |
||
202 | hb=hex(b) |
||
203 | hb = hb.replace("0x", "") |
||
204 | if len(hb)== 0: |
||
205 | hb = "00" |
||
206 | elif len(hb)==1: |
||
207 | hb = "0"+hb |
||
208 | else: |
||
209 | pass |
||
210 | rgbstring="#"+hr+hg+hb |
||
211 | rgbstring=rgbstring.upper() |
||
212 | return rgbstring |
||
213 | |||
214 | |||
215 | def drawColor(colorname, h, v, width, height): #h horizontal position, v vertical position |
||
13058 | subik | 216 | """draws a color chart field with its caption for the given colorname at the h and v position |
217 | """ |
||
12781 | subik | 218 | #get cmyk values and convert them to 0 - 255 values |
219 | color = scribus.getColor(colorname) |
||
220 | c= int(round(color[0]/2.55)) |
||
221 | m=int(round(color[1]/2.55)) |
||
222 | y=int(round(color[2]/2.55)) |
||
223 | k=int(round(color[3]/2.55)) |
||
224 | #get rgb color |
||
225 | rgbcolor=scribus.getColorAsRGB(colorname) |
||
226 | r=rgbcolor[0] |
||
227 | g=rgbcolor[1] |
||
228 | b=rgbcolor[2] |
||
229 | #get webcolor |
||
230 | webcolor=rgbhex(r, g, b) |
||
231 | #but String for Textbox together |
||
13058 | subik | 232 | colorDisplay = colorname |
233 | if scribus.isSpotColor(colorname): |
||
234 | colorDisplay += " (Spot Color)" |
||
235 | colorstring="%s\nC %i, M %i, Y %i, K %i, \nR %i, G %i, B %i \nRGB: %s" %(colorDisplay, c, m, y, k, r, g, b, webcolor) |
||
236 | |||
12781 | subik | 237 | #draw rectangle and set colors |
238 | rect=scribus.createRect(h, v, width, height) |
||
239 | scribus.setFillColor(colorname, rect) |
||
240 | #if total amount of color is < 20 draw outline in Black for rectangle, else in same color |
||
241 | if c +m+y+k < 20: |
||
242 | scribus.setLineColor("Black", rect) |
||
243 | else: |
||
244 | scribus.setLineColor(colorname, rect) |
||
245 | #create textbox and insert text |
||
246 | textbox=scribus.createText(h+width+5, v, 50, height) |
||
247 | #set proper font size |
||
248 | scribus.setFontSize(11, textbox) |
||
249 | scribus.setTextAlignment(scribus.ALIGN_LEFT, textbox) |
||
250 | #load the string into the textbox |
||
251 | scribus.insertText(colorstring, 0, textbox) |
||
252 | |||
253 | |||
254 | def createChart(): |
||
255 | """actually handles the whole chart creation process""" |
||
256 | prepareDocument() |
||
257 | # get page size |
||
258 | pageSize=scribus.getPageSize() |
||
259 | pageWidth=pageSize[0] |
||
260 | pageHeight=pageSize[1] |
||
261 | #pageMargins |
||
262 | pageMargins=scribus.getPageMargins() |
||
263 | topMargin=pageMargins[0] |
||
264 | leftMargin=pageMargins[1] |
||
265 | rightMargin=pageMargins[2] |
||
266 | bottomMargin=pageMargins[3] |
||
13058 | subik | 267 | |
12781 | subik | 268 | #color field dimensions |
269 | colorFieldWidth= pageWidth - leftMargin - rightMargin - (TEXT_BOX_WIDTH+HSPACE) #50+5 is the with of the textbox plus the space between textbox and colorfield |
||
13058 | subik | 270 | |
12781 | subik | 271 | #how much space does one field use? |
272 | vSpaceUsedByField = COLOR_FIELD_HEIGHT+VSPACE |
||
13058 | subik | 273 | |
12781 | subik | 274 | #how much space is available per row? |
275 | vSpaceAvailable=pageHeight-topMargin-bottomMargin-HEADERSIZE-FOOTERSIZE |
||
13058 | subik | 276 | |
12781 | subik | 277 | #counts the colorFields created for a page. reset this variable after creation of new page |
278 | colorFieldCounter=0 |
||
13058 | subik | 279 | |
12781 | subik | 280 | #get list of all colors in document |
281 | colorList = scribus.getColorNames() |
||
282 | #prepare the progressbar |
||
283 | colorNumber=len(colorList) |
||
284 | scribus.progressTotal(colorNumber) |
||
285 | #@TODO: implement possibility to abort script (button2=scribus.BUTTON_CANCEL) buttons should return int 1 or 2 |
||
286 | #scribus.messageBox("ColorChart Script by Sebastian Stetter", "...going to create a chart of "+str(colorNumber)+" colors.\n This may take a while.", button1 = scribus.BUTTON_OK) |
||
287 | scribus.statusMessage("Drawing color fields...") |
||
288 | stepCompleted=0 |
||
289 | #disable redrawing for better performance |
||
290 | scribus.setRedraw(False) |
||
291 | for color in colorList: |
||
292 | if (vSpaceUsedByField * (colorFieldCounter+1)) <= vSpaceAvailable: |
||
293 | # when there is enought space left draw a color field... |
||
13058 | subik | 294 | |
12781 | subik | 295 | #calculate Position for new colorField |
296 | h=leftMargin |
||
297 | v=topMargin + (vSpaceUsedByField * colorFieldCounter)+HEADERSIZE |
||
298 | #draw the colorField |
||
13058 | subik | 299 | drawColor(color, h, v, colorFieldWidth, COLOR_FIELD_HEIGHT) |
12781 | subik | 300 | colorFieldCounter = colorFieldCounter+1 |
301 | #update progressbar |
||
302 | stepCompleted = stepCompleted+1 |
||
303 | scribus.progressSet(stepCompleted) |
||
304 | else: |
||
305 | #not enough space? create a new page! |
||
306 | createPage() |
||
307 | #reset the colorFieldCounter to '0' since we created a new page |
||
308 | colorFieldCounter = 0 |
||
309 | h=leftMargin |
||
310 | v=topMargin + (vSpaceUsedByField * colorFieldCounter)+HEADERSIZE |
||
13058 | subik | 311 | drawColor(color, h, v, colorFieldWidth, COLOR_FIELD_HEIGHT) |
12781 | subik | 312 | colorFieldCounter = colorFieldCounter+1 |
313 | |||
314 | #update progressbar |
||
315 | stepCompleted = stepCompleted+1 |
||
316 | scribus.progressSet(stepCompleted) |
||
13058 | subik | 317 | |
12781 | subik | 318 | #make shure pages are redrawn |
319 | scribus.setRedraw(True) |
||
320 | |||
321 | |||
322 | def main(argv): |
||
323 | """just invokes createChart() and displays a message after the chart is finished.""" |
||
324 | createChart() |
||
13055 | subik | 325 | scribus.messageBox("ColorChart Script by Sebastian Stetter", "Your chart has been created, but not saved, yet!\nThanks for using ColorChart and Scribus!") |
12781 | subik | 326 | |
327 | |||
328 | def main_wrapper(argv): |
||
329 | """The main_wrapper() function disables redrawing, sets a sensible generic |
||
330 | status bar message, and optionally sets up the progress bar. It then runs |
||
331 | the main() function. Once everything finishes it cleans up after the main() |
||
332 | function, making sure everything is sane before the script terminates.""" |
||
333 | try: |
||
334 | scribus.statusMessage("Creating color chart...") |
||
335 | scribus.progressReset() |
||
336 | main(argv) |
||
337 | finally: |
||
338 | # Exit neatly even if the script terminated with an exception, |
||
339 | # so we leave the progress bar and status bar blank and make sure |
||
340 | # drawing is enabled. |
||
341 | if scribus.haveDoc(): |
||
342 | scribus.setRedraw(True) |
||
343 | scribus.statusMessage("") |
||
344 | scribus.progressReset() |
||
345 | |||
346 | # This code detects if the script is being run as a script, or imported as a module. |
||
347 | # It only runs main() if being run as a script. This permits you to import your script |
||
348 | # and control it manually for debugging. |
||
349 | if __name__ == '__main__': |
||
350 | main_wrapper(sys.argv) |