for (@ARGV) {
    die "All arguments must be decimal numbers!\n"
	unless /^([1-9][0-9]*|0)$/;
}

if ($#ARGV + 1 != 16) {
    die <<"EOF"
Invalid parameters.

Usage:
	$0 \\ # Many (16) arguments follow...
	   odd_lo_R odd_lo_G odd_lo_B	\\ #  odd scanline color, dark area
	   evn_lo_R evn_lo_G evn_lo_B	\\ # even scanline color, dark area
	   odd_hi_R odd_hi_G odd_hi_B	\\ #  odd scanline color, light area
	   evn_hi_R evn_hi_G evn_hi_B	\\ # even scanline color, light area
	   width			\\ # image width
	   lo_height hi_height		\\ # height of the dark and light areas
	   frame_delay			\\ # speed in ticks
		> output.gif
EOF
}

($aLoR, $aLoG, $aLoB,		#  odd scanline color, dark area
 $bLoR, $bLoG, $bLoB,		# even scanline color, dark area
 $aHiR, $aHiG, $aHiB,		#  odd scanline color, light area
 $bHiR, $bHiG, $bHiB,		# even scanline color, light area
 $W,				# image width
 $loH, $hiH,			# height of the dark and light areas
 $dly,				# speed in ticks
) = @ARGV;

$H = $loH + $hiH;		# Total height.

$tmpname = "temp.rgb";

$aLoPk = pack "CCC", $aLoR, $aLoG, $aLoB;
$aHiPk = pack "CCC", $aHiR, $aHiG, $aHiB;
$bLoPk = pack "CCC", $bLoR, $bLoG, $bLoB;
$bHiPk = pack "CCC", $bHiR, $bHiG, $bHiB;

$aLoPk x= $W;
$bLoPk x= $W;
$aHiPk x= $W;
$bHiPk x= $W;

open $RGB, ">$tmpname";
print $RGB ($_ & 1 ? $bLoPk : $aLoPk) for 0..$loH-1;
print $RGB ($_ & 1 ? $bHiPk : $aHiPk) for 0..$hiH-1;
close $RGB;

$cmd = 'magick';

$frames = sprintf "%d", $H / 2;
# If the height is odd, we need to repeat the animation twice to roll over back.
$frames *= 2 if $H & 1;

for $frN (0..$frames-1) {
    $rollY = $frN * 2;
    $cmd .= " ( -size ${W}x${H} -depth 8 rgb:$tmpname -roll +0+$rollY )";
}

$cmd .= " -layers OptimizePlus -loop -1 -delay $dly gif:-";

print STDERR $cmd, "\n";

# We pass an array to the ‘system’ to avoid going thru shell.
system split /\s+/, $cmd;
