fillFlood function

Image fillFlood(
  1. Image src,
  2. {required int x,
  3. required int y,
  4. required Color color,
  5. num threshold = 0.0,
  6. bool compareAlpha = false,
  7. Image? mask,
  8. Channel maskChannel = Channel.luminance}
)

Fill the 4-connected shape containing x,y in the image src with the given color.

Implementation

Image fillFlood(Image src,
    {required int x,
    required int y,
    required Color color,
    num threshold = 0.0,
    bool compareAlpha = false,
    Image? mask,
    Channel maskChannel = Channel.luminance}) {
  if (color.a == 0) {
    return src;
  }

  final visited = Uint8List(src.width * src.height);

  final srcColor = src.getPixel(x, y);
  if (!compareAlpha) {
    color.a = 0;
  }

  _TestPixel array;
  if (threshold > 0) {
    final lab = rgbToLab(srcColor.r, srcColor.g, srcColor.b);
    if (compareAlpha) {
      lab.add(srcColor.a.toDouble());
    }

    array = (int y, int x) =>
        visited[y * src.width + x] == 0 &&
        _testPixelLabColorDistance(src, x, y, lab, threshold);
  } else if (!compareAlpha) {
    array = (int y, int x) =>
        visited[y * src.width + x] == 0 &&
        _setAlpha(src.getPixel(x, y), 0) != srcColor;
  } else {
    array = (int y, int x) =>
        visited[y * src.width + x] == 0 && src.getPixel(x, y) != srcColor;
  }

  Pixel? p;
  void mark(int y, int x) {
    if (mask != null) {
      final m = mask.getPixel(x, y).getChannelNormalized(maskChannel);
      if (m > 0) {
        p = src.getPixel(x, y, p);
        p!
          ..r = mix(p!.r, color.r, m)
          ..g = mix(p!.g, color.g, m)
          ..b = mix(p!.b, color.b, m)
          ..a = mix(p!.a, color.a, m);
      }
    } else {
      src.setPixel(x, y, color);
    }
    visited[y * src.width + x] = 1;
  }

  _fill4(src, x, y, array, mark, visited);

  return src;
}