﻿function Fade(id, startColour, endColour, count, speed, delay ) { this.id = id; this.startColour = startColour; this.endColour = endColour; this.count = count; this.speed = speed; this.delay = delay; if (typeof this.id == "string") { this.obj = document.getElementById(id);} else { this.obj = this.id;}
this.colour = new Array(); this.steps = 0; this.init = init; this.fade = fade; this.parseColour = parseColour; function init() { first = this.parseColour(this.startColour, 'hex'); last = this.parseColour(this.endColour, 'hex'); this.colour = new Array(); this.colour[this.count] = this.startColour; for (i=0; i<this.count; i++) { temp = "rgb("; temp += parseInt(first[0]+(last[0]-first[0])/this.count*i); temp += ","; temp += parseInt(first[1]+(last[1]-first[1])/this.count*i); temp += ","; temp += parseInt(first[2]+(last[2]-first[2])/this.count*i); temp += ")"; this.colour[this.count-i] = temp;}
this.colour[0] = this.endColour; var thisObj = this; setTimeout( function() { thisObj.fade() }, this.delay);}
function fade() { if (this.count >= 0) { this.obj.style.backgroundColor = this.colour[this.count--]; var thisObj = this; setTimeout( function() { thisObj.fade() }, this.speed);}
}
function parseColour(colour, t) { var m = 1; col = colour.replace(/[\#rgb\(]*/,''); if (t == 'hex') { if (col.length == 3) { a = col.substr(0,1); b = col.substr(1,1); c = col.substr(2,1); col = a + a + b + b + c + c;}
var num = new Array(col.substr(0,2),col.substr(2,2),col.substr(4,2)); var base = 16;} else { var num = col.split(','); var base = 10;}
if (t == 'rgbp') {m = 2.55}
var ret = new Array(parseInt(num[0],base)*m,parseInt(num[1],base)*m,parseInt(num[2],base)*m); return(ret);}
}