Skip to content

Instantly share code, notes, and snippets.

@cowboy
cowboy / hex_pct.js
Created March 19, 2010 13:37
hex_pct: uber-simple color fader
// 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;
});
@cowboy
cowboy / gist:337716
Created March 19, 2010 16:07
Better indentation for TextMate
### Indent with opt-tab
### http://benalman.com/grab/6e0f7b.png
#!/usr/bin/perl -w
$tab_soft = ' ' x $ENV{'TM_TAB_SIZE'};
$tab_hard = "\t";
$tab = $ENV{'TM_SOFT_TABS'} eq "YES" ? $tab_soft : $tab_hard;
@cowboy
cowboy / spot-the-error.js
Created March 20, 2010 16:45
Damn browser crash...
(function($){
var supports_onhashchange = 'onhashchange' in window,
last_hash = location.hash,
timeout_id;
$.event.special.hashchange = {
setup: function() {
if ( supports_onhashchange ) { return false; }
//EnhanceJS isIE test idea
//detect IE and version number through injected conditional comments (no UA detect, no need for cond. compilation / jscript check)
//version arg is for IE version (optional)
//comparison arg supports 'lte', 'gte', etc (optional)
var isIE = (function(){
var doc = document,
@cowboy
cowboy / gist:360138
Created April 8, 2010 14:46 — forked from remy/gist:360113
setInterval and setTimeout patterns
// =========================
// SetInterval
// =========================
// While not truly accurate, setInterval is still fairly good, time-wise.
// Better for things like a "one second tick" but not great for expensive
// code executed at small intervals as iterations can "stack".
// (ECMAScript 5 strict mode compatible)
@cowboy
cowboy / stupid_triggerhandler.js
Created April 9, 2010 20:06
Triggering bound event handlers
// The jQuery way.
$(elem).bind( 'foo.bar', function(event){
do_something( event.target );
}).triggerHandler( 'foo.bar' );
// The old-school silly way.
function fn( event ){
@cowboy
cowboy / jquery_widget_factory_idea.js
Created April 11, 2010 03:52
stateful jQuery widget pattern idea
// Rough "widget factory" pattern idea. Maybe dumb, but I'm just fooling
// around at the moment.
(function($,undefined){
'$:nomunge'; // Used by YUI compressor.
var widget_name = 'myWidget',
default_method = 'init',
default_data = {};
-- OLD, but amusing nonetheless
-- http://www.wowinterface.com/downloads/info6813-CowboysMasterBaiter.html
-- INIT
CB_MB = {};
CB_MB.Version = "20006.003";
function CB_MB:OnLoad()
@cowboy
cowboy / gist:375615
Created April 22, 2010 18:29
What my code looked like in 1999 (custom game scripting language)
// mHUD version 5.0
//
// (c) 8/9/99 Cowboy, don't modify this without my permission
// -
// New in 5.0: NewOpts support! (see the NewOpts HELP for info, please!)
//
// Let me know of any BUGS, thanks. My email is [email protected]
// -
// NOTE: Requires Presto Pack 0.93 or greater!
//
@cowboy
cowboy / jquery.ba-domchanged.js
Created April 24, 2010 22:12
"domchanged" event idea for ralph holzmann
// Rough implementation, untested, etc
// Will obviously fail when non-overridden methods are used to change the DOM
(function($){
// All "DOM modifying" methods (probably not complete).
var methods = 'unwrap html wrapApp wrapInner append appendTo html prepend prependTo text after before insertAfter insertBefore'.split(' ');
$.each( methods, function( i, method ) {
// Store a reference to the original method.