def color_replace!( color1, color2)
      strcolor1 = color1.to_s
      strcolor2 = color2.to_s

      a = color2.a.chr

      (0..width*height).each do |i|
        if pixel_data[i*3..i*3+2] == strcolor1 then
          pixel_data[i*3..i*3+2] = strcolor2
          alpha_data[i] = a
        end
      end
    end
def color_replace!( color1, color2)
      strcolor1 = color1.to_s
      strcolor2 = color2.to_s

      a = color2.a.chr

      (0..width*height).each do |i|
        if pixel_data[i*3..i*3+2] == strcolor1 then
          pixel_data[i*3..i*3+2] = strcolor2
          alpha_data[i] = a
        end
      end
    end
Replace the color given in the first argument by the color given in the second with alpha values

 Examples

   image.color_replace!(Color.red, Color.black) # replace red with black
   image.color_replace!(Color.black, Color.coerce([0,0,0,128]) ) # replace black with %50 transparent black