color: accept #rgb/#rgba and #rrggbbaa in contrastTextColor; stop rgbToHex throwing
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.
This commit is contained in:
1 parent
b9adc87306
commit
5978a33283
2 files changed
+43
-13
No files matched your search
@@ -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}`;
|
||||
}
|
||||
Reference in new issue
Block a user