Welcome, guest | Sign In | My Account | Store | Cart

These functions, when given a magnitude 'mag' between cmin and cmax, return a colour tuple (red, green, blue) on a 0 to 255 scale. The tuple can consist of strings (strRgb) as required in Tk calls, or integers (rgb) as required in Java applications.

Python, 48 lines
 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
#!/usr/bin/env python
# 
"""
These functions, when given a magnitude mag between cmin and cmax, return
a colour tuple (red, green, blue). Light blue is cold (low magnitude)
and yellow is hot (high magnitude).

"""
import math

def floatRgb(mag, cmin, cmax):
       """
       Return a tuple of floats between 0 and 1 for the red, green and
       blue amplitudes.
       """

       try:
              # normalize to [0,1]
              x = float(mag-cmin)/float(cmax-cmin)
       except:
              # cmax = cmin
              x = 0.5
       blue = min((max((4*(0.75-x), 0.)), 1.))
       red  = min((max((4*(x-0.25), 0.)), 1.))
       green= min((max((4*math.fabs(x-0.5)-1., 0.)), 1.))
       return (red, green, blue)

def strRgb(mag, cmin, cmax):
       """
       Return a tuple of strings to be used in Tk plots.
       """

       red, green, blue = floatRgb(mag, cmin, cmax)       
       return "#%02x%02x%02x" % (red*255, green*255, blue*255)

def rgb(mag, cmin, cmax):
       """
       Return a tuple of integers to be used in AWT/Java plots.
       """

       red, green, blue = floatRgb(mag, cmin, cmax)
       return (int(red*255), int(green*255), int(blue*255))

def htmlRgb(mag, cmin, cmax):
       """
       Return a tuple of strings to be used in HTML documents.
       """
       return "#%02x%02x%02x"%rgb(mag, cmin, cmax)

These utility functions are widely used in my programs to create simple pseudo-colour graphics under Python-Tkinter and Jython-AWT. The colour map are linear functions of the three colours (red, green, blue) with saturation. Low magnitudes are associated with a light, cold blue colour, high magnitudes with a warm yellow shade.

3 comments

Nick Efford 20 years, 8 months ago  # | flag

Change the name of the 'map' function - to avoid confusion with the built-in function of the same name. Nick Efford

Alexander Pletzer (author) 20 years, 8 months ago  # | flag

Colormap. Following a comment, I changed the function name 'map' to 'strRgb' to avoid a clash with the built-in function. --Alex.

Kevin Dahlhausen 16 years, 10 months ago  # | flag

HTML color codes. Here is some code to return a html color string:

def htmlRgb(mag, cmin, cmax):
        return "#%02x%02x%02x"%rgb(mag, cmin, cmax)

Usage:

cmin = 0
cmax = 100
of = open("t.html", "w")
of.write( """<html > <body> <table>""")
for i in range(cmin, cmax):
    of.write( """<tr"><td bgcolor="%s">%d</td></tr>"""%( htmlRgb( i, cmin, cmax), i ) )
""" )
of.write("""</table> </body> </html>""")
Created by Alexander Pletzer on Fri, 16 Mar 2001 (PSF)
Python recipes (4591)
Alexander Pletzer's recipes (6)

Required Modules

Other Information and Tasks