enable lints unnecessary_new and unnecessary_const (#4)

This commit is contained in:
Alexandre Ardhuin
2018-10-05 22:52:16 +02:00
committed by GitHub
parent 983a50d2ba
commit 14ff267dc5
4 changed files with 166 additions and 169 deletions

View File

@ -134,8 +134,10 @@ linter:
- type_init_formals - type_init_formals
# - unawaited_futures # https://github.com/flutter/flutter/issues/5793 # - unawaited_futures # https://github.com/flutter/flutter/issues/5793
- unnecessary_brace_in_string_interps - unnecessary_brace_in_string_interps
- unnecessary_const
- unnecessary_getters_setters - unnecessary_getters_setters
# - unnecessary_lambdas # https://github.com/dart-lang/linter/issues/498 # - unnecessary_lambdas # https://github.com/dart-lang/linter/issues/498
- unnecessary_new
- unnecessary_null_aware_assignments - unnecessary_null_aware_assignments
- unnecessary_null_in_if_null_operators - unnecessary_null_in_if_null_operators
- unnecessary_overrides - unnecessary_overrides

View File

@ -10,27 +10,27 @@ import 'package:flutter/rendering.dart';
import 'package:palette_generator/palette_generator.dart'; import 'package:palette_generator/palette_generator.dart';
void main() => runApp(new MyApp()); void main() => runApp(MyApp());
const Color _kBackgroundColor = const Color(0xffa0a0a0); const Color _kBackgroundColor = Color(0xffa0a0a0);
const Color _kSelectionRectangleBackground = const Color(0x15000000); const Color _kSelectionRectangleBackground = Color(0x15000000);
const Color _kSelectionRectangleBorder = const Color(0x80000000); const Color _kSelectionRectangleBorder = Color(0x80000000);
const Color _kPlaceholderColor = const Color(0x80404040); const Color _kPlaceholderColor = Color(0x80404040);
/// The main Application class. /// The main Application class.
class MyApp extends StatelessWidget { class MyApp extends StatelessWidget {
// This widget is the root of your application. // This widget is the root of your application.
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return new MaterialApp( return MaterialApp(
title: 'Image Colors', title: 'Image Colors',
theme: new ThemeData( theme: ThemeData(
primarySwatch: Colors.green, primarySwatch: Colors.green,
), ),
home: const ImageColors( home: const ImageColors(
title: 'Image Colors', title: 'Image Colors',
image: const AssetImage('assets/landscape.png'), image: AssetImage('assets/landscape.png'),
imageSize: const Size(256.0, 170.0), imageSize: Size(256.0, 170.0),
), ),
); );
} }
@ -58,7 +58,7 @@ class ImageColors extends StatefulWidget {
@override @override
_ImageColorsState createState() { _ImageColorsState createState() {
return new _ImageColorsState(); return _ImageColorsState();
} }
} }
@ -69,7 +69,7 @@ class _ImageColorsState extends State<ImageColors> {
Offset currentDrag; Offset currentDrag;
PaletteGenerator paletteGenerator; PaletteGenerator paletteGenerator;
final GlobalKey imageKey = new GlobalKey(); final GlobalKey imageKey = GlobalKey();
@override @override
void initState() { void initState() {
@ -95,7 +95,7 @@ class _ImageColorsState extends State<ImageColors> {
setState(() { setState(() {
startDrag = localPosition; startDrag = localPosition;
currentDrag = startDrag; currentDrag = startDrag;
dragRegion = new Rect.fromPoints(startDrag, currentDrag); dragRegion = Rect.fromPoints(startDrag, currentDrag);
}); });
} }
@ -103,7 +103,7 @@ class _ImageColorsState extends State<ImageColors> {
void _onPanUpdate(DragUpdateDetails details) { void _onPanUpdate(DragUpdateDetails details) {
setState(() { setState(() {
currentDrag += details.delta; currentDrag += details.delta;
dragRegion = new Rect.fromPoints(startDrag, currentDrag); dragRegion = Rect.fromPoints(startDrag, currentDrag);
}); });
} }
@ -133,38 +133,38 @@ class _ImageColorsState extends State<ImageColors> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return new Scaffold( return Scaffold(
backgroundColor: _kBackgroundColor, backgroundColor: _kBackgroundColor,
appBar: new AppBar( appBar: AppBar(
title: new Text(widget.title), title: Text(widget.title),
), ),
body: Column( body: Column(
mainAxisSize: MainAxisSize.max, mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.start, mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[ children: <Widget>[
new Padding( Padding(
padding: const EdgeInsets.all(20.0), padding: const EdgeInsets.all(20.0),
// GestureDetector is used to handle the selection rectangle. // GestureDetector is used to handle the selection rectangle.
child: new GestureDetector( child: GestureDetector(
onPanDown: _onPanDown, onPanDown: _onPanDown,
onPanUpdate: _onPanUpdate, onPanUpdate: _onPanUpdate,
onPanCancel: _onPanCancel, onPanCancel: _onPanCancel,
onPanEnd: _onPanEnd, onPanEnd: _onPanEnd,
child: new Stack(children: <Widget>[ child: Stack(children: <Widget>[
new Image( Image(
key: imageKey, key: imageKey,
image: widget.image, image: widget.image,
width: widget.imageSize.width, width: widget.imageSize.width,
height: widget.imageSize.height, height: widget.imageSize.height,
), ),
// This is the selection rectangle. // This is the selection rectangle.
new Positioned.fromRect( Positioned.fromRect(
rect: dragRegion ?? region ?? Rect.zero, rect: dragRegion ?? region ?? Rect.zero,
child: new Container( child: Container(
decoration: new BoxDecoration( decoration: BoxDecoration(
color: _kSelectionRectangleBackground, color: _kSelectionRectangleBackground,
border: new Border.all( border: Border.all(
width: 1.0, width: 1.0,
color: _kSelectionRectangleBorder, color: _kSelectionRectangleBorder,
style: BorderStyle.solid, style: BorderStyle.solid,
@ -175,7 +175,7 @@ class _ImageColorsState extends State<ImageColors> {
), ),
// Use a FutureBuilder so that the palettes will be displayed when // Use a FutureBuilder so that the palettes will be displayed when
// the palette generator is done generating its data. // the palette generator is done generating its data.
new PaletteSwatches(generator: paletteGenerator), PaletteSwatches(generator: paletteGenerator),
], ],
), ),
); );
@ -199,32 +199,30 @@ class PaletteSwatches extends StatelessWidget {
Widget build(BuildContext context) { Widget build(BuildContext context) {
final List<Widget> swatches = <Widget>[]; final List<Widget> swatches = <Widget>[];
if (generator == null || generator.colors.isEmpty) { if (generator == null || generator.colors.isEmpty) {
return new Container(); return Container();
} }
for (Color color in generator.colors) { for (Color color in generator.colors) {
swatches.add(new PaletteSwatch(color: color)); swatches.add(PaletteSwatch(color: color));
} }
return Column( return Column(
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min, mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[ children: <Widget>[
new Wrap( Wrap(
children: swatches, children: swatches,
), ),
new Container(height: 30.0), Container(height: 30.0),
new PaletteSwatch( PaletteSwatch(label: 'Dominant', color: generator.dominantColor?.color),
label: 'Dominant', color: generator.dominantColor?.color), PaletteSwatch(
new PaletteSwatch(
label: 'Light Vibrant', color: generator.lightVibrantColor?.color), label: 'Light Vibrant', color: generator.lightVibrantColor?.color),
new PaletteSwatch( PaletteSwatch(label: 'Vibrant', color: generator.vibrantColor?.color),
label: 'Vibrant', color: generator.vibrantColor?.color), PaletteSwatch(
new PaletteSwatch(
label: 'Dark Vibrant', color: generator.darkVibrantColor?.color), label: 'Dark Vibrant', color: generator.darkVibrantColor?.color),
new PaletteSwatch( PaletteSwatch(
label: 'Light Muted', color: generator.lightMutedColor?.color), label: 'Light Muted', color: generator.lightMutedColor?.color),
new PaletteSwatch(label: 'Muted', color: generator.mutedColor?.color), PaletteSwatch(label: 'Muted', color: generator.mutedColor?.color),
new PaletteSwatch( PaletteSwatch(
label: 'Dark Muted', color: generator.darkMutedColor?.color), label: 'Dark Muted', color: generator.darkMutedColor?.color),
], ],
); );
@ -268,13 +266,13 @@ class PaletteSwatch extends StatelessWidget {
? const Placeholder( ? const Placeholder(
fallbackWidth: 34.0, fallbackWidth: 34.0,
fallbackHeight: 20.0, fallbackHeight: 20.0,
color: const Color(0xff404040), color: Color(0xff404040),
strokeWidth: 2.0, strokeWidth: 2.0,
) )
: new Container( : Container(
decoration: new BoxDecoration( decoration: BoxDecoration(
color: color, color: color,
border: new Border.all( border: Border.all(
width: 1.0, width: 1.0,
color: _kPlaceholderColor, color: _kPlaceholderColor,
style: colorDistance < 0.2 style: colorDistance < 0.2
@ -293,8 +291,8 @@ class PaletteSwatch extends StatelessWidget {
mainAxisAlignment: MainAxisAlignment.start, mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[ children: <Widget>[
swatch, swatch,
new Container(width: 5.0), Container(width: 5.0),
new Text(label), Text(label),
], ],
), ),
); );

View File

@ -114,14 +114,14 @@ class PaletteGenerator extends Diagnosticable {
filters ??= <PaletteFilter>[avoidRedBlackWhitePaletteFilter]; filters ??= <PaletteFilter>[avoidRedBlackWhitePaletteFilter];
maximumColorCount ??= _defaultCalculateNumberColors; maximumColorCount ??= _defaultCalculateNumberColors;
final _ColorCutQuantizer quantizer = new _ColorCutQuantizer( final _ColorCutQuantizer quantizer = _ColorCutQuantizer(
image, image,
maxColors: maximumColorCount, maxColors: maximumColorCount,
filters: filters, filters: filters,
region: region, region: region,
); );
final List<PaletteColor> colors = await quantizer.quantizedColors; final List<PaletteColor> colors = await quantizer.quantizedColors;
return new PaletteGenerator.fromColors( return PaletteGenerator.fromColors(
colors, colors,
targets: targets, targets: targets,
); );
@ -179,9 +179,9 @@ class PaletteGenerator extends Diagnosticable {
region.bottomRight.dy <= size.height), region.bottomRight.dy <= size.height),
'Region $region is outside the image $size'); 'Region $region is outside the image $size');
final ImageStream stream = imageProvider.resolve( final ImageStream stream = imageProvider.resolve(
new ImageConfiguration(size: size, devicePixelRatio: 1.0), ImageConfiguration(size: size, devicePixelRatio: 1.0),
); );
final Completer<ui.Image> imageCompleter = new Completer<ui.Image>(); final Completer<ui.Image> imageCompleter = Completer<ui.Image>();
Timer loadFailureTimeout; Timer loadFailureTimeout;
void imageListener(ImageInfo info, bool synchronousCall) { void imageListener(ImageInfo info, bool synchronousCall) {
loadFailureTimeout?.cancel(); loadFailureTimeout?.cancel();
@ -189,10 +189,10 @@ class PaletteGenerator extends Diagnosticable {
} }
if (timeout != Duration.zero) { if (timeout != Duration.zero) {
loadFailureTimeout = new Timer(timeout, () { loadFailureTimeout = Timer(timeout, () {
stream.removeListener(imageListener); stream.removeListener(imageListener);
imageCompleter.completeError( imageCompleter.completeError(
new TimeoutException( TimeoutException(
'Timeout occurred trying to load from $imageProvider'), 'Timeout occurred trying to load from $imageProvider'),
); );
}); });
@ -274,9 +274,9 @@ class PaletteGenerator extends Diagnosticable {
} }
void _selectSwatches() { void _selectSwatches() {
final Set<PaletteTarget> allTargets = new Set<PaletteTarget>.from( final Set<PaletteTarget> allTargets = Set<PaletteTarget>.from(
(targets ?? <PaletteTarget>[]) + PaletteTarget.baseTargets); (targets ?? <PaletteTarget>[]) + PaletteTarget.baseTargets);
final Set<Color> usedColors = new Set<Color>(); final Set<Color> usedColors = Set<Color>();
for (PaletteTarget target in allTargets) { for (PaletteTarget target in allTargets) {
target._normalizeWeights(); target._normalizeWeights();
selectedSwatches[target] = _generateScoredTarget(target, usedColors); selectedSwatches[target] = _generateScoredTarget(target, usedColors);
@ -315,7 +315,7 @@ class PaletteGenerator extends Diagnosticable {
PaletteColor paletteColor, PaletteTarget target, Set<Color> usedColors) { PaletteColor paletteColor, PaletteTarget target, Set<Color> usedColors) {
// Check whether the HSL lightness is within the correct range, and that // Check whether the HSL lightness is within the correct range, and that
// this color hasn't been used yet. // this color hasn't been used yet.
final HSLColor hslColor = new HSLColor.fromColor(paletteColor.color); final HSLColor hslColor = HSLColor.fromColor(paletteColor.color);
return hslColor.saturation >= target.minimumSaturation && return hslColor.saturation >= target.minimumSaturation &&
hslColor.saturation <= target.maximumSaturation && hslColor.saturation <= target.maximumSaturation &&
hslColor.lightness >= target.minimumLightness && hslColor.lightness >= target.minimumLightness &&
@ -324,7 +324,7 @@ class PaletteGenerator extends Diagnosticable {
} }
double _generateScore(PaletteColor paletteColor, PaletteTarget target) { double _generateScore(PaletteColor paletteColor, PaletteTarget target) {
final HSLColor hslColor = new HSLColor.fromColor(paletteColor.color); final HSLColor hslColor = HSLColor.fromColor(paletteColor.color);
double saturationScore = 0.0; double saturationScore = 0.0;
double valueScore = 0.0; double valueScore = 0.0;
@ -349,10 +349,10 @@ class PaletteGenerator extends Diagnosticable {
@override @override
void debugFillProperties(DiagnosticPropertiesBuilder properties) { void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties); super.debugFillProperties(properties);
properties.add(new IterableProperty<PaletteColor>( properties.add(IterableProperty<PaletteColor>(
'paletteColors', paletteColors, 'paletteColors', paletteColors,
defaultValue: <PaletteColor>[])); defaultValue: <PaletteColor>[]));
properties.add(new IterableProperty<PaletteTarget>('targets', targets, properties.add(IterableProperty<PaletteTarget>('targets', targets,
defaultValue: PaletteTarget.baseTargets)); defaultValue: PaletteTarget.baseTargets));
} }
} }
@ -444,7 +444,7 @@ class PaletteTarget extends Diagnosticable {
/// in luminance. /// in luminance.
/// ///
/// One of the base set of `targets` for [PaletteGenerator.fromImage], in [baseTargets]. /// One of the base set of `targets` for [PaletteGenerator.fromImage], in [baseTargets].
static final PaletteTarget lightVibrant = new PaletteTarget( static final PaletteTarget lightVibrant = PaletteTarget(
targetLightness: _targetLightLightness, targetLightness: _targetLightLightness,
minimumLightness: _minLightLightness, minimumLightness: _minLightLightness,
minimumSaturation: _minVibrantSaturation, minimumSaturation: _minVibrantSaturation,
@ -455,7 +455,7 @@ class PaletteTarget extends Diagnosticable {
/// light or dark. /// light or dark.
/// ///
/// One of the base set of `targets` for [PaletteGenerator.fromImage], in [baseTargets]. /// One of the base set of `targets` for [PaletteGenerator.fromImage], in [baseTargets].
static final PaletteTarget vibrant = new PaletteTarget( static final PaletteTarget vibrant = PaletteTarget(
minimumLightness: _minNormalLightness, minimumLightness: _minNormalLightness,
targetLightness: _targetNormalLightness, targetLightness: _targetNormalLightness,
maximumLightness: _maxNormalLightness, maximumLightness: _maxNormalLightness,
@ -467,7 +467,7 @@ class PaletteTarget extends Diagnosticable {
/// luminance. /// luminance.
/// ///
/// One of the base set of `targets` for [PaletteGenerator.fromImage], in [baseTargets]. /// One of the base set of `targets` for [PaletteGenerator.fromImage], in [baseTargets].
static final PaletteTarget darkVibrant = new PaletteTarget( static final PaletteTarget darkVibrant = PaletteTarget(
targetLightness: _targetDarkLightness, targetLightness: _targetDarkLightness,
maximumLightness: _maxDarkLightness, maximumLightness: _maxDarkLightness,
minimumSaturation: _minVibrantSaturation, minimumSaturation: _minVibrantSaturation,
@ -478,7 +478,7 @@ class PaletteTarget extends Diagnosticable {
/// luminance. /// luminance.
/// ///
/// One of the base set of `targets` for [PaletteGenerator.fromImage], in [baseTargets]. /// One of the base set of `targets` for [PaletteGenerator.fromImage], in [baseTargets].
static final PaletteTarget lightMuted = new PaletteTarget( static final PaletteTarget lightMuted = PaletteTarget(
targetLightness: _targetLightLightness, targetLightness: _targetLightLightness,
minimumLightness: _minLightLightness, minimumLightness: _minLightLightness,
targetSaturation: _targetMutedSaturation, targetSaturation: _targetMutedSaturation,
@ -489,7 +489,7 @@ class PaletteTarget extends Diagnosticable {
/// light or dark. /// light or dark.
/// ///
/// One of the base set of `targets` for [PaletteGenerator.fromImage], in [baseTargets]. /// One of the base set of `targets` for [PaletteGenerator.fromImage], in [baseTargets].
static final PaletteTarget muted = new PaletteTarget( static final PaletteTarget muted = PaletteTarget(
minimumLightness: _minNormalLightness, minimumLightness: _minNormalLightness,
targetLightness: _targetNormalLightness, targetLightness: _targetNormalLightness,
maximumLightness: _maxNormalLightness, maximumLightness: _maxNormalLightness,
@ -501,7 +501,7 @@ class PaletteTarget extends Diagnosticable {
/// luminance. /// luminance.
/// ///
/// One of the base set of `targets` for [PaletteGenerator.fromImage], in [baseTargets]. /// One of the base set of `targets` for [PaletteGenerator.fromImage], in [baseTargets].
static final PaletteTarget darkMuted = new PaletteTarget( static final PaletteTarget darkMuted = PaletteTarget(
targetLightness: _targetDarkLightness, targetLightness: _targetDarkLightness,
maximumLightness: _maxDarkLightness, maximumLightness: _maxDarkLightness,
targetSaturation: _targetMutedSaturation, targetSaturation: _targetMutedSaturation,
@ -560,24 +560,24 @@ class PaletteTarget extends Diagnosticable {
@override @override
void debugFillProperties(DiagnosticPropertiesBuilder properties) { void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties); super.debugFillProperties(properties);
final PaletteTarget defaultTarget = new PaletteTarget(); final PaletteTarget defaultTarget = PaletteTarget();
properties.add(new DoubleProperty('minimumSaturation', minimumSaturation, properties.add(DoubleProperty('minimumSaturation', minimumSaturation,
defaultValue: defaultTarget.minimumSaturation)); defaultValue: defaultTarget.minimumSaturation));
properties.add(new DoubleProperty('targetSaturation', targetSaturation, properties.add(DoubleProperty('targetSaturation', targetSaturation,
defaultValue: defaultTarget.targetSaturation)); defaultValue: defaultTarget.targetSaturation));
properties.add(new DoubleProperty('maximumSaturation', maximumSaturation, properties.add(DoubleProperty('maximumSaturation', maximumSaturation,
defaultValue: defaultTarget.maximumSaturation)); defaultValue: defaultTarget.maximumSaturation));
properties.add(new DoubleProperty('minimumLightness', minimumLightness, properties.add(DoubleProperty('minimumLightness', minimumLightness,
defaultValue: defaultTarget.minimumLightness)); defaultValue: defaultTarget.minimumLightness));
properties.add(new DoubleProperty('targetLightness', targetLightness, properties.add(DoubleProperty('targetLightness', targetLightness,
defaultValue: defaultTarget.targetLightness)); defaultValue: defaultTarget.targetLightness));
properties.add(new DoubleProperty('maximumLightness', maximumLightness, properties.add(DoubleProperty('maximumLightness', maximumLightness,
defaultValue: defaultTarget.maximumLightness)); defaultValue: defaultTarget.maximumLightness));
properties.add(new DoubleProperty('saturationWeight', saturationWeight, properties.add(DoubleProperty('saturationWeight', saturationWeight,
defaultValue: defaultTarget.saturationWeight)); defaultValue: defaultTarget.saturationWeight));
properties.add(new DoubleProperty('lightnessWeight', lightnessWeight, properties.add(DoubleProperty('lightnessWeight', lightnessWeight,
defaultValue: defaultTarget.lightnessWeight)); defaultValue: defaultTarget.lightnessWeight));
properties.add(new DoubleProperty('populationWeight', populationWeight, properties.add(DoubleProperty('populationWeight', populationWeight,
defaultValue: defaultTarget.populationWeight)); defaultValue: defaultTarget.populationWeight));
} }
} }
@ -633,8 +633,8 @@ class PaletteColor extends Diagnosticable {
void _ensureTextColorsGenerated() { void _ensureTextColorsGenerated() {
if (_titleTextColor == null || _bodyTextColor == null) { if (_titleTextColor == null || _bodyTextColor == null) {
const Color white = const Color(0xffffffff); const Color white = Color(0xffffffff);
const Color black = const Color(0xff000000); const Color black = Color(0xff000000);
// First check white, as most colors will be dark // First check white, as most colors will be dark
final int lightBodyAlpha = final int lightBodyAlpha =
_calculateMinimumAlpha(white, color, _minContrastBodyText); _calculateMinimumAlpha(white, color, _minContrastBodyText);
@ -683,10 +683,8 @@ class PaletteColor extends Diagnosticable {
// background // background
foreground = Color.alphaBlend(foreground, background); foreground = Color.alphaBlend(foreground, background);
} }
final double lightness1 = final double lightness1 = HSLColor.fromColor(foreground).lightness + 0.05;
new HSLColor.fromColor(foreground).lightness + 0.05; final double lightness2 = HSLColor.fromColor(background).lightness + 0.05;
final double lightness2 =
new HSLColor.fromColor(background).lightness + 0.05;
return math.max(lightness1, lightness2) / math.min(lightness1, lightness2); return math.max(lightness1, lightness2) / math.min(lightness1, lightness2);
} }
@ -763,12 +761,11 @@ class PaletteColor extends Diagnosticable {
@override @override
void debugFillProperties(DiagnosticPropertiesBuilder properties) { void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties); super.debugFillProperties(properties);
properties.add(new DiagnosticsProperty<Color>('color', color)); properties.add(DiagnosticsProperty<Color>('color', color));
properties properties
.add(new DiagnosticsProperty<Color>('titleTextColor', titleTextColor)); .add(DiagnosticsProperty<Color>('titleTextColor', titleTextColor));
properties properties.add(DiagnosticsProperty<Color>('bodyTextColor', bodyTextColor));
.add(new DiagnosticsProperty<Color>('bodyTextColor', bodyTextColor)); properties.add(IntProperty('population', population, defaultValue: 0));
properties.add(new IntProperty('population', population, defaultValue: 0));
} }
@override @override
@ -922,7 +919,7 @@ class _ColorVolumeBox {
// find median along the longest dimension // find median along the longest dimension
final int splitPoint = _findSplitPoint(); final int splitPoint = _findSplitPoint();
final _ColorVolumeBox newBox = final _ColorVolumeBox newBox =
new _ColorVolumeBox(splitPoint + 1, _upperIndex, histogram, colors); _ColorVolumeBox(splitPoint + 1, _upperIndex, histogram, colors);
// Now change this box's upperIndex and recompute the color boundaries // Now change this box's upperIndex and recompute the color boundaries
_upperIndex = splitPoint; _upperIndex = splitPoint;
_fitMinimumBox(); _fitMinimumBox();
@ -1009,8 +1006,8 @@ class _ColorVolumeBox {
final int redMean = (redSum / totalPopulation).round(); final int redMean = (redSum / totalPopulation).round();
final int greenMean = (greenSum / totalPopulation).round(); final int greenMean = (greenSum / totalPopulation).round();
final int blueMean = (blueSum / totalPopulation).round(); final int blueMean = (blueSum / totalPopulation).round();
return new PaletteColor( return PaletteColor(
new Color.fromARGB(0xff, redMean, greenMean, blueMean), Color.fromARGB(0xff, redMean, greenMean, blueMean),
totalPopulation, totalPopulation,
); );
} }
@ -1070,7 +1067,7 @@ class _ColorCutQuantizer {
final int position = row * rowStride + col * 4; final int position = row * rowStride + col * 4;
// Convert from RGBA to ARGB. // Convert from RGBA to ARGB.
final int pixel = pixels.getUint32(position); final int pixel = pixels.getUint32(position);
final Color result = new Color((pixel << 24) | (pixel >> 8)); final Color result = Color((pixel << 24) | (pixel >> 8));
byteCount += 4; byteCount += 4;
yield result; yield result;
} }
@ -1098,7 +1095,7 @@ class _ColorCutQuantizer {
((1 << quantizeWordWidth) - 1) << quantizeShift; ((1 << quantizeWordWidth) - 1) << quantizeShift;
Color quantizeColor(Color color) { Color quantizeColor(Color color) {
return new Color.fromARGB( return Color.fromARGB(
color.alpha, color.alpha,
color.red & quantizeWordMask, color.red & quantizeWordMask,
color.green & quantizeWordMask, color.green & quantizeWordMask,
@ -1131,7 +1128,7 @@ class _ColorCutQuantizer {
// the colors. // the colors.
_paletteColors.clear(); _paletteColors.clear();
for (Color color in hist.keys) { for (Color color in hist.keys) {
_paletteColors.add(new PaletteColor(color, hist[color])); _paletteColors.add(PaletteColor(color, hist[color]));
} }
} else { } else {
// We need use quantization to reduce the number of colors // We need use quantization to reduce the number of colors
@ -1152,9 +1149,9 @@ class _ColorCutQuantizer {
// Create the priority queue which is sorted by volume descending. This // Create the priority queue which is sorted by volume descending. This
// means we always split the largest box in the queue // means we always split the largest box in the queue
final PriorityQueue<_ColorVolumeBox> priorityQueue = final PriorityQueue<_ColorVolumeBox> priorityQueue =
new HeapPriorityQueue<_ColorVolumeBox>(volumeComparator); HeapPriorityQueue<_ColorVolumeBox>(volumeComparator);
// To start, offer a box which contains all of the colors // To start, offer a box which contains all of the colors
priorityQueue.add(new _ColorVolumeBox( priorityQueue.add(_ColorVolumeBox(
0, histogram.length - 1, histogram, histogram.keys.toList())); 0, histogram.length - 1, histogram, histogram.keys.toList()));
// Now go through the boxes, splitting them until we have reached maxColors // Now go through the boxes, splitting them until we have reached maxColors
// or there are no more boxes to split // or there are no more boxes to split

View File

@ -26,29 +26,29 @@ class FakeImageProvider extends ImageProvider<FakeImageProvider> {
@override @override
Future<FakeImageProvider> obtainKey(ImageConfiguration configuration) { Future<FakeImageProvider> obtainKey(ImageConfiguration configuration) {
return new SynchronousFuture<FakeImageProvider>(this); return SynchronousFuture<FakeImageProvider>(this);
} }
@override @override
ImageStreamCompleter load(FakeImageProvider key) { ImageStreamCompleter load(FakeImageProvider key) {
assert(key == this); assert(key == this);
return new OneFrameImageStreamCompleter( return OneFrameImageStreamCompleter(
new SynchronousFuture<ImageInfo>( SynchronousFuture<ImageInfo>(
new ImageInfo(image: _image, scale: scale), ImageInfo(image: _image, scale: scale),
), ),
); );
} }
} }
Future<ImageProvider> loadImage(String name) async { Future<ImageProvider> loadImage(String name) async {
File imagePath = new File(path.joinAll(<String>['assets', name])); File imagePath = File(path.joinAll(<String>['assets', name]));
if (path.split(Directory.current.absolute.path).last != 'test') { if (path.split(Directory.current.absolute.path).last != 'test') {
imagePath = new File(path.join('test', imagePath.path)); imagePath = File(path.join('test', imagePath.path));
} }
final Uint8List data = new Uint8List.fromList(imagePath.readAsBytesSync()); final Uint8List data = Uint8List.fromList(imagePath.readAsBytesSync());
final ui.Codec codec = await ui.instantiateImageCodec(data); final ui.Codec codec = await ui.instantiateImageCodec(data);
final ui.FrameInfo frameInfo = await codec.getNextFrame(); final ui.FrameInfo frameInfo = await codec.getNextFrame();
return new FakeImageProvider(frameInfo.image); return FakeImageProvider(frameInfo.image);
} }
void main() async { void main() async {
@ -102,8 +102,8 @@ void main() async {
test('PaletteGenerator works with regions', () async { test('PaletteGenerator works with regions', () async {
final ImageProvider imageProvider = testImages['dominant']; final ImageProvider imageProvider = testImages['dominant'];
Rect region = new Rect.fromLTRB(0.0, 0.0, 100.0, 100.0); Rect region = Rect.fromLTRB(0.0, 0.0, 100.0, 100.0);
const Size size = const Size(100.0, 100.0); const Size size = Size(100.0, 100.0);
PaletteGenerator palette = await PaletteGenerator.fromImageProvider( PaletteGenerator palette = await PaletteGenerator.fromImageProvider(
imageProvider, imageProvider,
region: region, region: region,
@ -112,14 +112,14 @@ void main() async {
expect(palette.dominantColor.color, expect(palette.dominantColor.color,
within<Color>(distance: 8, from: const Color(0xff0000ff))); within<Color>(distance: 8, from: const Color(0xff0000ff)));
region = new Rect.fromLTRB(0.0, 0.0, 10.0, 10.0); region = Rect.fromLTRB(0.0, 0.0, 10.0, 10.0);
palette = await PaletteGenerator.fromImageProvider(imageProvider, palette = await PaletteGenerator.fromImageProvider(imageProvider,
region: region, size: size); region: region, size: size);
expect(palette.paletteColors.length, equals(1)); expect(palette.paletteColors.length, equals(1));
expect(palette.dominantColor.color, expect(palette.dominantColor.color,
within<Color>(distance: 8, from: const Color(0xffff0000))); within<Color>(distance: 8, from: const Color(0xffff0000)));
region = new Rect.fromLTRB(0.0, 0.0, 30.0, 20.0); region = Rect.fromLTRB(0.0, 0.0, 30.0, 20.0);
palette = await PaletteGenerator.fromImageProvider(imageProvider, palette = await PaletteGenerator.fromImageProvider(imageProvider,
region: region, size: size); region: region, size: size);
expect(palette.paletteColors.length, equals(3)); expect(palette.paletteColors.length, equals(3));
@ -131,22 +131,22 @@ void main() async {
final PaletteGenerator palette = final PaletteGenerator palette =
await PaletteGenerator.fromImageProvider(testImages['landscape']); await PaletteGenerator.fromImageProvider(testImages['landscape']);
final List<PaletteColor> expectedSwatches = <PaletteColor>[ final List<PaletteColor> expectedSwatches = <PaletteColor>[
new PaletteColor(const Color(0xff3f630c), 10137), PaletteColor(const Color(0xff3f630c), 10137),
new PaletteColor(const Color(0xff3c4b2a), 4773), PaletteColor(const Color(0xff3c4b2a), 4773),
new PaletteColor(const Color(0xff81b2e9), 4762), PaletteColor(const Color(0xff81b2e9), 4762),
new PaletteColor(const Color(0xffc0d6ec), 4714), PaletteColor(const Color(0xffc0d6ec), 4714),
new PaletteColor(const Color(0xff4c4f50), 2465), PaletteColor(const Color(0xff4c4f50), 2465),
new PaletteColor(const Color(0xff5c635b), 2463), PaletteColor(const Color(0xff5c635b), 2463),
new PaletteColor(const Color(0xff6e80a2), 2421), PaletteColor(const Color(0xff6e80a2), 2421),
new PaletteColor(const Color(0xff9995a3), 1214), PaletteColor(const Color(0xff9995a3), 1214),
new PaletteColor(const Color(0xff676c4d), 1213), PaletteColor(const Color(0xff676c4d), 1213),
new PaletteColor(const Color(0xffc4b2b2), 1173), PaletteColor(const Color(0xffc4b2b2), 1173),
new PaletteColor(const Color(0xff445166), 1040), PaletteColor(const Color(0xff445166), 1040),
new PaletteColor(const Color(0xff475d83), 1019), PaletteColor(const Color(0xff475d83), 1019),
new PaletteColor(const Color(0xff7e7360), 589), PaletteColor(const Color(0xff7e7360), 589),
new PaletteColor(const Color(0xfff6b835), 286), PaletteColor(const Color(0xfff6b835), 286),
new PaletteColor(const Color(0xffb9983d), 152), PaletteColor(const Color(0xffb9983d), 152),
new PaletteColor(const Color(0xffe3ab35), 149), PaletteColor(const Color(0xffe3ab35), 149),
]; ];
final Iterable<Color> expectedColors = final Iterable<Color> expectedColors =
expectedSwatches.map<Color>((PaletteColor swatch) => swatch.color); expectedSwatches.map<Color>((PaletteColor swatch) => swatch.color);
@ -191,22 +191,22 @@ void main() async {
imageProvider, imageProvider,
filters: filters); filters: filters);
final List<PaletteColor> expectedSwatches = <PaletteColor>[ final List<PaletteColor> expectedSwatches = <PaletteColor>[
new PaletteColor(const Color(0xff3f630c), 10137), PaletteColor(const Color(0xff3f630c), 10137),
new PaletteColor(const Color(0xff3c4b2a), 4773), PaletteColor(const Color(0xff3c4b2a), 4773),
new PaletteColor(const Color(0xff81b2e9), 4762), PaletteColor(const Color(0xff81b2e9), 4762),
new PaletteColor(const Color(0xffc0d6ec), 4714), PaletteColor(const Color(0xffc0d6ec), 4714),
new PaletteColor(const Color(0xff4c4f50), 2465), PaletteColor(const Color(0xff4c4f50), 2465),
new PaletteColor(const Color(0xff5c635b), 2463), PaletteColor(const Color(0xff5c635b), 2463),
new PaletteColor(const Color(0xff6e80a2), 2421), PaletteColor(const Color(0xff6e80a2), 2421),
new PaletteColor(const Color(0xff9995a3), 1214), PaletteColor(const Color(0xff9995a3), 1214),
new PaletteColor(const Color(0xff676c4d), 1213), PaletteColor(const Color(0xff676c4d), 1213),
new PaletteColor(const Color(0xffc4b2b2), 1173), PaletteColor(const Color(0xffc4b2b2), 1173),
new PaletteColor(const Color(0xff445166), 1040), PaletteColor(const Color(0xff445166), 1040),
new PaletteColor(const Color(0xff475d83), 1019), PaletteColor(const Color(0xff475d83), 1019),
new PaletteColor(const Color(0xff7e7360), 589), PaletteColor(const Color(0xff7e7360), 589),
new PaletteColor(const Color(0xfff6b835), 286), PaletteColor(const Color(0xfff6b835), 286),
new PaletteColor(const Color(0xffb9983d), 152), PaletteColor(const Color(0xffb9983d), 152),
new PaletteColor(const Color(0xffe3ab35), 149), PaletteColor(const Color(0xffe3ab35), 149),
]; ];
final Iterable<Color> expectedColors = final Iterable<Color> expectedColors =
expectedSwatches.map<Color>((PaletteColor swatch) => swatch.color); expectedSwatches.map<Color>((PaletteColor swatch) => swatch.color);
@ -220,22 +220,22 @@ void main() async {
palette = await PaletteGenerator.fromImageProvider(imageProvider, palette = await PaletteGenerator.fromImageProvider(imageProvider,
filters: filters); filters: filters);
final List<PaletteColor> blueSwatches = <PaletteColor>[ final List<PaletteColor> blueSwatches = <PaletteColor>[
new PaletteColor(const Color(0xff4c5c75), 1515), PaletteColor(const Color(0xff4c5c75), 1515),
new PaletteColor(const Color(0xff7483a1), 1505), PaletteColor(const Color(0xff7483a1), 1505),
new PaletteColor(const Color(0xff515661), 1476), PaletteColor(const Color(0xff515661), 1476),
new PaletteColor(const Color(0xff769dd4), 1470), PaletteColor(const Color(0xff769dd4), 1470),
new PaletteColor(const Color(0xff3e4858), 777), PaletteColor(const Color(0xff3e4858), 777),
new PaletteColor(const Color(0xff98a3bc), 760), PaletteColor(const Color(0xff98a3bc), 760),
new PaletteColor(const Color(0xffb4c7e0), 760), PaletteColor(const Color(0xffb4c7e0), 760),
new PaletteColor(const Color(0xff99bbe5), 742), PaletteColor(const Color(0xff99bbe5), 742),
new PaletteColor(const Color(0xffcbdef0), 701), PaletteColor(const Color(0xffcbdef0), 701),
new PaletteColor(const Color(0xff1c212b), 429), PaletteColor(const Color(0xff1c212b), 429),
new PaletteColor(const Color(0xff393c46), 417), PaletteColor(const Color(0xff393c46), 417),
new PaletteColor(const Color(0xff526483), 394), PaletteColor(const Color(0xff526483), 394),
new PaletteColor(const Color(0xff61708b), 372), PaletteColor(const Color(0xff61708b), 372),
new PaletteColor(const Color(0xff5e8ccc), 345), PaletteColor(const Color(0xff5e8ccc), 345),
new PaletteColor(const Color(0xff587ab4), 194), PaletteColor(const Color(0xff587ab4), 194),
new PaletteColor(const Color(0xff5584c8), 182), PaletteColor(const Color(0xff5584c8), 182),
]; ];
final Iterable<Color> expectedBlues = final Iterable<Color> expectedBlues =
blueSwatches.map<Color>((PaletteColor swatch) => swatch.color); blueSwatches.map<Color>((PaletteColor swatch) => swatch.color);
@ -250,22 +250,22 @@ void main() async {
palette = await PaletteGenerator.fromImageProvider(imageProvider, palette = await PaletteGenerator.fromImageProvider(imageProvider,
filters: filters); filters: filters);
final List<PaletteColor> blueGreenSwatches = <PaletteColor>[ final List<PaletteColor> blueGreenSwatches = <PaletteColor>[
new PaletteColor(const Color(0xffc8e8f8), 87), PaletteColor(const Color(0xffc8e8f8), 87),
new PaletteColor(const Color(0xff5c6c74), 73), PaletteColor(const Color(0xff5c6c74), 73),
new PaletteColor(const Color(0xff6f8088), 49), PaletteColor(const Color(0xff6f8088), 49),
new PaletteColor(const Color(0xff687880), 49), PaletteColor(const Color(0xff687880), 49),
new PaletteColor(const Color(0xff506068), 45), PaletteColor(const Color(0xff506068), 45),
new PaletteColor(const Color(0xff485860), 39), PaletteColor(const Color(0xff485860), 39),
new PaletteColor(const Color(0xff405058), 21), PaletteColor(const Color(0xff405058), 21),
new PaletteColor(const Color(0xffd6ebf3), 11), PaletteColor(const Color(0xffd6ebf3), 11),
new PaletteColor(const Color(0xff2f3f47), 7), PaletteColor(const Color(0xff2f3f47), 7),
new PaletteColor(const Color(0xff0f1f27), 6), PaletteColor(const Color(0xff0f1f27), 6),
new PaletteColor(const Color(0xffc0e0f0), 6), PaletteColor(const Color(0xffc0e0f0), 6),
new PaletteColor(const Color(0xff203038), 3), PaletteColor(const Color(0xff203038), 3),
new PaletteColor(const Color(0xff788890), 2), PaletteColor(const Color(0xff788890), 2),
new PaletteColor(const Color(0xff384850), 2), PaletteColor(const Color(0xff384850), 2),
new PaletteColor(const Color(0xff98a8b0), 1), PaletteColor(const Color(0xff98a8b0), 1),
new PaletteColor(const Color(0xffa8b8c0), 1), PaletteColor(const Color(0xffa8b8c0), 1),
]; ];
final Iterable<Color> expectedBlueGreens = final Iterable<Color> expectedBlueGreens =
blueGreenSwatches.map<Color>((PaletteColor swatch) => swatch.color); blueGreenSwatches.map<Color>((PaletteColor swatch) => swatch.color);
@ -301,8 +301,8 @@ void main() async {
// Passing targets augments the baseTargets, and those targets are found. // Passing targets augments the baseTargets, and those targets are found.
final List<PaletteTarget> saturationExtremeTargets = <PaletteTarget>[ final List<PaletteTarget> saturationExtremeTargets = <PaletteTarget>[
new PaletteTarget(minimumSaturation: 0.85), PaletteTarget(minimumSaturation: 0.85),
new PaletteTarget(maximumSaturation: .25), PaletteTarget(maximumSaturation: .25),
]; ];
palette = await PaletteGenerator.fromImageProvider(imageProvider, palette = await PaletteGenerator.fromImageProvider(imageProvider,
targets: saturationExtremeTargets); targets: saturationExtremeTargets);