Updated 2016-01-14 08:05:32 by pooryorick

The built-in tk_chooseColor widget is the standard way to allow users to choose a color. There certainly are cases when one wants to present a different appearance to the same function, though. One color picker appears at http://www.tcl.tk/community/hobbs/tcl/capp/ .

D. McC: Nov 17 2005 - Does it really? I just looked, and I didn't see one. What is it called, and what is the link to it from the URL above?

CJL - On the referenced page, click 'Tcl Plugin Apps'.

Other Color Pickers  edit

ClrPick -A Colour Selection Megawidget
WISH Color Picker
FF colorpicker
colorsel, by arezey

Color Picker by [Steve Offut]  edit

Here's an old-time implementation that [Steve Offut] preserved:
#! /usr/local/bin/wish

proc setColor {tossit} {

    global redval grnval blueval collist hexval
   
    set hexval [format %02X%02X%02X $redval $grnval $blueval]
        .butfrm.visual.button conf \
        -bg #$hexval \
        -activebackground #$hexval
    set temp [expr [expr $blueval + $grnval] + $redval]
    set bming [expr abs([expr $grnval - $blueval])]
    set rminb [expr abs([expr $redval  - $grnval])]
    set gminr [expr abs([expr $grnval - $redval])]

    set sumodiff [expr $gminr + [expr $bming + $rminb]]
    if {$sumodiff < 350 && $temp < 400} {
    .butfrm.visual.button conf \
        -fg white \
        -activeforeground white   
    } else {
    .butfrm.visual.button conf \
        -fg black \
        -activeforeground black
    }
}

