&lt;html&gt;&lt;head&gt;
    &lt;style&gt;
        td { 
            font-size: 12px;
            font-family: monospace;
            text-align: center;
            padding-left: 3px;
            padding-right: 3px
        }
        td.bright { color: #eee; }
        td.dark   { color: #222; }
    &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;table&gt;
&lt;?
# we create a 9x9 table selecting a different background for each cell

for {set i 0} { $i &lt; 9 } {incr i} {
    puts &quot;&lt;tr&gt;&quot;
    for {set j 0} {$j &lt; 9} {incr j} {

        set r [expr int(255 * ($i + $j) / 16)] 
        set g [expr int(255 * (8 + $j - $i) / 16)]
        set b [expr int(255 * (abs (4 - $i) + abs(4 - $j)) / 8)]

# determining the background luminosity (YIQ space of NTSC) and choosing
# the foreground color accordingly in order maintain maximum contrast

        if { [expr ($r*0.29894)+($g*0.58704)+($b*0.11402)] &gt; 148.0} {
            set cssclass &quot;dark&quot;
        } else {
            set cssclass &quot;bright&quot;
        }

        set cell [format &quot;%3d %3d %3d&quot; $r $g $b]

        puts [format \
            &quot;&lt;td bgcolor=\&quot;%02x%02x%02x\&quot; class=\&quot;%s\&quot;&gt;%s&lt;/td&gt;&quot; \
                                            $r $g $b $cssclass $cell]
    }
    puts &quot;&lt;/tr&gt;&quot;
}
?&gt;
    &lt;/table&gt;
&lt;/body&gt;&lt;/html&gt;
