File: colorcube.py

package info (click to toggle)
htmlgen 2.2.2-12.1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,840 kB
  • ctags: 1,480
  • sloc: python: 4,518; makefile: 93
file content (87 lines) | stat: -rw-r--r-- 3,509 bytes parent folder | download | duplicates (5)
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

"""Utility to generate a table of browser safe colors.
"""
__version__ = '$Id: colorcube.py,v 1.0 1998/03/19 18:57:56 friedric Exp $'
__author__ = "Robin Friedrich"
__date__ = "Feb. 18, 1998"

# colorcube.py
# COPYRIGHT (C) 1998  ROBIN FRIEDRICH  email:Robin.Friedrich@pdq.net
# Permission to  use, copy, modify, and distribute this software and its
# documentation  for  any  purpose  and  without fee  is hereby granted,
# provided that the above copyright notice appear in all copies and that
# both that copyright notice and this permission notice appear in
# supporting documentation.
# THE  AUTHOR  DISCLAIMS  ALL  WARRANTIES WITH  REGARD TO THIS SOFTWARE,
# INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
# EVENT  SHALL THE  AUTHOR  BE  LIABLE  FOR  ANY  SPECIAL,   INDIRECT OR
# CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
# USE, DATA OR PROFITS,  WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.

from HTMLgen import *

hexes = ('00','33','66','99','CC','FF')

def main(filename):
    # Create a fresh document
    doc = SimpleDocument(title='Web Safe Colors', bgcolor = '#FFFFFF')
    # Create an invisible table for layout
    bodytable = TableLite(cellpadding=5, border=0, html_escape="off")
    # Set up a table for the 216 color cube
    colortable = TableLite(cellpadding=0, border=0, cellspacing=1, html_escape="off")
    colortable.append(Caption("The 216 Web Color Cube"))
    for i in (0,1,2,3,4,5):
        for j in (0,1,2,3,4,5):
            tr = TR()
            for k in (0,1,2,3,4,5):
                tr.append(makecell("#%s%s%s" % (hexes[i], hexes[j], hexes[k])))
            colortable.append(tr)
    # append the table as a cell in the body table
    bodyrow = TR(TD(colortable))
    # Set up a table for the greyscale
    greytable = TableLite(cellpadding=0, border=0, cellspacing=1, html_escape="off")
    greytable.append(Caption("The 16 Grey Levels"))
    for a in '0123456789ABCDEF':
        greytable.append( TR( makecell("#%s%s%s%s%s%s" % ((a,)*6)) ) )
    bodyrow.append(TD(greytable))
    # Set up a table containing the pure colors
    puretable = TableLite(cellpadding=0, border=0, cellspacing=1, html_escape="off")
    puretable.append(Caption("The Pure Colors"))
    for a in '123456789ABCDEF':
        tr = TR()
        tr.append(makecell("#%s%s0000" % (a,a) ) )
        tr.append(makecell("#00%s%s00" % (a,a) ) )
        tr.append(makecell("#0000%s%s" % (a,a) ) )
        puretable.append(tr)
    bodyrow.append(TD(puretable))
    # Now attach the body row to the bodytable
    bodytable.append(bodyrow)
    # Attach the bodytable to the document and write it all out
    doc.append(bodytable)
    doc.write(filename)

def makecell(color):
    """Return a table cell object (TD) of the given color tag
    """
    cell = TD(bgcolor=color, align='center', height=26, width=60)
    cell.append(Font(color, color = contrast(color), size = -2))
    return cell

def contrast(color):
    """Compute luminous efficiency of given color and return either
    white or black based on which would contrast more.
    """
    # I know this is not exact; just my guess
    R = eval('0x'+color[1:3])
    G = eval('0x'+color[3:5])
    B = eval('0x'+color[5:7])
    lumen = 0.6*R + G + 0.3*B
    if lumen > 250:
        return "#000000"
    else:
        return "#FFFFFF"

if __name__ == '__main__':
    main('colorcube.html')