|
Post by [dmsuperman] on Jan 10, 2006 18:34:03 GMT
This is a little function to invert a hexadecimal color. It removes non-alphanumeric characters first.
<script type="text/javascript"> <!-- //Color inverter by [dmsuperman] //Open source
function invertColor(iColor){ iColor = iColor.toUpperCase(); iColor = iColor.replace(/[^A-Z0-9]/, ""); var colors = "0123456789ABCDEF"; var allCols = 15; var iRtnCol = "#"; for(x = 0; x < iColor.length; x++){ var curCol = iColor.charAt(x); var colPos = 15 - colors.indexOf(curCol); var newCol = colors.charAt(colPos); } return(iRtnCol); } var iColor = "#0004EA"; alert(iColor + " - " + invertColor(iColor)); //--> </script>
|
|
|
Post by acoolie530 on Jan 10, 2006 21:59:30 GMT
So... 0004EA would be turned into FFFB16?
|
|
|
Post by [dmsuperman] on Jan 12, 2006 3:49:06 GMT
yes, and you can have the has mark as well. It also makes them all upper-case before conversion.
|
|
|
Post by Eric on Jan 12, 2006 4:13:55 GMT
Not too shabby
|
|
|
Post by acoolie530 on Jan 12, 2006 21:13:49 GMT
Looks good, but two things: Why is the color made to upper case twice and could you give me an example of a use for this?
|
|
|
Post by [dmsuperman] on Jan 13, 2006 0:43:21 GMT
1) I convert the input color to upper case for less work (rather than replacing f and F with 0) 2) I dunno, I remember Shadow made something like this where he inverted the entire page's colors with a button click, just to do something.
|
|
|
Post by Eric on Jan 13, 2006 1:53:35 GMT
1) I convert the input color to upper case for less work (rather than replacing f and F with 0) 2) I dunno, I remember Shadow made something like this where he inverted the entire page's colors with a button click, just to do something. What ACoolie is talking about for #1 is this: var colors = "0123456789ABCDEF"; ... iRtnCol += newCol.toUpperCase();You see, the letters are already capital, but you do it again later on.
|
|
|
Post by Xephra on Jan 13, 2006 13:51:23 GMT
Yeah, you already have them uppercase.
Not a bad code. =)
|
|
|
Post by [dmsuperman] on Jan 13, 2006 21:26:48 GMT
Oh, I dunno maybe I had that from before or somefing... I'll remove it.
|
|