Created
March 19, 2010 13:37
-
-
Save cowboy/337501 to your computer and use it in GitHub Desktop.
hex_pct: uber-simple color fader
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
// uber-simple color fader (very limited) | |
// see http://jsfiddle.net/cowboy/XWaNe/ | |
function hex_pct(base,pct){ | |
return base.replace( /00|ff/ig, function(a){ | |
var scale = 0.8, | |
pct_adj = Math.max( 0, Math.min( 1, pct ) ) * scale + ( 1 - scale ) / 2, | |
val = ( pct > 0.5 ? ( pct_adj - 0.5 ) : pct_adj ) * 2 * 255; | |
return pct_adj != 0.5 && ( ( pct_adj > 0.5 ) == ( a == '00' ) ) ? ( '0' + parseInt(val).toString(16) ).substr(-2) : a; | |
}); | |
}; | |
var colors = 'ff0000 00ff00 0000ff ff00ff ffff00 00ffff'.split(' '); | |
$.each( colors, function(i,v){ | |
var container = $('<div class="container"/>').appendTo('body'); | |
for( var pct = 0; pct <= 1; pct += 0.03 ) { | |
$('<div class="color"/>') | |
.text( Math.round( pct * 100 ) + '%' ) | |
.css( 'background', hex_pct( '#' + v, pct ) ) | |
.css( 'color', hex_pct( '#' + v, pct - 0.3 ) ) | |
.css( 'border', '1px solid ' + hex_pct( '#' + v, pct - 0.2 ) ) | |
.appendTo( container ); | |
} | |
}); | |
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment