#!/usr/bin/perl -w
######################################################################
# scale.pl
######################################################################
# Perl Power! - Michael Schilli 1998
######################################################################

use Tk; 

$top = MainWindow->new();

$red = $top->Scale(-from => 0, -to => 255, 
                   -orient => "horizontal",
                   -label => "Red", -command => \&upd_color);
$green = $top->Scale(-from => 0, -to => 255, 
                     -orient => "horizontal",
                     -label => "Green", 
                     -command => \&upd_color);
$blue = $top->Scale(-from => 0, -to => 255, 
                    -orient => "horizontal",
                    -label => "Blue", 
                    -command => \&upd_color);

$red->pack(); 
$green->pack(); 
$blue->pack(); 

MainLoop;

# upd_color - update foreground and background color

sub upd_color {
    my $background = "#";
    my $total      = 0;

    # read values and build numerical color description
foreach $i ($red,$green,$blue) {
        my $value = $i->get();
        $background .= sprintf("%02x", $value);
        $total += $value;
    }

    # labeling color in function
    # of total brightness
    my $foreground = $total < 255 ? "white" : "black";

    foreach $i ($red,$green,$blue) {
        $i->configure(-background => $background, 
                      -foreground => $foreground);
    }
}

