-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixup! feat(systemtags): add color support
Signed-off-by: skjnldsv <[email protected]>
- Loading branch information
Showing
2 changed files
with
93 additions
and
13 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,41 @@ | ||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
type rgb = { | ||
red: number | ||
green: number | ||
blue: number | ||
} | ||
|
||
/** | ||
* Convert hex color to RGB | ||
*/ | ||
export function hexToRGB(hex: string): rgb { | ||
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex) | ||
if (!result) { | ||
throw new Error('Invalid color') | ||
} | ||
|
||
return { | ||
red: parseInt(result[1], 16), | ||
green: parseInt(result[2], 16), | ||
blue: parseInt(result[3], 16), | ||
} | ||
} | ||
|
||
/** | ||
* Calculate luminance of provided hex color | ||
*/ | ||
export function calculateLuma(color: string): number { | ||
const rgb = hexToRGB(color) | ||
return (0.2126 * rgb.red + 0.7152 * rgb.green + 0.0722 * rgb.blue) / 255 | ||
} | ||
|
||
/** | ||
* Do we need to invert the text if color is too bright? | ||
*/ | ||
export function invertTextColor(color: string): boolean { | ||
return calculateLuma(color) > 0.6 | ||
} |