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 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
|
<!DOCTYPE HTML PUBLIC "-//Netscape_Microsoft//DTD HTML 3.0//EN">
<HTML>
<!-- This file generated using the Python HTMLgen module. -->
<HEAD>
<META NAME="GENERATOR" CONTENT="HTMLgen 1.1">
<TITLE>PmwColor.py</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<PRE>
<FONT COLOR="#DD0000"># Functions for converting colors and modifying the color scheme of</FONT>
<FONT COLOR="#DD0000"># an application.</FONT>
import math
import string
import sys
import Tkinter
_PI = math.pi
_TWO_PI = _PI * 2
_THIRD_PI = _PI / 3
_SIXTH_PI = _PI / 6
<STRONG>def setscheme</STRONG>(root, background=None, **kw):
root = root._root()
palette = apply(_calcPalette, (root, background,), kw)
for option, value in palette.items():
root.option_add(<FONT COLOR="#009900">'*'</FONT> + option, value, <FONT COLOR="#009900">'widgetDefault'</FONT>)
<STRONG>def getdefaultpalette</STRONG>(root):
<FONT COLOR="#DD0000"># Return the default values of all options, using the defaults</FONT>
<FONT COLOR="#DD0000"># from a few widgets.</FONT>
ckbtn = Tkinter.Checkbutton(root)
entry = Tkinter.Entry(root)
scbar = Tkinter.Scrollbar(root)
orig = {}
orig[<FONT COLOR="#009900">'activeBackground'</FONT>] = ckbtn.configure(<FONT COLOR="#009900">'activebackground'</FONT>)[4]
orig[<FONT COLOR="#009900">'activeForeground'</FONT>] = ckbtn.configure(<FONT COLOR="#009900">'activeforeground'</FONT>)[4]
orig[<FONT COLOR="#009900">'background'</FONT>] = ckbtn.configure(<FONT COLOR="#009900">'background'</FONT>)[4]
orig[<FONT COLOR="#009900">'disabledForeground'</FONT>] = ckbtn.configure(<FONT COLOR="#009900">'disabledforeground'</FONT>)[4]
orig[<FONT COLOR="#009900">'foreground'</FONT>] = ckbtn.configure(<FONT COLOR="#009900">'foreground'</FONT>)[4]
orig[<FONT COLOR="#009900">'highlightBackground'</FONT>] = ckbtn.configure(<FONT COLOR="#009900">'highlightbackground'</FONT>)[4]
orig[<FONT COLOR="#009900">'highlightColor'</FONT>] = ckbtn.configure(<FONT COLOR="#009900">'highlightcolor'</FONT>)[4]
orig[<FONT COLOR="#009900">'insertBackground'</FONT>] = entry.configure(<FONT COLOR="#009900">'insertbackground'</FONT>)[4]
orig[<FONT COLOR="#009900">'selectColor'</FONT>] = ckbtn.configure(<FONT COLOR="#009900">'selectcolor'</FONT>)[4]
orig[<FONT COLOR="#009900">'selectBackground'</FONT>] = entry.configure(<FONT COLOR="#009900">'selectbackground'</FONT>)[4]
orig[<FONT COLOR="#009900">'selectForeground'</FONT>] = entry.configure(<FONT COLOR="#009900">'selectforeground'</FONT>)[4]
orig[<FONT COLOR="#009900">'troughColor'</FONT>] = scbar.configure(<FONT COLOR="#009900">'troughcolor'</FONT>)[4]
ckbtn.destroy()
entry.destroy()
scbar.destroy()
return orig
<FONT COLOR="#DD0000">#======================================================================</FONT>
<FONT COLOR="#DD0000"># Functions dealing with brightness, hue, saturation and intensity of colors.</FONT>
<STRONG>def changebrightness</STRONG>(root, colorName, brightness):
<FONT COLOR="#DD0000"># Convert the color name into its hue and back into a color of the</FONT>
<FONT COLOR="#DD0000"># required brightness.</FONT>
if colorName[0] == <FONT COLOR="#009900">'#'</FONT>:
<FONT COLOR="#DD0000"># Extract rgb information from the color name itself, assuming</FONT>
<FONT COLOR="#DD0000"># it is either #rgb, #rrggbb, #rrrgggbbb, or #rrrrggggbbbb</FONT>
<FONT COLOR="#DD0000"># This is useful, since tk may return incorrect rgb values if</FONT>
<FONT COLOR="#DD0000"># the colormap is full - it will return the rbg values of the</FONT>
<FONT COLOR="#DD0000"># closest color available.</FONT>
colorName = colorName[1:]
digits = len(colorName) / 3
factor = 16 ** (4 - digits)
rgb = (
string.atoi(colorName[0:digits], 16) * factor,
string.atoi(colorName[digits:digits * 2], 16) * factor,
string.atoi(colorName[digits * 2:digits * 3], 16) * factor,
)
else:
<FONT COLOR="#DD0000"># We have no choice but to ask Tk what the rgb values are.</FONT>
rgb = root.winfo_rgb(colorName)
f = float(256 * 256 - 1) <FONT COLOR="#DD0000"># max size of rgb values returned from Tk</FONT>
rgb = (rgb[0] / f, rgb[1] / f, rgb[2] / f)
hue, saturation, intensity = rgb2hsi(rgb)
if saturation == 0.0:
hue = None
return hue2name(hue, brightness)
<STRONG>def hue2name</STRONG>(hue, brightness = None):
<FONT COLOR="#DD0000"># Convert the requested hue and brightness into a color name. If</FONT>
<FONT COLOR="#DD0000"># hue is None, return a grey of the requested brightness.</FONT>
if hue is None:
h = 0.0
s = 0.0
rgb = hsi2rgb(0.0, 0.0, brightness)
else:
while hue < 0:
hue = hue + _TWO_PI
while hue >= _TWO_PI:
hue = hue - _TWO_PI
h = hue
rgb = hsi2rgb(h, 1.0, 1.0)
if brightness is not None:
b = rgb2brightness(rgb)
i = 1.0 - (1.0 - brightness) * b
s = hsi2saturation(brightness, h, i)
rgb = hsi2rgb(h, s, i)
return rgb2name(rgb)
<STRONG>def hsi2saturation</STRONG>(brightness, h, i):
if h >= _TWO_PI:
h = h - _TWO_PI
h = h / _THIRD_PI
f = h - math.floor(h)
pp = i
pq = i * f
pt = i - i * f
pv = 0
h = int(h)
if h == 0: rgb = (pv, pt, pp)
elif h == 1: rgb = (pq, pv, pp)
elif h == 2: rgb = (pp, pv, pt)
elif h == 3: rgb = (pp, pq, pv)
elif h == 4: rgb = (pt, pp, pv)
elif h == 5: rgb = (pv, pp, pq)
return (i - brightness) / rgb2brightness(rgb)
<STRONG>def hsi2rgb</STRONG>(hue, saturation, intensity):
i = intensity
if saturation == 0:
rgb = [i, i, i]
else:
hue = hue / _THIRD_PI
f = hue - math.floor(hue)
p = i * (1.0 - saturation)
q = i * (1.0 - saturation * f)
t = i * (1.0 - saturation * (1.0 - f))
hue = int(hue)
if hue == 0: rgb = [i, t, p]
elif hue == 1: rgb = [q, i, p]
elif hue == 2: rgb = [p, i, t]
elif hue == 3: rgb = [p, q, i]
elif hue == 4: rgb = [t, p, i]
elif hue == 5: rgb = [i, p, q]
for index in range(3):
val = rgb[index]
if val < 0.0:
val = 0.0
if val > 1.0:
val = 1.0
rgb[index] = val
return rgb
<STRONG>def average</STRONG>(col1, col2, fraction):
return (
col2[0] * fraction + col1[0] * (1.0 - fraction),
col2[1] * fraction + col1[1] * (1.0 - fraction),
col2[2] * fraction + col1[2] * (1.0 - fraction)
)
<STRONG>def rgb2name</STRONG>(rgb):
return <FONT COLOR="#009900">'#%02x%02x%02x'</FONT> % \
(int(rgb[0] * 255), int(rgb[1] * 255), int(rgb[2] * 255))
<STRONG>def rgb2brightness</STRONG>(rgb):
rf = 0.299
gf = 0.587
bf = 0.114
return rf * rgb[0] + gf * rgb[1] + bf * rgb[2]
<STRONG>def rgb2hsi</STRONG>(rgb):
max = 0.0
min = 1.0
for index in range(3):
if rgb[index] > max:
max = rgb[index]
if rgb[index] < min:
min = rgb[index]
intensity = max
if max != 0:
saturation = (max - min) / max
else:
saturation = 0.0
hue = 0.0
if saturation != 0.0:
c = []
for index in range(3):
c.append((max - rgb[index]) / (max - min))
if rgb[0] == max:
hue = c[2] - c[1]
elif rgb[1] == max:
hue = 2 + c[0] - c[2]
elif rgb[2] == max:
hue = 4 + c[1] - c[0]
hue = hue * _THIRD_PI
if hue < 0.0:
hue = hue + _TWO_PI
return (hue, saturation, intensity)
<STRONG>def _calcPalette</STRONG>(root, background=None, **kw):
<FONT COLOR="#DD0000"># Create a map that has the complete new palette. If some colors</FONT>
<FONT COLOR="#DD0000"># aren't specified, compute them from other colors that are specified.</FONT>
new = {}
for key, value in kw.items():
new[key] = value
if background is not None:
new[<FONT COLOR="#009900">'background'</FONT>] = background
if not new.has_key(<FONT COLOR="#009900">'background'</FONT>):
raise ValueError, <FONT COLOR="#009900">'must specify a background color'</FONT>
if not new.has_key(<FONT COLOR="#009900">'foreground'</FONT>):
new[<FONT COLOR="#009900">'foreground'</FONT>] = <FONT COLOR="#009900">'black'</FONT>
f = float(256 * 256 - 1) <FONT COLOR="#DD0000"># max size of rgb values returned from Tk</FONT>
bg = root.winfo_rgb(new[<FONT COLOR="#009900">'background'</FONT>])
bg = (bg[0] / f, bg[1] / f, bg[2] / f)
fg = root.winfo_rgb(new[<FONT COLOR="#009900">'foreground'</FONT>])
fg = (fg[0] / f, fg[1] / f, fg[2] / f)
for i in (<FONT COLOR="#009900">'activeForeground'</FONT>, <FONT COLOR="#009900">'insertBackground'</FONT>, <FONT COLOR="#009900">'selectForeground'</FONT>,
<FONT COLOR="#009900">'highlightColor'</FONT>):
if not new.has_key(i):
new[i] = new[<FONT COLOR="#009900">'foreground'</FONT>]
if not new.has_key(<FONT COLOR="#009900">'disabledForeground'</FONT>):
newCol = average(bg, fg, 0.3)
new[<FONT COLOR="#009900">'disabledForeground'</FONT>] = rgb2name(newCol)
if not new.has_key(<FONT COLOR="#009900">'highlightBackground'</FONT>):
new[<FONT COLOR="#009900">'highlightBackground'</FONT>] = new[<FONT COLOR="#009900">'background'</FONT>]
<FONT COLOR="#DD0000"># Set <lighterBg> to a color that is a little lighter that the</FONT>
<FONT COLOR="#DD0000"># normal background. To do this, round each color component up by</FONT>
<FONT COLOR="#DD0000"># 9% or 1/3 of the way to full white, whichever is greater.</FONT>
lighterBg = []
for i in range(3):
lighterBg.append(bg[i])
inc1 = lighterBg[i] * 0.09
inc2 = (1.0 - lighterBg[i]) / 3
if inc1 > inc2:
lighterBg[i] = lighterBg[i] + inc1
else:
lighterBg[i] = lighterBg[i] + inc2
if lighterBg[i] > 1.0:
lighterBg[i] = 1.0
<FONT COLOR="#DD0000"># Set <darkerBg> to a color that is a little darker that the</FONT>
<FONT COLOR="#DD0000"># normal background.</FONT>
darkerBg = (bg[0] * 0.9, bg[1] * 0.9, bg[2] * 0.9)
if not new.has_key(<FONT COLOR="#009900">'activeBackground'</FONT>):
<FONT COLOR="#DD0000"># If the foreground is dark, pick a light active background.</FONT>
<FONT COLOR="#DD0000"># If the foreground is light, pick a dark active background.</FONT>
<FONT COLOR="#DD0000"># XXX This has been disabled, since it does not look very</FONT>
<FONT COLOR="#DD0000"># good with dark backgrounds. If this is ever fixed, the</FONT>
<FONT COLOR="#DD0000"># selectBackground and troughColor options should also be fixed.</FONT>
if rgb2brightness(fg) < 0.5:
new[<FONT COLOR="#009900">'activeBackground'</FONT>] = rgb2name(lighterBg)
else:
new[<FONT COLOR="#009900">'activeBackground'</FONT>] = rgb2name(lighterBg)
if not new.has_key(<FONT COLOR="#009900">'selectBackground'</FONT>):
new[<FONT COLOR="#009900">'selectBackground'</FONT>] = rgb2name(darkerBg)
if not new.has_key(<FONT COLOR="#009900">'troughColor'</FONT>):
new[<FONT COLOR="#009900">'troughColor'</FONT>] = rgb2name(darkerBg)
if not new.has_key(<FONT COLOR="#009900">'selectColor'</FONT>):
new[<FONT COLOR="#009900">'selectColor'</FONT>] = <FONT COLOR="#009900">'yellow'</FONT>
return new
<STRONG>def spectrum</STRONG>(numColors, correction = 1.0, saturation = 1.0, intensity = 1.0,
extraOrange = 1, returnHues = 0):
colorList = []
division = numColors / 7.0
for index in range(numColors):
if extraOrange:
if index < 2 * division:
hue = index / division
else:
hue = 2 + 2 * (index - 2 * division) / division
hue = hue * _SIXTH_PI
else:
hue = index * _TWO_PI / numColors
if returnHues:
colorList.append(hue)
else:
rgb = hsi2rgb(hue, saturation, intensity)
if correction != 1.0:
rgb = correct(rgb, correction)
name = rgb2name(rgb)
colorList.append(name)
return colorList
<STRONG>def correct</STRONG>(rgb, correction):
correction = float(correction)
rtn = []
for index in range(3):
rtn.append((1 - (1 - rgb[index]) ** correction) ** (1 / correction))
return rtn
<FONT COLOR="#DD0000">#==============================================================================</FONT>
<STRONG>def _recolorTree</STRONG>(widget, oldpalette, newcolors):
<FONT COLOR="#DD0000"># Change the colors in a widget and its descendants.</FONT>
<FONT COLOR="#DD0000"># Change the colors in <widget> and all of its descendants,</FONT>
<FONT COLOR="#DD0000"># according to the <newcolors> dictionary. It only modifies</FONT>
<FONT COLOR="#DD0000"># colors that have their default values as specified by the</FONT>
<FONT COLOR="#DD0000"># <oldpalette> variable. The keys of the <newcolors> dictionary</FONT>
<FONT COLOR="#DD0000"># are named after widget configuration options and the values are</FONT>
<FONT COLOR="#DD0000"># the new value for that option.</FONT>
for dbOption in newcolors.keys():
option = string.lower(dbOption)
try:
value = widget.cget(option)
except:
sys.exc_traceback = None <FONT COLOR="#DD0000"># Clean up object references</FONT>
continue
if oldpalette is None or value == oldpalette[dbOption]:
apply(widget.configure, (), {option : newcolors[dbOption]})
for child in widget.winfo_children():
_recolorTree(child, oldpalette, newcolors)
<STRONG>def changecolor</STRONG>(widget, background=None, **kw):
root = widget._root()
if not hasattr(widget, <FONT COLOR="#009900">'_Pmw_oldpalette'</FONT>):
widget._Pmw_oldpalette = getdefaultpalette(root)
newpalette = apply(_calcPalette, (root, background,), kw)
_recolorTree(widget, widget._Pmw_oldpalette, newpalette)
widget._Pmw_oldpalette = newpalette
</PRE>
</BODY> </HTML>
|