From adf3ddfbe001fe2756df6c9468deabff047c19ca Mon Sep 17 00:00:00 2001 From: skjnldsv Date: Thu, 14 Nov 2024 20:15:30 +0100 Subject: [PATCH] fixup! feat(systemtags): add color support Signed-off-by: skjnldsv --- .../src/components/SystemTagPicker.vue | 65 +++++++++++++++---- apps/systemtags/src/utils/colorUtils.ts | 41 ++++++++++++ 2 files changed, 93 insertions(+), 13 deletions(-) create mode 100644 apps/systemtags/src/utils/colorUtils.ts diff --git a/apps/systemtags/src/components/SystemTagPicker.vue b/apps/systemtags/src/components/SystemTagPicker.vue index 9f36dee598d07..c2596f145d3a7 100644 --- a/apps/systemtags/src/components/SystemTagPicker.vue +++ b/apps/systemtags/src/components/SystemTagPicker.vue @@ -36,7 +36,7 @@
  • - - + + +
  • + +
  • { + chipCloneEl.style.setProperty(key, value) + }) + } + const chipHtml = chipCloneEl.outerHTML return chipHtml.replace('%s', escapeHTML(sanitize(tag.displayName))) }, @@ -506,6 +520,19 @@ export default defineComponent({ showInfo(t('systemtags', 'File tags modification canceled')) this.$emit('close', null) }, + + tagListStyle(tag: TagWithId): Record { + return { + '--color-primary': `#${tag.color || primaryColor}`, + '--color-primary-text': this.primaryElementTextColor(tag.color || primaryColor), + '--color-primary-element': `#${tag.color || primaryColor}`, + '--color-primary-element-text': this.primaryElementTextColor(tag.color || primaryColor), + } + }, + + primaryElementTextColor(color: string): string { + return invertTextColor(color) ? '#000000' : '#ffffff' + }, }, }) @@ -554,11 +581,23 @@ export default defineComponent({ } .systemtags-picker__tag-color { - margin-inline-start: var(--default-grid-baseline); + margin-inline-start: calc(var(--default-grid-baseline) * 2); color: var(--color-primary-element); width: var(--default-clickable-area); height: var(--default-clickable-area); display: flex; + + button { + border-radius: 50%; + span { + display: none; + } + + &:focus span, + &:hover span { + display: block; + } + } } .systemtags-picker__tag-create { diff --git a/apps/systemtags/src/utils/colorUtils.ts b/apps/systemtags/src/utils/colorUtils.ts new file mode 100644 index 0000000000000..dfe41217eff6b --- /dev/null +++ b/apps/systemtags/src/utils/colorUtils.ts @@ -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 +}