Skip to content

Commit

Permalink
Fix temperature bars for temperatures lower -20C
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Kissling committed Dec 13, 2023
1 parent d20c858 commit 027beb0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/clock-weather-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ console.info(
})

const gradientMap: Map<number, Rgb> = new Map()
.set(-20, new Rgb(0, 60, 98)) // dark blue
.set(-10, new Rgb(120, 162, 204)) // darker blue
.set(0, new Rgb(164, 195, 210)) // light blue
.set(10, new Rgb(121, 210, 179)) // turquoise
Expand Down Expand Up @@ -346,12 +347,18 @@ export class ClockWeatherCard extends LitElement {
}

private gradient (rgbs: Rgb[], fromPercent: number, toPercent: number): string {
if (rgbs.length <= 1) {
const rgb = rgbs[0] ?? new Rgb(255, 255, 255)
return [rgb, rgb]
.map((rgb) => rgb.toRgbString())
.join(',')
}
const [fromRgb, fromIndex] = this.calculateRgb(rgbs, fromPercent, 'left')
const [toRgb, toIndex] = this.calculateRgb(rgbs, toPercent, 'right')
const between = rgbs.slice(fromIndex + 1, toIndex)

return [fromRgb, ...between, toRgb]
.map((rgb) => `rgb(${rgb.r},${rgb.g},${rgb.b})`)
.map((rgb) => rgb.toRgbString())
.join(',')
}

Expand Down
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ export class Rgb {
this.g = g
this.b = b
}

toRgbString (): string {
return `rgb(${this.r}, ${this.g}, ${this.b})`
}
}

export interface TemperatureSensor extends HassEntity {
Expand Down

0 comments on commit 027beb0

Please sign in to comment.