Created
April 22, 2009 16:45
-
-
Save champierre/99904 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Script that darken a hex color. | |
Released under the MIT license | |
Junya Ishihara <[email protected]> | |
http://champierre.com | |
*/ | |
function darken(hexstr, scalefactor) { | |
var r = scalefactor; | |
var a, i; | |
if (typeof(hexstr) != 'string') { | |
return hexstr; | |
} | |
hexstr = hexstr.replace(/[^0-9a-f]+/ig, ''); | |
if (hexstr.length == 3) { | |
a = hexstr.split(''); | |
} else if (hexstr.length == 6) { | |
a = hexstr.match(/(\w{2})/g); | |
} else { | |
return hexstr; | |
} | |
for (i=0; i<a.length; i++) { | |
if (a[i].length == 2) | |
a[i] = parseInt(a[i], 16); | |
else { | |
a[i] = parseInt(a[i], 16); | |
a[i] = a[i]*16 + a[i]; | |
} | |
} | |
var maxColor = parseInt('ff', 16); | |
function _darken(a) { | |
return a * r; | |
} | |
for (i=0; i<a.length; i++) { | |
a[i] = _darken(a[i]); | |
a[i] = Math.floor(a[i]).toString(16); | |
if (a[i].length == 1) { | |
a[i] = '0' + a[i]; | |
} | |
} | |
return a.join(''); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment