Why, its elementary, dear Watson. Just grab all possible style-keys, check if they contain the word color, convert them from camel case to hyphenated, then replace all occurrences of the color with the new one using a complex regex system. DUH!
...I have too much free time, sorry:
var keys = Object.keys($('html').get(0).style);
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec('#789034');
var rgb = 'rgb(' + parseInt(result[1], 16) + ', ' + parseInt(result[2], 16) + ', ' + parseInt(result[3], 16) + ')';
for (var k in keys) {
key = keys[k].replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
if (key.indexOf('color') > -1) {
$("*").each(function () {
if ($(this).css(key) == rgb) {
$(this).css(key, '#456780');
}
});
}
}
div {
height: 50px;
width: 50px;
}
.common-color {
color:#789034;
}
.new-cls {
border-style: solid;
border-color:#789034;
}
.awesome-one {
background-color:#789034;
height:200px;
width:300px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="common-color">hello</div>
<p class="new-cls">cool</p>
<div class="awesome-one"></div>
EDIT:
Or, if you would rather, here is a simple function to use. Color 1 will be replaced by color 2:
function replaceColor(color1, color2) {
var keys = Object.keys($('html').get(0).style);
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(color1);
var rgb = 'rgb(' + parseInt(result[1], 16) + ', ' + parseInt(result[2], 16) + ', ' + parseInt(result[3], 16) + ')';
for (var k in keys) {
key = keys[k].replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
if (key.indexOf('color') > -1) {
$("*").each(function () {
if ($(this).css(key) == rgb) {
$(this).css(key, color2);
}
});
}
}
}
color: #789034;
and add this class to all the elements where this color is required. And then it'll be easy to change color using jQuery:$('.myClass').css('color', '#456780');
– Tushar 1 hour ago