proc picker {} {
   
    global collist colnamelist redval grnval blueval
   
    frame .butfrm \
        -relief raised \
        -border 3 \
        -width 250 \
        -height 410
        frame .butfrm.sliders
        set temp .butfrm.sliders
        scale $temp.r \
        -label Red: \
        -from 0 \
        -to 255 \
        -showvalue 1 \
        -sliderrelief raised \
        -sliderlength 10 \
        -troughcolor red \
        -variable redval \
        -command {setColor} \
        -orient horizontal
   
    scale $temp.g \
        -label Green: \
        -from 0 \
        -to 255 \
        -showvalue 1 \
        -sliderrelief raised \
        -troughcolor green \
        -sliderlength 10 \
        -variable grnval \
        -command {setColor} \
        -orient horizontal
   
    scale $temp.b \
        -label Blue: \
        -from 0 \
        -to 255 \
        -showvalue 1 \
        -sliderrelief raised \
        -troughcolor blue \
        -sliderlength 10 \
        -variable blueval \
        -command {setColor} \
        -orient horizontal
   
    pack $temp.r $temp.g $temp.b -expand 1
    frame .butfrm.visual
    button .butfrm.visual.button \
        -bg black \
        -fg white \
        -text "Pick Your Color" \
        -height 6 \
        -width 10
    entry .butfrm.visual.ent \
        -width 7 \
        -textvariable hexval \
        -relief raised
        #-font courier14
   
   
    place $temp \
        -in .butfrm \
        -relx 0.0 \
        -rely 0.0 \
        -anchor nw
    pack .butfrm.visual.button \
        -anchor nw \
        -side top
    pack .butfrm.visual.ent \
        -anchor n \
        -side bottom
    place .butfrm.visual \
        -in .butfrm \
        -anchor nw \
        -relx 0.5\
        -y 35
   
   
   
    frame .butfrm.int \
        -relief raised \
        -border 3
    canvas .butfrm.int.scrolled \
        -yscrollcommand ".butfrm.int.scroller set" \
        -height 200 \
        -width 200 \
        -relief raised \
        -bg black
   
    scrollbar .butfrm.int.scroller \
        -command ".butfrm.int.scrolled yview" \
        -relief raised \
        -width 12
    pack .butfrm.int.scroller \
        -side right \
        -fill y \
        -anchor ne
    pack .butfrm.int.scrolled \
        -side left \
        -anchor nw \
        -expand 1
    set frmlist [list]
    set framer 0
    set frm .butfrm.int.scrolled
    set colnum [llength $collist]
    for {set i 0} {$i < $colnum} {incr i} {
        set tag [$frm create rect 4 [expr $framer * 30] 200 [
            expr [expr $framer * 30] + 30] \
            -fill #[lindex $collist $i] -width 2]
        scan [lindex $collist $i] "%02x%02x%02x" red green blue
       
        $frm bind $tag <ButtonPress-1> "set redval $red; \
            set grnval $green; \
            set blueval $blue; \
            setColor 0"
       
        set temp [expr $red + [expr $green + $blue]]
        set bming [expr abs([expr $green - $blue])]
        set rminb [expr abs([expr $red - $green])]
        set gminr [expr abs([expr $green - $red])]
       
        set sumodiff [expr $gminr + [expr $bming + $rminb]]
       
        if {$sumodiff < 350 && $temp < 400} {
            set tag [$frm create text 100 [expr [expr $framer *30] + 15] \
                -text [lindex $colnamelist $i] \
                -fill white]
        } else {
            set tag [$frm create text 100 [expr [expr $framer *30] + 15] \
                -text [lindex $colnamelist $i] \
                -fill black]
        }
        $frm bind $tag <ButtonPress-1> "set redval $red; \
            set grnval $green; \
            set blueval $blue; \
            setColor 0"
       
        incr framer
    }
    place .butfrm.int \
        -in .butfrm \
        -x 7 \
        -y 190
    pack .butfrm
    $frm conf -scrollregion "0 0 [. cget -width] [expr $framer * 30]"
    #$frm.$framer conf -width 400 -height 400
    update
}
   

    # While CL doesn't like these magic numbers, preferring to
    # calculate them dynamically with [winfo rgb ...], he preserves
    # them here out of respect for the seniority of this program.
    set collist {
             FFFAFA F8F8FF F5F5F5 DCDCDC FFFAF0 FDF5E6 FAF0E6 FAEBD7 
FFEFD5 FFEBCD FFE4C4 FFDAB9 FFDEAD FFE4B5 FFF8DC FFFFF0 FFFACD FFF5EE 
F0FFF0 F5FFFA F0FFFF F0F8FF E6E6FA FFF0F5 FFE4E1 FFFFFF 000000 2F4F4F 
696969 708090 778899 BEBEBE D3D3D3 191970 000080 6495ED 483D8B 6A5ACD 
7B68EE 8470FF 0000CD 4169E1 0000FF 1E90FF 00BFFF 87CEEB 87CEFA 4682B4 
B0C4DE ADD8E6 B0E0E6 AFEEEE 00CED1 48D1CC 40E0D0 00FFFF E0FFFF 5F9EA0 
66CDAA 7FFFD4 006400 556B2F 8FBC8F 2E8B57 3CB371 20B2AA 98FB98 00FF7F 
7CFC00 00FF00 7FFF00 00FA9A ADFF2F 32CD32 9ACD32 228B22 6B8E23 BDB76B 
F0E68C EEE8AA FAFAD2 FFFFE0 FFFF00 FFD700 EEDD82 DAA520 B8860B BC8F8F 
CD5C5C 8B4513 A0522D CD853F DEB887 F5F5DC F5DEB3 F4A460 D2B48C D2691E 
B22222 A52A2A E9967A FA8072 FFA07A FFA500 FF8C00 FF7F50 F08080 FF6347 
FF4500 FF0000 FF69B4 FF1493 FFC0CB FFB6C1 DB7093 B03060 C71585 D02090 
FF00FF EE82EE DDA0DD DA70D6 BA55D3 9932CC 9400D3 8A2BE2 A020F0 9370DB 
D8BFD8 EEE9E9 CDC9C9 8B8989 EEE5DE CDC5BF 8B8682 FFEFDB EEDFCC CDC0B0 
8B8378 EED5B7 CDB79E 8B7D6B EECBAD CDAF95 8B7765 EECFA1 CDB38B 8B795E 
EEE9BF CDC9A5 8B8970 EEE8CD CDC8B1 8B8878 EEEEE0 CDCDC1 8B8B83 E0EEE0 
C1CDC1 838B83 EEE0E5 CDC1C5 8B8386 EED5D2 CDB7B5 8B7D7B E0EEEE C1CDCD 
838B8B 836FFF 7A67EE 6959CD 473C8B 4876FF 436EEE 3A5FCD 27408B 0000EE 
00008B 1C86EE 1874CD 104E8B 63B8FF 5CACEE 4F94CD 36648B 00B2EE 009ACD 
00688B 87CEFF 7EC0EE 6CA6CD 4A708B B0E2FF A4D3EE 8DB6CD 607B8B C6E2FF 
B9D3EE 9FB6CD 6C7B8B CAE1FF BCD2EE A2B5CD 6E7B8B BFEFFF B2DFEE 9AC0CD 
68838B D1EEEE B4CDCD 7A8B8B BBFFFF AEEEEE 96CDCD 668B8B 98F5FF 8EE5EE 
7AC5CD 53868B 00F5FF 00E5EE 00C5CD 00868B 00EEEE 00CDCD 008B8B 97FFFF 
8DEEEE 79CDCD 528B8B 76EEC6 458B74 C1FFC1 B4EEB4 9BCD9B 698B69 54FF9F 
4EEE94 43CD80 9AFF9A 90EE90 7CCD7C 548B54 00EE76 00CD66 008B45 00EE00 
00CD00 008B00 76EE00 66CD00 458B00 C0FF3E B3EE3A 698B22 CAFF70 BCEE68 
A2CD5A 6E8B3D FFF68F EEE685 CDC673 8B864E FFEC8B EEDC82 CDBE70 8B814C 
EEEED1 CDCDB4 8B8B7A EEEE00 CDCD00 8B8B00 EEC900 CDAD00 8B7500 FFC125 
EEB422 CD9B1D 8B6914 FFB90F EEAD0E CD950C 8B6508 FFC1C1 EEB4B4 CD9B9B 
8B6969 FF6A6A EE6363 CD5555 8B3A3A FF8247 EE7942 CD6839 8B4726 FFD39B 
EEC591 CDAA7D 8B7355 FFE7BA EED8AE CDBA96 8B7E66 FFA54F EE9A49 8B5A2B 
FF7F24 EE7621 CD661D FF3030 EE2C2C CD2626 8B1A1A FF4040 EE3B3B CD3333 
8B2323 FF8C69 EE8262 CD7054 8B4C39 EE9572 CD8162 8B5742 EE9A00 CD8500 
8B5A00 FF7F00 EE7600 CD6600 8B4500 FF7256 EE6A50 CD5B45 8B3E2F EE5C42 
CD4F39 8B3626 EE4000 CD3700 8B2500 EE0000 CD0000 8B0000 EE1289 CD1076 
8B0A50 FF6EB4 EE6AA7 CD6090 8B3A62 FFB5C5 EEA9B8 CD919E 8B636C FFAEB9 
EEA2AD CD8C95 8B5F65 FF82AB EE799F CD6889 8B475D FF34B3 EE30A7 CD2990 
8B1C62 FF3E96 EE3A8C CD3278 8B2252 EE00EE CD00CD 8B008B FF83FA EE7AE9 
CD69C9 8B4789 FFBBFF EEAEEE CD96CD 8B668B E066FF D15FEE B452CD 7A378B 
BF3EFF B23AEE 9A32CD 68228B 9B30FF 912CEE 7D26CD 551A8B AB82FF 9F79EE 
8968CD 5D478B FFE1FF EED2EE CDB5CD 8B7B8B 030303 050505 080808 0A0A0A 
0D0D0D 0F0F0F 121212 141414 171717 1A1A1A 1C1C1C 1F1F1F 212121 242424 
262626 292929 2B2B2B 2E2E2E 303030 333333 363636 383838 3B3B3B 3D3D3D 
404040 424242 454545 474747 4A4A4A 4D4D4D 4F4F4F 525252 545454 575757 
595959 5C5C5C 5E5E5E 616161 636363 666666 6B6B6B 6E6E6E 707070 737373 
757575 787878 7A7A7A 7D7D7D 7F7F7F 828282 858585 878787 8A8A8A 8C8C8C 
8F8F8F 919191 949494 969696 999999 9C9C9C 9E9E9E A1A1A1 A3A3A3 A6A6A6 
A8A8A8 ABABAB ADADAD B0B0B0 B3B3B3 B5B5B5 B8B8B8 BABABA BDBDBD BFBFBF 
C2C2C2 C4C4C4 C7C7C7 C9C9C9 CCCCCC CFCFCF D1D1D1 D4D4D4 D6D6D6 D9D9D9 
DBDBDB DEDEDE E0E0E0 E3E3E3 E5E5E5 E8E8E8 EBEBEB EDEDED F0F0F0 F2F2F2 
F7F7F7 FAFAFA FCFCFC}

    set colnamelist {
             snow {ghost white} {white smoke} gainsboro {floral white} 
{old lace} linen {antique white} {papaya whip} {blanched almond} bisque 
{peach puff} {navajo white} moccasin cornsilk ivory {lemon chiffon} 
seashell honeydew {mint cream} azure {alice blue} lavender {lavender blush} 
{misty rose} white black {dark slate gray} {dim gray} {slate gray} 
{light slate gray} gray {light grey} {midnight blue} navy 
{cornflower blue} {dark slate blue} {slate blue} {medium slate blue} 
{light slate blue} {medium blue} {royal blue} blue {dodger blue}  
{deep sky blue} {sky blue} {light sky blue} {steel blue} {light steel blue} 
{light blue} {powder blue} {pale turquoise} {dark turquoise} {medium turquoise} 
turquoise cyan {light cyan} {cadet blue} {medium aquamarine} 
aquamarine {dark green} {dark olive green} {dark sea green} {sea green} 
{medium sea green} {light sea green} {pale green} {spring green} {lawn green} 
green chartreuse {medium spring green} {green yellow} {lime green} 
{yellow green} {forest green} {olive drab} {dark khaki} khaki  
{pale goldenrod} {light goldenrod yellow} {light yellow} yellow gold 
{light goldenrod} goldenrod {dark goldenrod} {rosy brown} {indian red} 
{saddle brown} sienna peru burlywood beige wheat {sandy brown} tan 
chocolate firebrick brown {dark salmon} salmon {light salmon} orange 
{dark orange} coral {light coral} tomato {orange red} red {hot pink} 
{deep pink} pink {light pink} {pale violet red} maroon {medium violet red} 
{violet red} magenta violet plum orchid {medium orchid} 
{dark orchid} {dark violet} {blue violet} purple {medium purple} thistle snow2 
snow3 snow4 seashell2 seashell3 seashell4 AntiqueWhite1 AntiqueWhite2 
AntiqueWhite3 AntiqueWhite4 bisque2 bisque3 bisque4 PeachPuff2 
PeachPuff3 PeachPuff4 NavajoWhite2 NavajoWhite3 NavajoWhite4 
LemonChiffon2 LemonChiffon3 LemonChiffon4 cornsilk2 cornsilk3 cornsilk4 
ivory2 ivory3 ivory4 honeydew2 honeydew3 honeydew4 LavenderBlush2 
LavenderBlush3 LavenderBlush4 MistyRose2 MistyRose3 MistyRose4 azure2 
azure3 azure4 SlateBlue1 SlateBlue2 SlateBlue3 SlateBlue4 RoyalBlue1 
RoyalBlue2 RoyalBlue3 RoyalBlue4 blue2 blue4 DodgerBlue2 DodgerBlue3 
DodgerBlue4 SteelBlue1 SteelBlue2 SteelBlue3 SteelBlue4 DeepSkyBlue2 
DeepSkyBlue3 DeepSkyBlue4 SkyBlue1 SkyBlue2 SkyBlue3 SkyBlue4 
LightSkyBlue1 LightSkyBlue2 LightSkyBlue3 LightSkyBlue4 SlateGray1 
SlateGray2 SlateGray3 SlateGray4 LightSteelBlue1 LightSteelBlue2 
LightSteelBlue3 LightSteelBlue4 LightBlue1 LightBlue2 LightBlue3 
LightBlue4 LightCyan2 LightCyan3 LightCyan4 PaleTurquoise1 
PaleTurquoise2 PaleTurquoise3 PaleTurquoise4 CadetBlue1 CadetBlue2 
CadetBlue3 CadetBlue4 turquoise1 turquoise2 turquoise3 turquoise4 cyan2 
cyan3 cyan4 DarkSlateGray1 DarkSlateGray2 DarkSlateGray3 DarkSlateGray4 
aquamarine2 aquamarine4 DarkSeaGreen1 DarkSeaGreen2 DarkSeaGreen3 
DarkSeaGreen4 SeaGreen1 SeaGreen2 SeaGreen3 PaleGreen1 PaleGreen2 
PaleGreen3 PaleGreen4 SpringGreen2 SpringGreen3 SpringGreen4 green2 
green3 green4 chartreuse2 chartreuse3 chartreuse4 OliveDrab1 OliveDrab2 
OliveDrab4 DarkOliveGreen1 DarkOliveGreen2 DarkOliveGreen3 
DarkOliveGreen4 khaki1 khaki2 khaki3 khaki4 LightGoldenrod1 
LightGoldenrod2 LightGoldenrod3 LightGoldenrod4 LightYellow2 
LightYellow3 LightYellow4 yellow2 yellow3 yellow4 gold2 gold3 gold4 
goldenrod1 goldenrod2 goldenrod3 goldenrod4 DarkGoldenrod1 
DarkGoldenrod2 DarkGoldenrod3 DarkGoldenrod4 RosyBrown1 RosyBrown2 
RosyBrown3 RosyBrown4 IndianRed1 IndianRed2 IndianRed3 IndianRed4 
sienna1 sienna2 sienna3 sienna4 burlywood1 burlywood2 burlywood3 
burlywood4 wheat1 wheat2 wheat3 wheat4 tan1 tan2 tan4 chocolate1 
chocolate2 chocolate3 firebrick1 firebrick2 firebrick3 firebrick4 brown1 
brown2 brown3 brown4 salmon1 salmon2 salmon3 salmon4 LightSalmon2 
LightSalmon3 LightSalmon4 orange2 orange3 orange4 DarkOrange1 
DarkOrange2 DarkOrange3 DarkOrange4 coral1 coral2 coral3 coral4 tomato2 
tomato3 tomato4 OrangeRed2 OrangeRed3 OrangeRed4 red2 red3 red4 
DeepPink2 DeepPink3 DeepPink4 HotPink1 HotPink2 HotPink3 HotPink4 pink1 
pink2 pink3 pink4 LightPink1 LightPink2 LightPink3 LightPink4 
PaleVioletRed1 PaleVioletRed2 PaleVioletRed3 PaleVioletRed4 maroon1 
maroon2 maroon3 maroon4 VioletRed1 VioletRed2 VioletRed3 VioletRed4 
magenta2 magenta3 magenta4 orchid1 orchid2 orchid3 orchid4 plum1 plum2 
plum3 plum4 MediumOrchid1 MediumOrchid2 MediumOrchid3 MediumOrchid4 
DarkOrchid1 DarkOrchid2 DarkOrchid3 DarkOrchid4 purple1 purple2 purple3 
purple4 MediumPurple1 MediumPurple2 MediumPurple3 MediumPurple4 thistle1 
thistle2 thistle3 thistle4 gray1 gray2 gray3 gray4 gray5 gray6 gray7 
gray8 gray9 gray10 gray11 gray12 gray13 gray14 gray15 gray16 gray17 
gray18 gray19 gray20 gray21 gray22 gray23 gray24 gray25 gray26 gray27 
gray28 gray29 gray30 gray31 gray32 gray33 gray34 gray35 gray36 gray37 
gray38 gray39 gray40 gray42 gray43 gray44 gray45 gray46 gray47 gray48 
gray49 gray50 gray51 gray52 gray53 gray54 gray55 gray56 gray57 gray58 
gray59 gray60 gray61 gray62 gray63 gray64 gray65 gray66 gray67 gray68 
gray69 gray70 gray71 gray72 gray73 gray74 gray75 gray76 gray77 gray78 
gray79 gray80 gray81 gray82 gray83 gray84 gray85 gray86 gray87 gray88 
gray89 gray90 gray91 gray92 gray93 gray94 gray95 gray97 gray98 gray99}


set redval 0
set blueval 0
set grnval 0
picker
wm title . {Color Picker}

LES This code doesn't work for me until I replace collist [list \ ... ] with collist { ... } and replace colnamelist [list \ ... ] with colnamelist { ... }

SO 2005-01-08: ... hmmm... This code is certainly broken, I have no recollection of posting this (OTOH, I may not remember what I did yesterday ...). This was not something I wrote, but may well have been something I had on hand during the 8.0 days ... I will search the archives..

pcam 2009-03-25: This code was simply the victim of pagination. The list command required the multiline command indicator \ at the end of each line. I took the easier approach and replaced the list command with the list definition { ... } and that was all it required.

WJP 2006-04-11: Here's a stand-alone color-picker that I wrote in Tcl/Tk: http://billposer.org/Software/ColorExplorer.html.

D. McC 2009-Mar-25: Or you could try WISH Color Picker Plus. pcam 25/03/2009. Tried it but your code required more modifications and editing (not directly rippable from wiki). Shame.

HaO 2010-06-17 BWidget has a color picker called ChooseColor

SeS 2010-06-19 Yes, HaO, and here you see it in action, a demonstration of the current state of tG², BWidget's has some nice widgets, wish I had seen the tab/notebook much earlier...