Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 360b8e2

Browse files
authoredApr 17, 2024
fix: MultiPurposeLabel text color (mealie-recipes#3485)
1 parent 0b851e7 commit 360b8e2

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed
 

‎frontend/components/Domain/ShoppingList/MultiPurposeLabel.vue

+23-12
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88

99
<script lang="ts">
1010
import { computed, defineComponent } from "@nuxtjs/composition-api";
11+
// @ts-ignore missing color types
12+
import Color from "@sphinxxxx/color-conversion";
1113
import { MultiPurposeLabelSummary } from "~/lib/api/types/recipe";
1214
15+
1316
export default defineComponent({
1417
props: {
1518
label: {
@@ -34,19 +37,27 @@ export default defineComponent({
3437
const ACCESSIBILITY_THRESHOLD = 0.179;
3538
3639
function pickTextColorBasedOnBgColorAdvanced(bgColor: string, lightColor: string, darkColor: string) {
37-
const color = bgColor.charAt(0) === "#" ? bgColor.substring(1, 7) : bgColor;
38-
const r = parseInt(color.substring(0, 2), 16); // hexToR
39-
const g = parseInt(color.substring(2, 4), 16); // hexToG
40-
const b = parseInt(color.substring(4, 6), 16); // hexToB
41-
const uicolors = [r / 255, g / 255, b / 255];
42-
const c = uicolors.map((col) => {
43-
if (col <= 0.03928) {
44-
return col / 12.92;
40+
try {
41+
const color = new Color(bgColor);
42+
43+
// if opacity is less than 0.3 always return dark color
44+
if (color._rgba[3] < 0.3) {
45+
return darkColor;
4546
}
46-
return Math.pow((col + 0.055) / 1.055, 2.4);
47-
});
48-
const L = 0.2126 * c[0] + 0.7152 * c[1] + 0.0722 * c[2];
49-
return L > ACCESSIBILITY_THRESHOLD ? darkColor : lightColor;
47+
48+
const uicolors = [color._rgba[0] / 255, color._rgba[1] / 255, color._rgba[2] / 255];
49+
const c = uicolors.map((col) => {
50+
if (col <= 0.03928) {
51+
return col / 12.92;
52+
}
53+
return Math.pow((col + 0.055) / 1.055, 2.4);
54+
});
55+
const L = 0.2126 * c[0] + 0.7152 * c[1] + 0.0722 * c[2];
56+
return L > ACCESSIBILITY_THRESHOLD ? darkColor : lightColor;
57+
} catch (error) {
58+
console.warn(error);
59+
return "black";
60+
}
5061
}
5162
5263
return {

0 commit comments

Comments
 (0)
Please sign in to comment.