Grin with cat attached (wechsler) wrote,
Grin with cat attached
wechsler

  • Mood:

Coding practice

Just so I can still claim to write perl, and since you wondered

 
This is the RGB-average colour of the proposed EU flag


 
This is the CIEXYZ-average colour of the proposed EU flag



[wechsler@home ~] ~/xpmavg.pl < euroflag410x283.xpm
Total: R:0x11dd32e/18731822 G:0xcc85be/13403582 B:0xc474f3/12874995 Pels:112580
Avg: R:0xa6/166.386764967134 G:0x77/119.058287440043 B:0x72/114.363075146562 Pels:112580


#!/usr/bin/perl

#XPM averager: Read in a 2-char XPM file and produce pixel totals and avg
#Usage: xpmavg.pl < file.xpm

while ($line = <>) {
	if($line =~ /"(..)\s+c\s#(\w\w)(\w\w)(\w\w)"/) {
		$r{$1}=hex($2);
		$g{$1}=hex($3);
		$b{$1}=hex($4);
		$hadpal=1;
	} elsif ($hadpal && $line =~ /".*"/) {
		$line =~ s/"(.*)"/$1/;
		while ($line =~ s/^(..)//) {
			if($r{$1}) {
				$red+=$r{$1};	
				$green+=$g{$1};	
				$blue+=$b{$1};
				$pels++;
			}
		}
	}
}
print("\n");
printf("Total: R:0x%02x/$red G:0x%02x/$green B:0x%02x/$blue Pels:$pels\n",$red,$green,$blue); 
$red/=$pels;
$green/=$pels;
$blue/=$pels;
printf("Avg: R:0x%02x/$red G:0x%02x/$green B:0x%02x/$blue Pels:$pels\n",$red,$green,$blue); 

Subscribe
  • Post a new comment

    Error

    Comments allowed for friends only

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 8 comments