chiark / gitweb /
New puzzle: 'Flood'.
[sgt-puzzles.git] / icons / square.pl
index 81be9d710c5e51a45b5a8ef5b3067ef597e21314..815b94b5321c0c15d473084d92356103dc38cbc6 100755 (executable)
@@ -26,9 +26,18 @@ $al = scalar @$data;
 die "wrong amount of image data ($al, expected $xl) from $infile\n"
   unless $al == $xl;
 
-# Find the background colour. We assume the image already has a
-# border, so this is just the pixel colour of the top left corner.
-$back = $data->[0];
+# Find the background colour, by looking around the entire border
+# and finding the most popular pixel colour.
+for ($i = 0; $i < $w; $i++) {
+    $pcount{$data->[$i]}++; # top row
+    $pcount{$data->[($h-1)*$w+$i]}++; # bottom row
+}
+for ($i = 1; $i < $h-1; $i++) {
+    $pcount{$data->[$i*$w]}++; # left column
+    $pcount{$data->[$i*$w+$w-1]}++; # right column
+}
+@plist = sort { $pcount{$b} <=> $pcount{$a} } keys %pcount;
+$back = $plist[0];
 
 # Crop rows and columns off the image to find the central rectangle
 # of non-background stuff.