You can't in a GIF. For JPEG, PNG, WBMP, however:
First, load the image. This will require previous knowledge of the filetype, or a switch.
$im = imagecreatefrompng("file.png");
Now, set the color index:
$index = imagecolorat($im,$x,$y);
Get the color from this index:
$rgb = imagecolorsforindex($im,$index);
Then set variables for the colours, if you so wish:
$r = $rgb["red"];
$g = $rgb["green"];
$b = $rgb["blue"];
In RGB, the color is $r,$g,$b. For a grayscale equivalent, it is (int)(($r+$g+$b)/3),(int)(($r+$g+$b)/3),(int)(($r+$g+$b)/3).
To set a variable color for this color index, add this line:
imagecolorallocate($im,$r,$g,$b);
Or use the index.