Skip to content

Commit

Permalink
Use rgb theme variables for qrcode (#19726)
Browse files Browse the repository at this point in the history
  • Loading branch information
piitaya authored and bramkragten committed Feb 8, 2024
1 parent eafbcdb commit f4b1d87
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/components/ha-qr-code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { LitElement, PropertyValues, css, html, nothing } from "lit";
import { customElement, property, query, state } from "lit/decorators";
import QRCode from "qrcode";
import "./ha-alert";
import { rgb2hex } from "../common/color/convert-color";

@customElement("ha-qr-code")
export class HaQrCode extends LitElement {
Expand Down Expand Up @@ -65,6 +66,26 @@ export class HaQrCode extends LitElement {
changedProperties.has("centerImage"))
) {
const computedStyles = getComputedStyle(this);
const textRgb = computedStyles.getPropertyValue(
"--rgb-primary-text-color"
);
const backgroundRgb = computedStyles.getPropertyValue(
"--rgb-card-background-color"
);
const textHex = rgb2hex(
textRgb.split(",").map((a) => parseInt(a, 10)) as [
number,
number,
number,
]
);
const backgroundHex = rgb2hex(
backgroundRgb.split(",").map((a) => parseInt(a, 10)) as [
number,
number,
number,
]
);

QRCode.toCanvas(canvas, this.data, {
errorCorrectionLevel:
Expand All @@ -74,8 +95,8 @@ export class HaQrCode extends LitElement {
margin: this.margin,
maskPattern: this.maskPattern,
color: {
light: computedStyles.getPropertyValue("--card-background-color"),
dark: computedStyles.getPropertyValue("--primary-text-color"),
light: backgroundHex,
dark: textHex,
},
}).catch((err) => {
this._error = err.message;
Expand Down

0 comments on commit f4b1d87

Please sign in to comment.