מעדכן שבאדיבות @dovid נעזרתי בקוד הזה:
function hexToRgb(hex) {
// Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF")
var shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
hex = hex.replace(shorthandRegex, function(m, r, g, b) {
return r + r + g + g + b + b;
});
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? "rgb(" + [
parseInt(result[1], 16),
parseInt(result[2], 16),
parseInt(result[3], 16)
].join(', ') + ")" : null;
}
// Function to change a color to another one
function colorChange(colorOld, colorNew, strict = false) {
// If hex notation, convert to rgb
if (colorOld.includes('#'))
colorOld = hexToRgb(colorOld);
// Loop through all elements styles
[...document.all].forEach(elm => {
let cStyle = getComputedStyle(elm);
[...cStyle].forEach(prop => {
// Escape if not a string
if (typeof cStyle[prop] !== 'string') return;
// Check if colorOld is in property
if (cStyle[prop].includes(colorOld)){
// If strict, colorOld is replaced only if it's the only value of the property
if (!strict || cStyle[prop] === colorOld)
elm.style[prop] = cStyle[prop].replace(colorOld, colorNew); // Replace color
}
})
})
};
colorChange("#D81D59", 'orange');