#!/bin/sh
#PostDisplay -*- TCL -*- Created: ramsan&miguel Jan-1997, Modified: miguel ???-1998


#----------------------PostDisplay ALONE---------------------------
# the next line restarts using wish \
	exec wish8.0 "$0" "$@"

set CCOtroLook 0

set CCRunningAlone 0
if { ![info exists GIDDEFAULT] } {
    set CCRunningAlone 1
    set tcl_interactive 0
    set tk_strictMotif 1

    if { [catch { set GIDDEFAULT $env(GIDDEFAULT)}]!=1 } {
	set GIDDEFAULTTCL $GIDDEFAULT
	catch { set GIDDEFAULTTCL $env(GIDDEFAULTTCL) }
	set auto_path "$GIDDEFAULTTCL"
	source $GIDDEFAULTTCL/init.tcl
	source $GIDDEFAULTTCL/tk.tcl
    }
    wm withdraw .
}

#  LOOK AT THE END OF THE FILE !!!
#-------------------------------------------------------------------

proc CCInitValues { } {
    global ContourColors CCRunningAlone

    if { $CCRunningAlone} {
	set ContourColors(Original) "10 Rainbow #7f0000 #00007f RGB Linear Disenno { }"
    } else {
	set ContourColors(Original) [.central.s info post get cur_contour_color_config]
    }

    set ContourColors(NumeroColores) [ lindex $ContourColors(Original) 0]
    set ContourColors(ColorMapType) [ lindex $ContourColors(Original) 1]
    set ContourColors(UserMinColor) [ lindex $ContourColors(Original) 2]
    set ContourColors(UserMaxColor) [ lindex $ContourColors(Original) 3]
    set ContourColors(MapScaleType) [ lindex $ContourColors(Original) 4]
    set ContourColors(MapScaleRamp) [ lindex $ContourColors(Original) 5]
    set ContourColors(UserMode) [ lindex $ContourColors(Original) 6]

    set ContourColors(OutMinColor) #333333
    set ContourColors(OutMaxColor) #666666

    set ContourColors(ColoresPivote) [ lindex $ContourColors(Original) 7]
    if { [ llength $ContourColors(ColoresPivote)] == 0} {
	set ContourColors(ColoresPivote) "$ContourColors(UserMinColor) #808080 $ContourColors(UserMaxColor)"
    }
    if { [ llength $ContourColors(ColoresPivote)] < 3 } {
	for { set ll [ llength $ContourColors(ColoresPivote)]} { $ll < 3} { incr ll} {
	    lappend $ContourColors(ColoresPivote) #808080
	}
    }
    set ContourColors(NumPivotes) [ llength $ContourColors(ColoresPivote)]

    #set ContourColors(UserMode) "old DosColores"
}



proc CCAceptar { w} {
    global ContourColors CCRunningAlone

    if { $CCRunningAlone} { 
	set ContourColors(Original) ""
	lappend ContourColors(Original) $ContourColors(NumeroColores) 
	lappend ContourColors(Original) $ContourColors(ColorMapType)
	lappend ContourColors(Original) $ContourColors(UserMinColor)
	lappend ContourColors(Original) $ContourColors(UserMaxColor)
	lappend ContourColors(Original) $ContourColors(MapScaleType)
	lappend ContourColors(Original) $ContourColors(MapScaleRamp)

	return
    }

    set oldAutomaticRedraw [.central.s info variables AutomaticRedraw]
    .central.s process escape escape escape escape utilities variables \
	    AutomaticRedraw -1 escape escape 

    set esc4 "escape escape escape escape"

    set cmd ".central.s process $esc4"
    append cmd " Results ContOptions NumberOfColor $ContourColors(NumeroColores)"

    switch "[ string tolower $ContourColors(ColorMapType)]" {
	"rainbow" {
	    append cmd " $esc4 Results ContOptions SetMinOptions MinColor Standard"
	    append cmd " $esc4 Results ContOptions SetMaxOptions MaxColor Standard"
	    append cmd " $esc4 Results ContOptions ColorRamp $ContourColors(MapScaleRamp)"
	}
	"inv. rainbow" {
	    append cmd " $esc4 Results ContOptions SetMinOptions MinColor InverseStd"
	    append cmd " $esc4 Results ContOptions SetMaxOptions MaxColor InverseStd"
	    append cmd " $esc4 Results ContOptions ColorRamp $ContourColors(MapScaleRamp)"
	}
	"terrain" {
	    append cmd " $esc4 Results ContOptions ColorRamp TerrainMap"
	}
	"black & white" {
	    append cmd " $esc4 Results ContOptions SetMinOptions MinColor Black"
	    append cmd " $esc4 Results ContOptions SetMaxOptions MaxColor White"
	    append cmd " $esc4 Results ContOptions ColorRamp $ContourColors(MapScaleRamp)"
	}
	"user defined" {
	    if { "[ string tolower $ContourColors(UserMode)]" == "doscolores"} {
		append cmd " $esc4 Results ContOptions SetMinOptions MinColor user $ContourColors(UserMinColor)"
		append cmd " $esc4 Results ContOptions SetMaxOptions MaxColor user $ContourColors(UserMaxColor)"
		append cmd " $esc4 Results ContOptions ColorRamp $ContourColors(MapScaleRamp)"
		append cmd " $esc4 Results ContOptions ColorRamp $ContourColors(MapScaleType)"
	    } else {
		# "[ string tolower $ContourColors(UserMode)]" == "disenno"
		append cmd " $esc4 Results ContOptions ColorRamp DefineMap [ llength $ContourColors(ColoresPivote)]"
		append cmd " $ContourColors(ColoresPivote)"
	    }
	}
    }

    append cmd " $esc4 Utilities Redisplay"
    eval $cmd
    
    .central.s process escape escape escape escape utilities variables \
	    AutomaticRedraw $oldAutomaticRedraw escape escape 
#    .central.s makecurrent
#    .central.s process redraw
    .gid.central.s render

#    WarnWin "Values accepted." $w
}

proc CCResetValores { w} {
    global ContourColors

    set ContourColors(NumeroColores) [ lindex $ContourColors(Original) 0]
    set ContourColors(ColorMapType) [ lindex $ContourColors(Original) 1]
    set ContourColors(UserMinColor) [ lindex $ContourColors(Original) 2]
    set ContourColors(UserMaxColor) [ lindex $ContourColors(Original) 3]
    set ContourColors(MapScaleType) [ lindex $ContourColors(Original) 4]
    set ContourColors(MapScaleRamp) [ lindex $ContourColors(Original) 5]
    set ContourColors(UserMode) [ lindex $ContourColors(Original) 6]
    set ContourColors(ColoresPivote) [ lindex $ContourColors(Original) 7]
    if { [ llength $ContourColors(ColoresPivote)] == 0} {
	set ContourColors(ColoresPivote) "$ContourColors(UserMinColor) $ContourColors(UserMaxColor)"
    }
    set ContourColors(NumPivotes) [ llength $ContourColors(ColoresPivote)]

    CCActualizaCanvas
}

