Visitar URL original
[palette_generator] Migrate to null safety by mzdm · Pull Request #287 · flutter/packages · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
f187cd4
update CHANGELOG.md
mzdm Feb 13, 2021
52576e2
add package to package list in the base README.md
mzdm Feb 13, 2021
b8d221c
migrate lib
mzdm Feb 13, 2021
b5d8bd4
migrate example
mzdm Feb 13, 2021
5bfa13c
add an argument for running integration tests
mzdm Feb 13, 2021
930a4d8
correct sorting
mzdm Feb 13, 2021
5d8c667
migrate lib
mzdm Feb 14, 2021
e4df198
migrate lib tests
mzdm Feb 14, 2021
86277a0
update CHANGELOG.md
mzdm Feb 14, 2021
5c54b9f
migrate app example
mzdm Feb 14, 2021
20e26a3
[pointer_interceptor] Revert "correct sorting"
mzdm Feb 14, 2021
b397fae
[pointer_interceptor] Revert "add an argument for running integration…
mzdm Feb 14, 2021
b7a73e2
[pointer_interceptor] Revert "migrate example"
mzdm Feb 14, 2021
36460a3
[pointer_interceptor] Revert "migrate lib"
mzdm Feb 14, 2021
46ce68e
[pointer_interceptor] Revert "add package to package list in the base…
mzdm Feb 14, 2021
9f0a365
[pointer_interceptor] Revert "update CHANGELOG.md"
mzdm Feb 14, 2021
e6ea988
some reviews fixes
mzdm Feb 16, 2021
bbb0547
review: make histogram non-nullable
mzdm Feb 16, 2021
00306fb
Revert "review: make histogram non-nullable"
mzdm Feb 16, 2021
d05a75c
remove null type references from the docs
mzdm Feb 16, 2021
8495164
review: an attempt to make PaletteColor non-nullable
mzdm Feb 17, 2021
c6c1891
review: #289 landed
mzdm Feb 18, 2021
0d9a578
review: an attempt to make PaletteColor non-nullable - tests
mzdm Feb 18, 2021
1c3ed84
review: #289 landed - change version in CHANGELOG.md
mzdm Feb 18, 2021
44cdf50
review: an attempt to make PaletteColor non-nullable - formatting
mzdm Feb 18, 2021
154115f
Merge remote-tracking branch 'upstream/master' into nnbd-palette-gen
mzdm Feb 18, 2021
419e6f7
bump SDK constraint
mzdm Feb 24, 2021
84ebdd8
formatting
mzdm Feb 24, 2021
83c569c
review: change docs
mzdm Feb 24, 2021
df2c2c0
review: Revert breaking changes
mzdm Feb 26, 2021
9d242dc
review: make PaletteColor map non-nullable
mzdm Feb 26, 2021
0855080
review: minor doc change
mzdm Feb 26, 2021
72def04
Revert "review: minor doc change"
mzdm Feb 26, 2021
97b91ed
bump ver to stable null safety
mzdm Mar 5, 2021
b42f619
review: histogram non-nullable
mzdm Mar 5, 2021
50a4b30
review: fixes
mzdm Mar 5, 2021
3e6578f
review: update pubspec.yaml example
mzdm Mar 8, 2021
c50376e
review: address comments
mzdm Mar 8, 2021
4af0d6b
formatting
mzdm Mar 8, 2021
d5e675e
revert changes to dominant color
mzdm Mar 8, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
review: an attempt to make PaletteColor non-nullable
  • Loading branch information
