Click to See Complete Forum and Search --> : Subtracting Hex values


andersoni
07-23-2003, 05:26 PM
I am wondering if anyone knows how to subtract hex color values? I am in a situation where I need to display certain colors on a site but depending on conditions, I need each of the colors a decreased(softened) a uniform value. So if I know the two colors, how do I determine the differance between the two and then subtract the differance from the other colors?

Thanks

Ian

Charles
07-23-2003, 06:01 PM
<script type="text/javascript">
<!--
alert (parseInt('0000ff', 16) - parseInt('00000f', 16))
// -->
</script>

Exuro
07-23-2003, 06:26 PM
Here's some code I came up with that might help:

<html>
<head>
<script language="JavaScript" type="text/javascript">
<!--
function darken() {
&nbsp;&nbsp;var txt = document.getElementById('txt').style;
&nbsp;&nbsp;oldColor = (txt.color).substr(1);
&nbsp;&nbsp;newColor = "#" + (parseInt(oldColor, 16)-parseInt("111111", 16)).toString(16);
&nbsp;&nbsp;if (newColor.length!=7) { newColor="#000000"; }
&nbsp;&nbsp;txt.color = newColor;
}
//-->
</script>
</head>
<body onload="document.getElementById('txt').style.color='#FFFFFF'">
<div id="txt">Text</div>
<input type="button" onclick="darken()" value="Darken Text">
</body>
</html>

andersoni
07-23-2003, 10:37 PM
Help out a little,

I have #B30000 as my dark color and #EAB6B6 as my lighter color.

I am looking for a way to find the hex value of the first minus the second. That way I can make the code to subtract this differance (using some of the code you provided) and get my other lighter colors.

Making any sense?

Ian

Exuro
07-23-2003, 11:37 PM
Okay, I edited my script (quite a bit) and it should work for you now (I hope)!