
# Stuff to manage the colormap.
#
# Copyright (c) 1993 ICEM Systems.
# Author:  Wayne A. Christopher
#          Pacific Marketing and Communication, Berkeley, California
#          faustus@cs.berkeley.edu, pmac!faustus@uunet.uu.net

# All these routines take images as their arguments.

set cmap_colordata {
    #0000ff
    #007fff
    #00ffff
    #00ff7f
    #00ff00
    #7fff00
    #ffff00
    #ff7f00
    #ff0000
}

# This loses old colormaps.  We should try and remove them.

proc cmap_create {w {images ""} {min ""} {max ""} {title ""}} {
    global cmap_image cmap_images cmap_title
    
    # We never want more than one of these
    set cmap [lindex [array names cmap_images] 0]
    if {$cmap == ""} {
	set cmap [tdv_color_map -viewer $w]
    }
    cmap_adjust $w $cmap $min $max
    
    eval lappend cmap_images($cmap) $images
    set cmap_title($cmap) $title
    foreach im $images { $im configure -cmap $cmap }
    return $cmap
}

proc cmap_adjust {w cmap min max {title ""}} {
    global cmap_colordata cmap_image
    
    if {($min == "") || ($max == "") || ($cmap == "NULL")} return

    if {$min > $max} {
	set min [expr {($min + $max) / 2}]
	set max $min
    }
    set min_limit 1e-64 ;# ????
    if {$max - $min < $min_limit} {
	if {abs($min) > $min_limit} {
	    set incr [expr {$min / 100.0}]
	} else {
	    set incr $min_limit
	}
	set min [expr {$min - $incr}]
	set max [expr {$max + $incr}]
    }
    set num [llength $cmap_colordata]
    $cmap configure -size $num
    set step [expr {($max - $min) / ($num - 1.0)}]
    for {set i 0} {$i < $num} {incr i} {
	$cmap setval $i [expr {$min + $i * $step}] [lindex $cmap_colordata $i]
    }
    if {($title != "") && [info exists cmap_image($w)]} {
	$cmap_image($w) configure -title $title
    }
}

proc cmap_delete {w cmap} {
    global cmap_images cmap_image
    
    foreach im $cmap_images($cmap) { $im configure -cmap NULL }
    if {[info exists cmap_image($w)] && \
	    ([$cmap_image($w) get -cmap] == $cmap)} {
	$cmap_image($w) delete
	unset cmap_image($w)
    }
}

proc cmap_make_current {w image} {
    global cmap_colordata cmap_image tdv_width tdv_height cmap_title
    global cmap_text_color job_legend_sigfigs job_legend_figstype
    
    #puts "image = $image"
    set cmap NULL
    if {$image != ""} { set cmap [$image get -cmap] }
    #puts "cmap is $cmap"
    
    if {$cmap == "NULL"} {
	if [info exists cmap_image($w)] {
	    $cmap_image($w) delete
	    unset cmap_image($w)
	}
	return
    }
    if [info exists cmap_image($w)] {
	$cmap_image($w) configure -cmap $cmap -title $cmap_title($cmap)
    } else {
	set ww [expr {$tdv_width($w) / 35}]
	set hh [expr {$tdv_height($w) / 3}]
	set xx [expr {$tdv_width($w) - 100 - $ww}]
	set yy 50
	set cmap_image($w) [tdv_crange_im -pos "$xx $yy 0" -transform 0 \
				-sigfigs $job_legend_sigfigs \
				-sigfigstype $job_legend_figstype \
				-cr_width $ww -cr_height $hh -cmap $cmap \
				-title $cmap_title($cmap) -all_slaves 1]
	$w add_image $cmap_image($w)
	$w add_tag $cmap_image($w) anno_pickable
    }
    if [info exists cmap_text_color] {
	$cmap_image($w) configure -color $cmap_text_color
    }
}

proc cmap_update_sigfigs {} {
    global job_legend_sigfigs job_legend_figstype cmap_image
    #    foreach w [array names cmap_image($w)] 
    foreach w [array names cmap_image] {
	$cmap_image($w) configure -sigfigs $job_legend_sigfigs -sigfigstype $job_legend_figstype
    }
}

proc cmap_bnw {w on} {
    global cmap_image cmap_saved_colors
    
    if ![info exists cmap_image($w)] return
    set cmap [$cmap_image($w) get -cmap]
    if $on {
	set n [$cmap get -size]
	set incr [expr {1 / ($n + 3.0)}]
	set cmap_saved_colors ""
	for {set i 0} {$i < $n} {incr i} {
	    set v [lindex [$cmap getval $i] 0]
	    set c [lindex [$cmap getval $i] 1]
	    lappend cmap_saved_colors "$i $v $c"
	    set nc [expr {($i + 2) * $incr}]
	    set xx [format %2.2x [expr {int(255 * $nc)}]]
	    $cmap setval $i $v #$xx$xx$xx
	}
    } else {
	foreach cc $cmap_saved_colors {
	    eval $cmap setval $cc
	}
    }
}

proc cmap_drag {w crange entry starty endy pixels} {
    set cmap [$crange get -cmap]
    set num [$cmap get -size]
    incr num -1
    set min [lindex [$cmap getval 0] 0]
    set max [lindex [$cmap getval $num] 0]
    #puts "entry $entry min $min max $max"
    set oval [lindex [$cmap getval $entry] 0]
    set nval [expr {$oval - ($endy - $starty) / $pixels}]
    if {$entry == 0} {
	cmap_adjust $w $cmap $nval $max
    } elseif {$entry == $num} {
	cmap_adjust $w $cmap $min $nval
    } else {
	$cmap setval $entry $nval ""
    }
}
