Subversion Repositories Integrator Subversion

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 espaco 1
/**
2
 * A class to parse color values
3
 * @author Stoyan Stefanov <sstoo@gmail.com>
4
 * @link   http://www.phpied.com/rgb-color-parser-in-javascript/
5
 * @license Use it if you like it
6
 */
7
function RGBColor(color_string)
8
{
9
    this.ok = false;
10
 
11
    // strip any leading #
12
    if (color_string.charAt(0) == '#') { // remove # if any
13
        color_string = color_string.substr(1,6);
14
    }
15
 
16
    color_string = color_string.replace(/ /g,'');
17
    color_string = color_string.toLowerCase();
18
 
19
    // before getting into regexps, try simple matches
20
    // and overwrite the input
21
    var simple_colors = {
22
        aliceblue: 'f0f8ff',
23
        antiquewhite: 'faebd7',
24
        aqua: '00ffff',
25
        aquamarine: '7fffd4',
26
        azure: 'f0ffff',
27
        beige: 'f5f5dc',
28
        bisque: 'ffe4c4',
29
        black: '000000',
30
        blanchedalmond: 'ffebcd',
31
        blue: '0000ff',
32
        blueviolet: '8a2be2',
33
        brown: 'a52a2a',
34
        burlywood: 'deb887',
35
        cadetblue: '5f9ea0',
36
        chartreuse: '7fff00',
37
        chocolate: 'd2691e',
38
        coral: 'ff7f50',
39
        cornflowerblue: '6495ed',
40
        cornsilk: 'fff8dc',
41
        crimson: 'dc143c',
42
        cyan: '00ffff',
43
        darkblue: '00008b',
44
        darkcyan: '008b8b',
45
        darkgoldenrod: 'b8860b',
46
        darkgray: 'a9a9a9',
47
        darkgreen: '006400',
48
        darkkhaki: 'bdb76b',
49
        darkmagenta: '8b008b',
50
        darkolivegreen: '556b2f',
51
        darkorange: 'ff8c00',
52
        darkorchid: '9932cc',
53
        darkred: '8b0000',
54
        darksalmon: 'e9967a',
55
        darkseagreen: '8fbc8f',
56
        darkslateblue: '483d8b',
57
        darkslategray: '2f4f4f',
58
        darkturquoise: '00ced1',
59
        darkviolet: '9400d3',
60
        deeppink: 'ff1493',
61
        deepskyblue: '00bfff',
62
        dimgray: '696969',
63
        dodgerblue: '1e90ff',
64
        feldspar: 'd19275',
65
        firebrick: 'b22222',
66
        floralwhite: 'fffaf0',
67
        forestgreen: '228b22',
68
        fuchsia: 'ff00ff',
69
        gainsboro: 'dcdcdc',
70
        ghostwhite: 'f8f8ff',
71
        gold: 'ffd700',
72
        goldenrod: 'daa520',
73
        gray: '808080',
74
        green: '008000',
75
        greenyellow: 'adff2f',
76
        honeydew: 'f0fff0',
77
        hotpink: 'ff69b4',
78
        indianred : 'cd5c5c',
79
        indigo : '4b0082',
80
        ivory: 'fffff0',
81
        khaki: 'f0e68c',
82
        lavender: 'e6e6fa',
83
        lavenderblush: 'fff0f5',
84
        lawngreen: '7cfc00',
85
        lemonchiffon: 'fffacd',
86
        lightblue: 'add8e6',
87
        lightcoral: 'f08080',
88
        lightcyan: 'e0ffff',
89
        lightgoldenrodyellow: 'fafad2',
90
        lightgrey: 'd3d3d3',
91
        lightgreen: '90ee90',
92
        lightpink: 'ffb6c1',
93
        lightsalmon: 'ffa07a',
94
        lightseagreen: '20b2aa',
95
        lightskyblue: '87cefa',
96
        lightslateblue: '8470ff',
97
        lightslategray: '778899',
98
        lightsteelblue: 'b0c4de',
99
        lightyellow: 'ffffe0',
100
        lime: '00ff00',
101
        limegreen: '32cd32',
102
        linen: 'faf0e6',
103
        magenta: 'ff00ff',
104
        maroon: '800000',
105
        mediumaquamarine: '66cdaa',
106
        mediumblue: '0000cd',
107
        mediumorchid: 'ba55d3',
108
        mediumpurple: '9370d8',
109
        mediumseagreen: '3cb371',
110
        mediumslateblue: '7b68ee',
111
        mediumspringgreen: '00fa9a',
112
        mediumturquoise: '48d1cc',
113
        mediumvioletred: 'c71585',
114
        midnightblue: '191970',
115
        mintcream: 'f5fffa',
116
        mistyrose: 'ffe4e1',
117
        moccasin: 'ffe4b5',
118
        navajowhite: 'ffdead',
119
        navy: '000080',
120
        oldlace: 'fdf5e6',
121
        olive: '808000',
122
        olivedrab: '6b8e23',
123
        orange: 'ffa500',
124
        orangered: 'ff4500',
125
        orchid: 'da70d6',
126
        palegoldenrod: 'eee8aa',
127
        palegreen: '98fb98',
128
        paleturquoise: 'afeeee',
129
        palevioletred: 'd87093',
130
        papayawhip: 'ffefd5',
131
        peachpuff: 'ffdab9',
132
        peru: 'cd853f',
133
        pink: 'ffc0cb',
134
        plum: 'dda0dd',
135
        powderblue: 'b0e0e6',
136
        purple: '800080',
137
        red: 'ff0000',
138
        rosybrown: 'bc8f8f',
139
        royalblue: '4169e1',
140
        saddlebrown: '8b4513',
141
        salmon: 'fa8072',
142
        sandybrown: 'f4a460',
143
        seagreen: '2e8b57',
144
        seashell: 'fff5ee',
145
        sienna: 'a0522d',
146
        silver: 'c0c0c0',
147
        skyblue: '87ceeb',
148
        slateblue: '6a5acd',
149
        slategray: '708090',
150
        snow: 'fffafa',
151
        springgreen: '00ff7f',
152
        steelblue: '4682b4',
153
        tan: 'd2b48c',
154
        teal: '008080',
155
        thistle: 'd8bfd8',
156
        tomato: 'ff6347',
157
        turquoise: '40e0d0',
158
        violet: 'ee82ee',
159
        violetred: 'd02090',
160
        wheat: 'f5deb3',
161
        white: 'ffffff',
162
        whitesmoke: 'f5f5f5',
163
        yellow: 'ffff00',
164
        yellowgreen: '9acd32'
165
    };
166
    for (var key in simple_colors) {
167
        if (color_string == key) {
168
            color_string = simple_colors[key];
169
        }
170
    }
171
    // emd of simple type-in colors
172
 
173
    // array of color definition objects
174
    var color_defs = [
175
        {
176
            re: /^rgb\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})\)$/,
177
            example: ['rgb(123, 234, 45)', 'rgb(255,234,245)'],
178
            process: function (bits){
179
                return [
180
                    parseInt(bits[1]),
181
                    parseInt(bits[2]),
182
                    parseInt(bits[3])
183
                ];
184
            }
185
        },
186
        {
187
            re: /^(\w{2})(\w{2})(\w{2})$/,
188
            example: ['#00ff00', '336699'],
189
            process: function (bits){
190
                return [
191
                    parseInt(bits[1], 16),
192
                    parseInt(bits[2], 16),
193
                    parseInt(bits[3], 16)
194
                ];
195
            }
196
        },
197
        {
198
            re: /^(\w{1})(\w{1})(\w{1})$/,
199
            example: ['#fb0', 'f0f'],
200
            process: function (bits){
201
                return [
202
                    parseInt(bits[1] + bits[1], 16),
203
                    parseInt(bits[2] + bits[2], 16),
204
                    parseInt(bits[3] + bits[3], 16)
205
                ];
206
            }
207
        }
208
    ];
209
 
210
    // search through the definitions to find a match
211
    for (var i = 0; i < color_defs.length; i++) {
212
        var re = color_defs[i].re;
213
        var processor = color_defs[i].process;
214
        var bits = re.exec(color_string);
215
        if (bits) {
216
            channels = processor(bits);
217
            this.r = channels[0];
218
            this.g = channels[1];
219
            this.b = channels[2];
220
            this.ok = true;
221
        }
222
 
223
    }
224
 
225
    // validate/cleanup values
226
    this.r = (this.r < 0 || isNaN(this.r)) ? 0 : ((this.r > 255) ? 255 : this.r);
227
    this.g = (this.g < 0 || isNaN(this.g)) ? 0 : ((this.g > 255) ? 255 : this.g);
228
    this.b = (this.b < 0 || isNaN(this.b)) ? 0 : ((this.b > 255) ? 255 : this.b);
229
 
230
    // some getters
231
    this.toRGB = function () {
232
        return 'rgb(' + this.r + ', ' + this.g + ', ' + this.b + ')';
233
    }
234
    this.toHex = function () {
235
        var r = this.r.toString(16);
236
        var g = this.g.toString(16);
237
        var b = this.b.toString(16);
238
        if (r.length == 1) r = '0' + r;
239
        if (g.length == 1) g = '0' + g;
240
        if (b.length == 1) b = '0' + b;
241
        return '#' + r + g + b;
242
    }
243
 
244
    // help
245
    this.getHelpXML = function () {
246
 
247
        var examples = new Array();
248
        // add regexps
249
        for (var i = 0; i < color_defs.length; i++) {
250
            var example = color_defs[i].example;
251
            for (var j = 0; j < example.length; j++) {
252
                examples[examples.length] = example[j];
253
            }
254
        }
255
        // add type-in colors
256
        for (var sc in simple_colors) {
257
            examples[examples.length] = sc;
258
        }
259
 
260
        var xml = document.createElement('ul');
261
        xml.setAttribute('id', 'rgbcolor-examples');
262
        for (var i = 0; i < examples.length; i++) {
263
            try {
264
                var list_item = document.createElement('li');
265
                var list_color = new RGBColor(examples[i]);
266
                var example_div = document.createElement('div');
267
                example_div.style.cssText =
268
                        'margin: 3px; '
269
                        + 'border: 1px solid black; '
270
                        + 'background:' + list_color.toHex() + '; '
271
                        + 'color:' + list_color.toHex()
272
                ;
273
                example_div.appendChild(document.createTextNode('test'));
274
                var list_item_value = document.createTextNode(
275
                    ' ' + examples[i] + ' -> ' + list_color.toRGB() + ' -> ' + list_color.toHex()
276
                );
277
                list_item.appendChild(example_div);
278
                list_item.appendChild(list_item_value);
279
                xml.appendChild(list_item);
280
 
281
            } catch(e){}
282
        }
283
        return xml;
284
 
285
    }
286
 
287
}
288