proc CCCambiaColor { w colvar} {
    upvar $colvar UserColor
    global CCOtroLook

    set color_viejo $UserColor
    set color_nuevo [ tk_chooseColor -parent $w -initialcolor $color_viejo]
    if { "$color_nuevo" != ""} {
	set n [ scan $color_nuevo #%2x%2x%2x r g b]
	if { $n == 3} {
	    if { $CCOtroLook } {
		PlanoConf $w colornormal $color_nuevo
		PlanoConf $w coloractivo [ CCColorActivo $color_nuevo 34]
	    } else {
		$w conf -background $color_nuevo
	    }   
	    set UserColor $color_nuevo
	}
    }
}

proc CCNumColsChange { name1 name2 op} {
    global ContourColors

    after cancel CCActualizaCanvas

    set w $ContourColors(Window)
    set color_fondo [ CCGetRGB $w [ $w cget -background]]

    if { "$ContourColors(NumeroColores)" != ""} {
	if { ![ regexp {^[0-9]*$} $ContourColors(NumeroColores) dum] } {
	    set num 0
	    scan $ContourColors(NumeroColores) %d num
	    set ContourColors(NumeroColores) $num
	    $ContourColors(NumeroColoresEntry) del 0 end
	    $ContourColors(NumeroColoresEntry) ins end $num
	    bell -displayof $ContourColors(NumeroColoresEntry)
	}

    }

    after 1500 CCActualizaCanvas
}

proc CCColorMapTypeChange { name1 name2 op} {
    global ContourColors

    after cancel CCActualizaCanvas

    set w $ContourColors(Window)
    set color_fondo [ CCGetRGB $w [ $w cget -background]]

    switch "[ string tolower $ContourColors(ColorMapType)]" {
	"rainbow" {
	    foreach iwd $ContourColors(lista_user_map_estado) {
		$iwd conf -state disabled
	    }
	    if { [ llength $ContourColors(UserMode)] == 1} {
		set ContourColors(UserMode) "old $ContourColors(UserMode)"
	    }
	    #$ContourColors(sitio_imagen) conf -image [ lindex $ContourColors(lista_user_map_imagenes) 0]
	}
	"inv. rainbow" {
	    foreach iwd $ContourColors(lista_user_map_estado) {
		$iwd conf -state disabled
	    }
	    if { [ llength $ContourColors(UserMode)] == 1} {
		set ContourColors(UserMode) "old $ContourColors(UserMode)"
	    }
	    #$ContourColors(sitio_imagen) conf -image [ lindex $ContourColors(lista_user_map_imagenes) 1]
	}
	"terrain" {
	    foreach iwd $ContourColors(lista_user_map_estado) {
		$iwd conf -state disabled
	    }
	    if { [ llength $ContourColors(UserMode)] == 1} {
		set ContourColors(UserMode) "old $ContourColors(UserMode)"
	    }
	    #$ContourColors(sitio_imagen) conf -image [ lindex $ContourColors(lista_user_map_imagenes) 2]
	}
	"black & white" {
	    foreach iwd $ContourColors(lista_user_map_estado) {
		$iwd conf -state disabled
	    }
	    if { [ llength $ContourColors(UserMode)] == 1} {
		set ContourColors(UserMode) "old $ContourColors(UserMode)"
	    }
	    #$ContourColors(sitio_imagen) conf -image [ lindex $ContourColors(lista_user_map_imagenes) 3]
	}
	"user defined" {
	    foreach iwd $ContourColors(lista_user_map_estado) {
		$iwd conf -state normal
	    }
	    if { [ llength $ContourColors(UserMode)] != 1} {
		set ContourColors(UserMode) [lindex $ContourColors(UserMode) 1]
	    }
	    #$ContourColors(sitio_imagen) conf -image [ lindex $ContourColors(lista_user_map_imagenes) 0]
	}
    }

    after 1500 CCActualizaCanvas
}

proc CCColorMapUserModeChange { name1 name2 op} {
    global ContourColors
    global CCOtroLook

    set w $ContourColors(Window)
    set color_fondo [ CCGetRGB $w [ $w cget -background]]
    
    switch "$ContourColors(UserMode)" {
	"DosColores" {
	    foreach iwd $ContourColors(lista_dos_colores_estado) {
		if { $CCOtroLook} {
		    PlanoConf $iwd estado normal
		} else {
		    $iwd conf -state normal
		}
	    }
	    foreach iwd $ContourColors(lista_dos_colores_colores) {
		$iwd conf -foreground black
	    }
	    foreach iwd $ContourColors(lista_disenno_estado) {
		UpDownEntryConf $iwd -state disabled -foreground grey60
		#PlanoConf $iwd estado disabled
	    }
	    foreach iwd $ContourColors(lista_disenno_colores) {
		$iwd conf -foreground [ CCColorActivo $color_fondo]
	    }
	}

	"Disenno" {
	    foreach iwd $ContourColors(lista_dos_colores_estado) {
		if { $CCOtroLook} {
		    PlanoConf $iwd estado disabled
		} else {
		    $iwd conf -state disabled
		}
	    }
	    foreach iwd $ContourColors(lista_dos_colores_colores) {
		$iwd conf -foreground [ CCColorActivo $color_fondo]
	    }
	    foreach iwd $ContourColors(lista_disenno_estado) {
		UpDownEntryConf $iwd -state normal -foreground black
	    }
	    foreach iwd $ContourColors(lista_disenno_colores) {
		#PlanoConf $iwd estado normal
		$iwd conf -foreground black
	    }
	}

	default {
	    foreach iwd $ContourColors(lista_dos_colores_estado) {
		if { $CCOtroLook} {
		    PlanoConf $iwd estado disabled
		} else {
		    $iwd conf -state disabled
		}
	    }
	    foreach iwd $ContourColors(lista_dos_colores_colores) {
		$iwd conf -foreground [ CCColorActivo $color_fondo]
	    }
	    foreach iwd $ContourColors(lista_disenno_estado) {
		UpDownEntryConf $iwd -state disabled -foreground grey60
		#PlanoConf $iwd estado disabled
	    }
	    foreach iwd $ContourColors(lista_disenno_colores) {
		#PlanoConf $iwd estado disabled
		$iwd conf -foreground [ CCColorActivo $color_fondo]
	    }
	}
    }
}


proc CCColorUserMapChange { name1 name2 op} {
    global ContourColors
    after cancel CCActualizaCanvas
    after 1500 CCActualizaCanvas
}

proc CCCreaEscalaColores { cols_pivote ncolor} {
    
    set n [ llength $cols_pivote]
    set ninterior [ expr ( $ncolor - 1) / ( $n - 1)]
    
    set lst_colores ""
    set jcolor 0
    for { set i 0} { ( $i < [ expr $n - 2]) && ( $jcolor < $ncolor)} { incr i} {
	set col_ini [ lindex $cols_pivote $i ]
	lappend lst_colores $col_ini
	incr jcolor

	set col_fin [ lindex $cols_pivote [ expr $i + 1]]
	scan $col_ini #%2x%2x%2x ci_red ci_green ci_blue
	scan $col_fin #%2x%2x%2x cf_red cf_green cf_blue

	set dif_red [ expr double( $cf_red - $ci_red)]
	set dif_green [ expr double( $cf_green - $ci_green)]
	set dif_blue [ expr double( $cf_blue - $ci_blue)]
	for { set j 1} { ( $j < $ninterior) && ( $jcolor < $ncolor)} { incr j} {
	    set d [ expr double( $j) / double( $ninterior)]
	    set new_red [ expr int( 0.5 + $d * $dif_red + double( $ci_red))]
	    set new_green [ expr int( 0.5 + $d * $dif_green + double( $ci_green))]
	    set new_blue [ expr int( 0.5 + $d * $dif_blue + double( $ci_blue))]
	    lappend lst_colores [ format #%02x%02x%02x $new_red $new_green $new_blue]
	    incr jcolor
	}
    }

    if { $jcolor < $ncolor} {
	set col_ini [ lindex $cols_pivote $i ]
	lappend lst_colores $col_ini
	incr jcolor
	
	set col_fin [ lindex $cols_pivote [ expr $i + 1]]
	
	scan $col_ini #%2x%2x%2x ci_red ci_green ci_blue
	scan $col_fin #%2x%2x%2x cf_red cf_green cf_blue
	
	set dif_red [ expr double( $cf_red - $ci_red)]
	set dif_green [ expr double( $cf_green - $ci_green)]
	set dif_blue [ expr double( $cf_blue - $ci_blue)]

	set ninterior [ expr $ncolor - $jcolor]
	for { set j 1} { ( $j < $ninterior) && ( $jcolor < $ncolor)} { incr j} {
	    set d [ expr double( $j) / double( $ninterior)]
	    set new_red [ expr int( 0.5 + $d * $dif_red + double( $ci_red))]
	    set new_green [ expr int( 0.5 + $d * $dif_green + double( $ci_green))]
	    set new_blue [ expr int( 0.5 + $d * $dif_blue + double( $ci_blue))]
	    lappend lst_colores [ format #%02x%02x%02x $new_red $new_green $new_blue]
	    incr jcolor
	}
    }

    if { $jcolor < $ncolor} {
	lappend lst_colores [ lindex $cols_pivote [ expr $n - 1]]
	incr jcolor
	if { $jcolor < $ncolor} {
	    WarnWin [_ "Something happens with the colors: %1\$s - %2\$s" $jcolor $ncolor]
	}
    }
	
    return $lst_colores
}

proc rgb2hsv { r g b} {
    set h 0.0
    set s 0.0
    set v 0.0
    #  real m, rl, gl, bl;
    
    set v $r
    if { $g > $v} { set v $g}
    if { $b > $v} { set v $b}

    set m $r
    if { $g < $m} { set m $g}
    if { $b < $m} { set m $b}

    if { $v != 0} { 
	set s [ expr double( $v - $m) / double( $v)]
    } else { 
	set s 0.0
    }
    
    if { $s != 0.0} {
	set dm [ expr double( $v - $m)]
	if { [ expr abs( $dm)] > 1e-20} {
	    #dist( color, rojo)
	    set rl [ expr double( $v - $r) / double( $dm)] 
	    #dist( color, verde)
	    set gl [ expr double( $v - $g) / double( $dm)] 
	    #dist( color, blue)
	    set bl [ expr double( $v - $b) / double( $dm)] 
	} else {
	    set rl 0.0
	    set gl 0.0
	    set bl 0.0
	}
	if { $v == $r} {
	    if { $m == $g} {
		set h [ expr 5.0 + $bl]
	    } else {
		set h [ expr 1.0 - $gl]
	    }
	} elseif { $v == $g} {
	    if { $m == $b} {
		set h [ expr 1.0 + $rl]
	    } else {
		set h [ expr 3.0 - $bl]
	    }
	} else {
	    if { $m == $r} {
		set h [ expr 3.0 + $gl]
	    } else {
		set h [ expr 5.0 - $rl]
	    }
	}
	set h [ expr $h * 60.0]
	if { $h == 360.0} { set h 0.0}
    } else { # h indefinido
	set h 0.0
    }
    
    return "$h $s $v"
}

proc hsv2rgb { h s v} {
    set r 0.0
    set g 0.0
    set b 0.0
    
    if { $h == 360.0} { set h 0.0}
    # h = 0..6;
    set h [ expr $h / 60.0] 
    set i [ expr int( floor( $h))]
    set f [ expr $h - $i]
    set p1 [ expr $v * ( 1.0 - $s)]
    set p2 [ expr $v * ( 1.0 - $s * $f)]
    set p3 [ expr $v * ( 1.0 - $s * ( 1.0 - $f))]
    switch $i {
	0 {
	    set r $v
	    set g $p3
	    set b $p1
	}
	1 {
	    set r $p2
	    set g $v 
	    set b $p1
	} 
	2 {
	    set r $p1
	    set g $v 
	    set b $p3
	}
	3 {
	    set r $p1
	    set g $p2
	    set b $v
	}
	4 {
	    set r $p3
	    set g $p1
	    set b $v
	}
	5 {
	    set r $v
	    set g $p1
	    set b $p2
	}
    }
    
    return "$r $g $b"
}

proc CCListaColores { tipo_mapa ncolores usermincolor usermaxcolor mapscaletype mapscaleramp usermode colorespivote} {
    set col_lst ""

    switch "[ string tolower $tipo_mapa]" {
	"rainbow" {
	    set ncol [ expr $ncolores - 1]
	    if { $ncol <= 0} { set ncol 1}
	    set xfir [ expr $ncol * 0.10]
	    set xqua [ expr $ncol * 0.35]
	    set xmed [ expr $ncol * 0.50]
	    set xthi [ expr $ncol * 0.65]
	    set xlas [ expr $ncol * 0.90]
	    set xdiv1 [ expr $xqua - $xfir]
	    set xdiv2 [ expr $xmed - $xqua]
	    
	    for { set jcolor 1} { $jcolor <= $ncolores} { incr jcolor} {
		set xcolor [ expr $jcolor - 1]

		# Los Azules
		if { $xcolor <= $xfir } { 
		    set blue [ expr 0.5 + $xcolor / ( $xfir * 2.0)]
		} elseif { $xcolor <= $xqua } { 
		    set blue 1.0 
		} elseif { $xcolor <= $xmed } { 
		    set blue [ expr 1.0 - ( $xcolor - $xqua) / $xdiv2] 
		} else { set blue 0.0 }
		
		# Los Verdes
		if { $xcolor <= $xfir } { 
		    set green 0.0
		} elseif { $xcolor <= $xqua } { 
		    set green [ expr ( $xcolor - $xfir) / $xdiv1]
		} elseif { $xcolor <= $xthi } { 
		    set green 1.0
		} elseif { $xcolor <= $xlas } { 
		    set green [ expr 1.0 - ( $xcolor - $xthi) / $xdiv1]
		} else { set green 0.0 }
        
		# I los Rohos
		if { $xcolor <= $xmed } { 
		    set red 0.0 
		} elseif { $xcolor <= $xthi} { 
		    set red [ expr ( $xcolor - $xmed) / $xdiv2] 
		} elseif { $xcolor <= $xlas} { 
		    set red 1.0 
		} else { 
		    set red [ expr 1.0 - ( $xcolor - $xlas) / ( $xfir * 2.0)] 
		}

		if { $red < 0.0 } { set red 0.0 }
		if { $green < 0.0 } { set green 0.0 }
		if { $blue < 0.0 } { set blue 0.0 }
		if { $red > 1.0 } { set red 1.0 }
		if { $green > 1.0 } { set green 1.0 }
		if { $blue > 1.0 } { set blue 1.0 }
       
		set red [ expr int( $red * 255.0)]
		set green [ expr int( $green * 255.0)]
		set blue [ expr int( $blue * 255.0)]
		lappend col_lst [ format #%02x%02x%02x $red $green $blue]
	    }
	}
	"inv. rainbow" {
	    set ncol [ expr $ncolores - 1]
	    if { $ncol <= 0} { set ncol 1}
	    set xfir [ expr $ncol * 0.10]
	    set xqua [ expr $ncol * 0.35]
	    set xmed [ expr $ncol * 0.50]
	    set xthi [ expr $ncol * 0.65]
	    set xlas [ expr $ncol * 0.90]
	    set xdiv1 [ expr $xqua - $xfir]
	    set xdiv2 [ expr $xmed - $xqua]
	    
	    for { set jcolor 1} { $jcolor <= $ncolores} { incr jcolor} {
		set xcolor [ expr $ncolores - $jcolor]

		# Los Azules
		if { $xcolor <= $xfir } { 
		    set blue [ expr 0.5 + $xcolor / ( $xfir * 2.0)]
		} elseif { $xcolor <= $xqua } { 
		    set blue 1.0 
		} elseif { $xcolor <= $xmed } { 
		    set blue [ expr 1.0 - ( $xcolor - $xqua) / $xdiv2] 
		} else { set blue 0.0 }
		
		# Los Verdes
		if { $xcolor <= $xfir } { 
		    set green 0.0
		} elseif { $xcolor <= $xqua } { 
		    set green [ expr ( $xcolor - $xfir) / $xdiv1]
		} elseif { $xcolor <= $xthi } { 
		    set green 1.0
		} elseif { $xcolor <= $xlas } { 
		    set green [ expr 1.0 - ( $xcolor - $xthi) / $xdiv1]
		} else { set green 0.0 }
        
		# I los Rohos
		if { $xcolor <= $xmed } { 
		    set red 0.0 
		} elseif { $xcolor <= $xthi} { 
		    set red [ expr ( $xcolor - $xmed) / $xdiv2] 
		} elseif { $xcolor <= $xlas} { 
		    set red 1.0 
		} else { 
		    set red [ expr 1.0 - ( $xcolor - $xlas) / ( $xfir * 2.0)] 
		}

		if { $red < 0.0 } { set red 0.0 }
		if { $green < 0.0 } { set green 0.0 }
		if { $blue < 0.0 } { set blue 0.0 }
		if { $red > 1.0 } { set red 1.0 }
		if { $green > 1.0 } { set green 1.0 }
		if { $blue > 1.0 } { set blue 1.0 }
       
		set red [ expr int( 0.5 + $red * 255.0)]
		set green [ expr int( 0.5 + $green * 255.0)]
		set blue [ expr int( 0.5 + $blue * 255.0)]
		lappend col_lst [ format #%02x%02x%02x $red $green $blue]
	    }
	}
	"terrain" {
	    set pivotes_terreno "#3f5f00 #a0a000 #ffa000 #7f6f4f #fffffb"
	    set col_lst [ CCCreaEscalaColores $pivotes_terreno $ncolores]
	}
	"black & white" {
	    set ncol [ expr $ncolores - 1]
	    if { $ncol <= 0} { set ncol 1}
	    for { set j  0} { $j < $ncolores} { incr j} {
		set d [ expr double( $j) / double( $ncol)]
		set d [ expr log10( 1.0 + 9.0 * $d) ]

		set d [ expr int( 0.5 + $d * 255.0)]
		lappend col_lst [ format #%02x%02x%02x $d $d $d]
	    }
	}
	"user defined" {
	    if { "[ string tolower $usermode]" == "doscolores"} {
		set ci_1 0.0
		set ci_2 0.0
		set ci_3 0.0
		set cf_1 0.0
		set cf_2 0.0
		set cf_3 0.0
		switch $mapscaletype {
		    RGB {
			scan $usermincolor #%2x%2x%2x ci_1 ci_2 ci_3
			scan $usermaxcolor #%2x%2x%2x cf_1 cf_2 cf_3
		    }
		    HSV {
			scan $usermincolor #%2x%2x%2x r g b
			set ci [ rgb2hsv [ expr $r / 255.0] [ expr $g / 255.0] [ expr $b / 255.0]]
			set ci_1 [ lindex $ci 0]
			set ci_2 [ lindex $ci 1]
			set ci_3 [ lindex $ci 2]
			scan $usermaxcolor #%2x%2x%2x r g b
			set cf [ rgb2hsv [ expr $r / 255.0] [ expr $g / 255.0] [ expr $b / 255.0]]
			set cf_1 [ lindex $cf 0]
			set cf_2 [ lindex $cf 1]
			set cf_3 [ lindex $cf 2]
		    }
		}
		
		set col_lst $usermincolor

		if { $ncolores == 1} {
		} elseif { $ncolores == 2} {
		    lappend col_lst $usermaxcolor
		} else {
		    set dif_1 [ expr $cf_1 - $ci_1]
		    set dif_2 [ expr $cf_2 - $ci_2]
		    set dif_3 [ expr $cf_3 - $ci_3]
		    set pi 3.14159265358979323846
		    set atanpi [ expr atan( $pi)]
		    set atanpi2 [ expr $atanpi + $atanpi]
		    set inv2atanpi [ expr 1.0 / $atanpi2]
		    set inv2pi [ expr 1.0 / ( $pi + $pi)]
		    for { set jcolor 0} { $jcolor < [ expr $ncolores - 1]} { incr jcolor} {
			set d [ expr double( $jcolor) / double( $ncolores - 2)]
			switch "[ string tolower $mapscaleramp]" {
			    "arctang" {
				set d [ expr atan( $pi * ( $d + $d - 1.0)) * $inv2atanpi + 0.5]
			    }
			    "tangent" {
				set d [ expr tan( $atanpi * ( $d + $d - 1.0)) * $inv2pi + 0.5]
			    }
			    "linear" {
			    }
			}
			set new_1 [ expr $d * $dif_1 + $ci_1]
			set new_2 [ expr $d * $dif_2 + $ci_2]
			set new_3 [ expr $d * $dif_3 + $ci_3]
			if { "$mapscaletype" == "HSV"} {
			    set new [ hsv2rgb $new_1 $new_2 $new_3]
			    set new_1 [ expr int( 0.5 + 255.0 * [ lindex $new 0])]
			    set new_2 [ expr int( 0.5 + 255.0 * [ lindex $new 1])]
			    set new_3 [ expr int( 0.5 + 255.0 * [ lindex $new 2])]
			} else {
			    set new_1 [ expr int( $new_1)]
			    set new_2 [ expr int( $new_2)]
			    set new_3 [ expr int( $new_3)]
			}
			lappend col_lst [ format #%02x%02x%02x $new_1 $new_2 $new_3]
		    }
		}
	    } else {
		# "[ string tolower $usermode]" == "disenno"
		set col_lst [ CCCreaEscalaColores $colorespivote $ncolores]
	    }
	}
    }

    return $col_lst
}


proc CCActualizaCanvas { } {
    global ContourColors CCOtroLook

    after cancel CCActualizaCanvas

    set w $ContourColors(Window)
    if { ![winfo exists $w] } return

    if { $ContourColors(NumeroColores) == 0} {
	#WarnWin "'Number of colors' is 0" $w
	return
    }
    set color_fondo [ CCGetRGB $w [ $w cget -background]]

    set canvas_ampliado 0
    if { ( "[ string tolower $ContourColors(ColorMapType)]" == "user defined") && \
	    ( "$ContourColors(UserMode)" == "Disenno") } {
	set canvas_ampliado 1
	$ContourColors(sitio_imagen) conf -width 50
    } else {
	$ContourColors(sitio_imagen) conf -width 50
	set x_off 25
    }


    # WarnWinText "CCListaColores $ContourColors(ColorMapType) $ContourColors(NumeroColores) \
    # 	      $ContourColors(UserMinColor) $ContourColors(UserMaxColor) $ContourColors(MapScaleType) \
    # 	      $ContourColors(MapScaleRamp)"
    set colores [ CCListaColores $ContourColors(ColorMapType) $ContourColors(NumeroColores) \
	    $ContourColors(UserMinColor) $ContourColors(UserMaxColor) $ContourColors(MapScaleType) \
	    $ContourColors(MapScaleRamp) $ContourColors(UserMode) $ContourColors(ColoresPivote)]
    # WarnWinText $colores

    # limpiamos
    $ContourColors(sitio_imagen) delete all
    
    set hcanvas [ $ContourColors(sitio_imagen) cget -height]
    set wcanvas [ $ContourColors(sitio_imagen) cget -width]
    # $ContourColors(sitio_imagen) create rectangle 1 1 $wcanvas $hcanvas

    for { set hcolor 0} { [ expr $hcolor * $ContourColors(NumeroColores)] < $hcanvas} { incr hcolor} {
    }
    if { $hcolor > 0} { set hcolor [ expr $hcolor - 1]}
    set hoff [ expr int( 0.5 + ( $hcanvas - $hcolor * $ContourColors(NumeroColores)) * 0.5)]
    
    # WarnWinText "$hcanvas > $hcolor * $ContourColors(NumeroColores) + $hoff"

    set wcolor 15
    set woff 0
    if { $canvas_ampliado } {
	## tambien hay que dibujar los pivotes
	set woff [ expr $wcanvas - $wcolor - 4]
    } else {
	## solo la leyenda
	set woff [ expr 6 + $x_off]
    }

    set ncol [ llength $colores]
    for { set icol 0} { $icol < $ncol } { incr icol} {
    	 set color_relleno [ lindex $colores [ expr $ncol - $icol - 1]]
    	 $ContourColors(sitio_imagen) create rectangle \
		 $woff [ expr $hoff + $icol * $hcolor] \
		 [ expr $woff + $wcolor] [ expr $hoff + ( $icol + 1) * $hcolor] \
		 -fill $color_relleno -outline ""
    }

    # para diseņar la escala de colores
    if { $canvas_ampliado } {
	## pintar colores pivote
	set n [ llength $ContourColors(ColoresPivote)]
	set tam_piv 10

	set woff [ expr $woff - 10 - $tam_piv]
	
	set h_min_piv $hoff
	set h_max_piv [ expr $hoff + $ncol * $hcolor]
	
	#set pivotes_id ""
	
	if { [ expr ( $n + 1) * $tam_piv] >= $hcanvas} {
	    # hay mas pivotes * tamaņo que espacio en el canvas
	    for { set hcolor 0} { [ expr $hcolor * $n] < $hcanvas} { incr hcolor} {
	    }
	    if { $hcolor > 0} { set hcolor [ expr $hcolor - 1]}
	    set hoff [ expr int( 0.5 + ( $hcanvas - $hcolor * $n) * 0.5)]
	    
	    for { set icol 0} { $icol < $n } { incr icol} {
		set color_relleno [ lindex $ContourColors(ColoresPivote) [ expr $n - $icol - 1]]
		#lappend pivotes_id [ $ContourColors(sitio_imagen) create rectangle \
			$woff [ expr $hoff + $icol * $hcolor] \
			[ expr $woff + $tam_piv] [ expr $hoff + ( $icol + 1) * $hcolor] \
			-fill $color_relleno -outline ""]
	    }

	} else {
	    set hpiv [ expr $h_max_piv - $h_min_piv - $tam_piv]
	    set hdif [ expr double( $hpiv) / double( $n - 1.0)]
	    set hoff $h_min_piv
	    for { set icol 0} { $icol < $n } { incr icol} {
		set color_relleno [ lindex $ContourColors(ColoresPivote) [ expr $n - $icol - 1]]
		set id_col [ $ContourColors(sitio_imagen) create rectangle \
			$woff [ expr $hoff + $icol * $hdif] \
			[ expr $woff + $tam_piv] [ expr $hoff + $icol * $hdif + $tam_piv] \
			-fill $color_relleno -outline  $color_relleno]
		#lappend pivotes_id $id_col
		$ContourColors(sitio_imagen) bind $id_col <Any-Enter> \
			"$ContourColors(sitio_imagen) itemconfig current -outline white"
		$ContourColors(sitio_imagen) bind $id_col <Any-Leave> \
			"$ContourColors(sitio_imagen) itemconfig current -outline $color_relleno"
		$ContourColors(sitio_imagen) bind $id_col <1> \
			"CCCambiaColorPivote [ expr $n - $icol - 1] $id_col"
	    }
	}

	# botones de aņadir pivotes y quitar pivotes

	set tam_bot 20.0
	set tam_bot_2 [ expr $tam_bot * 0.5]
	set woff [ expr $woff - 12 - $tam_bot]

	set hbot [ expr $hcanvas]
	set hdif [ expr double( $hbot) / 3.0]
	set hoff [ expr $hdif - $tam_bot_2]

if { 0} {

	## boton mas
	if { [ winfo exist $ContourColors(sitio_imagen).mas]} { destroy  $ContourColors(sitio_imagen).mas}
	if { $CCOtroLook} {
	    BotonPlano $ContourColors(sitio_imagen).mas + "CCAnyadeColorPivote" \
		    black [ CCColorSombra $color_fondo] $color_fondo white
	    $ContourColors(sitio_imagen).mas conf -padx 0 -pady 2 -width 3
	} else {
	    if { ![ info exist ContourColors(mas_bitmap)] } {
		# mas invertido
		# set ContourColors(mas_bitmap) [ image create bitmap -data { 
		#     #define boton_mas_width 15
		#     #define boton_mas_height 15
		#     static unsigned char boton_mas_bits[] = {
		# 	  0x00, 0x00, 0x00, 0x00, 0xfc, 0x1f, 0xfc, 0x1f, 0x7c, 0x1f, 0x7c, 0x1f,
		# 	  0x7c, 0x1f, 0x0c, 0x18, 0x7c, 0x1f, 0x7c, 0x1f, 0x7c, 0x1f, 0xfc, 0x1f,
		# 	  0xfc, 0x1f, 0x00, 0x00, 0x00, 0x00};
		#     }  -foreground red]
		# mas normal
		set ContourColors(mas_bitmap) [ image create bitmap -data { 
		    #define boton_mas_width 15
		    #define boton_mas_height 15
		    static unsigned char boton_mas_bits[] = {
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x80, 0x00,
			0x80, 0x00, 0xf0, 0x07, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
		    }  -foreground red] 
            }
	    button $ContourColors(sitio_imagen).mas -image $ContourColors(mas_bitmap) -command "CCAnyadeColorPivote"
	}
	$ContourColors(sitio_imagen) create window $woff $hoff -anchor nw -window $ContourColors(sitio_imagen).mas

	# set mas_base [ $ContourColors(sitio_imagen) create rectangle \
	# 	  $woff $hoff \
	# 	  [ expr $woff + $tam_bot] [ expr $hoff + $tam_bot] \
	# 	  -fill black -outline ""]
	# 
	# set mas_rect [ $ContourColors(sitio_imagen) create rectangle \
	# 	  [ expr $woff + 1] [ expr $hoff + 1] \
	# 	  [ expr $woff + $tam_bot - 2] [ expr $hoff + $tam_bot - 2] \
	# 	  -outline $color_fondo]
	# 
	# set mas_line_1 [ $ContourColors(sitio_imagen) create line \
	# 	  [ expr $woff + 4] [ expr $hoff + $tam_bot_2] \
	# 	  [ expr $woff + $tam_bot - 4] [ expr $hoff + $tam_bot_2] \
	# 	  -fill $color_fondo]
	# 
	# set mas_line_2 [ $ContourColors(sitio_imagen) create line \
	# 	  [ expr $woff + $tam_bot_2 - 1.0] [ expr $hoff + 4] \
	# 	  [ expr $woff + $tam_bot_2 - 1.0] [ expr $hoff + $tam_bot - 4] \
	# 	  -fill $color_fondo]
	# 
	# foreach id_tag "$mas_rect $mas_line_1 $mas_line_2" {
	#     $ContourColors(sitio_imagen) bind $id_tag <Any-Enter> \
	# 	      "$ContourColors(sitio_imagen) itemconfig $mas_rect -outline white; \
	# 	      $ContourColors(sitio_imagen) itemconfig $mas_line_1 -fill white; \
	# 	      $ContourColors(sitio_imagen) itemconfig $mas_line_2 -fill white;" 
	#     $ContourColors(sitio_imagen) bind $id_tag <Any-Leave> \
	# 	      "$ContourColors(sitio_imagen) itemconfig $mas_rect -outline $color_fondo; \
	# 	      $ContourColors(sitio_imagen) itemconfig $mas_line_1 -fill $color_fondo; \
	# 	      $ContourColors(sitio_imagen) itemconfig $mas_line_2 -fill $color_fondo;" 
	# }

	## boton menos
	if { [ winfo exist $ContourColors(sitio_imagen).menos]} { destroy  $ContourColors(sitio_imagen).menos}
	if { $CCOtroLook} {
	    BotonPlano $ContourColors(sitio_imagen).menos - "CCQuitaColorPivote" black [ CCColorSombra $color_fondo] $color_fondo white
	    $ContourColors(sitio_imagen).menos conf -padx 0 -pady 2 -width 3
	} else {
	    if { ![ info exist ContourColors(menos_bitmap)] } {
		# menos invertido
		# set ContourColors(menos_bitmap) [ image create bitmap -data { 
		#     #define boton_menos_width 15
		#     #define boton_menos_height 15
		#     static unsigned char boton_menos_bits[] = {
		# 	  0x00, 0x00, 0x00, 0x00, 0xfc, 0x1f, 0xfc, 0x1f, 0xfc, 0x1f, 0xfc, 0x1f,
		# 	  0xfc, 0x1f, 0x0c, 0x18, 0xfc, 0x1f, 0xfc, 0x1f, 0xfc, 0x1f, 0xfc, 0x1f,
		# 	  0xfc, 0x1f, 0x00, 0x00, 0x00, 0x00};
		#     } -foreground blue] 
		# menos normal
		set ContourColors(menos_bitmap) [ image create bitmap -data { 
		    #define boton_menos_width 15
		    #define boton_menos_height 15
		    static unsigned char boton_menos_bits[] = {
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0xf0, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
		    } -foreground blue] 
            }
	    button $ContourColors(sitio_imagen).menos -image $ContourColors(menos_bitmap) -command "CCQuitaColorPivote"
	}
	$ContourColors(sitio_imagen) create window $woff [ expr $hoff + $hdif] -anchor nw -window $ContourColors(sitio_imagen).menos

	# $ContourColors(sitio_imagen) create rectangle \
	# 	  $woff [ expr $hoff + $hdif] \
	# 	  [ expr $woff + $tam_bot] [ expr $hoff + $hdif + $tam_bot] \
	# 	  -fill black -outline ""
	# 
	# set menos_rect [ $ContourColors(sitio_imagen) create rectangle \
	# 	  [ expr $woff + 1] [ expr $hoff + $hdif + 1] \
	# 	  [ expr $woff + $tam_bot - 2] [ expr $hoff + $hdif + $tam_bot - 2] \
	# 	  -outline $color_fondo]
	# 
	# set  menos_line [ $ContourColors(sitio_imagen) create line \
	# 	  [ expr $woff + 4] [ expr $hoff + $tam_bot_2] \
	# 	  [ expr $woff + $tam_bot - 4] [ expr $hoff + $tam_bot_2] \
	# 	  -fill $color_fondo]
	

    }
}

}

proc CCAnyadeColorPivote { } {
    global ContourColors

    lappend ContourColors(ColoresPivote) #808080
    incr ContourColors(NumPivotes)
    CCActualizaCanvas
}

proc CCNumPivotesChange { op name1 name2} {
    global ContourColors

    if { "$ContourColors(NumPivotes)" == ""} { return}

    set l [ llength $ContourColors(ColoresPivote)] 

    if { $ContourColors(NumPivotes) < 3} {
	set ContourColors(NumPivotes) 3
	bell
	return
    }

    if { $ContourColors(NumPivotes) > $l} {
	after cancel CCActualizaCanvas
	for { set i $l } { $i < $ContourColors(NumPivotes)} { incr i} {
	    lappend ContourColors(ColoresPivote) #808080
	}
	after 1500 CCActualizaCanvas
    } elseif { $ContourColors(NumPivotes) < $l} {
	after cancel CCActualizaCanvas
	set ContourColors(ColoresPivote) [ lrange $ContourColors(ColoresPivote) 0 [ expr $ContourColors(NumPivotes) - 1]]
	after 1500 CCActualizaCanvas
    }

}

proc CCQuitaColorPivote { } {
    global ContourColors

    if { [ llength $ContourColors(ColoresPivote)] > 3} {
	set ContourColors(ColoresPivote) [ lrange $ContourColors(ColoresPivote) 0 [ expr [ llength $ContourColors(ColoresPivote)] - 2]]

    set ContourColors(NumPivotes) [ expr $ContourColors(NumPivotes) - 1]
	CCActualizaCanvas
    } else {
	bell
    }
}

proc CCCambiaColorPivote { num_pivote tag_pivote} {
    global ContourColors

    set color_viejo [ $ContourColors(sitio_imagen) itemcget $tag_pivote -fill]
    set color_nuevo [ tk_chooseColor -parent $ContourColors(sitio_imagen) -initialcolor $color_viejo]
    if { "$color_nuevo" != ""} {
	set n [ scan $color_nuevo #%2x%2x%2x r g b]
	if { $n == 3} {
	    $ContourColors(sitio_imagen) itemconfig $tag_pivote -fill $color_nuevo
	    #WarnWinText "$ContourColors(ColoresPivote)"
	    #WarnWinText "$num_pivote -> $color_nuevo"
	    set new_lst ""
	    set np [ llength $ContourColors(ColoresPivote)]
	    for { set ip 0 } { $ip < $np} { incr ip} {
		if { $ip != $num_pivote} {
		    lappend new_lst [ lindex $ContourColors(ColoresPivote) $ip]
		} else {
		    lappend new_lst $color_nuevo
		}
	    }
	    set ContourColors(ColoresPivote) $new_lst
	    #WarnWinText "$ContourColors(ColoresPivote)"
	    after 1500 CCActualizaCanvas
	}
    }
}



proc PostContourColors { { w .wContourColors} { CCRunningAlone 0}} {
    global ContourColors GIDDEFAULT CCOtroLook
    
    if { $CCRunningAlone} {
	toplevel $w
    } else {
	InitWindow $w [_ "Contour colors"] PostContourColorsWindowGeom PostContourColors
    }

    set ContourColors(Window) $w

    catch {
	trace vdelete ContourColors(NumeroColores) w CCNumColsChange
	trace vdelete ContourColors(ColorMapType) w CCColorMapTypeChange
	trace vdelete ContourColors(UserMode) w CCColorMapUserModeChange
	trace vdelete ContourColors(UserMinColor) w CCColorUserMapChange
	trace vdelete ContourColors(UserMaxColor) w CCColorUserMapChange
	trace vdelete ContourColors(MapScaleType) w CCColorUserMapChange
	trace vdelete ContourColors(MapScaleRamp) w CCColorUserMapChange
	trace vdelete ContourColors(NumPivotes) w CCNumPivotesChange
    }

    CCInitValues

    frame $w.basico -bd 0 -relief ridge

    set color_fondo [ CCGetRGB $w [ $w cget -background]]

    # Number of colors
    frame $w.basico.nc -bd 0 -relief groove
    label $w.basico.nc.num_cols_txt -text [_ "Number of colors: "]
    entry $w.basico.nc.num_cols -width 4 -textvariable ContourColors(NumeroColores)
    if { $CCOtroLook} {
	$w.basico.nc.num_cols conf -bd 0
    } else {
	$w.basico.nc.num_cols conf -bd 1 -relief sunken
    }
    grid $w.basico.nc.num_cols_txt $w.basico.nc.num_cols -pady 2 -padx 4 -sticky wn
    set ContourColors(NumeroColoresEntry) $w.basico.nc.num_cols

    GiDPopUpHelp $w.basico.nc "The color map for contours will use this amount of colors"

    ### # Outlimit colors
    ### frame $w.basico.oc -bd 0 -relief groove
    ### 
    ### frame $w.basico.oc.max
    ### label $w.basico.oc.max.txt -text [_ "Max:"]
    ### BotonPlano $w.basico.oc.max.b "" "CCCambiaColor $w.basico.oc.max.b ContourColors(OutMaxColor)" \
    ### 	  $ContourColors(OutMaxColor) [ CCColorActivo $ContourColors(OutMaxColor) 34]
    ### $w.basico.oc.max.b conf -width 4
    ### grid $w.basico.oc.max.txt $w.basico.oc.max.b -pady 2 -padx 0 -sticky en
    ### 
    ### frame $w.basico.oc.min 
    ### label $w.basico.oc.min.txt -text [_ "Min:"]
    ### BotonPlano $w.basico.oc.min.b "" "CCCambiaColor $w.basico.oc.min.b ContourColors(OutMinColor)" \
    ### 	  $ContourColors(OutMinColor) [ CCColorActivo $ContourColors(OutMinColor) 34]
    ### $w.basico.oc.min.b conf -width 4
    ### grid $w.basico.oc.min.txt $w.basico.oc.min.b -pady 2 -padx 0 -sticky en
    ### 
    ### grid $w.basico.oc.max $w.basico.oc.min -padx 2
    

    # Color map
    frame $w.basico.cm -bd 0 -relief groove
    
    frame $w.basico.cm.map -bd 0
    label $w.basico.cm.map.txt -text [_ "Color map:"]
    if { $CCOtroLook} {
	OptionMenuPlano $w.basico.cm.map.om ContourColors(ColorMapType) \
		{ Rainbow "Inv. rainbow" Terrain "Black & White" "User defined"} \
		[ CCColorSombra $color_fondo] [ CCColorActivo $color_fondo]
    } else {
	set menu [ tk_optionMenu  $w.basico.cm.map.om ContourColors(ColorMapType) Rainbow "Inv. rainbow" Terrain "Black & White" "User defined"]
	$w.basico.cm.map.om conf -bd 1
	$menu conf -bd 1 -activeborderwidth 1
    }
    $w.basico.cm.map.om conf -width 11
    grid $w.basico.cm.map.txt $w.basico.cm.map.om -pady 2 -padx 4 -sticky wn

    GiDPopUpHelp $w.basico.cm.map "Type of color map to be used for contours: \n"\
	    "four predefined ones and one customizable"

    frame $w.basico.cm.img -bd 0

    #set ContourColors(lista_user_map_imagenes) ""
    #foreach vi "rainbow invrainbow terrain blackwhite" {
    #	 if { ![info exist ContoursColors(${vi}_img)]} {
    #	     set ContoursColors(${vi}_img) [ image create photo -file $GIDDEFAULT/bitmaps/${vi}.gif]
    #	 }
    #	 lappend ContourColors(lista_user_map_imagenes) $ContoursColors(${vi}_img)
    #}
    #label $w.basico.cm.img.l -image $ContoursColors(rainbow_img)
    #grid  $w.basico.cm.img.l
    #set ContourColors(sitio_imagen) $w.basico.cm.img.l
    #canvas $w.basico.cm.img.c -width 25 -height 150
    canvas $w.basico.cm.img.c -width 50 -height 150
    grid  $w.basico.cm.img.c
    set ContourColors(sitio_imagen) $w.basico.cm.img.c

    GiDPopUpHelp $w.basico.cm.img "Preview of the contour map which interactivelly shows the changes "\
	    "on the options in this window. \n" \
	    "Changes will only be used in GiD, after pressing the 'Apply' button."\
	    "In 'Design' mode, the 'Key colors' can also be defined just clicking over them."

    radiobutton $w.basico.cm.dos_colores_1 -text [_ "Two colors scale:"] -variable ContourColors(UserMode) \
	    -value DosColores -command "CCActualizaCanvas"

    GiDPopUpHelp $w.basico.cm.dos_colores_1 "The 'User defined' color map will be defined by two colors"

    frame $w.basico.cm.max
    label $w.basico.cm.max.txt -text [_ "Max:"]
    if {  $CCOtroLook} {
	BotonPlano $w.basico.cm.max.b "" "CCCambiaColor $w.basico.cm.max.b ContourColors(UserMaxColor)" \
		$ContourColors(UserMaxColor) [ CCColorActivo $ContourColors(UserMaxColor) 34]
    } else {
	button $w.basico.cm.max.b -command "CCCambiaColor $w.basico.cm.max.b ContourColors(UserMaxColor)" -bd 1
    }
    $w.basico.cm.max.b conf -width 3
    if { !$CCOtroLook } {
	$w.basico.cm.max.b conf -background $ContourColors(UserMaxColor)
    }
    grid $w.basico.cm.max.txt -row 0 -column 0 -pady 2 -padx 0 -sticky en
    grid $w.basico.cm.max.b -row 0 -column 1 -pady 2 -padx 0 -sticky en

    GiDPopUpHelp $w.basico.cm.max "Selection of the Maximum color for the "\
	    "'Two colors' 'User defined' contour color map"

    frame $w.basico.cm.min 
    label $w.basico.cm.min.txt -text [_ "Min:"]
    if {  $CCOtroLook} {
	BotonPlano $w.basico.cm.min.b "" "CCCambiaColor $w.basico.cm.min.b ContourColors(UserMinColor)" \
		$ContourColors(UserMinColor) [ CCColorActivo $ContourColors(UserMinColor) 34]
    } else {
	button  $w.basico.cm.min.b -command "CCCambiaColor $w.basico.cm.min.b ContourColors(UserMinColor)" -bd 1
    }
    $w.basico.cm.min.b conf -width 3
    if { !$CCOtroLook } {
	$w.basico.cm.min.b conf -background $ContourColors(UserMinColor)
    }
    grid $w.basico.cm.min.txt -row 0 -column 0 -pady 2 -padx 0 -sticky en
    grid $w.basico.cm.min.b -row 0 -column 1 -pady 2 -padx 0 -sticky en

    GiDPopUpHelp $w.basico.cm.min "Selection of the Minimum color for the "\
	    "'Two colors' 'User defined' contour color map"

    # frame $w.basico.cm.scale -bd 0
    # label $w.basico.cm.scale.txt -text [_ "Type:"] -padx 2
    if { $CCOtroLook} {
	OptionMenuPlano $w.basico.cm.om ContourColors(MapScaleType) "RGB HSV" \
		[ CCColorSombra $color_fondo] [ CCColorActivo $color_fondo]
    } else {
	set menu [ tk_optionMenu $w.basico.cm.om ContourColors(MapScaleType) RGB HSV]
	$w.basico.cm.om conf -bd 1
	$menu conf -bd 1 -activeborderwidth 1
    }
    $w.basico.cm.om conf -width 4 -padx 2

    GiDPopUpHelp $w.basico.cm.om "Type of color 'interpolation' between the two choosen ones: \n"\
	    "HSV: a rotation along the colors according the HSV convention, \n"\
	    "RGB: a gradual change of the Red, Green and Blue components."

    if { $CCOtroLook } {
	OptionMenuPlano $w.basico.cm.om2 ContourColors(MapScaleRamp) "Linear Tangent ArcTang" \
		[ CCColorSombra $color_fondo] [ CCColorActivo $color_fondo]
    } else {
	set menu [ tk_optionMenu $w.basico.cm.om2 ContourColors(MapScaleRamp) Linear Tangent ArcTang ]
	$w.basico.cm.om2 conf -bd 1
	$menu conf -bd 1 -activeborderwidth 1
    }
    $w.basico.cm.om2 conf -width 7 -padx 2

    GiDPopUpHelp $w.basico.cm.om2 "Distribution of the colors between the two choosen ones: \n"\
	    "Linear: colors distributed uniformly, \n"\
	    "Tangent: colors concentrated in the middle of the 'interpolation', \n"\
	    "ArcTangent: colors concentrated in the ends. \n"

    #grid $w.basico.cm.scale.txt $w.basico.cm.scale.om $w.basico.cm.scale.om2 -pady 2 -padx 2 -sticky wn
    #grid  $w.basico.cm.om -pady 2 -padx 2 -sticky en
    #grid  $w.basico.cm.om2 -pady 2 -padx 2 -sticky en

    #set ContourColors(lista_dos_colores_colores) "$w.basico.cm.max.txt $w.basico.cm.min.txt $w.basico.cm.scale.txt"
    set ContourColors(lista_dos_colores_colores) "$w.basico.cm.max.txt $w.basico.cm.min.txt"
    set ContourColors(lista_dos_colores_estado) "$w.basico.cm.max.b $w.basico.cm.min.b $w.basico.cm.om $w.basico.cm.om2"

    frame $w.basico.cm.avz -bd 0
    radiobutton $w.basico.cm.avz.dos_colores_2 -text [_ "Design:"] -variable ContourColors(UserMode) \
	    -value Disenno -command "CCActualizaCanvas"
    #BotonPlano $w.basico.cm.avz.avanzado "Advanced >>" "WarnWin {Still not available} $w" \
    #	     [ CCColorSombra $color_fondo] [ CCColorActivo $color_fondo]
    label $w.basico.cm.avz.avanzado -text [_ "Key colors:"]
    UpDownEntry $w.basico.cm.avz.e -variable ContourColors(NumPivotes)\
	    -up-command "CCAnyadeColorPivote" -down-command "CCQuitaColorPivote" \
	    -entry-options "-relief sunken -width 4" \
	    -button-options "-bd 1" \
	    -up-color red -down-color blue
    grid $w.basico.cm.avz.dos_colores_2 -row 0 -column 0 -sticky w -pady 2 -padx 0
    grid $w.basico.cm.avz.avanzado -row 0 -column 1 -sticky w -pady 2
    grid $w.basico.cm.avz.e -row 0 -column 2 -sticky e -pady 2 -padx 2

    GiDPopUpHelp $w.basico.cm.avz.e "Number of colors to be used as 'Key colors' "\
	    "in the 'Design' 'User defined' contour color map."

    GiDPopUpHelp $w.basico.cm.avz "The 'User defined' color map "\
	    "will be defined by several 'Key colors', which can be choosed by the user. "\
	    "These 'Key colors' are equally spaced along the map."

    set ContourColors(lista_disenno_colores) "$w.basico.cm.avz.avanzado"
    set ContourColors(lista_disenno_estado) "$w.basico.cm.avz.e"

    grid rowconfigure $w.basico.cm.avz 0 -weight 1
    grid columnconfigure $w.basico.cm.avz 0 -weight 1
    #grid columnconfigure $w.basico.cm.avz 1 -weight 1


    grid $w.basico.cm.map -row 0 -column 0 -padx 0 -pady 0 -sticky wn -columnspan 3
    grid $w.basico.cm.dos_colores_1 -row 1 -column 0 -padx 0 -pady 0 -sticky wn  -columnspan 2
    grid $w.basico.cm.max -row 2 -column 1 -padx 0 -pady 0 -sticky en
    grid $w.basico.cm.min -row 3 -column 1 -padx 0 -pady 0 -sticky en
    #grid $w.basico.cm.scale -row 2 -column 0 -padx 0 -pady 0 -sticky en -rowspan 2
    grid  $w.basico.cm.om -row 2 -column 0 -pady 0 -padx 0 -sticky en -padx 4
    grid  $w.basico.cm.om2 -row 3 -column 0  -pady 0 -padx 0 -sticky en -padx 4
    grid $w.basico.cm.img -row 1 -column 2 -rowspan 4 -padx 4
    grid $w.basico.cm.avz -row 4 -column 0 -padx 0 -pady 0 -ipadx 0 -ipady 0 -sticky ews -columnspan 2

    set ContourColors(lista_user_map_estado) "$w.basico.cm.dos_colores_1 $w.basico.cm.avz.dos_colores_2"

    grid rowconfigure $w.basico.cm 0 -weight 1
    grid rowconfigure $w.basico.cm 1 -weight 1
    grid rowconfigure $w.basico.cm 2 -weight 1
    grid rowconfigure $w.basico.cm 3 -weight 1
    grid rowconfigure $w.basico.cm 4 -weight 1
    #grid rowconfigure $w.basico.cm 5 -weight 1
    grid columnconfigure $w.basico.cm 0 -weight 1
    #grid columnconfigure $w.basico.cm 1 -weight 1


    grid $w.basico.nc -sticky nwe
    ### grid $w.basico.oc -sticky nwe
    grid $w.basico.cm -sticky nwes

    grid rowconfigure $w.basico 1 -weight 1
    grid columnconfigure $w.basico 0 -weight 1

    ####################################
    ### Botones
    ####################################

    frame $w.botones -bd 0 -background [ CCColorActivo $color_fondo]

    if {  $CCOtroLook} {
	BotonPlano $w.botones.aceptar Apply "CCAceptar $w" #bbddbb #ccffcc
	BotonPlano $w.botones.reset Reset "CCResetValores $w" #ddddbb #ffffcc
	BotonPlano $w.botones.cerrar Close "destroy $w" #ddbbbb #ffcccc
    } else {
	button $w.botones.aceptar -text [_ "Apply"] -command "CCAceptar $w"
	button $w.botones.reset -text [_ "Reset"] -command "CCResetValores $w"
	button $w.botones.cerrar -text [_ "Close"] -command "destroy $w"
    }

    GiDPopUpHelp $w.botones.aceptar "The color map defined in this window will be used in GiD "\
	    "when contours, isosurfaces, etc. are displayed."

    GiDPopUpHelp $w.botones.reset "Values of this window will be restored to the ones used in GiD "\
	    "before the window was openned."

    GiDPopUpHelp $w.botones.cerrar "The window will be closed losing the changes made but not applied."

    grid $w.botones.aceptar $w.botones.reset $w.botones.cerrar -pady 10 -padx 4 -sticky ews

    grid $w.basico -sticky wens -pady 4 -padx 4
    grid $w.botones -sticky sew

    grid rowconfigure $w 0 -weight 1
    grid columnconfigure $w 0 -weight 1

    focus $w.basico.nc.num_cols

    trace variable ContourColors(NumeroColores) w CCNumColsChange
    trace variable ContourColors(ColorMapType) w CCColorMapTypeChange
    trace variable ContourColors(UserMode) w CCColorMapUserModeChange
    trace variable ContourColors(UserMinColor) w CCColorUserMapChange
    trace variable ContourColors(UserMaxColor) w CCColorUserMapChange
    trace variable ContourColors(MapScaleType) w CCColorUserMapChange
    trace variable ContourColors(MapScaleRamp) w CCColorUserMapChange
    trace variable ContourColors(NumPivotes) w CCNumPivotesChange

    set ContourColors(NumeroColores) $ContourColors(NumeroColores)
    set ContourColors(UserMode) $ContourColors(UserMode)
    set ContourColors(ColorMapType) $ContourColors(ColorMapType)

}


#----------------------SetCoord ALONE---------------------------
if { $CCRunningAlone == 1 } {
    #global GIDDEFAULT env

    #set GIDDEFAULT $env(GIDDEFAULT)
    PostContourColors .wPostCC $CCRunningAlone
    .wPostCC.botones.cerrar conf -command "destroy .wPostCC; exit"
} 
#-----------------------------------------------------------------

