#!/usr/athena/bin/perl
@n = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f');
$sgttyb_t   = 'C4 S';
$winsz_t = "S S S S";  # rows,cols, xpixel, ypixel
$default_num_cols = 80; # if can't be gotten from ioctl...
$TIOCGETP = 0x40067408;
$TIOCSETP = 0x80067409;
$TIOCGWINSZ = 0x40087468;
$RAW = 0x20;
$ECHO = 0x8;

sub start {
#    system 'stty raw -echo';

    ioctl(STDIN,$TIOCGETP,$sgttyb) || die "Can't ioctl TIOCGETP: $!";
    @tty_buf = unpack($sgttyb_t,$sgttyb);
    $tty_buf[4] |= $RAW;
    $tty_buf[4] &= ~$ECHO;
    $sgttyb = pack($sgttyb_t,@tty_buf);
    ioctl(STDIN,$TIOCSETP,$sgttyb) || die "Can't ioctl TIOCSETP: $!";
}


print("Welcome to Color Fiddle!!\n");
print("-------------------------\n");
print("\nR) Add some Red\t\tr) Remove some red\n");
print("G) Add some Green\tg) Remove some green\n");
print("B) Add some Blue\tb) Remove some blue\n");
print("?) What color is it?\tx) Exit Color Fiddle\n");
print("\n");
$foo = `xsetroot -solid #000`;
$red = 0;
$green = 0;
$blue = 0;
#&start;

$|=1;
while(1){
$char = getc(STDIN);
#chop($char);

if($char eq "R"){
	$red++;
	if($red>15){$red=15;}
}
elsif($char eq "r"){
	$red--;
	if($red<0){$red=0;}
}
elsif($char eq "G"){
	$green++;
	if($green>15){$green=15;}
}
elsif($char eq "g"){
	$green--;
	if($green<0){$green=0;}
}
elsif($char eq "B"){
	$blue++;
	if($blue>15){$blue=15;}
}
elsif($char eq "b"){
	$blue--;
	if($blue<0){$blue=0;}
}
elsif($char eq "?"){
	$color = $n[$red].$n[$green].$n[$blue];
	print("Color: #$color\n");
}
elsif($char eq "x"){
	exit(1);
}

$color = "#".$n[$red].$n[$green].$n[$blue];
$foo = `xsetroot -solid $color`;
#print("$color, $red, $green, $blue\n");

}


