1 /*jslint bitwise:false*/
  2 /*global one*/
  3 one.include('js:one/color.js');
  4 one.include('js:one/color/RGB.js');
  5 
  6 /**
  7  * Parse a hex string. Please use {@link one.color#parse} instead.
  8  * @param strHex The hex string, e.g. <tt>"#abc"</tt>,
  9  * <tt>"123abc"<tt>....
 10  * @return {one.color.RGB} Color object representing the parsed
 11  * color, or false if the string couldn't be parsed.
 12  * @private
 13  */
 14 one.color.fromHex = function (strHex) {
 15     if (strHex.length < 6) {
 16         // Allow CSS shorthand
 17         strHex = strHex.replace(/^#?([0-9a-f])([0-9a-f])([0-9a-f])$/i, '$1$1$2$2$3$3');
 18     }
 19     // Split strHex into red, green, and blue components
 20     var hexMatch = strHex.match(/^#?([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])$/i);
 21     if (hexMatch) {
 22         return new one.color.RGB(
 23             parseInt(hexMatch[1], 16) / 255,
 24             parseInt(hexMatch[2], 16) / 255,
 25             parseInt(hexMatch[3], 16) / 255
 26         );
 27     }
 28 };
 29 
 30 /**
 31  * Regex for matching CSS RGBA color strings
 32  * @name one.color.rgbaRegex
 33  * @private
 34  */
 35 one.color.rgbaRegex =/^rgba?\(\s*(\.\d+|\d+(?:\.\d+)?)(%)?\s*,\s*(\.\d+|\d+(?:\.\d+)?)(%)?\s*,\s*(\.\d+|\d+(?:\.\d+)?)(%)?\s*(?:,\s*(\.\d+|\d+(?:\.\d+))\s*)?\)/i;
 36 
 37 /**
 38  * Parse a CSS RGBA string. Please use {@link one.color#parse} instead.
 39  * @param strCSS The CSS RGBA string, e.g. <tt>"rgb(100, 255, 100)"</tt>,
 40  * <tt>"rgba(0, 0, 255, 0.5)"<tt>, <tt>"rgb(0%, 100%, 0%)"</tt>....
 41  * @return {one.color.RGB} Color object representing the parsed
 42  * color, or false if the string couldn't be parsed.
 43  * @private
 44  */
 45 one.color.fromCSSRGBA = function (strCSS) {
 46     var match = strCSS.match(one.color.rgbaRegex);
 47 
 48     if (match) {
 49         return new one.color.RGB(
 50             parseFloat(match[1]) / (match[2] ? 100 : 255),
 51             parseFloat(match[3]) / (match[4] ? 100 : 255),
 52             parseFloat(match[5]) / (match[6] ? 100 : 255),
 53             parseFloat(match[7])
 54         );
 55     }
 56 };
 57 
 58 /**
 59  * Parse a hex string, 24-bit integer or object representing a color.
 60  * If a one.color.(RGB|HSL|HSV|CMYK) object is provided, it will be returned
 61  * as-is, so in library code you can use {@link one.color.parse} to be flexible
 62  * about how colors are provided to you:
 63  * <pre><code>
 64 function foo (color) {
 65 color = color.parse(color);
 66 // Now we are sure that color is a one.color.(RGB|CMYK|HSL|HSV) object, even if it was provided as a hex string.
 67 }
 68  * </code></pre>
 69  * @param {String|Object} obj A hex string, integer value, or
 70  * object to parse.
 71  * @return {one.color.RGB|one.color.HSL|one.color.HSV|one.color.CMYK} Color object representing the
 72  * parsed color, or false if the input couldn't be parsed.
 73  */
 74 one.color.parse = function (obj) {
 75     if (obj.charCodeAt) {
 76         return one.color.fromCSSRGBA(obj) || one.color.fromHex(obj);
 77     } else if (typeof obj === 'object' && obj.isColor) {
 78         return obj;
 79     } else if (Object.prototype.toString.apply(obj) === '[object Array]') {
 80         return new one.color[obj[0]](obj.slice(1, obj.length));
 81     } else if (!isNaN(obj)) {
 82         // Strange integer representation sometimes returned by document.queryCommandValue in some browser...
 83         return new one.color.RGB((obj & 0xFF) / 255, ((obj & 0xFF00) >> 8) / 255, ((obj & 0xFF0000) >> 16) / 255);
 84     }
 85     return false;
 86 };
 87