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