#!bin/wish
wm title . "Code des couleurs"
wm geometry . 180x120


proc init {} {
global tab
    set tab(couleur,0) black
    set tab(couleur,1) brown
    set tab(couleur,2) red
    set tab(couleur,3) orange
    set tab(couleur,4) yellow
    set tab(couleur,5) green
    set tab(couleur,6) SteelBlue1  ;# blue
    set tab(couleur,7) violet
    set tab(couleur,8) gray
    set tab(couleur,9) white
    
    set tab(multi,0) black
    set tab(multi,1) brown
    set tab(multi,2) red
    set tab(multi,3) orange
    set tab(multi,4) yellow
    set tab(multi,5) green
    set tab(multi,6) SteelBlue1  ;# blue
    set tab(multi,7) violet
    
    set tab(res) 0
    
    for  {set i 0} {$i<10} {incr i} {
        set tab($i) 0

    }
    
    frame .f1 -width 2 -height 15
        
    pack .f1
    
    frame .an -borderwidth 1 -relief sunken 
    for  {set i 0} {$i<3} {incr i} {
        ;# set cmd "button .an.b$i -textvariable tab($i) -command {changeVal $i 1} -bg black -fg white"
        set cmd "label .an.b$i -textvariable tab($i) -bg black -fg white -width 2"
        eval $cmd
        grid .an.b$i -column $i -row 0
        set cmd "bind .an.b$i <1> {changeVal $i 1} "
        eval $cmd
        set cmd "bind .an.b$i <3> {changeVal $i 0} "
        eval $cmd
    }
    label .an.sp -text " * 10^" -relief groove
    grid .an.sp -column 3 -row 0
    
    label .an.b4 -textvariable tab(4) -bg black -fg white -width 2 
    grid .an.b4 -column 4 -row 0
    bind .an.b4 <1> {changeVal 4 1}
    bind .an.b4 <3> {changeVal 4 0}
    
    frame .f2    
    label .f2.res -textvariable tab(res) -bg white -width 10
    label .f2.ohm -text "Ohms"
    grid .f2.res .f2.ohm
    pack .an .f2 -pady 5
    
    bind . <Escape> exit
}

 init ;# Initialisation du programme

proc changeVal {bt sens} {
    global tab 
    if {$sens==0} {
        set tab($bt) [expr ($tab($bt) - 1) %10]
    } else  {
        set tab($bt) [expr ($tab($bt) + 1) %10]
    }
    
    if {$tab($bt)>1} {
        set fg black
    } else  {
        set fg white
    }
    set cmd ".an.b$bt configure  -bg $tab(couleur,$tab($bt)) -fg $fg"
    
    eval $cmd
    switch $bt {
        "1" {
          
        }
    }
    for  {set i $tab(4);set coef 1} {$i>0} {set i [expr $i-1]} { set coef [expr $coef * 10] }
    
    set tab(res) [expr (100 * $tab(0)) + (10* $tab(1)) + $tab(2)]
    set tab(res) [expr $tab(res) * $coef ]
    update
}

