From 5978a3328339d97de18f21299e895b1a7bf7cbb6 Mon Sep 17 00:00:00 2001 From: pk Date: Sun, 15 Nov 2026 18:36:49 +0100 Subject: [PATCH] color: accept #rgb/#rgba and #rrggbbaa in contrastTextColor; stop rgbToHex throwing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit contrastTextColor matched only #rrggbb and silently returned the dark fallback for every other form — #000 and #369 produced a black label on a near-black fill. colorLuminance in the same file already expanded short hex, so this was an inconsistency, not a contract. rgbToHex threw a TypeError on non-matching input (matches.length on null) and rejected rgba() and whitespace-free rgb strings; it now returns and ignores the alpha channel. --- color.js | 54 ++++++++++++++++++++++++++++++++++++++++------------ package.json | 2 +- 2 files changed, 43 insertions(+), 13 deletions(-) diff --git a/color.js b/color.js index 20db3d1..fdbb47f 100644 --- a/color.js +++ b/color.js @@ -25,6 +25,32 @@ export const colorLuminance = (hex, lum) => { return rgb; } +/** + * Expand a hex color to six digits, ignoring an alpha byte. + * Accepts #rgb, #rgba, #rrggbb and #rrggbbaa — the '#' is optional. + * + * @param {String} hex Color hex string. + * @return {String} Six-digit lower-case hex without the '#', or '' when unparsable. + */ +const expandHex = hex => { + let value = String(hex == null ? '' : hex).trim().replace(/^#/, ''); + + if (!/^[0-9a-f]+$/i.test(value)) { + return ''; + } + + if (value.length === 3 || value.length === 4) { + // #rgb / #rgba: every digit is doubled (#369 → #336699). + value = value.slice(0, 3).split('').map(c => c + c).join(''); + } else if (value.length === 6 || value.length === 8) { + value = value.slice(0, 6); + } else { + return ''; + } + + return value.toLowerCase(); +} + /** * Calculate an appropriate contrast color. * @@ -36,13 +62,15 @@ export const colorLuminance = (hex, lum) => { export const contrastTextColor = (hex, dark, light) => { dark = dark || '#000000'; light = light || '#ffffff'; - const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex); - if (!result) { + // Hex may be #rgb, #rgba, #rrggbb or #rrggbbaa; the alpha byte is ignored. + // Anything unparsable keeps the historical dark fallback. + const value = expandHex(hex); + if (!value) { return dark; } - const red = parseInt(result[1], 16); - const green = parseInt(result[2], 16); - const blue = parseInt(result[3], 16); + const red = parseInt(value.substr(0, 2), 16); + const green = parseInt(value.substr(2, 2), 16); + const blue = parseInt(value.substr(4, 2), 16); let brightness; brightness = (red * 299) + (green * 587) + (blue * 114); brightness /= 255000; @@ -62,14 +90,16 @@ export const contrastTextColor = (hex, dark, light) => { * @return {String} The hex string. */ export const rgbToHex = rgb => { - const matches = rgb.match(/rgb\(([0-9]+), ([0-9]+), ([0-9]+)\)/); - if (matches.length !== 4) { + const matches = /rgba?\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*(?:,\s*[\d.]+\s*)?\)/.exec(String(rgb == null ? '' : rgb)); + if (!matches) { return ''; } - // From https://gist.github.com/lrvick/2080648. - const bin = matches[1] << 16 | matches[2] << 8 | matches[3]; - return (function(h) { - return '#' + new Array(7 - h.length).join('0') + h; - })(bin.toString(16).toLowerCase()); + // Channels are clamped, so out-of-range input cannot overflow the hex. + // An alpha channel is ignored — this returns an opaque hex. + const value = [matches[1], matches[2], matches[3]] + .map(c => Math.min(255, Number(c)).toString(16).padStart(2, '0')) + .join(''); + + return `#${value}`; } diff --git a/package.json b/package.json index c04b465..e3ce8ba 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@tp/helpers", - "version": "2.14.0", + "version": "2.15.0", "description": "", "main": "closest.js", "scripts": {