add a convenience function for mering attrs with default attrs, checking, and None...
[PyX.git] / color.py
blob822ac58fff3c36491f99aff06b30cd799a2385cf
1 # -*- encoding: utf-8 -*-
4 # Copyright (C) 2002-2004, 2006, 2013 Jörg Lehmann <joergl@users.sourceforge.net>
5 # Copyright (C) 2003-2006 Michael Schindler <m-schindler@users.sourceforge.net>
6 # Copyright (C) 2002-2011 André Wobst <wobsta@users.sourceforge.net>
8 # This file is part of PyX (http://pyx.sourceforge.net/).
10 # PyX is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
15 # PyX is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with PyX; if not, write to the Free Software
22 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 import binascii, colorsys, logging, math, struct
25 from . import attr, style, pdfwriter
27 logger = logging.getLogger("pyx")
29 # device-dependent (nonlinear) functions for color conversion
30 # UCRx : [0,1] -> [-1, 1] UnderColorRemoval (removes black from c, y, m)
31 # BG : [0,1] -> [0, 1] BlackGeneration (generate the black from the nominal k-value)
32 # as long as we have no further knowledge we define them linearly with constants 1
33 def _UCRc(x): return x
34 def _UCRm(x): return x
35 def _UCRy(x): return x
36 def _BG(x): return x
38 def set(UCRc=None, UCRm=None, UCRy=None, BG=None):
39 global _UCRc
40 global _UCRm
41 global _UCRy
42 global _BG
44 if UCRc is not None:
45 _UCRc = UCRc
46 if UCRm is not None:
47 _UCRm = UCRm
48 if UCRy is not None:
49 _UCRy = UCRy
50 if BG is not None:
51 _BG = BG
54 class color(attr.exclusiveattr, style.strokestyle, style.fillstyle):
55 """base class for all colors"""
56 def __init__(self):
57 super().__init__(color)
60 clear = attr.clearclass(color)
63 class gray(color):
65 """grey tones"""
67 def __init__(self, gray=0.0):
68 super().__init__()
69 if gray<0 or gray>1:
70 raise ValueError("Value gray out of range [0,1]")
71 self.gray = gray
73 def processPS(self, file, writer, context, registry):
74 file.write("%f setgray\n" % self.gray)
76 def processPDF(self, file, writer, context, registry):
77 if context.strokeattr:
78 file.write("%f G\n" % self.gray)
79 if context.fillattr:
80 file.write("%f g\n" % self.gray)
82 def cmyk(self):
83 return cmyk(0, 0, 0, 1 - self.gray)
85 def gray(self):
86 return gray(self.gray)
87 grey = gray
89 def hsb(self):
90 return hsb(0, 0, self.gray)
92 def rgb(self):
93 return rgb(self.gray, self.gray, self.gray)
95 def colorspacestring(self):
96 return "/DeviceGray"
98 def to8bitbytes(self):
99 return bytes((int(self.gray*255),))
101 gray.black = gray(0.0)
102 gray.white = gray(1.0)
103 grey = gray
106 class rgb(color):
108 """rgb colors"""
110 def __init__(self, r=0.0, g=0.0, b=0.0):
111 super().__init__()
112 if r<0 or r>1:
113 raise ValueError("Value r out of range [0,1]")
114 if g<0 or g>1:
115 raise ValueError("Value g out of range [0,1]")
116 if b<0 or b>1:
117 raise ValueError("Value b out of range [0,1]")
118 self.r = r
119 self.g = g
120 self.b = b
122 def processPS(self, file, writer, context, registry):
123 file.write("%f %f %f setrgbcolor\n" % (self.r, self.g, self.b))
125 def processPDF(self, file, writer, context, registry):
126 if context.strokeattr:
127 file.write("%f %f %f RG\n" % (self.r, self.g, self.b))
128 if context.fillattr:
129 file.write("%f %f %f rg\n" % (self.r, self.g, self.b))
131 def cmyk(self):
132 # conversion to cmy
133 c, m, y = 1 - self.r, 1 - self.g, 1 - self.b
134 # conversion from cmy to cmyk with device-dependent functions
135 k = min([c, m, y])
136 return cmyk(min(1, max(0, c - _UCRc(k))),
137 min(1, max(0, m - _UCRm(k))),
138 min(1, max(0, y - _UCRy(k))),
139 _BG(k))
141 def gray(self):
142 return gray(0.3*self.r + 0.59*self.g + 0.11*self.b)
143 grey = gray
145 def hsb(self):
146 h, s, b = colorsys.rgb_to_hsv(self.r, self.g, self.b)
147 return hsb(h, s, b)
149 def rgb(self):
150 return rgb(self.r, self.g, self.b)
152 def colorspacestring(self):
153 return "/DeviceRGB"
155 def to8bitbytes(self):
156 return struct.pack("BBB", int(self.r*255), int(self.g*255), int(self.b*255))
158 def tohexstring(self, cssstrip=1, addhash=1):
159 hexstring = binascii.b2a_hex(self.to8bitbytes())
160 if cssstrip and hexstring[0] == hexstring[1] and hexstring[2] == hexstring[3] and hexstring[4] == hexstring[5]:
161 hexstring = "".join([hexstring[0], hexstring[1], hexstring[2]])
162 if addhash:
163 hexstring = "#" + hexstring
164 return hexstring
167 def rgbfromhexstring(hexstring):
168 hexstring = hexstring.strip().lstrip("#")
169 if len(hexstring) == 3:
170 hexstring = "".join([hexstring[0], hexstring[0], hexstring[1], hexstring[1], hexstring[2], hexstring[2]])
171 elif len(hexstring) != 6:
172 raise ValueError("3 or 6 digit hex number expected (with optional leading hash character)")
173 return rgb(*[value/255.0 for value in struct.unpack("BBB", binascii.a2b_hex(hexstring))])
175 rgb.red = rgb(1, 0, 0)
176 rgb.green = rgb(0, 1, 0)
177 rgb.blue = rgb(0, 0, 1)
178 rgb.white = rgb(1, 1, 1)
179 rgb.black = rgb(0, 0, 0)
182 class hsb(color):
184 """hsb colors"""
186 def __init__(self, h=0.0, s=0.0, b=0.0):
187 super().__init__()
188 if h<0 or h>1:
189 raise ValueError("Value h out of range [0,1]")
190 if s<0 or s>1:
191 raise ValueError("Value s out of range [0,1]")
192 if b<0 or b>1:
193 raise ValueError("Value b out of range [0,1]")
194 self.h = h
195 self.s = s
196 self.b = b
198 def processPS(self, file, writer, context, registry):
199 file.write("%f %f %f sethsbcolor\n" % (self.h, self.s, self.b))
201 def processPDF(self, file, writer, context, registry):
202 self.rgb().processPDF(file, writer, context, registry)
204 def cmyk(self):
205 return self.rgb().cmyk()
207 def gray(self):
208 return self.rgb().gray()
209 grey = gray
211 def hsb(self):
212 return hsb(self.h, self.s, self.b)
214 def rgb(self):
215 r, g, b = colorsys.hsv_to_rgb(self.h, self.s, self.b)
216 return rgb(r, g, b)
218 def colorspacestring(self):
219 raise RuntimeError("colorspace string not available for hsb colors")
222 class cmyk(color):
224 """cmyk colors"""
226 def __init__(self, c=0.0, m=0.0, y=0.0, k=0.0):
227 super().__init__()
228 if c<0 or c>1:
229 raise ValueError("Value c out of range [0,1]")
230 if m<0 or m>1:
231 raise ValueError("Value m out of range [0,1]")
232 if y<0 or y>1:
233 raise ValueError("Value y out of range [0,1]")
234 if k<0 or k>1:
235 raise ValueError("Value k out of range [0,1]")
236 self.c = c
237 self.m = m
238 self.y = y
239 self.k = k
241 def processPS(self, file, writer, context, registry):
242 file.write("%f %f %f %f setcmykcolor\n" % (self.c, self.m, self.y, self.k))
244 def processPDF(self, file, writer, context, registry):
245 if context.strokeattr:
246 file.write("%f %f %f %f K\n" % (self.c, self.m, self.y, self.k))
247 if context.fillattr:
248 file.write("%f %f %f %f k\n" % (self.c, self.m, self.y, self.k))
250 def cmyk(self):
251 return cmyk(self.c, self.m, self.y, self.k)
253 def gray(self):
254 return gray(1 - min([1, 0.3*self.c + 0.59*self.m + 0.11*self.y + self.k]))
255 grey = gray
257 def hsb(self):
258 return self.rgb().hsb()
260 def rgb(self):
261 # conversion to cmy:
262 c = min(1, self.c + self.k)
263 m = min(1, self.m + self.k)
264 y = min(1, self.y + self.k)
265 # conversion from cmy to rgb:
266 return rgb(1 - c, 1 - m, 1 - y)
268 def colorspacestring(self):
269 return "/DeviceCMYK"
271 def to8bitbytes(self):
272 return struct.pack("BBBB", int(self.c*255), int(self.m*255), int(self.y*255), int(self.k*255))
274 cmyk.GreenYellow = cmyk(0.15, 0, 0.69, 0)
275 cmyk.Yellow = cmyk(0, 0, 1, 0)
276 cmyk.Goldenrod = cmyk(0, 0.10, 0.84, 0)
277 cmyk.Dandelion = cmyk(0, 0.29, 0.84, 0)
278 cmyk.Apricot = cmyk(0, 0.32, 0.52, 0)
279 cmyk.Peach = cmyk(0, 0.50, 0.70, 0)
280 cmyk.Melon = cmyk(0, 0.46, 0.50, 0)
281 cmyk.YellowOrange = cmyk(0, 0.42, 1, 0)
282 cmyk.Orange = cmyk(0, 0.61, 0.87, 0)
283 cmyk.BurntOrange = cmyk(0, 0.51, 1, 0)
284 cmyk.Bittersweet = cmyk(0, 0.75, 1, 0.24)
285 cmyk.RedOrange = cmyk(0, 0.77, 0.87, 0)
286 cmyk.Mahogany = cmyk(0, 0.85, 0.87, 0.35)
287 cmyk.Maroon = cmyk(0, 0.87, 0.68, 0.32)
288 cmyk.BrickRed = cmyk(0, 0.89, 0.94, 0.28)
289 cmyk.Red = cmyk(0, 1, 1, 0)
290 cmyk.OrangeRed = cmyk(0, 1, 0.50, 0)
291 cmyk.RubineRed = cmyk(0, 1, 0.13, 0)
292 cmyk.WildStrawberry = cmyk(0, 0.96, 0.39, 0)
293 cmyk.Salmon = cmyk(0, 0.53, 0.38, 0)
294 cmyk.CarnationPink = cmyk(0, 0.63, 0, 0)
295 cmyk.Magenta = cmyk(0, 1, 0, 0)
296 cmyk.VioletRed = cmyk(0, 0.81, 0, 0)
297 cmyk.Rhodamine = cmyk(0, 0.82, 0, 0)
298 cmyk.Mulberry = cmyk(0.34, 0.90, 0, 0.02)
299 cmyk.RedViolet = cmyk(0.07, 0.90, 0, 0.34)
300 cmyk.Fuchsia = cmyk(0.47, 0.91, 0, 0.08)
301 cmyk.Lavender = cmyk(0, 0.48, 0, 0)
302 cmyk.Thistle = cmyk(0.12, 0.59, 0, 0)
303 cmyk.Orchid = cmyk(0.32, 0.64, 0, 0)
304 cmyk.DarkOrchid = cmyk(0.40, 0.80, 0.20, 0)
305 cmyk.Purple = cmyk(0.45, 0.86, 0, 0)
306 cmyk.Plum = cmyk(0.50, 1, 0, 0)
307 cmyk.Violet = cmyk(0.79, 0.88, 0, 0)
308 cmyk.RoyalPurple = cmyk(0.75, 0.90, 0, 0)
309 cmyk.BlueViolet = cmyk(0.86, 0.91, 0, 0.04)
310 cmyk.Periwinkle = cmyk(0.57, 0.55, 0, 0)
311 cmyk.CadetBlue = cmyk(0.62, 0.57, 0.23, 0)
312 cmyk.CornflowerBlue = cmyk(0.65, 0.13, 0, 0)
313 cmyk.MidnightBlue = cmyk(0.98, 0.13, 0, 0.43)
314 cmyk.NavyBlue = cmyk(0.94, 0.54, 0, 0)
315 cmyk.RoyalBlue = cmyk(1, 0.50, 0, 0)
316 cmyk.Blue = cmyk(1, 1, 0, 0)
317 cmyk.Cerulean = cmyk(0.94, 0.11, 0, 0)
318 cmyk.Cyan = cmyk(1, 0, 0, 0)
319 cmyk.ProcessBlue = cmyk(0.96, 0, 0, 0)
320 cmyk.SkyBlue = cmyk(0.62, 0, 0.12, 0)
321 cmyk.Turquoise = cmyk(0.85, 0, 0.20, 0)
322 cmyk.TealBlue = cmyk(0.86, 0, 0.34, 0.02)
323 cmyk.Aquamarine = cmyk(0.82, 0, 0.30, 0)
324 cmyk.BlueGreen = cmyk(0.85, 0, 0.33, 0)
325 cmyk.Emerald = cmyk(1, 0, 0.50, 0)
326 cmyk.JungleGreen = cmyk(0.99, 0, 0.52, 0)
327 cmyk.SeaGreen = cmyk(0.69, 0, 0.50, 0)
328 cmyk.Green = cmyk(1, 0, 1, 0)
329 cmyk.ForestGreen = cmyk(0.91, 0, 0.88, 0.12)
330 cmyk.PineGreen = cmyk(0.92, 0, 0.59, 0.25)
331 cmyk.LimeGreen = cmyk(0.50, 0, 1, 0)
332 cmyk.YellowGreen = cmyk(0.44, 0, 0.74, 0)
333 cmyk.SpringGreen = cmyk(0.26, 0, 0.76, 0)
334 cmyk.OliveGreen = cmyk(0.64, 0, 0.95, 0.40)
335 cmyk.RawSienna = cmyk(0, 0.72, 1, 0.45)
336 cmyk.Sepia = cmyk(0, 0.83, 1, 0.70)
337 cmyk.Brown = cmyk(0, 0.81, 1, 0.60)
338 cmyk.Tan = cmyk(0.14, 0.42, 0.56, 0)
339 cmyk.Gray = cmyk(0, 0, 0, 0.50)
340 cmyk.Grey = cmyk.Gray
341 cmyk.Black = cmyk(0, 0, 0, 1)
342 cmyk.White = cmyk(0, 0, 0, 0)
343 cmyk.white = cmyk.White
344 cmyk.black = cmyk.Black
347 class palette(attr.changelist):
348 """color palettes
350 A color palette is a discrete, ordered list of colors"""
352 palette.clear = attr.clearclass(palette)
355 # gradients
358 class gradient(attr.changeattr):
360 """base class for color gradients
362 A gradient is a continuous collection of colors with a single parameter ranging from 0 to 1
363 to address them"""
365 def getcolor(self, param):
366 """return color corresponding to param"""
367 pass
369 def select(self, index, n_indices):
370 """return a color corresponding to an index out of n_indices"""
371 if n_indices == 1:
372 param = 0
373 else:
374 param = index / (n_indices - 1.0)
375 return self.getcolor(param)
377 gradient.clear = attr.clearclass(gradient)
380 # gradient with arbitrary non-linear dependency
383 class functiongradient_gray(gradient):
385 """arbitrary non-linear gradients of gray colors
387 f_gray: a function mapping [0,1] to the gray value
390 def __init__(self, f_gray):
391 super().__init__()
392 self.f_gray = f_gray
394 def getcolor(self, param):
395 return gray(self.f_gray(param))
398 class functiongradient_cmyk(gradient):
400 """arbitrary non-linear gradients of cmyk colors
402 f_c: a function mapping [0,1] to the c component
403 f_m: a function mapping [0,1] to the m component
404 f_y: a function mapping [0,1] to the y component
405 f_k: a function mapping [0,1] to the k component
408 def __init__(self, f_c, f_m, f_y, f_k):
409 super().__init__()
410 self.f_c = f_c
411 self.f_m = f_m
412 self.f_y = f_y
413 self.f_k = f_k
415 def getcolor(self, param):
416 return cmyk(self.f_c(param), self.f_m(param), self.f_y(param), self.f_k(param))
419 class functiongradient_hsb(gradient):
421 """arbitrary non-linear gradients of hsb colors
423 f_h: a function mapping [0,1] to the h component
424 f_s: a function mapping [0,1] to the s component
425 f_b: a function mapping [0,1] to the b component
428 def __init__(self, f_h, f_s, f_b):
429 super().__init__()
430 self.f_h = f_h
431 self.f_s = f_s
432 self.f_b = f_b
434 def getcolor(self, param):
435 return hsb(self.f_h(param), self.f_s(param), self.f_b(param))
438 class functiongradient_rgb(gradient):
440 """arbitrary non-linear gradients of rgb colors
442 f_r: a function mapping [0,1] to the r component
443 f_g: a function mapping [0,1] to the b component
444 f_b: a function mapping [0,1] to the b component
447 def __init__(self, f_r, f_g, f_b):
448 super().__init__()
449 self.f_r = f_r
450 self.f_g = f_g
451 self.f_b = f_b
453 def getcolor(self, param):
454 return rgb(self.f_r(param), self.f_g(param), self.f_b(param))
457 # factory functions for gradients interpolating linearly between two colors
460 def lineargradient_cmyk(mincolor, maxcolor):
461 return functiongradient_cmyk(lambda x:maxcolor.c * x + mincolor.c * (1-x),
462 lambda x:maxcolor.m * x + mincolor.m * (1-x),
463 lambda x:maxcolor.y * x + mincolor.y * (1-x),
464 lambda x:maxcolor.k * x + mincolor.k * (1-x))
466 def lineargradient_gray(mincolor, maxcolor):
467 return functiongradient_gray(lambda x:maxcolor.gray * x + mincolor.gray * (1-x))
469 def lineargradient_hsb(mincolor, maxcolor):
470 return functiongradient_hsb(lambda x:maxcolor.h * x + mincolor.h * (1-x),
471 lambda x:maxcolor.s * x + mincolor.s * (1-x),
472 lambda x:maxcolor.b * x + mincolor.b * (1-x))
474 def lineargradient_rgb(mincolor, maxcolor):
475 return functiongradient_rgb(lambda x:maxcolor.r * x + mincolor.r * (1-x),
476 lambda x:maxcolor.g * x + mincolor.g * (1-x),
477 lambda x:maxcolor.b * x + mincolor.b * (1-x))
481 # gradients converted into other color spaces
484 class rgbgradient(gradient):
486 "a gradient, which takes another gradient and returns rgb colors"
488 def __init__(self, gradient):
489 super().__init__()
490 self.gradient = gradient
492 def getcolor(self, param):
493 return self.gradient.getcolor(param).rgb()
496 class cmykgradient(gradient):
498 "a gradient, which takes another gradient and returns cmyk colors"
500 def __init__(self, gradient):
501 super().__init__()
502 self.gradient = gradient
504 def getcolor(self, param):
505 return self.gradient.getcolor(param).cmyk()
508 gradient.Gray = lineargradient_gray(gray.white, gray.black)
509 gradient.Grey = gradient.Gray
510 gradient.ReverseGray = lineargradient_gray(gray.black, gray.white)
511 gradient.ReverseGrey = gradient.ReverseGray
512 gradient.BlackYellow = functiongradient_rgb( # compare this with reversegray above
513 f_r=lambda x: 2*x*(1-x)**5 + 3.5*x**2*(1-x)**3 + 2.1*x*x*(1-x)**2 + 3.0*x**3*(1-x)**2 + x**0.5*(1-(1-x)**2),
514 f_g=lambda x: 1.5*x**2*(1-x)**3 - 0.8*x**3*(1-x)**2 + 2.0*x**4*(1-x) + x**4,
515 f_b=lambda x: 5*x*(1-x)**5 - 0.5*x**2*(1-x)**3 + 0.3*x*x*(1-x)**2 + 5*x**3*(1-x)**2 + 0.5*x**6)
516 gradient.YellowBlack = functiongradient_rgb(
517 f_r=lambda x: 2*(1-x)*x**5 + 3.5*(1-x)**2*x**3 + 2.1*(1-x)*(1-x)*x**2 + 3.0*(1-x)**3*x**2 + (1-x)**0.5*(1-x**2),
518 f_g=lambda x: 1.5*(1-x)**2*x**3 - 0.8*(1-x)**3*x**2 + 2.0*(1-x)**4*x + (1-x)**4,
519 f_b=lambda x: 5*(1-x)*x**5 - 0.5*(1-x)**2*x**3 + 0.3*(1-x)*(1-x)*x**2 + 5*(1-x)**3*x**2 + 0.5*(1-x)**6)
520 gradient.RedGreen = lineargradient_rgb(rgb.red, rgb.green)
521 gradient.RedBlue = lineargradient_rgb(rgb.red, rgb.blue)
522 gradient.GreenRed = lineargradient_rgb(rgb.green, rgb.red)
523 gradient.GreenBlue = lineargradient_rgb(rgb.green, rgb.blue)
524 gradient.BlueRed = lineargradient_rgb(rgb.blue, rgb.red)
525 gradient.BlueGreen = lineargradient_rgb(rgb.blue, rgb.green)
526 gradient.RedBlack = lineargradient_rgb(rgb.red, rgb.black)
527 gradient.BlackRed = lineargradient_rgb(rgb.black, rgb.red)
528 gradient.RedWhite = lineargradient_rgb(rgb.red, rgb.white)
529 gradient.WhiteRed = lineargradient_rgb(rgb.white, rgb.red)
530 gradient.GreenBlack = lineargradient_rgb(rgb.green, rgb.black)
531 gradient.BlackGreen = lineargradient_rgb(rgb.black, rgb.green)
532 gradient.GreenWhite = lineargradient_rgb(rgb.green, rgb.white)
533 gradient.WhiteGreen = lineargradient_rgb(rgb.white, rgb.green)
534 gradient.BlueBlack = lineargradient_rgb(rgb.blue, rgb.black)
535 gradient.BlackBlue = lineargradient_rgb(rgb.black, rgb.blue)
536 gradient.BlueWhite = lineargradient_rgb(rgb.blue, rgb.white)
537 gradient.WhiteBlue = lineargradient_rgb(rgb.white, rgb.blue)
538 gradient.Rainbow = lineargradient_hsb(hsb(0, 1, 1), hsb(2.0/3.0, 1, 1))
539 gradient.ReverseRainbow = lineargradient_hsb(hsb(2.0/3.0, 1, 1), hsb(0, 1, 1))
540 gradient.Hue = lineargradient_hsb(hsb(0, 1, 1), hsb(1, 1, 1))
541 gradient.ReverseHue = lineargradient_hsb(hsb(1, 1, 1), hsb(0, 1, 1))
542 rgbgradient.Rainbow = rgbgradient(gradient.Rainbow)
543 rgbgradient.ReverseRainbow = rgbgradient(gradient.ReverseRainbow)
544 rgbgradient.Hue = rgbgradient(gradient.Hue)
545 rgbgradient.ReverseHue = rgbgradient(gradient.ReverseHue)
546 cmykgradient.Rainbow = cmykgradient(gradient.Rainbow)
547 cmykgradient.ReverseRainbow = cmykgradient(gradient.ReverseRainbow)
548 cmykgradient.Hue = cmykgradient(gradient.Hue)
549 cmykgradient.ReverseHue = cmykgradient(gradient.ReverseHue)
550 def jet_r(x):
551 if x < 0.38: return 0
552 elif x < 0.62: return (x-0.38)/(0.62-0.38)
553 elif x < 0.87: return 1
554 else: return 0.5 + 0.5*(1-x)/(1-0.87)
555 def jet_g(x):
556 if x < 0.13: return 0
557 elif x < 0.38: return (x-0.13)/(0.38-0.13)
558 elif x < 0.62: return 1
559 elif x < 0.87: return (0.87-x)/(0.87-0.62)
560 else: return 0
561 def jet_b(x):
562 if x < 0.13: return 0.5 + 0.5*x/0.13
563 elif x < 0.38: return 1
564 elif x < 0.62: return 1-(x-0.38)/(0.62-0.38)
565 else: return 0
566 gradient.Jet = functiongradient_rgb(f_r=jet_r, f_g=jet_g, f_b=jet_b)
567 gradient.ReverseJet = functiongradient_rgb(f_r=lambda x: jet_r(1-x), f_g=lambda x: jet_g(1-x), f_b=lambda x: jet_b(1-x))
568 cmykgradient.Jet = cmykgradient(gradient.Jet)
569 cmykgradient.ReverseJet = cmykgradient(gradient.ReverseJet)
573 class PDFextgstate(pdfwriter.PDFobject):
575 def __init__(self, name, extgstate, registry):
576 pdfwriter.PDFobject.__init__(self, "extgstate", name)
577 registry.addresource("ExtGState", name, self)
578 self.name = name
579 self.extgstate = extgstate
581 def write(self, file, writer, registry):
582 file.write("%s\n" % self.extgstate)
585 class transparency(attr.exclusiveattr, style.strokestyle, style.fillstyle):
587 def __init__(self, value):
588 self.value = 1-value
589 attr.exclusiveattr.__init__(self, transparency)
591 def processPS(self, file, writer, context, registry):
592 logger.warning("Transparency not available in PostScript, proprietary ghostscript extension code inserted.")
593 file.write("%f .setshapealpha\n" % self.value)
595 def processPDF(self, file, writer, context, registry):
596 if context.strokeattr and context.fillattr:
597 registry.add(PDFextgstate("Transparency-%f" % self.value,
598 "<< /Type /ExtGState /CA %f /ca %f >>" % (self.value, self.value), registry))
599 file.write("/Transparency-%f gs\n" % self.value)
600 elif context.strokeattr:
601 registry.add(PDFextgstate("Transparency-Stroke-%f" % self.value,
602 "<< /Type /ExtGState /CA %f >>" % self.value, registry))
603 file.write("/Transparency-Stroke-%f gs\n" % self.value)
604 elif context.fillattr:
605 registry.add(PDFextgstate("Transparency-Fill-%f" % self.value,
606 "<< /Type /ExtGState /ca %f >>" % self.value, registry))
607 file.write("/Transparency-Fill-%f gs\n" % self.value)