#!/afs/ece/usr/tcl/bin/wish -f

# To run this program, you should make sure the top line reflects the location
# of wish on your system, and the following variables represent the locations
# of beth and an rgb.txt file to look at, and the means to execute tkcolors.

# File with list of colors in format "[red][green][blue]	[name]"
set color_file "/usr/lib/X11/rgb.txt"
# Location of Beth source directory.
set beth_dir "/afs/ece/usr/svoboda/src/beth"
# Shell command that will execute tkcolors.
set tkcolors_exec "tkcolors"

# This program can take a starting color as an argument. If none is given, it
# defaults to the first one in the file, It colors each line with its own
# color, until it runs out of spots in the colormap.
#
# Besides the usual Beth options, one can restart tkcolors with a new
# starting color, by clicking mousebutton 3 on that color.


set color_start $argv
set argc 0
set option $color_file
set configs -b
set embedded 1
set dont_change_title 1
wm title . "TK Colors"
wm iconname . "TK Colors"
source $beth_dir/beth.tcl
$text configure -width 50

scan [$text index end] "%d" end
set line 1

# Wait until file starts coming in...
while {[string length [$text get 1.0 end]] == 0} {
	after 100
	update ; update idletasks
}

if {($color_start != "")} {
	set char [string first "	$color_start\n" [$text get 1.0 end]]
	scan [$text index "1.0 +$char chars"] "%d" color_line
} else {set color_line 1}

set line $color_line
scan [$text index end] "%d" end

$text mark set insert $line.0	; $text yview -pickplace insert
bind $text <ButtonRelease-3> {eval exec $tkcolors_exec [get_color @%x,%y] & ; quit_beth}

while {($line < $end)} {
	set tabend "$line.[string last "	" [$text get $line.0 "$line.0 lineend"]] +1 chars"
	set tabstart "$tabend -2 chars"
	set color [$text get $tabend "$tabend lineend"]
	$text tag add tag$line $tabstart $tabend
	$text tag configure tag$line -background $color
	incr line
}
 
# Given an index, returns the color on the index's line
proc get_color {index} {
	global text
	scan [$text index $index] "%d.%d" row col
	set tabend "$row.[string last "	" [$text get $row.0 "$row.0 lineend"]] +1 chars"
	return [$text get $tabend "$tabend lineend"]
}
