/** * @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 ) }; if ( color_rgb.r > 0xFF ) { color_rgb.r = 0xFF; } else if ( color_rgb.r < 0x00 ) { color_rgb.r = 0x00; } if ( color_rgb.g > 0xFF ) { color_rgb.g = 0xFF; } else if ( color_rgb.g < 0x00 ) { color_rgb.g = 0x00; } if ( color_rgb.b > 0xFF ) { color_rgb.b = 0xFF; } else if ( color_rgb.b < 0x00 ) { color_rgb.b = 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 ); } }