colors

From Tcl/Tk 8.6 on, Tk uses Web colours instead of X11 ones, where they conflict.


Along with the color values available for syntactic use in Tcl scripts, several other facts about color might interest programmers. This page aims to collect all this sort of information.


Okay, here's the standard (and rather reliably portable) 6x6x6 color cube. This page has been around forever:

   http://the-light.com/colclick.html

The source file which realizes the description which appears in the hyperlink at the top of this page is xlib/xcolor.c. X11 (Unix) systems often have a showrgb command and/or .../lib/X11/rgb.txt which give the same (?) data. Programmatic access to the English-to-RGB-code map is available as

  winfo rgb . $color

RS has this wrapper to format the returned list of three decimals to the #RRRRGGGGBBBB form (all in hex) used in color input:

 proc colorhash color {eval format #%04X%04X%04X [winfo rgb . $color]}
 % colorhash red
 #FFFF00000000
 % colorhash green
 #0000FFFF0000
 % colorhash blue
 #00000000FFFF
 % colorhash white
 #FFFFFFFFFFFF
 % colorhash black
 #000000000000
 % colorhash grey50
 #7F7F7F7F7F7F

"Color pickers" answers common questions on this subject.


The W3C maintains a list [L1 ] of "Web-safe" colors.

KPV - here's that list, along with the closest named Tk color:

   W3C NAME    RGB        Closest TK color (~ means not an exact match)
    black     #000000   =>   black
    silver    #C0C0C0   =>   ~gray75 (or SystemButtonFace on classic Windows)
    gray      #808080   =>   ~gray50
    white     #FFFFFF   =>   white
    maroon    #800000   =>   ~darkred
    red       #FF0000   =>   red
    purple    #800080   =>   ~DarkMagenta
    fuchsia   #FF00FF   =>   magenta
    green     #008000   =>   ~green4
    lime      #00FF00   =>   green
    olive     #808000   =>   ~Gold4
    yellow    #FFFF00   =>   yellow
    navy      #000080   =>   navy
    blue      #0000FF   =>   blue
    teal      #008080   =>   ~turquoise4
    aqua      #00FFFF   =>   cyan

NB. what TK calls green is actually lime for W3C

MGS 2003/03/21 - These are not the "web-safe" colors - they are the 16 colors defined in HTML 3.2 and 4.01 and correspond to the basic VGA set on PCs. The "browser-safe" colors are constructed from colors where red, green and blue are restricted to the values:

   RGB  00  51 102 153 204 255
   HEX  00  33  66  99  CC  FF

... which gives 6 * 6 * 6 = 216 different colors.


xcolors is an interesting command under X11.


International Color Consortium maintains a page [L2 ] which has information on ICC color profiles for computer color matching.

Mildly related to this is "Selecting visually different RGB colors".


Percent to color: The following routine produces a color from an integer between 0 and 100, where 0 is red, 50 is yellow, and 100 is green (useful e.g. for painting progress bars):

 proc percent2rgb {n} {
    # map 0..100 to a red-yellow-green sequence
    set n     [expr {$n < 0? 0: $n > 100? 100: $n}]
    set red   [expr {$n > 75? 60 - ($n * 15 / 25) : 15}]
    set green [expr {$n < 50? $n * 15 / 50 : 15}]
    format    "#%01x%01x0" $red $green
 } ;# RS

"Color manipulation" [L3 ] is the name of an ASPN recipe.


Random color: EE presented this simple beauty in the Tcl chatroom on 2002-12-18:

 proc randomColor {} {format #%06x [expr {int(rand() * 0xFFFFFF)}]}

Tk syntax help - Arts and crafts of Tcl-Tk programming

[Discuss Death of the Websafe Color Palette?

        URL:
          http://hotwired.lycos.com/webmonkey/00/37/index2a.html?tw=design

later.]


Harmonic color wheel


EKB 1 May 2005

Here's code for a "browser-safe color picker." It's still possible that websafe is still useful (although see WebMonkey link above). So given that chance, here's the code if you need it:

    package require Iwidgets 4.0

    namespace eval bss {
       proc change {w rgb dir cmd} {
          set curr "0x[$w.$rgb get]"
          set new [expr $curr + $dir * 0x33]
          if {$new < 0x00} {set new 0xFF}
          if {$new > 0xFF} {set new 0x00}
          $w.$rgb delete 0 end
          $w.$rgb insert 0 [format %2.2X $new]
          set color "#[$w.r get][$w.g get][$w.b get]"
          setcolor $cmd $color
       }

       proc setcolor {cmd color} {
          # Use "%c" to represent the color
          regsub "%c" $cmd $color subcmd
          eval $subcmd
       }

       proc addspinner {w rgb cmd init} {
          iwidgets::spinner $w.$rgb -increment "bss::change $w $rgb 1 \{$cmd\}" \
                -decrement "bss::change  $w $rgb -1 \{$cmd\}" -width 2
          $w.$rgb insert 0 $init
          pack $w.$rgb -side left
       }

       proc new {w label cmd {color "#000000"}} {
          frame $w
          label $w.label -text "$label #"
          pack $w.label -side left
          addspinner $w r $cmd [string range $color 1 2]
          addspinner $w g $cmd [string range $color 3 4]
          addspinner $w b $cmd [string range $color 5 6]
          setcolor $cmd $color
       }
    }

    #####################################
    ##
    ## Implement
    ##
    #####################################
    frame .page
    frame .spinners
    pack .spinners -side right -fill y -expand no -anchor n
    pack .page -side left -fill both -expand yes

    ##------------------------
    ## Set up the page
    ##------------------------
    text .page.banner -font "Arial 16 bold" -height 1 -width 15 -borderwidth 0
    .page.banner insert end "Banner Title"
    pack .page.banner -side top -expand no -fill x

    text .page.body -font "Arial 10" -height 15 -width 40 -borderwidth 0
    .page.body tag configure link -font "Arial 10 bold"
    .page.body insert end "This is the body text. This is "
    .page.body insert end "a link" link
    .page.body insert end ". And this is more text."
    pack .page.body -side bottom -expand yes -fill both

    ##------------------------
    ## Set up the spinners
    ##------------------------

    #--Banner
    label .spinners.bannlbl -text "Banner" -font "Arial 14"
    pack .spinners.bannlbl -side top -anchor w -pady 4 -padx 2
    bss::new .spinners.bannbg "Background:" {.page.banner configure -bg %c} "#000000"
    bss::new .spinners.bannfg "Foreground:" {.page.banner configure -fg %c} "#FFFFFF"
    pack .spinners.bannbg -side top -anchor e -pady 2 -padx 2
    pack .spinners.bannfg -side top -anchor e -pady 2 -padx 2

    #--Body
    label .spinners.bodylbl -text "Body" -font "Arial 14"
    pack .spinners.bodylbl -side top -anchor w -pady 4 -padx 2
    bss::new .spinners.bodybg "Background:" {.page.body configure -bg %c} "#FFFFFF"
    bss::new .spinners.bodyfg "Foreground:" {.page.body configure -fg %c} "#000000"
    bss::new .spinners.bodylnk "Link:" {.page.body tag configure link -foreground %c} "#0000FF"
    pack .spinners.bodybg -side top -anchor e -pady 2 -padx 2
    pack .spinners.bodyfg -side top -anchor e -pady 2 -padx 2
    pack .spinners.bodylnk -side top -anchor e -pady 2 -padx 2

Here's what it looks like:

https://web.archive.org/web/20070208044917if_/http://www.kb-creative.net/screenshots/BrSafePkr.gif

Also see Web-Safe Colors


RS once suggested

    proc random_color {} {format #%06x [expr {int(rand()*0xFFFFFF)}]}

exercises a palette. - RS: No, it was EE (see higher up this page :)


APN Also see Colors package.


"Color theory for developers" [L4 ]


EKB My favorite web page for generating color schemes is the Wellstyled [ws] Color Scheme Generator 2 [L5 ].


MG One thing to beware of on Windows is that the platform specific named colours like SystemButtonFace don't seem to be updated for normal Tk widgets after the app is started. This wouldn't really be a problem - except Tile widgets do seem to update, so if you pack a ttk::frame into a regular frame and then change the Windows display settings, they'll end up being different colours, which looks very strange. (And, sadly, makes combining Tk and Ttk widgets that much more difficult.)