-
-
Save nicholasruunu/5277063 to your computer and use it in GitHub Desktop.
Made it a little more DRY
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
/** | |
* @author Jonathan Barronville | |
* @example | |
* var color_001 = colorShader( '000000', 255 ); | |
* // color_001 === 'ffffff'; | |
* var color_002 = colorShader( 'ffffff', -255 ); | |
* // color_002 === '000000'; | |
* @param {String} color_hex | |
* @param {Number} amount | |
* @return {String} | |
*/ | |
function colorShader( color_hex, amount ) { | |
'use strict'; | |
if ( ! isHexColorString( color_hex ) ) { | |
return null; | |
} | |
var color_number = parseInt( color_hex, 16 ); | |
var color_rgb = { | |
'r': ( ( color_number >> 16 ) + amount ), | |
'g': ( ( color_number & 0xFF ) + amount ), | |
'b': ( ( ( color_number >> 8 ) & 0xFF ) + amount ) | |
}; | |
for ( color in color_rgb ) { | |
if ( color > 0xFF ) { | |
color_rgb.color = 0xFF; | |
} else if ( color < 0x00 ) { | |
color_rgb.color = 0x00; | |
} | |
} | |
var color_new = ( | |
( color_rgb.r << 16 ) | color_rgb.g | ( color_rgb.b << 8 ) | |
).toString( 16 ); | |
if ( color_new.length === 6 ) { | |
return color_new; | |
} else { | |
return ( ( | |
new Array( ( 6 - color_new.length ) + 1 ) | |
).join( '0' ) + color_new ); | |
} | |
} |
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
function colorShader(a,b){"use strict";if(!isHexColorString(a))return null;var c=parseInt(a,16),d={r:(c>>16)+b,g:(255&c)+b,b:(255&c>>8)+b};d.r>255?d.r=255:0>d.r&&(d.r=0),d.g>255?d.g=255:0>d.g&&(d.g=0),d.b>255?d.b=255:0>d.b&&(d.b=0);var e=(d.r<<16|d.g|d.b<<8).toString(16);return 6===e.length?e:Array(6-e.length+1).join("0")+e} |
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
/** | |
* @author Jonathan Barronville | |
* @param {String} hex_string | |
* @return {Boolean} | |
*/ | |
function isHexColorString( hex_string ) { | |
'use strict'; | |
if ( {}.toString.call( hex_string ) !== '[object String]' ) { | |
return false; | |
} | |
hex_string = hex_string.trim(); | |
hex_string = hex_string.toLowerCase(); | |
if ( ! /^[0-9a-f]{6}$/.test( hex_string ) ) { | |
return false; | |
} | |
return true; | |
} |
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
function isHexColorString(a){"use strict";return"[object String]"!=={}.toString.call(a)?!1:(a=a.trim(),a=a.toLowerCase(),/^[0-9a-f]{6}$/.test(a)?!0:!1)} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment