remove unused imports
[PyX/mjg.git] / pyx / color.py
blob33dec9ceaaa8ab4f01f75c50d6fd6b36917f7072
1 #!/usr/bin/env python
2 # -*- coding: ISO-8859-1 -*-
5 # Copyright (C) 2002-2004 Jörg Lehmann <joergl@users.sourceforge.net>
6 # Copyright (C) 2003-2004 Michael Schindler <m-schindler@users.sourceforge.net>
7 # Copyright (C) 2002-2004 André Wobst <wobsta@users.sourceforge.net>
9 # This file is part of PyX (http://pyx.sourceforge.net/).
11 # PyX is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2 of the License, or
14 # (at your option) any later version.
16 # PyX is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
21 # You should have received a copy of the GNU General Public License
22 # along with PyX; if not, write to the Free Software
23 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25 import colorsys
26 import attr, style
28 class color(attr.exclusiveattr, style.strokestyle, style.fillstyle):
30 """base class for all colors"""
32 def __init__(self):
33 attr.exclusiveattr.__init__(self, color)
36 clear = attr.clearclass(color)
39 class grey(color):
41 """grey tones"""
43 def __init__(self, gray):
44 color.__init__(self)
45 if gray<0 or gray>1: raise ValueError
46 self.color = {"gray": gray}
48 def outputPS(self, file, writer, context):
49 file.write("%(gray)g setgray\n" % self.color)
51 def outputPDF(self, file, writer, context):
52 if context.strokeattr:
53 file.write("%(gray)f G\n" % self.color)
54 if context.fillattr:
55 file.write("%(gray)f g\n" % self.color)
57 grey.black = grey(0.0)
58 grey.white = grey(1.0)
59 gray = grey
62 class rgb(color):
64 """rgb colors"""
66 def __init__(self, r=0.0, g=0.0, b=0.0):
67 color.__init__(self)
68 if r<0 or r>1 or g<0 or g>1 or b<0 or b>1: raise ValueError
69 self.color = {"r": r, "g": g, "b": b}
71 def outputPS(self, file, writer, context):
72 file.write("%(r)g %(g)g %(b)g setrgbcolor\n" % self.color)
74 def outputPDF(self, file, writer, context):
75 if context.strokeattr:
76 file.write("%(r)f %(g)f %(b)f RG\n" % self.color)
77 if context.fillattr:
78 file.write("%(r)f %(g)f %(b)f rg\n" % self.color)
80 rgb.red = rgb(1 ,0, 0)
81 rgb.green = rgb(0 ,1, 0)
82 rgb.blue = rgb(0 ,0, 1)
83 rgb.white = rgb(1 ,1, 1)
84 rgb.black = rgb(0 ,0, 0)
87 class hsb(color):
89 """hsb colors"""
91 def __init__(self, h=0.0, s=0.0, b=0.0):
92 color.__init__(self)
93 if h<0 or h>1 or s<0 or s>1 or b<0 or b>1: raise ValueError
94 self.color = {"h": h, "s": s, "b": b}
96 def outputPS(self, file, writer, context):
97 file.write("%(h)g %(s)g %(b)g sethsbcolor\n" % self.color)
99 def outputPDF(self, file, writer, context):
100 r, g, b = colorsys.hsv_to_rgb(self.color["h"], self.color["s"], self.color["b"])
101 rgb(r, g, b).outputPDF(file, writer, context)
104 class cmyk(color):
106 """cmyk colors"""
108 def __init__(self, c=0.0, m=0.0, y=0.0, k=0.0):
109 color.__init__(self)
110 if c<0 or c>1 or m<0 or m>1 or y<0 or y>1 or k<0 or k>1: raise ValueError
111 self.color = {"c": c, "m": m, "y": y, "k": k}
113 def outputPS(self, file, writer, context):
114 file.write("%(c)g %(m)g %(y)g %(k)g setcmykcolor\n" % self.color)
116 def outputPDF(self, file, writer, context):
117 if context.strokeattr:
118 file.write("%(c)f %(m)f %(y)f %(k)f K\n" % self.color)
119 if context.fillattr:
120 file.write("%(c)f %(m)f %(y)f %(k)f k\n" % self.color)
123 cmyk.GreenYellow = cmyk(0.15, 0, 0.69, 0)
124 cmyk.Yellow = cmyk(0, 0, 1, 0)
125 cmyk.Goldenrod = cmyk(0, 0.10, 0.84, 0)
126 cmyk.Dandelion = cmyk(0, 0.29, 0.84, 0)
127 cmyk.Apricot = cmyk(0, 0.32, 0.52, 0)
128 cmyk.Peach = cmyk(0, 0.50, 0.70, 0)
129 cmyk.Melon = cmyk(0, 0.46, 0.50, 0)
130 cmyk.YellowOrange = cmyk(0, 0.42, 1, 0)
131 cmyk.Orange = cmyk(0, 0.61, 0.87, 0)
132 cmyk.BurntOrange = cmyk(0, 0.51, 1, 0)
133 cmyk.Bittersweet = cmyk(0, 0.75, 1, 0.24)
134 cmyk.RedOrange = cmyk(0, 0.77, 0.87, 0)
135 cmyk.Mahogany = cmyk(0, 0.85, 0.87, 0.35)
136 cmyk.Maroon = cmyk(0, 0.87, 0.68, 0.32)
137 cmyk.BrickRed = cmyk(0, 0.89, 0.94, 0.28)
138 cmyk.Red = cmyk(0, 1, 1, 0)
139 cmyk.OrangeRed = cmyk(0, 1, 0.50, 0)
140 cmyk.RubineRed = cmyk(0, 1, 0.13, 0)
141 cmyk.WildStrawberry = cmyk(0, 0.96, 0.39, 0)
142 cmyk.Salmon = cmyk(0, 0.53, 0.38, 0)
143 cmyk.CarnationPink = cmyk(0, 0.63, 0, 0)
144 cmyk.Magenta = cmyk(0, 1, 0, 0)
145 cmyk.VioletRed = cmyk(0, 0.81, 0, 0)
146 cmyk.Rhodamine = cmyk(0, 0.82, 0, 0)
147 cmyk.Mulberry = cmyk(0.34, 0.90, 0, 0.02)
148 cmyk.RedViolet = cmyk(0.07, 0.90, 0, 0.34)
149 cmyk.Fuchsia = cmyk(0.47, 0.91, 0, 0.08)
150 cmyk.Lavender = cmyk(0, 0.48, 0, 0)
151 cmyk.Thistle = cmyk(0.12, 0.59, 0, 0)
152 cmyk.Orchid = cmyk(0.32, 0.64, 0, 0)
153 cmyk.DarkOrchid = cmyk(0.40, 0.80, 0.20, 0)
154 cmyk.Purple = cmyk(0.45, 0.86, 0, 0)
155 cmyk.Plum = cmyk(0.50, 1, 0, 0)
156 cmyk.Violet = cmyk(0.79, 0.88, 0, 0)
157 cmyk.RoyalPurple = cmyk(0.75, 0.90, 0, 0)
158 cmyk.BlueViolet = cmyk(0.86, 0.91, 0, 0.04)
159 cmyk.Periwinkle = cmyk(0.57, 0.55, 0, 0)
160 cmyk.CadetBlue = cmyk(0.62, 0.57, 0.23, 0)
161 cmyk.CornflowerBlue = cmyk(0.65, 0.13, 0, 0)
162 cmyk.MidnightBlue = cmyk(0.98, 0.13, 0, 0.43)
163 cmyk.NavyBlue = cmyk(0.94, 0.54, 0, 0)
164 cmyk.RoyalBlue = cmyk(1, 0.50, 0, 0)
165 cmyk.Blue = cmyk(1, 1, 0, 0)
166 cmyk.Cerulean = cmyk(0.94, 0.11, 0, 0)
167 cmyk.Cyan = cmyk(1, 0, 0, 0)
168 cmyk.ProcessBlue = cmyk(0.96, 0, 0, 0)
169 cmyk.SkyBlue = cmyk(0.62, 0, 0.12, 0)
170 cmyk.Turquoise = cmyk(0.85, 0, 0.20, 0)
171 cmyk.TealBlue = cmyk(0.86, 0, 0.34, 0.02)
172 cmyk.Aquamarine = cmyk(0.82, 0, 0.30, 0)
173 cmyk.BlueGreen = cmyk(0.85, 0, 0.33, 0)
174 cmyk.Emerald = cmyk(1, 0, 0.50, 0)
175 cmyk.JungleGreen = cmyk(0.99, 0, 0.52, 0)
176 cmyk.SeaGreen = cmyk(0.69, 0, 0.50, 0)
177 cmyk.Green = cmyk(1, 0, 1, 0)
178 cmyk.ForestGreen = cmyk(0.91, 0, 0.88, 0.12)
179 cmyk.PineGreen = cmyk(0.92, 0, 0.59, 0.25)
180 cmyk.LimeGreen = cmyk(0.50, 0, 1, 0)
181 cmyk.YellowGreen = cmyk(0.44, 0, 0.74, 0)
182 cmyk.SpringGreen = cmyk(0.26, 0, 0.76, 0)
183 cmyk.OliveGreen = cmyk(0.64, 0, 0.95, 0.40)
184 cmyk.RawSienna = cmyk(0, 0.72, 1, 0.45)
185 cmyk.Sepia = cmyk(0, 0.83, 1, 0.70)
186 cmyk.Brown = cmyk(0, 0.81, 1, 0.60)
187 cmyk.Tan = cmyk(0.14, 0.42, 0.56, 0)
188 cmyk.Gray = cmyk(0, 0, 0, 0.50)
189 cmyk.Grey = cmyk.Gray
190 cmyk.Black = cmyk(0, 0, 0, 1)
191 cmyk.White = cmyk(0, 0, 0, 0)
192 cmyk.white = cmyk.White
193 cmyk.black = cmyk.Black
196 class palette(color, attr.changeattr):
198 """palette is a collection of two colors for calculating transitions between them"""
200 def __init__(self, mincolor, maxcolor, min=0, max=1):
201 color.__init__(self)
202 if mincolor.__class__ != maxcolor.__class__:
203 raise ValueError
204 self.colorclass = mincolor.__class__
205 self.mincolor = mincolor
206 self.maxcolor = maxcolor
207 self.min = min
208 self.max = max
210 def getcolor(self, index):
211 color = {}
212 for key in self.mincolor.color.keys():
213 color[key] = ((index - self.min) * self.maxcolor.color[key] +
214 (self.max - index) * self.mincolor.color[key]) / float(self.max - self.min)
215 return self.colorclass(**color)
217 def select(self, index, total):
218 if total == 1:
219 return self.mincolor
220 return self.getcolor(index/(total-1.0))
222 def outputPS(self, file, writer, context):
223 self.getcolor(0).outputPS(file, writer, context)
225 def outputPDF(self, file, writer, context):
226 self.getcolor(0).outputPDF(file, writer, context)
229 palette.Gray = palette(gray.white, gray.black)
230 palette.Grey = palette.Gray
231 palette.ReverseGray = palette(gray.black, gray.white)
232 palette.ReverseGrey = palette.ReverseGray
233 palette.RedGreen = palette(rgb.red, rgb.green)
234 palette.RedBlue = palette(rgb.red, rgb.blue)
235 palette.GreenRed = palette(rgb.green, rgb.red)
236 palette.GreenBlue = palette(rgb.green, rgb.blue)
237 palette.BlueRed = palette(rgb.blue, rgb.red)
238 palette.BlueGreen = palette(rgb.blue, rgb.green)
239 palette.RedBlack = palette(rgb.red, rgb.black)
240 palette.BlackRed = palette(rgb.black, rgb.red)
241 palette.RedWhite = palette(rgb.red, rgb.white)
242 palette.WhiteRed = palette(rgb.white, rgb.red)
243 palette.GreenBlack = palette(rgb.green, rgb.black)
244 palette.BlackGreen = palette(rgb.black, rgb.green)
245 palette.GreenWhite = palette(rgb.green, rgb.white)
246 palette.WhiteGreen = palette(rgb.white, rgb.green)
247 palette.BlueBlack = palette(rgb.blue, rgb.black)
248 palette.BlackBlue = palette(rgb.black, rgb.blue)
249 palette.BlueWhite = palette(rgb.blue, rgb.white)
250 palette.WhiteBlue = palette(rgb.white, rgb.blue)
251 palette.Rainbow = palette(hsb(0, 1, 1), hsb(2.0/3.0, 1, 1))
252 palette.ReverseRainbow = palette(hsb(2.0/3.0, 1, 1), hsb(0, 1, 1))
253 palette.Hue = palette(hsb(0, 1, 1), hsb(1, 1, 1))
254 palette.ReverseHue = palette(hsb(1, 1, 1), hsb(0, 1, 1))