Skip to content

Commit a1c5aaf

Browse files
committed
feat(#224): Support "random" as a color value
1 parent c020a01 commit a1c5aaf

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ Install through [HACS](https://hacs.xyz/)
7575
| name | string | **Optional** | entity name from HA | Custom label for this entity
7676
| icon | string | **Optional** | entity icon from HA | Custom icon for this entity
7777
| unit_of_measurement| string | **Optional** | unit_of_measurement from HA | Custom unit_of_measurement for this entity. Useful when using attribute. If it contains a unit prefix, that must be in latin. Ex GВт, not ГВт
78-
| color | string | **Optional** | var(--primary-color)| Color of the box. Example values: 'red', '#FFAA2C', 'rgb(255, 170, 44)'
78+
| color | string | **Optional** | var(--primary-color)| Color of the box. Example values: 'red', '#FFAA2C', 'rgb(255, 170, 44)', 'random' (assigns a random RGB color)
7979
| color_on_state | boolean | **Optional** | false | Color the box based on state value
8080
| color_limit | string | **Optional** | 1 | State value for coloring the box based on state value
8181
| color_above | string | **Optional** | var(--paper-item-icon-color)| Color for state value above color_limit

src/chart.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { HomeAssistant } from 'custom-card-helpers'; // This is a community main
99
import type { Config, SectionState, Box, ConnectionState, EntityConfigInternal, NormalizedState } from './types';
1010
import { localize } from './localize/localize';
1111
import styles from './styles';
12-
import { getEntityId, normalizeStateValue, renderError, sortBoxes } from './utils';
12+
import { getEntityId, normalizeStateValue, renderError, sortBoxes, generateRandomRGBColor } from './utils';
1313
import { HassEntities, HassEntity } from 'home-assistant-js-websocket';
1414
import { handleAction } from './handle-actions';
1515
import { filterConfigByZoomEntity } from './zoom';
@@ -40,6 +40,8 @@ export class Chart extends LitElement {
4040
@state() public zoomEntity?: EntityConfigInternal;
4141
@state() public error?: Error;
4242

43+
private randomColors = new Map<string, string>();
44+
4345
// https://lit.dev/docs/components/lifecycle/#reactive-update-cycle-performing
4446
protected shouldUpdate(changedProps: PropertyValues): boolean {
4547
if (!this.config) {
@@ -339,7 +341,13 @@ export class Chart extends LitElement {
339341
total += state;
340342

341343
let finalColor = entityConf.color || 'var(--primary-color)';
342-
if (entityConf.color_on_state) {
344+
if (entityConf.color === 'random') {
345+
const entityId = getEntityId(entityConf);
346+
if (!this.randomColors.has(entityId)) {
347+
this.randomColors.set(entityId, generateRandomRGBColor());
348+
}
349+
finalColor = this.randomColors.get(entityId)!;
350+
} else if (entityConf.color_on_state) {
343351
let state4color = state;
344352
if (entityConf.type === 'passthrough') {
345353
// passthrough color is based on the child state

src/utils.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ import {
3434
startOfYear,
3535
} from 'date-fns';
3636

37+
export function generateRandomRGBColor(): string {
38+
const r = Math.floor(Math.random() * 256);
39+
const g = Math.floor(Math.random() * 256);
40+
const b = Math.floor(Math.random() * 256);
41+
return `rgb(${r}, ${g}, ${b})`;
42+
}
43+
3744
export function cloneObj<T extends Record<string, unknown>>(obj: T): T {
3845
return JSON.parse(JSON.stringify(obj));
3946
}

0 commit comments

Comments
 (0)