filter method
Implementation
void filter(ImageFilter filter, [double? value]) {
switch (filter) {
case ImageFilter.threshold:
assert(value != null);
_applyPixelTransform(
this,
this,
(_, __, x, y, colorIn) =>
HSVColor.fromColor(colorIn).value > value! ? const Color(0xFFFFFFFF) : const Color(0xFF000000));
break;
case ImageFilter.gray:
_applyPixelTransform(
this, this, (_, __, x, y, colorIn) => HSVColor.fromColor(colorIn).withSaturation(0).toColor());
break;
case ImageFilter.opaque:
_applyPixelTransform(this, this, (_, __, x, y, colorIn) => colorIn.withOpacity(1.0));
break;
case ImageFilter.invert:
_applyPixelTransform(
this,
this,
(_, __, x, y, colorIn) => _colorFromPercentARGB(
colorIn.opacity,
1.0 - colorIn.redPercent,
1.0 - colorIn.greenPercent,
1.0 - colorIn.bluePercent,
));
break;
case ImageFilter.posterize:
if (value == null) {
throw Exception("You must provide a value to posterize an image");
}
if (value < 2 || value > 255) {
throw Exception("The posterize value must be between [2, 255]");
}
final level = value.round();
// Filter code adapted from the P5.js implementation
_applyPixelTransform(
this,
this,
(_, __, x, y, colorIn) => Color.fromARGB(
colorIn.alpha,
(((colorIn.red * level) >> 8) * 255 / (level - 1)).round(),
(((colorIn.green * level) >> 8) * 255 / (level - 1)).round(),
(((colorIn.blue * level) >> 8) * 255 / (level - 1)).round(),
));
break;
case ImageFilter.erode:
final outBuffer = copy();
_applyPixelTransform(this, outBuffer, _erode);
_pixels = outBuffer.pixels;
break;
case ImageFilter.dilate:
final outBuffer = copy();
_applyPixelTransform(this, outBuffer, _dilate);
_pixels = outBuffer.pixels;
break;
case ImageFilter.blur:
// TODO: Handle this case.
throw UnimplementedError();
}
}