fix(ui): robust contrast calculation for named colors and variables
All checks were successful
BotUI CI / build (push) Successful in 3m22s
All checks were successful
BotUI CI / build (push) Successful in 3m22s
This commit is contained in:
parent
093f417ff7
commit
756da22dd5
1 changed files with 17 additions and 7 deletions
|
|
@ -942,13 +942,23 @@
|
||||||
|
|
||||||
// Apply theme data from WebSocket events
|
// Apply theme data from WebSocket events
|
||||||
function getContrastYIQ(hexcolor) {
|
function getContrastYIQ(hexcolor) {
|
||||||
if (!hexcolor || typeof hexcolor !== 'string') return '#ffffff';
|
if (!hexcolor) return '#ffffff';
|
||||||
hexcolor = hexcolor.replace("#", "");
|
|
||||||
if (hexcolor.length === 3) hexcolor = hexcolor.split('').map(function (c) { return c + c; }).join('');
|
// Handle named colors and variables by letting the browser resolve them
|
||||||
if (hexcolor.length !== 6) return '#ffffff';
|
var temp = document.createElement("div");
|
||||||
var r = parseInt(hexcolor.substr(0, 2), 16);
|
temp.style.color = hexcolor;
|
||||||
var g = parseInt(hexcolor.substr(2, 2), 16);
|
temp.style.display = "none";
|
||||||
var b = parseInt(hexcolor.substr(4, 2), 16);
|
document.body.appendChild(temp);
|
||||||
|
var style = window.getComputedStyle(temp).color;
|
||||||
|
document.body.removeChild(temp);
|
||||||
|
|
||||||
|
var rgb = style.match(/\d+/g);
|
||||||
|
if (!rgb || rgb.length < 3) return '#ffffff';
|
||||||
|
|
||||||
|
var r = parseInt(rgb[0]);
|
||||||
|
var g = parseInt(rgb[1]);
|
||||||
|
var b = parseInt(rgb[2]);
|
||||||
|
|
||||||
var yiq = ((r * 299) + (g * 587) + (b * 114)) / 1000;
|
var yiq = ((r * 299) + (g * 587) + (b * 114)) / 1000;
|
||||||
return (yiq >= 128) ? '#000000' : '#ffffff';
|
return (yiq >= 128) ? '#000000' : '#ffffff';
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue