~open-arch-timesheets/timesheets/initial

« back to all changes in this revision

Viewing changes to ignored/curr remote js/ColourObject.js

  • Committer: Lucian
  • Date: 2010-01-29 15:23:55 UTC
  • Revision ID: lucian.pricop@yahoo.co.uk-20100129152355-izd5bc9dbyyguuop
nothing

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
function colourObject(red,green,blue){
 
3
        this.red = red;
 
4
        this.green = green;
 
5
        this.blue = 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]); 
 
13
        }
 
14
        this.toString = function (){
 
15
                return "rgb("+this.red+","+this.green+","+this.blue+")";
 
16
        }
 
17
        this.isEqual = function(otherColour){
 
18
                if(this.red == otherColour.red && this.green == otherColour.green && this.blue == otherColour.blue)
 
19
                        return true;
 
20
                return false;
 
21
        }
 
22
}
 
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];
 
43
        }
 
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;
 
55
                                        return suggested;
 
56
                                }
 
57
                        }
 
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;
 
62
                        return suggested;
 
63
                }
 
64
                //if asking for a colour
 
65
                if(!this.arrayFullyUsed)
 
66
                {
 
67
                        // look for the first unused colour
 
68
                        this.colourPointer = 0; 
 
69
                        while(this.colourPointer<this.colourArrayUsage.length && this.colourArrayUsage[this.colourPointer]==1)
 
70
                                this.colourPointer++;
 
71
                        if(this.colourPointer<this.colourArrayUsage.length)
 
72
                        {
 
73
                                this.colourArrayUsage[this.colourPointer] = 1;
 
74
                                return this.colourArray[this.colourPointer];
 
75
                        }
 
76
                }
 
77
                //if we got so far, then all the colours are used
 
78
                this.arrayFullyUsed = true;
 
79
                //generate a new one
 
80
                var r,g,b;
 
81
                var newColourObject = null;
 
82
                lastColour = this.colourArray[this.colourArray.length-1];
 
83
//              alert(lastColour);
 
84
                var rand = Math.round(Math.random() * 6);
 
85
//              alert(lastColour.red);
 
86
                switch(rand)
 
87
                {
 
88
                        case 0:
 
89
                                r = 255-lastColour.red;
 
90
                                g = 255-lastColour.green;
 
91
                                b = 255-lastColour.blue;
 
92
                                break;
 
93
                        case 1:
 
94
                                r = lastColour.red;
 
95
                                g = 255-lastColour.green;
 
96
                                b = 255-lastColour.blue;
 
97
                                break;
 
98
                        case 2:
 
99
                                r = 255-lastColour.red;
 
100
                                g = lastColour.green;
 
101
                                b = 255-lastColour.blue;
 
102
                                break;
 
103
                        case 3:
 
104
                                r = 255-lastColour.red;
 
105
                                g = 255-lastColour.green;
 
106
                                b = lastColour.blue;
 
107
                                break;
 
108
                        case 4:
 
109
                                r = lastColour.red;
 
110
                                g = lastColour.green;
 
111
                                b = 255-lastColour.blue;
 
112
                                break;
 
113
                        case 5:
 
114
                                r = 255-lastColour.red;
 
115
                                g = lastColour.green;
 
116
                                b = lastColour.blue;
 
117
                                break;
 
118
                        case 6:
 
119
                                r = lastColour.red;
 
120
                                g = 255-lastColour.green;
 
121
                                b = lastColour.blue;
 
122
                                break;
 
123
                }
 
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);
 
133
                var loops = 0;
 
134
                while(this.inArray(newColourObject,40) && loops<32)
 
135
                {
 
136
                        if(r<=g && r<=b)//r is min
 
137
                        {
 
138
                                r+=20;
 
139
                                if(r>255)
 
140
                                        r=0;
 
141
                        }
 
142
                        if(g<=r && g<=b)//g is min
 
143
                        {
 
144
                                g+=20;
 
145
                                if(g>255)
 
146
                                        g=0;
 
147
                        }
 
148
                        if(b<=g && b<=r)//b is min
 
149
                        {
 
150
                                b+=20;
 
151
                                if(b>255)
 
152
                                        b=0;
 
153
                        }
 
154
                        newColourObject.red = r;
 
155
                        newColourObject.green = g;
 
156
                        newColourObject.blue = b;
 
157
                        ++loops;
 
158
                }
 
159
                if(loops==32)
 
160
                {
 
161
//                      alert("more than 32 loops");
 
162
                        while(this.inArray(newColourObject,20) && loops<96)
 
163
                        {
 
164
                                r+=10;
 
165
                                if(r>255)
 
166
                                        r=0;
 
167
                                g+=10;
 
168
                                if(g>255)
 
169
                                        g=0;
 
170
                                b+=10;
 
171
                                if(b>255)
 
172
                                        b=0;
 
173
                                newColourObject.red = r;
 
174
                                newColourObject.green = g;
 
175
                                newColourObject.blue = b;
 
176
                                ++loops;
 
177
                        }
 
178
                        if(loops==96)
 
179
                                alert("second loop breached");
 
180
                }
 
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;
 
186
        }
 
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)
 
189
                        return true;
 
190
                for(i=0;i<this.colourArray.length;++i)
 
191
                {
 
192
                        if(this.getDistance(newColourObject,this.colourArray[i])<distance)
 
193
                                return true;
 
194
                }
 
195
                return false;
 
196
        }
 
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);
 
201
                return rD+gD+bD;
 
202
        }
 
203
        //returns the background colour of an element given as parameter
 
204
        this.getColour = function(object)
 
205
        {
 
206
                if(!object)
 
207
                        return null;
 
208
                if(object.style && object.style.backgroundColor)
 
209
                        colour = object.style.backgroundColor;
 
210
                else
 
211
                {
 
212
                        var style = object.getAttribute("style");
 
213
                        if(!style)
 
214
                                return null;
 
215
                        var colour;
 
216
                        var properties = style.split(";");
 
217
                        for(var propI=0;propI < properties.length; ++propI)
 
218
                                if(properties[propI].indexOf("background-color")!=-1)
 
219
                                {
 
220
                                        colour = trim(properties[propI].split(":")[1]);
 
221
                                        break;
 
222
                                }
 
223
                }
 
224
                //some browsers keep the colour in hexadecimal
 
225
                var position = colour.indexOf("#");
 
226
                if(position!=-1)
 
227
                {
 
228
                        if(colour.length <7)
 
229
                        {
 
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+")";
 
234
                        }
 
235
                        else if(colour.length ==7)
 
236
                        {
 
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+")";
 
241
                        }
 
242
                        else
 
243
                        {
 
244
                                alert("There's a problem in the colour object!!!")
 
245
                                return null;
 
246
                        }
 
247
                }
 
248
                return colour;
 
249
        }
 
250
        
 
251
}
 
 
b'\\ No newline at end of file'