Comparing version 2.0.6 to 2.0.7
@@ -24,68 +24,73 @@ /*global one*/ | ||
*/ | ||
(function (nameSpace, fn, parseint, parsefloat, round) { | ||
var installedColorSpaces = [], | ||
(function () { | ||
var FUNCTION = Function, | ||
PARSEINT = parseInt, | ||
PARSEFLOAT = parseFloat, | ||
ROUND = Math.round, | ||
installedColorSpaces = [], | ||
channelRegExp = /\s*(\.\d+|\d+(?:\.\d+)?)(%)?\s*/, | ||
alphaChannelRegExp = /\s*(\.\d+|\d+(?:\.\d+)?)\s*/, | ||
cssColorRegExp = new RegExp("^(rgb|hsl|hsv)a?\\(" + | ||
channelRegExp.source + "," + | ||
channelRegExp.source + "," + | ||
channelRegExp.source + | ||
"(?:," + alphaChannelRegExp.source + ")?" + | ||
"\\)$", "i"); | ||
function color (obj) { | ||
if (Object.prototype.toString.apply(obj) === '[object Array]') { | ||
if (obj[0].length === 4) { | ||
// Assumed 4 element int RGB array from canvas with all channels [0;255] | ||
return new color.RGB(obj[0] / 255, obj[1] / 255, obj[2] / 255, obj[3] / 255); | ||
} else { | ||
// Assumed destringified array from one.color.JSON() | ||
return new color[obj[0]](obj.slice(1, obj.length)); | ||
} | ||
} else if (obj.charCodeAt) { | ||
// Test for CSS rgb(....) string | ||
var matchCssSyntax = obj.match(cssColorRegExp); | ||
if (matchCssSyntax) { | ||
var colorSpaceName = matchCssSyntax[1].toUpperCase(), | ||
alpha = typeof matchCssSyntax[8] === 'undefined' ? matchCssSyntax[8] : parsefloat(matchCssSyntax[8]), | ||
hasHue = colorSpaceName[0] === 'H', | ||
firstChannelDivisor = matchCssSyntax[3] ? 100 : (hasHue ? 360 : 255), | ||
secondChannelDivisor = (matchCssSyntax[5] || hasHue) ? 100 : 255, | ||
thirdChannelDivisor = (matchCssSyntax[7] || hasHue) ? 100 : 255; | ||
if (typeof color[colorSpaceName] === 'undefined') { | ||
throw new Error("one.color." + colorSpaceName + " is not installed."); | ||
cssColorRegExp = new RegExp( | ||
"^(rgb|hsl|hsv)a?" + | ||
"\\(" + | ||
channelRegExp.source + "," + | ||
channelRegExp.source + "," + | ||
channelRegExp.source + | ||
"(?:," + alphaChannelRegExp.source + ")?" + | ||
"\\)$", "i"), | ||
ONECOLOR = one.color = function (obj) { | ||
if (Object.prototype.toString.apply(obj) === '[object Array]') { | ||
if (obj[0].length === 4) { | ||
// Assumed 4 element int RGB array from canvas with all channels [0;255] | ||
return new ONECOLOR.RGB(obj[0] / 255, obj[1] / 255, obj[2] / 255, obj[3] / 255); | ||
} else { | ||
// Assumed destringified array from one.color.JSON() | ||
return new ONECOLOR[obj[0]](obj.slice(1, obj.length)); | ||
} | ||
return new color[colorSpaceName]( | ||
parsefloat(matchCssSyntax[2]) / firstChannelDivisor, | ||
parsefloat(matchCssSyntax[4]) / secondChannelDivisor, | ||
parsefloat(matchCssSyntax[6]) / thirdChannelDivisor, | ||
alpha | ||
); | ||
} else if (obj.charCodeAt) { | ||
// Test for CSS rgb(....) string | ||
var matchCssSyntax = obj.match(cssColorRegExp); | ||
if (matchCssSyntax) { | ||
var colorSpaceName = matchCssSyntax[1].toUpperCase(), | ||
alpha = typeof matchCssSyntax[8] === 'undefined' ? matchCssSyntax[8] : PARSEFLOAT(matchCssSyntax[8]), | ||
hasHue = colorSpaceName[0] === 'H', | ||
firstChannelDivisor = matchCssSyntax[3] ? 100 : (hasHue ? 360 : 255), | ||
secondChannelDivisor = (matchCssSyntax[5] || hasHue) ? 100 : 255, | ||
thirdChannelDivisor = (matchCssSyntax[7] || hasHue) ? 100 : 255; | ||
if (typeof ONECOLOR[colorSpaceName] === 'undefined') { | ||
throw new Error("one.color." + colorSpaceName + " is not installed."); | ||
} | ||
return new ONECOLOR[colorSpaceName]( | ||
PARSEFLOAT(matchCssSyntax[2]) / firstChannelDivisor, | ||
PARSEFLOAT(matchCssSyntax[4]) / secondChannelDivisor, | ||
PARSEFLOAT(matchCssSyntax[6]) / thirdChannelDivisor, | ||
alpha | ||
); | ||
} | ||
// Assume hex syntax | ||
if (obj.length < 6) { | ||
// Allow CSS shorthand | ||
obj = obj.replace(/^#?([0-9a-f])([0-9a-f])([0-9a-f])$/i, '$1$1$2$2$3$3'); | ||
} | ||
// Split obj into red, green, and blue components | ||
var hexMatch = obj.match(/^#?([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])$/i); | ||
if (hexMatch) { | ||
return new ONECOLOR.RGB( | ||
PARSEINT(hexMatch[1], 16) / 255, | ||
PARSEINT(hexMatch[2], 16) / 255, | ||
PARSEINT(hexMatch[3], 16) / 255 | ||
); | ||
} | ||
} else if (typeof obj === 'object' && obj.isColor) { | ||
return obj; | ||
} else if (!isNaN(obj)) { | ||
// Strange integer representation sometimes returned by document.queryCommandValue in some browser... | ||
return new ONECOLOR.RGB((obj & 0xFF) / 255, ((obj & 0xFF00) >> 8) / 255, ((obj & 0xFF0000) >> 16) / 255); | ||
} | ||
// Assume hex syntax | ||
if (obj.length < 6) { | ||
// Allow CSS shorthand | ||
obj = obj.replace(/^#?([0-9a-f])([0-9a-f])([0-9a-f])$/i, '$1$1$2$2$3$3'); | ||
} | ||
// Split obj into red, green, and blue components | ||
var hexMatch = obj.match(/^#?([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])$/i); | ||
if (hexMatch) { | ||
return new color.RGB( | ||
parseint(hexMatch[1], 16) / 255, | ||
parseint(hexMatch[2], 16) / 255, | ||
parseint(hexMatch[3], 16) / 255 | ||
); | ||
} | ||
} else if (typeof obj === 'object' && obj.isColor) { | ||
return obj; | ||
} else if (!isNaN(obj)) { | ||
// Strange integer representation sometimes returned by document.queryCommandValue in some browser... | ||
return new color.RGB((obj & 0xFF) / 255, ((obj & 0xFF00) >> 8) / 255, ((obj & 0xFF0000) >> 16) / 255); | ||
} | ||
return false; | ||
}; | ||
return false; | ||
}; | ||
/*jslint evil:true*/ | ||
color.installColorSpace = function (colorSpaceName, propertyNames, config) { | ||
color[colorSpaceName] = new fn(propertyNames.join(","), | ||
ONECOLOR.installColorSpace = function (colorSpaceName, propertyNames, config) { | ||
ONECOLOR[colorSpaceName] = new FUNCTION(propertyNames.join(","), | ||
// Allow passing an array to the constructor: | ||
@@ -112,8 +117,8 @@ "if (Object.prototype.toString.apply(" + propertyNames[0] + ") === '[object Array]') {" + | ||
); | ||
color[colorSpaceName].propertyNames = propertyNames; | ||
ONECOLOR[colorSpaceName].propertyNames = propertyNames; | ||
var prototype = color[colorSpaceName].prototype; | ||
var prototype = ONECOLOR[colorSpaceName].prototype; | ||
['valueOf', 'hex', 'css', 'cssa'].forEach(function (methodName) { | ||
prototype[methodName] = prototype[methodName] || (colorSpaceName === 'RGB' ? prototype.hex : new fn("return this.rgb()." + methodName + "();")); | ||
prototype[methodName] = prototype[methodName] || (colorSpaceName === 'RGB' ? prototype.hex : new FUNCTION("return this.rgb()." + methodName + "();")); | ||
}); | ||
@@ -139,3 +144,3 @@ | ||
prototype.toJSON = new fn( | ||
prototype.toJSON = new FUNCTION( | ||
"return ['" + colorSpaceName + "', " + | ||
@@ -152,3 +157,3 @@ propertyNames.map(function (propertyName) { | ||
if (matchFromColorSpace) { | ||
color[matchFromColorSpace[1].toUpperCase()].prototype[colorSpaceName.toLowerCase()] = config[propertyName]; | ||
ONECOLOR[matchFromColorSpace[1].toUpperCase()].prototype[colorSpaceName.toLowerCase()] = config[propertyName]; | ||
} else { | ||
@@ -164,3 +169,3 @@ prototype[propertyName] = config[propertyName]; | ||
}; | ||
prototype.toString = new fn("return \"[one.color." + colorSpaceName + ":\"+" + propertyNames.map(function (propertyName, i) { | ||
prototype.toString = new FUNCTION("return \"[one.color." + colorSpaceName + ":\"+" + propertyNames.map(function (propertyName, i) { | ||
return "\" " + propertyNames[i] + "=\"+this._" + propertyName; | ||
@@ -171,3 +176,3 @@ }).join("+") + "+\"]\";"); | ||
propertyNames.forEach(function (propertyName, i) { | ||
prototype[propertyName] = new fn("value", "isDelta", | ||
prototype[propertyName] = new FUNCTION("value", "isDelta", | ||
// Simple getter mode: color.red() | ||
@@ -191,9 +196,9 @@ "if (typeof value === 'undefined') {" + | ||
var obj = {}; | ||
obj[sourceColorSpaceName.toLowerCase()] = new fn("return this.rgb()." + sourceColorSpaceName.toLowerCase() + "();"); // Fallback | ||
color[sourceColorSpaceName].propertyNames.forEach(function (propertyName, i) { | ||
obj[propertyName] = new fn("value", "isDelta", "return this." + sourceColorSpaceName.toLowerCase() + "()." + propertyName + "(value, isDelta);"); | ||
obj[sourceColorSpaceName.toLowerCase()] = new FUNCTION("return this.rgb()." + sourceColorSpaceName.toLowerCase() + "();"); // Fallback | ||
ONECOLOR[sourceColorSpaceName].propertyNames.forEach(function (propertyName, i) { | ||
obj[propertyName] = new FUNCTION("value", "isDelta", "return this." + sourceColorSpaceName.toLowerCase() + "()." + propertyName + "(value, isDelta);"); | ||
}); | ||
for (var prop in obj) { | ||
if (obj.hasOwnProperty(prop) && color[targetColorSpaceName].prototype[prop] === undefined) { | ||
color[targetColorSpaceName].prototype[prop] = obj[prop]; | ||
if (obj.hasOwnProperty(prop) && ONECOLOR[targetColorSpaceName].prototype[prop] === undefined) { | ||
ONECOLOR[targetColorSpaceName].prototype[prop] = obj[prop]; | ||
} | ||
@@ -241,3 +246,3 @@ } | ||
*/ | ||
color.installColorSpace('RGB', ['red', 'green', 'blue', 'alpha'], { | ||
ONECOLOR.installColorSpace('RGB', ['red', 'green', 'blue', 'alpha'], { | ||
/** | ||
@@ -248,3 +253,3 @@ * Get the standard RGB hex representation of the color. | ||
hex: function () { | ||
var hexString = (round(255 * this._red) * 0x10000 + round(255 * this._green) * 0x100 + round(255 * this._blue)).toString(16); | ||
var hexString = (ROUND(255 * this._red) * 0x10000 + ROUND(255 * this._green) * 0x100 + ROUND(255 * this._blue)).toString(16); | ||
return '#' + ('00000'.substr(0, 6 - hexString.length)) + hexString; | ||
@@ -259,3 +264,3 @@ }, | ||
css: function () { | ||
return "rgb(" + round(255 * this._red) + "," + round(255 * this._green) + "," + round(255 * this._blue) + ")"; | ||
return "rgb(" + ROUND(255 * this._red) + "," + ROUND(255 * this._green) + "," + ROUND(255 * this._blue) + ")"; | ||
}, | ||
@@ -269,9 +274,7 @@ | ||
cssa: function () { | ||
return "rgba(" + round(255 * this._red) + "," + round(255 * this._green) + "," + round(255 * this._blue) + "," + this._alpha + ")"; | ||
return "rgba(" + ROUND(255 * this._red) + "," + ROUND(255 * this._green) + "," + ROUND(255 * this._blue) + "," + this._alpha + ")"; | ||
} | ||
}); | ||
}()); | ||
nameSpace.color = color; | ||
}(one, Function, parseInt, parseFloat, Math.round)); | ||
/** | ||
@@ -278,0 +281,0 @@ * @name one.color.RGB.prototype.red |
@@ -140,4 +140,6 @@ /*global one*/ | ||
*/ | ||
(function (nameSpace, math) { | ||
nameSpace.installColorSpace('HSV', ['hue', 'saturation', 'value', 'alpha'], { | ||
(function () { | ||
var MATH = Math, | ||
ONECOLOR = one.color; | ||
ONECOLOR.installColorSpace('HSV', ['hue', 'saturation', 'value', 'alpha'], { | ||
rgb: function () { | ||
@@ -147,3 +149,3 @@ var hue = this._hue, | ||
value = this._value, | ||
i = math.min(5, math.floor(hue * 6)), | ||
i = MATH.min(5, MATH.floor(hue * 6)), | ||
f = hue * 6 - i, | ||
@@ -188,3 +190,3 @@ p = value * (1 - saturation), | ||
} | ||
return new nameSpace.RGB(red, green, blue, this._alpha); | ||
return new ONECOLOR.RGB(red, green, blue, this._alpha); | ||
}, | ||
@@ -204,3 +206,3 @@ | ||
} | ||
return new nameSpace.HSL(this._hue, saturation, l / 2, this._alpha); | ||
return new ONECOLOR.HSL(this._hue, saturation, l / 2, this._alpha); | ||
}, | ||
@@ -212,4 +214,4 @@ | ||
blue = this._blue, | ||
max = math.max(red, green, blue), | ||
min = math.min(red, green, blue), | ||
max = MATH.max(red, green, blue), | ||
min = MATH.min(red, green, blue), | ||
delta = max - min, | ||
@@ -234,5 +236,5 @@ hue, | ||
} | ||
return new nameSpace.HSV(hue, saturation, value, this._alpha); | ||
return new ONECOLOR.HSV(hue, saturation, value, this._alpha); | ||
} | ||
}); | ||
}(one.color, Math)); | ||
}()); |
@@ -36,68 +36,73 @@ /*global one*/ | ||
*/ | ||
(function (nameSpace, fn, parseint, parsefloat, round) { | ||
var installedColorSpaces = [], | ||
(function () { | ||
var FUNCTION = Function, | ||
PARSEINT = parseInt, | ||
PARSEFLOAT = parseFloat, | ||
ROUND = Math.round, | ||
installedColorSpaces = [], | ||
channelRegExp = /\s*(\.\d+|\d+(?:\.\d+)?)(%)?\s*/, | ||
alphaChannelRegExp = /\s*(\.\d+|\d+(?:\.\d+)?)\s*/, | ||
cssColorRegExp = new RegExp("^(rgb|hsl|hsv)a?\\(" + | ||
channelRegExp.source + "," + | ||
channelRegExp.source + "," + | ||
channelRegExp.source + | ||
"(?:," + alphaChannelRegExp.source + ")?" + | ||
"\\)$", "i"); | ||
function color (obj) { | ||
if (Object.prototype.toString.apply(obj) === '[object Array]') { | ||
if (obj[0].length === 4) { | ||
// Assumed 4 element int RGB array from canvas with all channels [0;255] | ||
return new color.RGB(obj[0] / 255, obj[1] / 255, obj[2] / 255, obj[3] / 255); | ||
} else { | ||
// Assumed destringified array from one.color.JSON() | ||
return new color[obj[0]](obj.slice(1, obj.length)); | ||
} | ||
} else if (obj.charCodeAt) { | ||
// Test for CSS rgb(....) string | ||
var matchCssSyntax = obj.match(cssColorRegExp); | ||
if (matchCssSyntax) { | ||
var colorSpaceName = matchCssSyntax[1].toUpperCase(), | ||
alpha = typeof matchCssSyntax[8] === 'undefined' ? matchCssSyntax[8] : parsefloat(matchCssSyntax[8]), | ||
hasHue = colorSpaceName[0] === 'H', | ||
firstChannelDivisor = matchCssSyntax[3] ? 100 : (hasHue ? 360 : 255), | ||
secondChannelDivisor = (matchCssSyntax[5] || hasHue) ? 100 : 255, | ||
thirdChannelDivisor = (matchCssSyntax[7] || hasHue) ? 100 : 255; | ||
if (typeof color[colorSpaceName] === 'undefined') { | ||
throw new Error("one.color." + colorSpaceName + " is not installed."); | ||
cssColorRegExp = new RegExp( | ||
"^(rgb|hsl|hsv)a?" + | ||
"\\(" + | ||
channelRegExp.source + "," + | ||
channelRegExp.source + "," + | ||
channelRegExp.source + | ||
"(?:," + alphaChannelRegExp.source + ")?" + | ||
"\\)$", "i"), | ||
ONECOLOR = one.color = function (obj) { | ||
if (Object.prototype.toString.apply(obj) === '[object Array]') { | ||
if (obj[0].length === 4) { | ||
// Assumed 4 element int RGB array from canvas with all channels [0;255] | ||
return new ONECOLOR.RGB(obj[0] / 255, obj[1] / 255, obj[2] / 255, obj[3] / 255); | ||
} else { | ||
// Assumed destringified array from one.color.JSON() | ||
return new ONECOLOR[obj[0]](obj.slice(1, obj.length)); | ||
} | ||
return new color[colorSpaceName]( | ||
parsefloat(matchCssSyntax[2]) / firstChannelDivisor, | ||
parsefloat(matchCssSyntax[4]) / secondChannelDivisor, | ||
parsefloat(matchCssSyntax[6]) / thirdChannelDivisor, | ||
alpha | ||
); | ||
} else if (obj.charCodeAt) { | ||
// Test for CSS rgb(....) string | ||
var matchCssSyntax = obj.match(cssColorRegExp); | ||
if (matchCssSyntax) { | ||
var colorSpaceName = matchCssSyntax[1].toUpperCase(), | ||
alpha = typeof matchCssSyntax[8] === 'undefined' ? matchCssSyntax[8] : PARSEFLOAT(matchCssSyntax[8]), | ||
hasHue = colorSpaceName[0] === 'H', | ||
firstChannelDivisor = matchCssSyntax[3] ? 100 : (hasHue ? 360 : 255), | ||
secondChannelDivisor = (matchCssSyntax[5] || hasHue) ? 100 : 255, | ||
thirdChannelDivisor = (matchCssSyntax[7] || hasHue) ? 100 : 255; | ||
if (typeof ONECOLOR[colorSpaceName] === 'undefined') { | ||
throw new Error("one.color." + colorSpaceName + " is not installed."); | ||
} | ||
return new ONECOLOR[colorSpaceName]( | ||
PARSEFLOAT(matchCssSyntax[2]) / firstChannelDivisor, | ||
PARSEFLOAT(matchCssSyntax[4]) / secondChannelDivisor, | ||
PARSEFLOAT(matchCssSyntax[6]) / thirdChannelDivisor, | ||
alpha | ||
); | ||
} | ||
// Assume hex syntax | ||
if (obj.length < 6) { | ||
// Allow CSS shorthand | ||
obj = obj.replace(/^#?([0-9a-f])([0-9a-f])([0-9a-f])$/i, '$1$1$2$2$3$3'); | ||
} | ||
// Split obj into red, green, and blue components | ||
var hexMatch = obj.match(/^#?([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])$/i); | ||
if (hexMatch) { | ||
return new ONECOLOR.RGB( | ||
PARSEINT(hexMatch[1], 16) / 255, | ||
PARSEINT(hexMatch[2], 16) / 255, | ||
PARSEINT(hexMatch[3], 16) / 255 | ||
); | ||
} | ||
} else if (typeof obj === 'object' && obj.isColor) { | ||
return obj; | ||
} else if (!isNaN(obj)) { | ||
// Strange integer representation sometimes returned by document.queryCommandValue in some browser... | ||
return new ONECOLOR.RGB((obj & 0xFF) / 255, ((obj & 0xFF00) >> 8) / 255, ((obj & 0xFF0000) >> 16) / 255); | ||
} | ||
// Assume hex syntax | ||
if (obj.length < 6) { | ||
// Allow CSS shorthand | ||
obj = obj.replace(/^#?([0-9a-f])([0-9a-f])([0-9a-f])$/i, '$1$1$2$2$3$3'); | ||
} | ||
// Split obj into red, green, and blue components | ||
var hexMatch = obj.match(/^#?([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])$/i); | ||
if (hexMatch) { | ||
return new color.RGB( | ||
parseint(hexMatch[1], 16) / 255, | ||
parseint(hexMatch[2], 16) / 255, | ||
parseint(hexMatch[3], 16) / 255 | ||
); | ||
} | ||
} else if (typeof obj === 'object' && obj.isColor) { | ||
return obj; | ||
} else if (!isNaN(obj)) { | ||
// Strange integer representation sometimes returned by document.queryCommandValue in some browser... | ||
return new color.RGB((obj & 0xFF) / 255, ((obj & 0xFF00) >> 8) / 255, ((obj & 0xFF0000) >> 16) / 255); | ||
} | ||
return false; | ||
}; | ||
return false; | ||
}; | ||
/*jslint evil:true*/ | ||
color.installColorSpace = function (colorSpaceName, propertyNames, config) { | ||
color[colorSpaceName] = new fn(propertyNames.join(","), | ||
ONECOLOR.installColorSpace = function (colorSpaceName, propertyNames, config) { | ||
ONECOLOR[colorSpaceName] = new FUNCTION(propertyNames.join(","), | ||
// Allow passing an array to the constructor: | ||
@@ -124,8 +129,8 @@ "if (Object.prototype.toString.apply(" + propertyNames[0] + ") === '[object Array]') {" + | ||
); | ||
color[colorSpaceName].propertyNames = propertyNames; | ||
ONECOLOR[colorSpaceName].propertyNames = propertyNames; | ||
var prototype = color[colorSpaceName].prototype; | ||
var prototype = ONECOLOR[colorSpaceName].prototype; | ||
['valueOf', 'hex', 'css', 'cssa'].forEach(function (methodName) { | ||
prototype[methodName] = prototype[methodName] || (colorSpaceName === 'RGB' ? prototype.hex : new fn("return this.rgb()." + methodName + "();")); | ||
prototype[methodName] = prototype[methodName] || (colorSpaceName === 'RGB' ? prototype.hex : new FUNCTION("return this.rgb()." + methodName + "();")); | ||
}); | ||
@@ -151,3 +156,3 @@ | ||
prototype.toJSON = new fn( | ||
prototype.toJSON = new FUNCTION( | ||
"return ['" + colorSpaceName + "', " + | ||
@@ -164,3 +169,3 @@ propertyNames.map(function (propertyName) { | ||
if (matchFromColorSpace) { | ||
color[matchFromColorSpace[1].toUpperCase()].prototype[colorSpaceName.toLowerCase()] = config[propertyName]; | ||
ONECOLOR[matchFromColorSpace[1].toUpperCase()].prototype[colorSpaceName.toLowerCase()] = config[propertyName]; | ||
} else { | ||
@@ -176,3 +181,3 @@ prototype[propertyName] = config[propertyName]; | ||
}; | ||
prototype.toString = new fn("return \"[one.color." + colorSpaceName + ":\"+" + propertyNames.map(function (propertyName, i) { | ||
prototype.toString = new FUNCTION("return \"[one.color." + colorSpaceName + ":\"+" + propertyNames.map(function (propertyName, i) { | ||
return "\" " + propertyNames[i] + "=\"+this._" + propertyName; | ||
@@ -183,3 +188,3 @@ }).join("+") + "+\"]\";"); | ||
propertyNames.forEach(function (propertyName, i) { | ||
prototype[propertyName] = new fn("value", "isDelta", | ||
prototype[propertyName] = new FUNCTION("value", "isDelta", | ||
// Simple getter mode: color.red() | ||
@@ -203,9 +208,9 @@ "if (typeof value === 'undefined') {" + | ||
var obj = {}; | ||
obj[sourceColorSpaceName.toLowerCase()] = new fn("return this.rgb()." + sourceColorSpaceName.toLowerCase() + "();"); // Fallback | ||
color[sourceColorSpaceName].propertyNames.forEach(function (propertyName, i) { | ||
obj[propertyName] = new fn("value", "isDelta", "return this." + sourceColorSpaceName.toLowerCase() + "()." + propertyName + "(value, isDelta);"); | ||
obj[sourceColorSpaceName.toLowerCase()] = new FUNCTION("return this.rgb()." + sourceColorSpaceName.toLowerCase() + "();"); // Fallback | ||
ONECOLOR[sourceColorSpaceName].propertyNames.forEach(function (propertyName, i) { | ||
obj[propertyName] = new FUNCTION("value", "isDelta", "return this." + sourceColorSpaceName.toLowerCase() + "()." + propertyName + "(value, isDelta);"); | ||
}); | ||
for (var prop in obj) { | ||
if (obj.hasOwnProperty(prop) && color[targetColorSpaceName].prototype[prop] === undefined) { | ||
color[targetColorSpaceName].prototype[prop] = obj[prop]; | ||
if (obj.hasOwnProperty(prop) && ONECOLOR[targetColorSpaceName].prototype[prop] === undefined) { | ||
ONECOLOR[targetColorSpaceName].prototype[prop] = obj[prop]; | ||
} | ||
@@ -251,3 +256,3 @@ } | ||
*/ | ||
color.installColorSpace('RGB', ['red', 'green', 'blue', 'alpha'], { | ||
ONECOLOR.installColorSpace('RGB', ['red', 'green', 'blue', 'alpha'], { | ||
/** | ||
@@ -258,3 +263,3 @@ * Get the standard RGB hex representation of the color. | ||
hex: function () { | ||
var hexString = (round(255 * this._red) * 0x10000 + round(255 * this._green) * 0x100 + round(255 * this._blue)).toString(16); | ||
var hexString = (ROUND(255 * this._red) * 0x10000 + ROUND(255 * this._green) * 0x100 + ROUND(255 * this._blue)).toString(16); | ||
return '#' + ('00000'.substr(0, 6 - hexString.length)) + hexString; | ||
@@ -269,3 +274,3 @@ }, | ||
css: function () { | ||
return "rgb(" + round(255 * this._red) + "," + round(255 * this._green) + "," + round(255 * this._blue) + ")"; | ||
return "rgb(" + ROUND(255 * this._red) + "," + ROUND(255 * this._green) + "," + ROUND(255 * this._blue) + ")"; | ||
}, | ||
@@ -279,9 +284,7 @@ | ||
cssa: function () { | ||
return "rgba(" + round(255 * this._red) + "," + round(255 * this._green) + "," + round(255 * this._blue) + "," + this._alpha + ")"; | ||
return "rgba(" + ROUND(255 * this._red) + "," + ROUND(255 * this._green) + "," + ROUND(255 * this._blue) + "," + this._alpha + ")"; | ||
} | ||
}); | ||
}()); | ||
nameSpace.color = color; | ||
}(one, Function, parseInt, parseFloat, Math.round)); | ||
/** | ||
@@ -507,4 +510,6 @@ * @name one.color.RGB.prototype.red | ||
*/ | ||
(function (nameSpace, math) { | ||
nameSpace.installColorSpace('HSV', ['hue', 'saturation', 'value', 'alpha'], { | ||
(function () { | ||
var MATH = Math, | ||
ONECOLOR = one.color; | ||
ONECOLOR.installColorSpace('HSV', ['hue', 'saturation', 'value', 'alpha'], { | ||
rgb: function () { | ||
@@ -514,3 +519,3 @@ var hue = this._hue, | ||
value = this._value, | ||
i = math.min(5, math.floor(hue * 6)), | ||
i = MATH.min(5, MATH.floor(hue * 6)), | ||
f = hue * 6 - i, | ||
@@ -555,3 +560,3 @@ p = value * (1 - saturation), | ||
} | ||
return new nameSpace.RGB(red, green, blue, this._alpha); | ||
return new ONECOLOR.RGB(red, green, blue, this._alpha); | ||
}, | ||
@@ -571,3 +576,3 @@ | ||
} | ||
return new nameSpace.HSL(this._hue, saturation, l / 2, this._alpha); | ||
return new ONECOLOR.HSL(this._hue, saturation, l / 2, this._alpha); | ||
}, | ||
@@ -579,4 +584,4 @@ | ||
blue = this._blue, | ||
max = math.max(red, green, blue), | ||
min = math.min(red, green, blue), | ||
max = MATH.max(red, green, blue), | ||
min = MATH.min(red, green, blue), | ||
delta = max - min, | ||
@@ -601,6 +606,6 @@ hue, | ||
} | ||
return new nameSpace.HSV(hue, saturation, value, this._alpha); | ||
return new ONECOLOR.HSV(hue, saturation, value, this._alpha); | ||
} | ||
}); | ||
}(one.color, Math)); | ||
}()); | ||
@@ -607,0 +612,0 @@ /*global one*/ |
@@ -1,1 +0,1 @@ | ||
typeof one=="undefined"&&(one={include:function(){}}),function(a,b,c,d,e){function j(a){if(Object.prototype.toString.apply(a)==="[object Array]")return a[0].length===4?new j.RGB(a[0]/255,a[1]/255,a[2]/255,a[3]/255):new j[a[0]](a.slice(1,a.length));if(a.charCodeAt){var b=a.match(i);if(b){var e=b[1].toUpperCase(),f=typeof b[8]=="undefined"?b[8]:d(b[8]),g=e[0]==="H",h=b[3]?100:g?360:255,k=b[5]||g?100:255,l=b[7]||g?100:255;if(typeof j[e]=="undefined")throw new Error("one.color."+e+" is not installed.");return new j[e](d(b[2])/h,d(b[4])/k,d(b[6])/l,f)}a.length<6&&(a=a.replace(/^#?([0-9a-f])([0-9a-f])([0-9a-f])$/i,"$1$1$2$2$3$3"));var m=a.match(/^#?([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])$/i);if(m)return new j.RGB(c(m[1],16)/255,c(m[2],16)/255,c(m[3],16)/255)}else{if(typeof a=="object"&&a.isColor)return a;if(!isNaN(a))return new j.RGB((a&255)/255,((a&65280)>>8)/255,((a&16711680)>>16)/255)}return!1}var f=[],g=/\s*(\.\d+|\d+(?:\.\d+)?)(%)?\s*/,h=/\s*(\.\d+|\d+(?:\.\d+)?)\s*/,i=new RegExp("^(rgb|hsl|hsv)a?\\("+g.source+","+g.source+","+g.source+"(?:,"+h.source+")?"+"\\)$","i");j.installColorSpace=function(a,c,d){function i(a,c){var d={};d[c.toLowerCase()]=new b("return this.rgb()."+c.toLowerCase()+"();"),j[c].propertyNames.forEach(function(a,e){d[a]=new b("value","isDelta","return this."+c.toLowerCase()+"()."+a+"(value, isDelta);")});for(var e in d)d.hasOwnProperty(e)&&j[a].prototype[e]===undefined&&(j[a].prototype[e]=d[e])}j[a]=new b(c.join(","),"if (Object.prototype.toString.apply("+c[0]+") === '[object Array]') {"+c.map(function(a,b){return a+"="+c[0]+"["+b+"];"}).reverse().join("")+"}"+"if ("+c.filter(function(a){return a!=="alpha"}).map(function(a){return"isNaN("+a+")"}).join("||")+"){"+'throw new Error("[one.color.'+a+']: Invalid color: ("+'+c.join('+","+')+'+")");}'+c.map(function(a){return a==="hue"?"this._hue=hue<0?hue-Math.floor(hue):hue%1":a==="alpha"?"this._alpha=(isNaN(alpha)||alpha>1)?1:(alpha<0?0:alpha);":"this._"+a+"="+a+"<0?0:("+a+">1?1:"+a+")"}).join(";")+";"),j[a].propertyNames=c;var e=j[a].prototype;["valueOf","hex","css","cssa"].forEach(function(c){e[c]=e[c]||(a==="RGB"?e.hex:new b("return this.rgb()."+c+"();"))}),e.isColor=!0,e.equals=function(b,d){typeof d=="undefined"&&(d=1e-10),b=b[a.toLowerCase()]();for(var e=0;e<c.length;e+=1)if(Math.abs(this["_"+c[e]]-b["_"+c[e]])>d)return!1;return!0},e.toJSON=new b("return ['"+a+"', "+c.map(function(a){return"this._"+a},this).join(", ")+"];");for(var g in d)if(d.hasOwnProperty(g)){var h=g.match(/^from(.*)$/);h?j[h[1].toUpperCase()].prototype[a.toLowerCase()]=d[g]:e[g]=d[g]}e[a.toLowerCase()]=function(){return this},e.toString=new b('return "[one.color.'+a+':"+'+c.map(function(a,b){return'" '+c[b]+'="+this._'+a}).join("+")+'+"]";'),c.forEach(function(a,d){e[a]=new b("value","isDelta","if (typeof value === 'undefined') {return this._"+a+";"+"}"+"if (isDelta) {"+"return new this.constructor("+c.map(function(b,c){return"this._"+b+(a===b?"+value":"")}).join(", ")+");"+"}"+"return new this.constructor("+c.map(function(b,c){return a===b?"value":"this._"+b}).join(", ")+");")}),f.forEach(function(b){i(a,b),i(b,a)}),f.push(a)},j.installColorSpace("RGB",["red","green","blue","alpha"],{hex:function(){var a=(e(255*this._red)*65536+e(255*this._green)*256+e(255*this._blue)).toString(16);return"#"+"00000".substr(0,6-a.length)+a},css:function(){return"rgb("+e(255*this._red)+","+e(255*this._green)+","+e(255*this._blue)+")"},cssa:function(){return"rgba("+e(255*this._red)+","+e(255*this._green)+","+e(255*this._blue)+","+this._alpha+")"}}),a.color=j}(one,Function,parseInt,parseFloat,Math.round),function(a,b){a.installColorSpace("HSV",["hue","saturation","value","alpha"],{rgb:function(){var c=this._hue,d=this._saturation,e=this._value,f=b.min(5,b.floor(c*6)),g=c*6-f,h=e*(1-d),i=e*(1-g*d),j=e*(1-(1-g)*d),k,l,m;switch(f){case 0:k=e,l=j,m=h;break;case 1:k=i,l=e,m=h;break;case 2:k=h,l=e,m=j;break;case 3:k=h,l=i,m=e;break;case 4:k=j,l=h,m=e;break;case 5:k=e,l=h,m=i}return new a.RGB(k,l,m,this._alpha)},hsl:function(){var b=(2-this._saturation)*this._value,c=this._saturation*this._value,d=b<=1?b:2-b,e;return d<1e-9?e=0:e=c/d,new a.HSL(this._hue,e,b/2,this._alpha)},fromRgb:function(){var c=this._red,d=this._green,e=this._blue,f=b.max(c,d,e),g=b.min(c,d,e),h=f-g,i,j=f===0?0:h/f,k=f;if(h===0)i=0;else switch(f){case c:i=(d-e)/h/6+(d<e?1:0);break;case d:i=(e-c)/h/6+1/3;break;case e:i=(c-d)/h/6+2/3}return new a.HSV(i,j,k,this._alpha)}})}(one.color,Math),one.color.installColorSpace("HSL",["hue","saturation","lightness","alpha"],{hsv:function(){var a=this._lightness*2,b=this._saturation*(a<=1?a:2-a),c;return a+b<1e-9?c=0:c=2*b/(a+b),new one.color.HSV(this._hue,c,(a+b)/2,this._alpha)},rgb:function(){return this.hsv().rgb()},fromRgb:function(){return this.hsv().hsl()}}),typeof module!="undefined"&&(module.exports=one.color); | ||
typeof one=="undefined"&&(one={include:function(){}}),function(){var a=Function,b=parseInt,c=parseFloat,d=Math.round,e=[],f=/\s*(\.\d+|\d+(?:\.\d+)?)(%)?\s*/,g=/\s*(\.\d+|\d+(?:\.\d+)?)\s*/,h=new RegExp("^(rgb|hsl|hsv)a?\\("+f.source+","+f.source+","+f.source+"(?:,"+g.source+")?"+"\\)$","i"),i=one.color=function(a){if(Object.prototype.toString.apply(a)==="[object Array]")return a[0].length===4?new i.RGB(a[0]/255,a[1]/255,a[2]/255,a[3]/255):new i[a[0]](a.slice(1,a.length));if(a.charCodeAt){var d=a.match(h);if(d){var e=d[1].toUpperCase(),f=typeof d[8]=="undefined"?d[8]:c(d[8]),g=e[0]==="H",j=d[3]?100:g?360:255,k=d[5]||g?100:255,l=d[7]||g?100:255;if(typeof i[e]=="undefined")throw new Error("one.color."+e+" is not installed.");return new i[e](c(d[2])/j,c(d[4])/k,c(d[6])/l,f)}a.length<6&&(a=a.replace(/^#?([0-9a-f])([0-9a-f])([0-9a-f])$/i,"$1$1$2$2$3$3"));var m=a.match(/^#?([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])$/i);if(m)return new i.RGB(b(m[1],16)/255,b(m[2],16)/255,b(m[3],16)/255)}else{if(typeof a=="object"&&a.isColor)return a;if(!isNaN(a))return new i.RGB((a&255)/255,((a&65280)>>8)/255,((a&16711680)>>16)/255)}return!1};i.installColorSpace=function(b,c,d){function j(b,c){var d={};d[c.toLowerCase()]=new a("return this.rgb()."+c.toLowerCase()+"();"),i[c].propertyNames.forEach(function(b,e){d[b]=new a("value","isDelta","return this."+c.toLowerCase()+"()."+b+"(value, isDelta);")});for(var e in d)d.hasOwnProperty(e)&&i[b].prototype[e]===undefined&&(i[b].prototype[e]=d[e])}i[b]=new a(c.join(","),"if (Object.prototype.toString.apply("+c[0]+") === '[object Array]') {"+c.map(function(a,b){return a+"="+c[0]+"["+b+"];"}).reverse().join("")+"}"+"if ("+c.filter(function(a){return a!=="alpha"}).map(function(a){return"isNaN("+a+")"}).join("||")+"){"+'throw new Error("[one.color.'+b+']: Invalid color: ("+'+c.join('+","+')+'+")");}'+c.map(function(a){return a==="hue"?"this._hue=hue<0?hue-Math.floor(hue):hue%1":a==="alpha"?"this._alpha=(isNaN(alpha)||alpha>1)?1:(alpha<0?0:alpha);":"this._"+a+"="+a+"<0?0:("+a+">1?1:"+a+")"}).join(";")+";"),i[b].propertyNames=c;var f=i[b].prototype;["valueOf","hex","css","cssa"].forEach(function(c){f[c]=f[c]||(b==="RGB"?f.hex:new a("return this.rgb()."+c+"();"))}),f.isColor=!0,f.equals=function(a,d){typeof d=="undefined"&&(d=1e-10),a=a[b.toLowerCase()]();for(var e=0;e<c.length;e+=1)if(Math.abs(this["_"+c[e]]-a["_"+c[e]])>d)return!1;return!0},f.toJSON=new a("return ['"+b+"', "+c.map(function(a){return"this._"+a},this).join(", ")+"];");for(var g in d)if(d.hasOwnProperty(g)){var h=g.match(/^from(.*)$/);h?i[h[1].toUpperCase()].prototype[b.toLowerCase()]=d[g]:f[g]=d[g]}f[b.toLowerCase()]=function(){return this},f.toString=new a('return "[one.color.'+b+':"+'+c.map(function(a,b){return'" '+c[b]+'="+this._'+a}).join("+")+'+"]";'),c.forEach(function(b,d){f[b]=new a("value","isDelta","if (typeof value === 'undefined') {return this._"+b+";"+"}"+"if (isDelta) {"+"return new this.constructor("+c.map(function(a,c){return"this._"+a+(b===a?"+value":"")}).join(", ")+");"+"}"+"return new this.constructor("+c.map(function(a,c){return b===a?"value":"this._"+a}).join(", ")+");")}),e.forEach(function(a){j(b,a),j(a,b)}),e.push(b)},i.installColorSpace("RGB",["red","green","blue","alpha"],{hex:function(){var a=(d(255*this._red)*65536+d(255*this._green)*256+d(255*this._blue)).toString(16);return"#"+"00000".substr(0,6-a.length)+a},css:function(){return"rgb("+d(255*this._red)+","+d(255*this._green)+","+d(255*this._blue)+")"},cssa:function(){return"rgba("+d(255*this._red)+","+d(255*this._green)+","+d(255*this._blue)+","+this._alpha+")"}})}(),function(){var a=Math,b=one.color;b.installColorSpace("HSV",["hue","saturation","value","alpha"],{rgb:function(){var c=this._hue,d=this._saturation,e=this._value,f=a.min(5,a.floor(c*6)),g=c*6-f,h=e*(1-d),i=e*(1-g*d),j=e*(1-(1-g)*d),k,l,m;switch(f){case 0:k=e,l=j,m=h;break;case 1:k=i,l=e,m=h;break;case 2:k=h,l=e,m=j;break;case 3:k=h,l=i,m=e;break;case 4:k=j,l=h,m=e;break;case 5:k=e,l=h,m=i}return new b.RGB(k,l,m,this._alpha)},hsl:function(){var a=(2-this._saturation)*this._value,c=this._saturation*this._value,d=a<=1?a:2-a,e;return d<1e-9?e=0:e=c/d,new b.HSL(this._hue,e,a/2,this._alpha)},fromRgb:function(){var c=this._red,d=this._green,e=this._blue,f=a.max(c,d,e),g=a.min(c,d,e),h=f-g,i,j=f===0?0:h/f,k=f;if(h===0)i=0;else switch(f){case c:i=(d-e)/h/6+(d<e?1:0);break;case d:i=(e-c)/h/6+1/3;break;case e:i=(c-d)/h/6+2/3}return new b.HSV(i,j,k,this._alpha)}})}(),one.color.installColorSpace("HSL",["hue","saturation","lightness","alpha"],{hsv:function(){var a=this._lightness*2,b=this._saturation*(a<=1?a:2-a),c;return a+b<1e-9?c=0:c=2*b/(a+b),new one.color.HSV(this._hue,c,(a+b)/2,this._alpha)},rgb:function(){return this.hsv().rgb()},fromRgb:function(){return this.hsv().hsl()}}),typeof module!="undefined"&&(module.exports=one.color); |
@@ -5,3 +5,4 @@ { | ||
"repository": "git@github.com:One-com/one-color.git", | ||
"version": "2.0.6", | ||
"version": "2.0.7", | ||
"keywords": ["ender", "color", "colour"], | ||
"maintainers": [ | ||
@@ -32,2 +33,3 @@ { | ||
"main": "one-color-debug.js", | ||
"ender": "lib/ender.js", | ||
"scripts": { | ||
@@ -34,0 +36,0 @@ "prepublish": "make; vows" |
@@ -22,25 +22,77 @@ var Color = require("../one-color-debug.js"), | ||
]; | ||
var colorChannels = []; | ||
var colorSpaces = spaces.map(function (item) { | ||
colorChannels = colorChannels.concat(item.channels); | ||
return item.name; | ||
}); | ||
var suite = vows.describe('Color'); | ||
var clr = new Color.RGB(0, 0, 0, 1); | ||
var batch = {} | ||
spaces.forEach(function(space) { | ||
var context = { | ||
topic: space.name | ||
}; | ||
var batch = {}; | ||
context['is a function'] = function (spaceName) { | ||
spaces.forEach(function (space) { | ||
var clr, | ||
context = { | ||
topic: space.name | ||
}; | ||
context['is a funtion'] = function (spaceName) { | ||
assert.isFunction(Color[spaceName]); | ||
}; | ||
context['has conversion method'] = function (spaceName) { | ||
assert.isFunction(clr[spaceName.toLowerCase()]); | ||
assert.isTrue(clr[spaceName.toLowerCase()]().isColor); | ||
context['can be constructed'] = function (spaceName) { | ||
clr = new Color[spaceName](0, 0, 0, 0); | ||
assert.isObject(clr); | ||
}; | ||
context['is Color'] = function (spaceName) { | ||
assert.isTrue(clr.isColor); | ||
}; | ||
colorSpaces.forEach(function (item) { | ||
context['has ' + item + ' conversion method'] = function (spaceName) { | ||
assert.isFunction(clr[item.toLowerCase()]); | ||
}; | ||
}); | ||
colorSpaces.forEach(function (item) { | ||
context[item + ' conversion'] = function (spaceName) { | ||
assert.isTrue(clr[item.toLowerCase()]().isColor); | ||
}; | ||
}); | ||
colorSpaces.forEach(function (item) { | ||
context['has equals method'] = function (spaceName) { | ||
assert.isFunction(clr.equals); | ||
}; | ||
}); | ||
colorSpaces.forEach(function (item) { | ||
context['equals'] = function (spaceName) { | ||
assert.isTrue(clr.equals(new Color[spaceName](0, 0, 0, 0))); | ||
}; | ||
}); | ||
var chans = { | ||
topic: new Color[space.name](0, 0, 0, 1) | ||
topic: new Color[space.name](0, 0, 0, 0) | ||
} | ||
space.channels.forEach(function(channel) { | ||
chans['has ' + channel + ' getter/setter'] = function (color) { | ||
chans[channel + ' getter/setter existance'] = function (color) { | ||
assert.isFunction(color[channel]); | ||
}; | ||
chans[channel + ' getter'] = function (color) { | ||
assert.equal(color[channel](), 0); | ||
}; | ||
chans[channel + ' setter'] = function (color) { | ||
assert.equal(color[channel](0)[channel](), 0); | ||
assert.equal(color[channel](0.5)[channel](), 0.5); | ||
if (channel === 'hue') { | ||
// Hue is considered a circle, and thus has periodic boundary conditions | ||
assert.equal(color[channel](1)[channel](), 0); | ||
assert.equal(color[channel](-0.1)[channel](), 0.9); | ||
assert.equal(color[channel](1.5)[channel](), 0.5); | ||
} else { | ||
assert.equal(color[channel](1)[channel](), 1); | ||
assert.equal(color[channel](-0.1)[channel](), 0); | ||
assert.equal(color[channel](1.1)[channel](), 1); | ||
} | ||
}; | ||
chans[channel + ' adjustment'] = function (color) { | ||
assert.equal(color[channel](0.5, true)[channel](), 0.5); | ||
}; | ||
}); | ||
@@ -52,4 +104,13 @@ context['channels'] = chans; | ||
/* | ||
valueOf: [Function: valueOf], | ||
hex: [Function], | ||
css: [Function], | ||
cssa: [Function], | ||
toJSON: [Function], | ||
toString: [Function] | ||
*/ | ||
suite.addBatch({ | ||
'Color API': batch | ||
'API': batch | ||
}).export(module); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3652780
79
5062