#!/usr/local/bin/perl
#
# colorname  RRGGBB...
#
#   Try to discover a given color pixel's real X windows color name
#
$COLOR = "/usr/tmp/color";   # the location of the inverted color database
$pixel_t = 'H12';

if ( ! -f $COLOR . ".dir" ) {
  warn( "Unable to find \"color\" database! -- tring to create it\n" );
  system( 'mk-color-db >/dev/null' );
}
dbmopen(%COLOR, $COLOR, undef) || die "Unable to open color database\n";

while( $_ = shift ) {
  if ( ($pixel) = /#?([0-9A-Fa-f]+)/ ) {
    $pixel = join( '', substr($pixel, 0, 1) x 4,
		       substr($pixel, 1, 1) x 4,
		       substr($pixel, 2, 1) x 4 ) if length($pixel) == 3;
    $pixel = join( '', substr($pixel, 0, 2) x 2,
		       substr($pixel, 2, 2) x 2,
		       substr($pixel, 4, 2) x 2 ) if length($pixel) == 6;
    if ( $color = $COLOR{ pack( $pixel_t, $pixel ) } ) {
      print "$color\n";
    } else {
      print "#$pixel\n";
    }
  }
  else {
    print STDERR "Argument $_ is not a hex color value\n";
  }
} # while argument

