Skip to content

Instantly share code, notes, and snippets.

@jonathanmarvens
Last active December 15, 2015 14:49
Show Gist options
  • Select an option

  • Save jonathanmarvens/5276974 to your computer and use it in GitHub Desktop.

Select an option

Save jonathanmarvens/5276974 to your computer and use it in GitHub Desktop.

Revisions

  1. jonathanmarvens revised this gist Mar 30, 2013. 4 changed files with 19 additions and 12 deletions.
    24 changes: 16 additions & 8 deletions color-shader.js
    Original file line number Diff line number Diff line change
    @@ -23,14 +23,22 @@ function colorShader( color_hex, amount ) {
    'b': ( ( ( color_number >> 8 ) & 0xFF ) + amount )
    };

    for ( var key in color_rgb ) {
    if ( {}.hasOwnProperty.call( color_rgb, key ) ) {
    if ( color_rgb[ key ] > 0xFF ) {
    color_rgb[ key ] = 0xFF;
    } else if ( color_rgb[ key ] < 0x00 ) {
    color_rgb[ key ] = 0x00;
    }
    }
    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 = (
    2 changes: 1 addition & 1 deletion color-shader.min.js
    Original file line number Diff line number Diff line change
    @@ -1 +1 @@
    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};for(var e in d)({}).hasOwnProperty.call(d,e)&&(d[e]>255?d[e]=255:0>d[e]&&(d[e]=0));var f=(d.r<<16|d.g|d.b<<8).toString(16);return 6===f.length?f:Array(6-f.length+1).join("0")+f}
    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}
    3 changes: 1 addition & 2 deletions is-hex-color-string.js
    Original file line number Diff line number Diff line change
    @@ -11,9 +11,8 @@ function isHexColorString( hex_string ) {
    }

    hex_string = hex_string.trim();
    hex_string = hex_string.toLowerCase();

    if ( ! /^[0-9a-f]{6}$/.test( hex_string ) ) {
    if ( ! ( /^[0-9a-f]{6}$/i ).test( hex_string ) ) {
    return false;
    }

    2 changes: 1 addition & 1 deletion is-hex-color-string.min.js
    Original file line number Diff line number Diff line change
    @@ -1 +1 @@
    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)}
    function isHexColorString(a){"use strict";return"[object String]"!=={}.toString.call(a)?!1:(a=a.trim(),/^[0-9a-f]{6}$/i.test(a)?!0:!1)}
  2. jonathanmarvens revised this gist Mar 30, 2013. 2 changed files with 9 additions and 17 deletions.
    24 changes: 8 additions & 16 deletions color-shader.js
    Original file line number Diff line number Diff line change
    @@ -23,22 +23,14 @@ function colorShader( color_hex, 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;
    for ( var key in color_rgb ) {
    if ( {}.hasOwnProperty.call( color_rgb, key ) ) {
    if ( color_rgb[ key ] > 0xFF ) {
    color_rgb[ key ] = 0xFF;
    } else if ( color_rgb[ key ] < 0x00 ) {
    color_rgb[ key ] = 0x00;
    }
    }
    }

    var color_new = (
    2 changes: 1 addition & 1 deletion color-shader.min.js
    Original file line number Diff line number Diff line change
    @@ -1 +1 @@
    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}
    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};for(var e in d)({}).hasOwnProperty.call(d,e)&&(d[e]>255?d[e]=255:0>d[e]&&(d[e]=0));var f=(d.r<<16|d.g|d.b<<8).toString(16);return 6===f.length?f:Array(6-f.length+1).join("0")+f}
  3. jonathanmarvens created this gist Mar 30, 2013.
    55 changes: 55 additions & 0 deletions color-shader.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@
    /**
    * @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 );
    }
    }
    1 change: 1 addition & 0 deletions color-shader.min.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    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}
    21 changes: 21 additions & 0 deletions is-hex-color-string.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    /**
    * @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;
    }
    1 change: 1 addition & 0 deletions is-hex-color-string.min.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    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)}