Instead of using imagecolorat() on each individual pixel try changing the pallette for the image
$im = imagecreatefromgif('myimage.gif');
imagecolorset($im, 0, 0xFF, 0, 0);
imagecolorset($im, 1, 0, 0xFF, 0);
imagecolorset($im, 2, 0, 0, 0xFF);
imagegif($im);
imagedestroy($im);