From 1f2d86f73cd415464fe61190ab16919a435d99ad Mon Sep 17 00:00:00 2001 From: SwingTheVine Date: Mon, 16 Feb 2026 04:16:20 -0500 Subject: [PATCH] SVG shinanigans --- src/main.js | 23 +++++++++++++++++++---- src/utils.js | 17 +++++++++++++++++ 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/main.js b/src/main.js index c47f710..dba4528 100644 --- a/src/main.js +++ b/src/main.js @@ -6,7 +6,7 @@ import Overlay from './Overlay.js'; import Observers from './observers.js'; import ApiManager from './apiManager.js'; import TemplateManager from './templateManager.js'; -import { consoleLog, consoleWarn } from './utils.js'; +import { calculateRelativeLuminance, consoleLog, consoleWarn } from './utils.js'; const name = GM_info.script.name.toString(); // Name of userscript const version = GM_info.script.version.toString(); // Version of userscript @@ -292,7 +292,7 @@ function buildWindowMain() { .addHr().buildElement() .addDiv({'class': 'bm-container'}) .addDiv({'class': 'bm-container'}) - .addButton({'class': 'bm-button-circle bm-button-pin', 'style': 'margin-top: 0;', 'innerHTML': ''}, + .addButton({'class': 'bm-button-circle bm-button-pin', 'style': 'margin-top: 0;', 'innerHTML': ''}, (instance, button) => { button.onclick = () => { const coords = instance.apiManager?.coordsTilePixel; // Retrieves the coords from the API manager @@ -509,9 +509,24 @@ function buildWindowFilter() { const colorList = new Overlay(name, version); colorList.addDiv({'id': 'bm-filter-container-colors', 'class': 'bm-container'}) - // For each color in the palette, construct the DOM tree + // For each color in the palette... for (const color of palette) { - colorList.addDiv({'class': 'bm-container'}) + + // Relative Luminance + const lumin = calculateRelativeLuminance(color.rgb); + + // Calculates if white or black text would contrast better with the palette color + const textColorForPaletteColorBackground = + (((1.05) / (lumin + 0.05)) > ((lumin + 0.05) / 0.05)) + ? 'white' : 'black'; + + // + + // Construct the DOM tree + colorList.addDiv({'class': 'bm-container flex-space-between'}) + .addDiv({'class': '' + textColorForPaletteColorBackground, 'style': `border: thick double white`}) + .addButton({'class': 'bm-button-trans', 'innerHTML': ``}).buildElement() + .buildElement() .addP({'textContent': `Color ID: ${color.id?.toString()?.padStart(2, '0')}, Name: ${color.name}`}).buildElement() .buildElement() } diff --git a/src/utils.js b/src/utils.js index 6027d0c..d5c6385 100644 --- a/src/utils.js +++ b/src/utils.js @@ -127,6 +127,23 @@ export function base64ToUint8(base64) { return array; } +/** Calcualtes the relative luminance of an RGB value + * @param {Array} array - The RGB values as an array + * @returns {Number} The relative luminance as a Number + * @since 0.88.180 + */ +export function calculateRelativeLuminance(array) { + + // Convert the 0-255 range to 0-1 + const srgb = array.map(channel => { + channel /= 255; + return (channel <= 0.03928) ? (channel / 12.92) : Math.pow((channel + 0.055) / 1.055, 2.4); + }); + + // https://en.wikipedia.org/wiki/Relative_luminance#Relative_luminance_and_%22gamma_encoded%22_colorspaces + return (0.2126 * srgb[0]) + (0.7152 * srgb[1]) + (0.0722 * srgb[2]); +} + /** Returns the coordinate input fields * @returns {Element[]} The 4 coordinate Inputs * @since 0.74.0