getPalette static method
Gets a random palette from a list of palettes and sorts its' colors by luminance.
Given if dark
or not, this method makes sure the luminance of the background color is valid.
Implementation
static Palette getPalette(List<Palette>? palettes, bool dark) {
Palette? result;
while (result == null) {
Palette palette = Rnd.getItem(palettes!);
List<Color> colors = Rnd.shuffle(palette.colors!) as List<Color>;
var luminance = colors[0].computeLuminance();
if (dark ? luminance <= .1 : luminance >= .1) {
var lumDiff = colors
.sublist(1)
.asMap()
.map(
(i, color) => MapEntry(
i,
[i, (luminance - color.computeLuminance()).abs()],
),
)
.values
.toList();
lumDiff.sort((List<num> a, List<num> b) {
return a[1].compareTo(b[1]);
});
List<Color> sortedColors =
lumDiff.map((d) => colors[d[0] + 1 as int]).toList();
result = Palette(
colors: [colors[0]] + sortedColors,
);
}
}
return result;
}