-
-
Notifications
You must be signed in to change notification settings - Fork 763
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Shopping List Label Text Color (#4302)
- Loading branch information
1 parent
f1d56ca
commit 3d1b087
Showing
3 changed files
with
52 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// @ts-ignore missing color types | ||
import Color from "@sphinxxxx/color-conversion"; | ||
|
||
const LIGHT_COLOR = "white"; | ||
const DARK_COLOR = "black"; | ||
const ACCESSIBILITY_THRESHOLD = 0.179; | ||
|
||
/* | ||
Function to pick the text color based on the background color. | ||
Based on -> https://stackoverflow.com/questions/3942878/how-to-decide-font-color-in-white-or-black-depending-on-background-color | ||
*/ | ||
export function getTextColor(bgColor: string | undefined): string { | ||
if (!bgColor) { | ||
return DARK_COLOR; | ||
} | ||
|
||
try { | ||
const color = new Color(bgColor); | ||
|
||
// if opacity is less than 0.3 always return dark color | ||
if (color._rgba[3] < 0.3) { | ||
return DARK_COLOR; | ||
} | ||
|
||
const uicolors = [color._rgba[0] / 255, color._rgba[1] / 255, color._rgba[2] / 255]; | ||
const c = uicolors.map((col) => { | ||
if (col <= 0.03928) { | ||
return col / 12.92; | ||
} | ||
return Math.pow((col + 0.055) / 1.055, 2.4); | ||
}); | ||
const L = 0.2126 * c[0] + 0.7152 * c[1] + 0.0722 * c[2]; | ||
return L > ACCESSIBILITY_THRESHOLD ? DARK_COLOR : LIGHT_COLOR; | ||
} catch (error) { | ||
console.warn(error); | ||
return DARK_COLOR; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters