Media Card: Colors more integrated with Art (#5105)

This commit is contained in:
Zack Arnett 2020-03-08 23:55:03 -04:00 committed by GitHub
parent 64ee7456dc
commit 959d8c3181
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -509,26 +509,47 @@ export class HuiMediaControlCard extends LitElement implements LovelaceCard {
.quality(1)
.getPalette()
.then((palette: Palette) => {
if (!palette.DarkMuted) {
const paletteColors: any[] = [];
Object.keys(palette).forEach((color) => {
paletteColors.push({
hex: palette[color]!.getHex(),
rgb: palette[color]!.getRgb(),
textColor: palette[color]!.getBodyTextColor(),
population: palette[color]!.getPopulation(),
});
});
if (!paletteColors.length) {
this._foregroundColor = undefined;
this._backgroundColor = undefined;
return;
}
this._backgroundColor = palette.DarkMuted.getHex();
paletteColors.sort((colorA, colorB) => {
if (colorA.population > colorB.population) {
return -1;
}
if (colorA.population < colorB.population) {
return 1;
}
if (
!palette.Vibrant ||
getContrastRatio(
palette.Vibrant.getRgb(),
palette.DarkMuted.getRgb()
) < CONTRAST_RATIO
) {
this._foregroundColor = palette.DarkMuted.getBodyTextColor();
return;
return 0;
});
this._backgroundColor = paletteColors[0].hex;
for (let i = 1; i < paletteColors.length; i++) {
if (
getContrastRatio(paletteColors[0].rgb, paletteColors[i].rgb) >=
CONTRAST_RATIO
) {
this._foregroundColor = paletteColors[i].hex;
return;
}
}
this._foregroundColor = palette.Vibrant.getHex();
this._foregroundColor = paletteColors[0].textColor;
})
.catch((err: any) => {
// tslint:disable-next-line:no-console