From e8307a2af7af23a55351c096bb40cb382308da80 Mon Sep 17 00:00:00 2001 From: Ajay Date: Sat, 5 Feb 2022 22:23:11 -0500 Subject: [PATCH] Add text color based off luminance for full video label --- src/components/CategoryPillComponent.tsx | 23 ++++++++++++++++++++-- src/config.ts | 10 ++++++++++ src/utils/genericUtils.ts | 25 +++++++++++++++++++++++- 3 files changed, 55 insertions(+), 3 deletions(-) diff --git a/src/components/CategoryPillComponent.tsx b/src/components/CategoryPillComponent.tsx index a00e38b2..5eeb716a 100644 --- a/src/components/CategoryPillComponent.tsx +++ b/src/components/CategoryPillComponent.tsx @@ -35,8 +35,7 @@ class CategoryPillComponent extends React.Component 128 ? "black" : "white"; + Config.config.categoryPillColors[this.state.segment?.category] = { + lastColor: color, + textColor + }; + + return textColor; + } + } + getTitleText(): string { const shortDescription = chrome.i18n.getMessage(`category_${this.state.segment?.category}_pill`); return (shortDescription ? shortDescription + ". ": "") + chrome.i18n.getMessage("categoryPillTitleText"); diff --git a/src/config.ts b/src/config.ts index a890f94a..63dd6af0 100644 --- a/src/config.ts +++ b/src/config.ts @@ -54,6 +54,14 @@ interface SBConfig { categoryPillUpdate: boolean, darkMode: boolean, + // Used to cache calculated text color info + categoryPillColors: { + [key in Category]: { + lastColor: string, + textColor: string + } + } + skipKeybind: Keybind, startSponsorKeybind: Keybind, submitKeybind: Keybind, @@ -210,6 +218,8 @@ const Config: SBObject = { categoryPillUpdate: false, darkMode: true, + categoryPillColors: {}, + /** * Default keybinds should not set "code" as that's gonna be different based on the user's locale. They should also only use EITHER ctrl OR alt modifiers (or none). * Using ctrl+alt, or shift may produce a different character that we will not be able to recognize in different locales. diff --git a/src/utils/genericUtils.ts b/src/utils/genericUtils.ts index b146e57a..3451c738 100644 --- a/src/utils/genericUtils.ts +++ b/src/utils/genericUtils.ts @@ -44,7 +44,30 @@ function getErrorMessage(statusCode: number, responseText: string): string { return errorMessage + postFix; } +/* Gets percieved luminance of a color */ +function getLuminance(color: string): number { + const {r, g, b} = hexToRgb(color); + return Math.sqrt(0.299 * (r * r) + 0.587 * (g * g) + 0.114 * (b * b)); +} + +/* From https://stackoverflow.com/a/5624139 */ +function hexToRgb(hex: string): {r: number, g: number, b: number} { + // Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF") + const shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i; + hex = hex.replace(shorthandRegex, function(m, r, g, b) { + return r + r + g + g + b + b; + }); + + const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex); + return result ? { + r: parseInt(result[1], 16), + g: parseInt(result[2], 16), + b: parseInt(result[3], 16) + } : null; + } + export const GenericUtils = { wait, - getErrorMessage + getErrorMessage, + getLuminance } \ No newline at end of file