The Imager module has some good tools for this. For example check out
. That script shows tinting, but you can probably find a way to replace just one color. Look thru the perldocs for Imager, there may be an example in there. I would think as a last resort, you could unpack the jpg to rgb values, then use a substitution regex on the color you want changed, then pack it back to binary.