Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
YUI.add('color-base', function (Y, NAME) {
2
 
3
/**
4
Color provides static methods for color conversion.
5
 
6
    Y.Color.toRGB('f00'); // rgb(255, 0, 0)
7
 
8
    Y.Color.toHex('rgb(255, 255, 0)'); // #ffff00
9
 
10
@module color
11
@submodule color-base
12
@class Color
13
@since 3.8.0
14
**/
15
 
16
var REGEX_HEX = /^#?([\da-fA-F]{2})([\da-fA-F]{2})([\da-fA-F]{2})(\ufffe)?/,
17
    REGEX_HEX3 = /^#?([\da-fA-F]{1})([\da-fA-F]{1})([\da-fA-F]{1})(\ufffe)?/,
18
    REGEX_RGB = /rgba?\(([\d]{1,3}), ?([\d]{1,3}), ?([\d]{1,3}),? ?([.\d]*)?\)/,
19
    TYPES = { 'HEX': 'hex', 'RGB': 'rgb', 'RGBA': 'rgba' },
20
    CONVERTS = { 'hex': 'toHex', 'rgb': 'toRGB', 'rgba': 'toRGBA' };
21
 
22
 
23
Y.Color = {
24
    /**
25
    @static
26
    @property KEYWORDS
27
    @type Object
28
    @since 3.8.0
29
    **/
30
    KEYWORDS: {
31
        'black': '000', 'silver': 'c0c0c0', 'gray': '808080', 'white': 'fff',
32
        'maroon': '800000', 'red': 'f00', 'purple': '800080', 'fuchsia': 'f0f',
33
        'green': '008000', 'lime': '0f0', 'olive': '808000', 'yellow': 'ff0',
34
        'navy': '000080', 'blue': '00f', 'teal': '008080', 'aqua': '0ff'
35
    },
36
 
37
    /**
38
        NOTE: `(\ufffe)?` is added to the Regular Expression to carve out a
39
        place for the alpha channel that is returned from toArray
40
        without compromising any usage of the Regular Expression
41
 
42
    @static
43
    @property REGEX_HEX
44
    @type RegExp
45
    @default /^#?([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})(\ufffe)?/
46
    @since 3.8.0
47
    **/
48
    REGEX_HEX: REGEX_HEX,
49
 
50
    /**
51
        NOTE: `(\ufffe)?` is added to the Regular Expression to carve out a
52
        place for the alpha channel that is returned from toArray
53
        without compromising any usage of the Regular Expression
54
 
55
    @static
56
    @property REGEX_HEX3
57
    @type RegExp
58
    @default /^#?([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})(\ufffe)?/
59
    @since 3.8.0
60
    **/
61
    REGEX_HEX3: REGEX_HEX3,
62
 
63
    /**
64
    @static
65
    @property REGEX_RGB
66
    @type RegExp
67
    @default /rgba?\(([0-9]{1,3}), ?([0-9]{1,3}), ?([0-9]{1,3}),? ?([.0-9]{1,3})?\)/
68
    @since 3.8.0
69
    **/
70
    REGEX_RGB: REGEX_RGB,
71
 
72
    re_RGB: REGEX_RGB,
73
 
74
    re_hex: REGEX_HEX,
75
 
76
    re_hex3: REGEX_HEX3,
77
 
78
    /**
79
    @static
80
    @property STR_HEX
81
    @type String
82
    @default #{*}{*}{*}
83
    @since 3.8.0
84
    **/
85
    STR_HEX: '#{*}{*}{*}',
86
 
87
    /**
88
    @static
89
    @property STR_RGB
90
    @type String
91
    @default rgb({*}, {*}, {*})
92
    @since 3.8.0
93
    **/
94
    STR_RGB: 'rgb({*}, {*}, {*})',
95
 
96
    /**
97
    @static
98
    @property STR_RGBA
99
    @type String
100
    @default rgba({*}, {*}, {*}, {*})
101
    @since 3.8.0
102
    **/
103
    STR_RGBA: 'rgba({*}, {*}, {*}, {*})',
104
 
105
    /**
106
    @static
107
    @property TYPES
108
    @type Object
109
    @default {'rgb':'rgb', 'rgba':'rgba'}
110
    @since 3.8.0
111
    **/
112
    TYPES: TYPES,
113
 
114
    /**
115
    @static
116
    @property CONVERTS
117
    @type Object
118
    @default {}
119
    @since 3.8.0
120
    **/
121
    CONVERTS: CONVERTS,
122
 
123
    /**
124
     Converts the provided string to the provided type.
125
     You can use the `Y.Color.TYPES` to get a valid `to` type.
126
     If the color cannot be converted, the original color will be returned.
127
 
128
     @public
129
     @method convert
130
     @param {String} str
131
     @param {String} to
132
     @return {String}
133
     @since 3.8.0
134
     **/
135
    convert: function (str, to) {
136
        var convert = Y.Color.CONVERTS[to.toLowerCase()],
137
            clr = str;
138
 
139
        if (convert && Y.Color[convert]) {
140
            clr = Y.Color[convert](str);
141
        }
142
 
143
        return clr;
144
    },
145
 
146
    /**
147
    Converts provided color value to a hex value string
148
 
149
    @public
150
    @method toHex
151
    @param {String} str Hex or RGB value string
152
    @return {String} returns array of values or CSS string if options.css is true
153
    @since 3.8.0
154
    **/
155
    toHex: function (str) {
156
        var clr = Y.Color._convertTo(str, 'hex'),
157
            isTransparent = clr.toLowerCase() === 'transparent';
158
 
159
        if (clr.charAt(0) !== '#' && !isTransparent) {
160
            clr = '#' + clr;
161
        }
162
 
163
        return isTransparent ? clr.toLowerCase() : clr.toUpperCase();
164
    },
165
 
166
    /**
167
    Converts provided color value to an RGB value string
168
    @public
169
    @method toRGB
170
    @param {String} str Hex or RGB value string
171
    @return {String}
172
    @since 3.8.0
173
    **/
174
    toRGB: function (str) {
175
        var clr = Y.Color._convertTo(str, 'rgb');
176
        return clr.toLowerCase();
177
    },
178
 
179
    /**
180
    Converts provided color value to an RGB value string
181
    @public
182
    @method toRGBA
183
    @param {String} str Hex or RGB value string
184
    @return {String}
185
    @since 3.8.0
186
    **/
187
    toRGBA: function (str) {
188
        var clr = Y.Color._convertTo(str, 'rgba' );
189
        return clr.toLowerCase();
190
    },
191
 
192
    /**
193
    Converts the provided color string to an array of values where the
194
        last value is the alpha value. Will return an empty array if
195
        the provided string is not able to be parsed.
196
 
197
        NOTE: `(\ufffe)?` is added to `HEX` and `HEX3` Regular Expressions to
198
        carve out a place for the alpha channel that is returned from
199
        toArray without compromising any usage of the Regular Expression
200
 
201
        Y.Color.toArray('fff');              // ['ff', 'ff', 'ff', 1]
202
        Y.Color.toArray('rgb(0, 0, 0)');     // ['0', '0', '0', 1]
203
        Y.Color.toArray('rgba(0, 0, 0, 0)'); // ['0', '0', '0', 1]
204
 
205
 
206
 
207
    @public
208
    @method toArray
209
    @param {String} str
210
    @return {Array}
211
    @since 3.8.0
212
    **/
213
    toArray: function(str) {
214
        // parse with regex and return "matches" array
215
        var type = Y.Color.findType(str).toUpperCase(),
216
            regex,
217
            arr,
218
            length,
219
            lastItem;
220
 
221
        if (type === 'HEX' && str.length < 5) {
222
            type = 'HEX3';
223
        }
224
 
225
        if (type.charAt(type.length - 1) === 'A') {
226
            type = type.slice(0, -1);
227
        }
228
 
229
        regex = Y.Color['REGEX_' + type];
230
 
231
        if (regex) {
232
            arr = regex.exec(str) || [];
233
            length = arr.length;
234
 
235
            if (length) {
236
 
237
                arr.shift();
238
                length--;
239
 
240
                if (type === 'HEX3') {
241
                    arr[0] += arr[0];
242
                    arr[1] += arr[1];
243
                    arr[2] += arr[2];
244
                }
245
 
246
                lastItem = arr[length - 1];
247
                if (!lastItem) {
248
                    arr[length - 1] = 1;
249
                }
250
            }
251
        }
252
 
253
        return arr;
254
 
255
    },
256
 
257
    /**
258
    Converts the array of values to a string based on the provided template.
259
    @public
260
    @method fromArray
261
    @param {Array} arr
262
    @param {String} template
263
    @return {String}
264
    @since 3.8.0
265
    **/
266
    fromArray: function(arr, template) {
267
        arr = arr.concat();
268
 
269
        if (typeof template === 'undefined') {
270
            return arr.join(', ');
271
        }
272
 
273
        var replace = '{*}';
274
 
275
        template = Y.Color['STR_' + template.toUpperCase()];
276
 
277
        if (arr.length === 3 && template.match(/\{\*\}/g).length === 4) {
278
            arr.push(1);
279
        }
280
 
281
        while ( template.indexOf(replace) >= 0 && arr.length > 0) {
282
            template = template.replace(replace, arr.shift());
283
        }
284
 
285
        return template;
286
    },
287
 
288
    /**
289
    Finds the value type based on the str value provided.
290
    @public
291
    @method findType
292
    @param {String} str
293
    @return {String}
294
    @since 3.8.0
295
    **/
296
    findType: function (str) {
297
        if (Y.Color.KEYWORDS[str]) {
298
            return 'keyword';
299
        }
300
 
301
        var index = str.indexOf('('),
302
            key;
303
 
304
        if (index > 0) {
305
            key = str.substr(0, index);
306
        }
307
 
308
        if (key && Y.Color.TYPES[key.toUpperCase()]) {
309
            return Y.Color.TYPES[key.toUpperCase()];
310
        }
311
 
312
        return 'hex';
313
 
314
    }, // return 'keyword', 'hex', 'rgb'
315
 
316
    /**
317
    Retrives the alpha channel from the provided string. If no alpha
318
        channel is present, `1` will be returned.
319
    @protected
320
    @method _getAlpha
321
    @param {String} clr
322
    @return {Number}
323
    @since 3.8.0
324
    **/
325
    _getAlpha: function (clr) {
326
        var alpha,
327
            arr = Y.Color.toArray(clr);
328
 
329
        if (arr.length > 3) {
330
            alpha = arr.pop();
331
        }
332
 
333
        return +alpha || 1;
334
    },
335
 
336
    /**
337
    Returns the hex value string if found in the KEYWORDS object
338
    @protected
339
    @method _keywordToHex
340
    @param {String} clr
341
    @return {String}
342
    @since 3.8.0
343
    **/
344
    _keywordToHex: function (clr) {
345
        var keyword = Y.Color.KEYWORDS[clr];
346
 
347
        if (keyword) {
348
            return keyword;
349
        }
350
    },
351
 
352
    /**
353
    Converts the provided color string to the value type provided as `to`
354
    @protected
355
    @method _convertTo
356
    @param {String} clr
357
    @param {String} to
358
    @return {String}
359
    @since 3.8.0
360
    **/
361
    _convertTo: function(clr, to) {
362
 
363
        if (clr === 'transparent') {
364
            return clr;
365
        }
366
 
367
        var from = Y.Color.findType(clr),
368
            originalTo = to,
369
            needsAlpha,
370
            alpha,
371
            method,
372
            ucTo;
373
 
374
        if (from === 'keyword') {
375
            clr = Y.Color._keywordToHex(clr);
376
            from = 'hex';
377
        }
378
 
379
        if (from === 'hex' && clr.length < 5) {
380
            if (clr.charAt(0) === '#') {
381
                clr = clr.substr(1);
382
            }
383
 
384
            clr = '#' + clr.charAt(0) + clr.charAt(0) +
385
                        clr.charAt(1) + clr.charAt(1) +
386
                        clr.charAt(2) + clr.charAt(2);
387
        }
388
 
389
        if (from === to) {
390
            return clr;
391
        }
392
 
393
        if (from.charAt(from.length - 1) === 'a') {
394
            from = from.slice(0, -1);
395
        }
396
 
397
        needsAlpha = (to.charAt(to.length - 1) === 'a');
398
        if (needsAlpha) {
399
            to = to.slice(0, -1);
400
            alpha = Y.Color._getAlpha(clr);
401
        }
402
 
403
        ucTo = to.charAt(0).toUpperCase() + to.substr(1).toLowerCase();
404
        method = Y.Color['_' + from + 'To' + ucTo ];
405
 
406
        // check to see if need conversion to rgb first
407
        // check to see if there is a direct conversion method
408
        // convertions are: hex <-> rgb <-> hsl
409
        if (!method) {
410
            if (from !== 'rgb' && to !== 'rgb') {
411
                clr = Y.Color['_' + from + 'ToRgb'](clr);
412
                from = 'rgb';
413
                method = Y.Color['_' + from + 'To' + ucTo ];
414
            }
415
        }
416
 
417
        if (method) {
418
            clr = ((method)(clr, needsAlpha));
419
        }
420
 
421
        // process clr from arrays to strings after conversions if alpha is needed
422
        if (needsAlpha) {
423
            if (!Y.Lang.isArray(clr)) {
424
                clr = Y.Color.toArray(clr);
425
            }
426
            clr.push(alpha);
427
            clr = Y.Color.fromArray(clr, originalTo.toUpperCase());
428
        }
429
 
430
        return clr;
431
    },
432
 
433
    /**
434
    Processes the hex string into r, g, b values. Will return values as
435
        an array, or as an rgb string.
436
    @protected
437
    @method _hexToRgb
438
    @param {String} str
439
    @param {Boolean} [toArray]
440
    @return {String|Array}
441
    @since 3.8.0
442
    **/
443
    _hexToRgb: function (str, toArray) {
444
        var r, g, b;
445
 
446
        /*jshint bitwise:false*/
447
        if (str.charAt(0) === '#') {
448
            str = str.substr(1);
449
        }
450
 
451
        str = parseInt(str, 16);
452
 
453
        r = str >> 16;
454
        g = str >> 8 & 0xFF;
455
        b = str & 0xFF;
456
 
457
        if (toArray) {
458
            return [r, g, b];
459
        }
460
 
461
        return 'rgb(' + r + ', ' + g + ', ' + b + ')';
462
    },
463
 
464
    /**
465
    Processes the rgb string into r, g, b values. Will return values as
466
        an array, or as a hex string.
467
    @protected
468
    @method _rgbToHex
469
    @param {String} str
470
    @param {Boolean} [toArray]
471
    @return {String|Array}
472
    @since 3.8.0
473
    **/
474
    _rgbToHex: function (str) {
475
        /*jshint bitwise:false*/
476
        var rgb = Y.Color.toArray(str),
477
            hex = rgb[2] | (rgb[1] << 8) | (rgb[0] << 16);
478
 
479
        hex = (+hex).toString(16);
480
 
481
        while (hex.length < 6) {
482
            hex = '0' + hex;
483
        }
484
 
485
        return '#' + hex;
486
    }
487
 
488
};
489
 
490
 
491
 
492
}, '3.18.1', {"requires": ["yui-base"]});