2
YUI 3.10.3 (build 2fb5187)
3
Copyright 2013 Yahoo! Inc. All rights reserved.
4
Licensed under the BSD License.
5
http://yuilibrary.com/license/
8
YUI.add('color-base', function (Y, NAME) {
11
Color provides static methods for color conversion.
13
Y.Color.toRGB('f00'); // rgb(255, 0, 0)
15
Y.Color.toHex('rgb(255, 255, 0)'); // #ffff00
23
var REGEX_HEX = /^#?([\da-fA-F]{2})([\da-fA-F]{2})([\da-fA-F]{2})(\ufffe)?/,
24
REGEX_HEX3 = /^#?([\da-fA-F]{1})([\da-fA-F]{1})([\da-fA-F]{1})(\ufffe)?/,
25
REGEX_RGB = /rgba?\(([\d]{1,3}), ?([\d]{1,3}), ?([\d]{1,3}),? ?([.\d]*)?\)/,
26
TYPES = { 'HEX': 'hex', 'RGB': 'rgb', 'RGBA': 'rgba' },
27
CONVERTS = { 'hex': 'toHex', 'rgb': 'toRGB', 'rgba': 'toRGBA' };
38
'black': '000', 'silver': 'c0c0c0', 'gray': '808080', 'white': 'fff',
39
'maroon': '800000', 'red': 'f00', 'purple': '800080', 'fuchsia': 'f0f',
40
'green': '008000', 'lime': '0f0', 'olive': '808000', 'yellow': 'ff0',
41
'navy': '000080', 'blue': '00f', 'teal': '008080', 'aqua': '0ff'
45
NOTE: `(\ufffe)?` is added to the Regular Expression to carve out a
46
place for the alpha channel that is returned from toArray
47
without compromising any usage of the Regular Expression
52
@default /^#?([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})(\ufffe)?/
58
NOTE: `(\ufffe)?` is added to the Regular Expression to carve out a
59
place for the alpha channel that is returned from toArray
60
without compromising any usage of the Regular Expression
65
@default /^#?([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})(\ufffe)?/
68
REGEX_HEX3: REGEX_HEX3,
74
@default /rgba?\(([0-9]{1,3}), ?([0-9]{1,3}), ?([0-9]{1,3}),? ?([.0-9]{1,3})?\)/
92
STR_HEX: '#{*}{*}{*}',
98
@default rgb({*}, {*}, {*})
101
STR_RGB: 'rgb({*}, {*}, {*})',
107
@default rgba({*}, {*}, {*}, {*})
110
STR_RGBA: 'rgba({*}, {*}, {*}, {*})',
116
@default {'rgb':'rgb', 'rgba':'rgba'}
131
Converts the provided string to the provided type.
132
You can use the `Y.Color.TYPES` to get a valid `to` type.
133
If the color cannot be converted, the original color will be returned.
142
convert: function (str, to) {
143
var convert = Y.Color.CONVERTS[to.toLowerCase()],
146
if (convert && Y.Color[convert]) {
147
clr = Y.Color[convert](str);
154
Converts provided color value to a hex value string
158
@param {String} str Hex or RGB value string
159
@return {String} returns array of values or CSS string if options.css is true
162
toHex: function (str) {
163
var clr = Y.Color._convertTo(str, 'hex'),
164
isTransparent = clr.toLowerCase() === 'transparent';
166
if (clr.charAt(0) !== '#' && !isTransparent) {
170
return isTransparent ? clr.toLowerCase() : clr.toUpperCase();
174
Converts provided color value to an RGB value string
177
@param {String} str Hex or RGB value string
181
toRGB: function (str) {
182
var clr = Y.Color._convertTo(str, 'rgb');
183
return clr.toLowerCase();
187
Converts provided color value to an RGB value string
190
@param {String} str Hex or RGB value string
194
toRGBA: function (str) {
195
var clr = Y.Color._convertTo(str, 'rgba' );
196
return clr.toLowerCase();
200
Converts the provided color string to an array of values where the
201
last value is the alpha value. Will return an empty array if
202
the provided string is not able to be parsed.
204
NOTE: `(\ufffe)?` is added to `HEX` and `HEX3` Regular Expressions to
205
carve out a place for the alpha channel that is returned from
206
toArray without compromising any usage of the Regular Expression
208
Y.Color.toArray('fff'); // ['ff', 'ff', 'ff', 1]
209
Y.Color.toArray('rgb(0, 0, 0)'); // ['0', '0', '0', 1]
210
Y.Color.toArray('rgba(0, 0, 0, 0)'); // ['0', '0', '0', 1]
220
toArray: function(str) {
221
// parse with regex and return "matches" array
222
var type = Y.Color.findType(str).toUpperCase(),
228
if (type === 'HEX' && str.length < 5) {
232
if (type.charAt(type.length - 1) === 'A') {
233
type = type.slice(0, -1);
236
regex = Y.Color['REGEX_' + type];
239
arr = regex.exec(str) || [];
247
if (type === 'HEX3') {
253
lastItem = arr[length - 1];
265
Converts the array of values to a string based on the provided template.
269
@param {String} template
273
fromArray: function(arr, template) {
276
if (typeof template === 'undefined') {
277
return arr.join(', ');
282
template = Y.Color['STR_' + template.toUpperCase()];
284
if (arr.length === 3 && template.match(/\{\*\}/g).length === 4) {
288
while ( template.indexOf(replace) >= 0 && arr.length > 0) {
289
template = template.replace(replace, arr.shift());
296
Finds the value type based on the str value provided.
303
findType: function (str) {
304
if (Y.Color.KEYWORDS[str]) {
308
var index = str.indexOf('('),
312
key = str.substr(0, index);
315
if (key && Y.Color.TYPES[key.toUpperCase()]) {
316
return Y.Color.TYPES[key.toUpperCase()];
321
}, // return 'keyword', 'hex', 'rgb'
324
Retrives the alpha channel from the provided string. If no alpha
325
channel is present, `1` will be returned.
332
_getAlpha: function (clr) {
334
arr = Y.Color.toArray(clr);
336
if (arr.length > 3) {
344
Returns the hex value string if found in the KEYWORDS object
346
@method _keywordToHex
351
_keywordToHex: function (clr) {
352
var keyword = Y.Color.KEYWORDS[clr];
360
Converts the provided color string to the value type provided as `to`
368
_convertTo: function(clr, to) {
370
if (clr === 'transparent') {
374
var from = Y.Color.findType(clr),
381
if (from === 'keyword') {
382
clr = Y.Color._keywordToHex(clr);
386
if (from === 'hex' && clr.length < 5) {
387
if (clr.charAt(0) === '#') {
391
clr = '#' + clr.charAt(0) + clr.charAt(0) +
392
clr.charAt(1) + clr.charAt(1) +
393
clr.charAt(2) + clr.charAt(2);
400
if (from.charAt(from.length - 1) === 'a') {
401
from = from.slice(0, -1);
404
needsAlpha = (to.charAt(to.length - 1) === 'a');
406
to = to.slice(0, -1);
407
alpha = Y.Color._getAlpha(clr);
410
ucTo = to.charAt(0).toUpperCase() + to.substr(1).toLowerCase();
411
method = Y.Color['_' + from + 'To' + ucTo ];
413
// check to see if need conversion to rgb first
414
// check to see if there is a direct conversion method
415
// convertions are: hex <-> rgb <-> hsl
417
if (from !== 'rgb' && to !== 'rgb') {
418
clr = Y.Color['_' + from + 'ToRgb'](clr);
420
method = Y.Color['_' + from + 'To' + ucTo ];
425
clr = ((method)(clr, needsAlpha));
428
// process clr from arrays to strings after conversions if alpha is needed
430
if (!Y.Lang.isArray(clr)) {
431
clr = Y.Color.toArray(clr);
434
clr = Y.Color.fromArray(clr, originalTo.toUpperCase());
441
Processes the hex string into r, g, b values. Will return values as
442
an array, or as an rgb string.
446
@param {Boolean} [toArray]
447
@return {String|Array}
450
_hexToRgb: function (str, toArray) {
453
/*jshint bitwise:false*/
454
if (str.charAt(0) === '#') {
458
str = parseInt(str, 16);
468
return 'rgb(' + r + ', ' + g + ', ' + b + ')';
472
Processes the rgb string into r, g, b values. Will return values as
473
an array, or as a hex string.
477
@param {Boolean} [toArray]
478
@return {String|Array}
481
_rgbToHex: function (str) {
482
/*jshint bitwise:false*/
483
var rgb = Y.Color.toArray(str),
484
hex = rgb[2] | (rgb[1] << 8) | (rgb[0] << 16);
486
hex = (+hex).toString(16);
488
while (hex.length < 6) {
499
}, '3.10.3', {"requires": ["yui-base"]});