From 756da22dd5d2101d02471732e27b29cd97e805ed Mon Sep 17 00:00:00 2001 From: "Rodrigo Rodriguez (Pragmatismo)" Date: Fri, 20 Feb 2026 20:37:47 -0300 Subject: [PATCH] fix(ui): robust contrast calculation for named colors and variables --- ui/suite/chat/chat.html | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/ui/suite/chat/chat.html b/ui/suite/chat/chat.html index 84acbbc..8a2da6a 100644 --- a/ui/suite/chat/chat.html +++ b/ui/suite/chat/chat.html @@ -942,13 +942,23 @@ // Apply theme data from WebSocket events function getContrastYIQ(hexcolor) { - if (!hexcolor || typeof hexcolor !== 'string') return '#ffffff'; - hexcolor = hexcolor.replace("#", ""); - if (hexcolor.length === 3) hexcolor = hexcolor.split('').map(function (c) { return c + c; }).join(''); - if (hexcolor.length !== 6) return '#ffffff'; - var r = parseInt(hexcolor.substr(0, 2), 16); - var g = parseInt(hexcolor.substr(2, 2), 16); - var b = parseInt(hexcolor.substr(4, 2), 16); + if (!hexcolor) return '#ffffff'; + + // Handle named colors and variables by letting the browser resolve them + var temp = document.createElement("div"); + temp.style.color = hexcolor; + temp.style.display = "none"; + 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; return (yiq >= 128) ? '#000000' : '#ffffff'; }