colorToAlpha static method

Image colorToAlpha(
  1. Image image,
  2. int r,
  3. int g,
  4. int b,
)

Makes pixels of the image transparent if color is defined as r, g, b.

returns an image, yet to be encoded.

Implementation

static Image colorToAlpha(Image image, int r, int g, int b) {
  if (image.numChannels == 3) {
    image = image.convert(numChannels: 4);
  }
  var data = image.data!;
  for (var row = 0; row < data.height; row = row + 1) {
    for (var col = 0; col < data.width; col = col + 1) {
      var p = data.getPixel(col, row);
      if (p.r.toInt() == r && p.g.toInt() == g && p.b.toInt() == b) {
        p.setRgba(r, g, b, 0);
      }
    }
  }
  return image;
  // for (var i = 0; i < length; i = i + 4) {
  //   if (pixels[i] == r && pixels[i + 1] == g && pixels[i + 2] == b) {
  //     pixels[i + 3] = 0;
  //   }
  // }
  // set the fact that it now has an alpha channel, else it will not work if prior rgb
  // image.channels = Channels.rgba;
}