Skip to content

Commit

Permalink
Add threshold to BinaryQuantizer
Browse files Browse the repository at this point in the history
  • Loading branch information
brendan-duncan committed May 12, 2024
1 parent 61e6ef3 commit ff549a0
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/src/util/binary_quantizer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,26 @@ import 'quantizer.dart';

class BinaryQuantizer extends Quantizer {
final Palette _palette;
final num threshold;

BinaryQuantizer() : _palette = PaletteUint8(2, 3) {
BinaryQuantizer({this.threshold = 0.5}) : _palette = PaletteUint8(2, 3) {
_palette.setRgb(1, 255, 255, 255);
}

@override
Palette get palette => _palette;

@override
Color getQuantizedColor(Color c) => c.luminanceNormalized < 0.5
Color getQuantizedColor(Color c) => c.luminanceNormalized < threshold
? ColorRgb8(_palette.getRed(0) as int, _palette.getGreen(0) as int,
_palette.getBlue(0) as int)
: ColorRgb8(_palette.getRed(1) as int, _palette.getGreen(1) as int,
_palette.getBlue(1) as int);

@override
int getColorIndex(Color c) => c.luminanceNormalized < 0.5 ? 0 : 1;
int getColorIndex(Color c) => c.luminanceNormalized < threshold ? 0 : 1;

@override
int getColorIndexRgb(int r, int g, int b) =>
getLuminanceRgb(r, g, b) < 0.5 ? 0 : 1;
getLuminanceRgb(r, g, b) < threshold ? 0 : 1;
}

0 comments on commit ff549a0

Please sign in to comment.