mzdm committed Feb 17, 2021
commit 849516497291cb338a47a27c1f4cb1f987352ecb
26 changes: 13 additions & 13 deletions packages/palette_generator/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ class PaletteSwatches extends StatelessWidget {
return Container();
}
for (Color color in paletteGen.colors) {
swatches.add(PaletteSwatch(color: color));
swatches.add(PaletteSwatch(paletteColor: PaletteColor(color: color)));
}
return Column(
mainAxisAlignment: MainAxisAlignment.center,
Expand All @@ -218,17 +218,17 @@ class PaletteSwatches extends StatelessWidget {
),
Container(height: 30.0),
PaletteSwatch(
label: 'Dominant', color: paletteGen.dominantColor?.color),
label: 'Dominant', paletteColor: paletteGen.dominantColor),
PaletteSwatch(
label: 'Light Vibrant', color: paletteGen.lightVibrantColor?.color),
PaletteSwatch(label: 'Vibrant', color: paletteGen.vibrantColor?.color),
label: 'Light Vibrant', paletteColor: paletteGen.lightVibrantColor),
PaletteSwatch(label: 'Vibrant', paletteColor: paletteGen.vibrantColor),
PaletteSwatch(
label: 'Dark Vibrant', color: paletteGen.darkVibrantColor?.color),
label: 'Dark Vibrant', paletteColor: paletteGen.darkVibrantColor),
PaletteSwatch(
label: 'Light Muted', color: paletteGen.lightMutedColor?.color),
PaletteSwatch(label: 'Muted', color: paletteGen.mutedColor?.color),
label: 'Light Muted', paletteColor: paletteGen.lightMutedColor),
PaletteSwatch(label: 'Muted', paletteColor: paletteGen.mutedColor),
PaletteSwatch(
label: 'Dark Muted', color: paletteGen.darkMutedColor?.color),
label: 'Dark Muted', paletteColor: paletteGen.darkMutedColor),
],
);
}
Expand All @@ -243,12 +243,12 @@ class PaletteSwatch extends StatelessWidget {
/// placeholder instead, to indicate that there is no color.
const PaletteSwatch({
Key? key,
this.color,
required this.paletteColor,
this.label,
}) : super(key: key);

/// The color of the swatch.
final Color? color;
final PaletteColor paletteColor;
Comment thread
mzdm marked this conversation as resolved.
Resolved

/// The optional label to display next to the swatch.
final String? label;
Expand All @@ -259,15 +259,15 @@ class PaletteSwatch extends StatelessWidget {
// so that we can put a border around those color swatches that are too
// close to the background's saturation and lightness. We ignore hue for
// the comparison.
final HSLColor hslColor = HSLColor.fromColor(color ?? Colors.transparent);
final HSLColor hslColor = HSLColor.fromColor(paletteColor.color);
final HSLColor backgroundAsHsl = HSLColor.fromColor(_kBackgroundColor);
final double colorDistance = math.sqrt(
math.pow(hslColor.saturation - backgroundAsHsl.saturation, 2.0) +
math.pow(hslColor.lightness - backgroundAsHsl.lightness, 2.0));

Widget swatch = Padding(
padding: const EdgeInsets.all(2.0),
child: color == null
child: !paletteColor.isTargetColorFound
? const Placeholder(
fallbackWidth: 34.0,
fallbackHeight: 20.0,
Expand All @@ -276,7 +276,7 @@ class PaletteSwatch extends StatelessWidget {
)
: Container(
decoration: BoxDecoration(
color: color,
color: paletteColor.color,
border: Border.all(
width: 1.0,
color: _kPlaceholderColor,
Expand Down
92 changes: 52 additions & 40 deletions packages/palette_generator/lib/palette_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class PaletteGenerator with Diagnosticable {
PaletteGenerator.fromColors(
this.paletteColors, {
this.targets = const <PaletteTarget>[],
}) : selectedSwatches = <PaletteTarget, PaletteColor?>{} {
}) : selectedSwatches = <PaletteTarget, PaletteColor>{} {
Comment thread
mzdm marked this conversation as resolved.
Resolved
_sortSwatches();
_selectSwatches();
}
Expand Down Expand Up @@ -218,7 +218,7 @@ class PaletteGenerator with Diagnosticable {
static const int _defaultCalculateNumberColors = 16;

/// Provides a map of the selected paletteColors for each target in [targets].
final Map<PaletteTarget, PaletteColor?> selectedSwatches;
final Map<PaletteTarget, PaletteColor> selectedSwatches;

/// The list of [PaletteColor]s that make up the palette, sorted from most
/// dominant color to least dominant color.
Expand All @@ -238,40 +238,46 @@ class PaletteGenerator with Diagnosticable {
}
}

/// Returns a vibrant color from the palette. Might be null if an appropriate
/// target color could not be found.
PaletteColor? get vibrantColor => selectedSwatches[PaletteTarget.vibrant];

/// Returns a light and vibrant color from the palette. Might be null if an
/// appropriate target color could not be found.
PaletteColor? get lightVibrantColor =>
selectedSwatches[PaletteTarget.lightVibrant];

/// Returns a dark and vibrant color from the palette. Might be null if an
/// appropriate target color could not be found.
PaletteColor? get darkVibrantColor =>
selectedSwatches[PaletteTarget.darkVibrant];

/// Returns a muted color from the palette. Might be null if an appropriate
/// target color could not be found.
PaletteColor? get mutedColor => selectedSwatches[PaletteTarget.muted];

/// Returns a muted and light color from the palette. Might be null if an
/// appropriate target color could not be found.
PaletteColor? get lightMutedColor =>
selectedSwatches[PaletteTarget.lightMuted];

/// Returns a muted and dark color from the palette. Might be null if an
/// appropriate target color could not be found.
PaletteColor? get darkMutedColor => selectedSwatches[PaletteTarget.darkMuted];
/// Returns a vibrant color from the palette. If an appropriate target color
/// could not be found then properties `color` is set to grey color and
/// `population` to 1.
PaletteColor get vibrantColor => selectedSwatches[PaletteTarget.vibrant]!;

/// Returns a light and vibrant color from the palette. If an appropriate
/// target color could not be found then properties `color` is set to
/// grey color and `population` to 1.
PaletteColor get lightVibrantColor =>
selectedSwatches[PaletteTarget.lightVibrant]!;

/// Returns a dark and vibrant color from the palette. If an appropriate
/// target color could not be found then properties `color` is set to
/// grey color and `population` to 1.
PaletteColor get darkVibrantColor =>
selectedSwatches[PaletteTarget.darkVibrant]!;

/// Returns a muted color from the palette. If an appropriate target
/// color could not be found then properties `color` is set to grey color
/// and `population` to 1.
PaletteColor get mutedColor => selectedSwatches[PaletteTarget.muted]!;

/// Returns a muted and light color from the palette. If an appropriate
/// target color could not be found then properties `color` is set to
/// grey color and `population` to 1.
PaletteColor get lightMutedColor =>
selectedSwatches[PaletteTarget.lightMuted]!;

/// Returns a muted and dark color from the palette. If an appropriate
/// target color could not be found then properties `color` is set to
/// grey color and `population` to 1.
PaletteColor get darkMutedColor => selectedSwatches[PaletteTarget.darkMuted]!;

/// The dominant color (the color with the largest population).
PaletteColor? get dominantColor => _dominantColor;
PaletteColor? _dominantColor;
PaletteColor get dominantColor => _dominantColor;
late PaletteColor _dominantColor;
Comment thread
mzdm marked this conversation as resolved.
Resolved

void _sortSwatches() {
if (paletteColors.isEmpty) {
Comment thread
mzdm marked this conversation as resolved.
Resolved
_dominantColor = null;
_dominantColor = PaletteColor();
return;
}
// Sort from most common to least common.
Expand All @@ -287,7 +293,9 @@ class PaletteGenerator with Diagnosticable {
final Set<Color> usedColors = <Color>{};
for (PaletteTarget target in allTargets) {
target._normalizeWeights();
selectedSwatches[target] = _generateScoredTarget(target, usedColors);
final PaletteColor? scoredPalleteColor =
_generateScoredTarget(target, usedColors);
selectedSwatches[target] = scoredPalleteColor ?? PaletteColor();
}
}

Expand Down Expand Up @@ -346,9 +354,9 @@ class PaletteGenerator with Diagnosticable {
valueScore = target.lightnessWeight *
(1.0 - (hslColor.lightness - target.targetLightness).abs());
}
if (_dominantColor != null && target.populationWeight > 0.0) {
if (target.populationWeight > 0.0) {
populationScore = target.populationWeight *
(paletteColor.population / _dominantColor!.population);
(paletteColor.population / _dominantColor.population);
}

return saturationScore + valueScore + populationScore;
Expand Down Expand Up @@ -596,18 +604,21 @@ typedef _ContrastCalculator = double Function(Color a, Color b, int alpha);
/// * [PaletteGenerator], a class for selecting color palettes from images.
class PaletteColor with Diagnosticable {
/// Generate a [PaletteColor].
PaletteColor(this.color, this.population);
PaletteColor({this.color = const Color(0xff404040), this.population = 1});

static const double _minContrastTitleText = 3.0;
static const double _minContrastBodyText = 4.5;

/// The color that this palette color represents.
/// The color that this palette color represents. Defaults to a grey color.
final Color color;

/// The number of pixels in the source image that this palette color
/// represents.
/// represents. Defaults to 1.
final int population;

/// Returns `true` if this color was found at the selected target.
bool get isTargetColorFound => color.value != 0xff404040;

/// The color of title text for use with this palette color.
Color get titleTextColor {
if (_titleTextColor == null) {
Expand Down Expand Up @@ -996,8 +1007,8 @@ class _ColorVolumeBox {
final int greenMean = (greenSum / totalPopulation).round();
final int blueMean = (blueSum / totalPopulation).round();
return PaletteColor(
Color.fromARGB(0xff, redMean, greenMean, blueMean),
totalPopulation,
color: Color.fromARGB(0xff, redMean, greenMean, blueMean),
population: totalPopulation,
);
}
}
Expand Down Expand Up @@ -1200,7 +1211,8 @@ class _ColorCutQuantizer {
// the colors.
_paletteColors.clear();
for (Color color in hist.keys) {
_paletteColors.add(PaletteColor(color, hist[color]!.value));
_paletteColors
.add(PaletteColor(color: color, population: hist[color]!.value));
}
} else {
// We need use quantization to reduce the number of colors
Expand Down