2
function colourObject(red,green,blue){
6
this.fromString = function(string){
7
var tmpArr = string.split("(");
8
var tmp = tmpArr[1].substr(0,tmpArr[1].length-1);
9
tmpArr = tmp.split(",");
10
this.red = parseInt(tmpArr[0]);
11
this.green = parseInt(tmpArr[1]);
12
this.blue = parseInt(tmpArr[2]);
14
this.toString = function (){
15
return "rgb("+this.red+","+this.green+","+this.blue+")";
17
this.isEqual = function(otherColour){
18
if(this.red == otherColour.red && this.green == otherColour.green && this.blue == otherColour.blue)
23
function masterColourObject(){
24
this.colourArray = new Array( new colourObject(255,0,51),
25
new colourObject(255,255,51),
26
new colourObject(51,0,255),
27
new colourObject(0,255,204),
28
new colourObject(204,204,204),
29
new colourObject(204,51,0),
30
new colourObject(153,51,102),
31
new colourObject(255,153,255),
32
new colourObject(51,204,255),
33
new colourObject(0,255,0),
34
new colourObject(170,170,170),
35
new colourObject(153,102,51),
36
new colourObject(51,153,204),
37
new colourObject(238,204,51));
38
this.colourArrayUsage = new Array(0,0,0,0,0,0,0,0,0,0,0,0,0,0);
39
this.colourPointer = -1;
40
this.arrayFullyUsed = false;
41
this.currentColour = function (){
42
return this.colourArray[this.colourPointer];
44
this.nextColour = function (suggestedColour){
45
if(suggestedColour !=null && suggestedColour !="null" && suggestedColour !="" )
46
{//if we're given a colour
47
var suggested = new colourObject(0,0,0);
48
suggested.fromString(suggestedColour);
49
for(i=0;i<this.colourArray.length;++i)
50
{//search the array for a match
51
if(this.colourArray[i].isEqual(suggested))
52
{//if found, mark it as used
53
this.colourArrayUsage[i] = 1;
54
this.colourPointer = i;
58
//if it wasn't found, add it to the array and mark it as used
59
this.colourArray.push(suggested);
60
this.colourArrayUsage.push(1);
61
this.colourPointer = this.colourArray.length-1;
64
//if asking for a colour
65
if(!this.arrayFullyUsed)
67
// look for the first unused colour
68
this.colourPointer = 0;
69
while(this.colourPointer<this.colourArrayUsage.length && this.colourArrayUsage[this.colourPointer]==1)
71
if(this.colourPointer<this.colourArrayUsage.length)
73
this.colourArrayUsage[this.colourPointer] = 1;
74
return this.colourArray[this.colourPointer];
77
//if we got so far, then all the colours are used
78
this.arrayFullyUsed = true;
81
var newColourObject = null;
82
lastColour = this.colourArray[this.colourArray.length-1];
84
var rand = Math.round(Math.random() * 6);
85
// alert(lastColour.red);
89
r = 255-lastColour.red;
90
g = 255-lastColour.green;
91
b = 255-lastColour.blue;
95
g = 255-lastColour.green;
96
b = 255-lastColour.blue;
99
r = 255-lastColour.red;
100
g = lastColour.green;
101
b = 255-lastColour.blue;
104
r = 255-lastColour.red;
105
g = 255-lastColour.green;
110
g = lastColour.green;
111
b = 255-lastColour.blue;
114
r = 255-lastColour.red;
115
g = lastColour.green;
120
g = 255-lastColour.green;
124
//no matter what has happend previously, the maximum will get the opposite value
125
if(r>=g && r>=b)//r is max
126
r = 255-lastColour.red;
127
else if(g>=r && g>=b)//g is max
128
g = 255-lastColour.green;
129
else if(b>=g && b>=r)//b is max
130
b = 255-lastColour.blue;
131
newColourObject=new colourObject(r,g,b);
132
// alert(newColourObject);
134
while(this.inArray(newColourObject,40) && loops<32)
136
if(r<=g && r<=b)//r is min
142
if(g<=r && g<=b)//g is min
148
if(b<=g && b<=r)//b is min
154
newColourObject.red = r;
155
newColourObject.green = g;
156
newColourObject.blue = b;
161
// alert("more than 32 loops");
162
while(this.inArray(newColourObject,20) && loops<96)
173
newColourObject.red = r;
174
newColourObject.green = g;
175
newColourObject.blue = b;
179
alert("second loop breached");
181
// alert(loops+" dist("+newColourObject+","+lastColour+")="+this.getDistance(newColourObject,lastColour));
182
this.colourArray.push(newColourObject);
183
this.colourArrayUsage.push(1);
184
this.colourPointer = this.colourArray.length-1;
185
return newColourObject;
187
this.inArray = function(newColourObject,distance){
188
if(this.getDistance(newColourObject,new colourObject(0,0,0))<20 || this.getDistance(newColourObject,new colourObject(255,255,255))<120)
190
for(i=0;i<this.colourArray.length;++i)
192
if(this.getDistance(newColourObject,this.colourArray[i])<distance)
197
this.getDistance = function(colourA,colourB){
198
var rD = Math.abs(colourA.red - colourB.red);
199
var gD = Math.abs(colourA.green - colourB.green);
200
var bD = Math.abs(colourA.blue - colourB.blue);
203
//returns the background colour of an element given as parameter
204
this.getColour = function(object)
208
if(object.style && object.style.backgroundColor)
209
colour = object.style.backgroundColor;
212
var style = object.getAttribute("style");
216
var properties = style.split(";");
217
for(var propI=0;propI < properties.length; ++propI)
218
if(properties[propI].indexOf("background-color")!=-1)
220
colour = trim(properties[propI].split(":")[1]);
224
//some browsers keep the colour in hexadecimal
225
var position = colour.indexOf("#");
230
var r = parseInt(colour.substr(position+1,1)+colour.substr(position+1,1));
231
var g = parseInt(colour.substr(position+2,1)+colour.substr(position+2,1));
232
var b = parseInt(colour.substr(position+3,1)+colour.substr(position+3,1));
233
colour = "rgb("+r+","+g+","+b+")";
235
else if(colour.length ==7)
237
var r = parseInt(colour.substr(position+1,2));
238
var g = parseInt(colour.substr(position+3,2));
239
var b = parseInt(colour.substr(position+5,2));
240
colour = "rgb("+r+","+g+","+b+")";
244
alert("There's a problem in the colour object!!!")
b'\\ No newline at end of file'