aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorAjay <[email protected]>2022-02-05 22:23:11 -0500
committerAjay <[email protected]>2022-02-05 22:23:11 -0500
commite8307a2af7af23a55351c096bb40cb382308da80 (patch)
tree2c122da86b44f4e89d7de2a853ce376b446fc908 /src
parent0ac4ef7a4bd276660cfecb1066f63937537113d4 (diff)
downloadSponsorBlock-e8307a2af7af23a55351c096bb40cb382308da80.tar.gz
SponsorBlock-e8307a2af7af23a55351c096bb40cb382308da80.zip
Add text color based off luminance for full video label
Diffstat (limited to 'src')
-rw-r--r--src/components/CategoryPillComponent.tsx23
-rw-r--r--src/config.ts10
-rw-r--r--src/utils/genericUtils.ts25
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<CategoryPillProps, CategoryP
const style: React.CSSProperties = {
backgroundColor: this.getColor(),
display: this.state.show ? "flex" : "none",
- color: this.state.segment?.category === "sponsor"
- || this.state.segment?.category === "exclusive_access" ? "white" : "black",
+ color: this.getTextColor(),
}
return (
@@ -116,6 +115,26 @@ class CategoryPillComponent extends React.Component<CategoryPillProps, CategoryP
return configObject?.color;
}
+ private getTextColor(): string {
+ const color = this.getColor();
+ if (!color) return null;
+
+ const existingCalculatedColor = Config.config.categoryPillColors[this.state.segment?.category];
+ if (existingCalculatedColor && existingCalculatedColor.lastColor === color) {
+ return existingCalculatedColor.textColor;
+ } else {
+ const luminance = GenericUtils.getLuminance(color);
+ console.log(luminance)
+ const textColor = luminance > 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