From 2c29c0cfab570f8f1a1f63e5d2a3900ae676d12d Mon Sep 17 00:00:00 2001 From: Kienz Date: Sat, 25 May 2013 19:42:31 +0200 Subject: [PATCH] Fix typo, add hsl/hsla (with whitespaces) unit tests and move stuff outside of functions --- src/color.class.js | 25 ++++++++++++++++--------- src/parser.js | 10 +++++----- src/text.class.js | 2 +- test/unit/color.js | 25 +++++++++++++++++++++++-- 4 files changed, 45 insertions(+), 17 deletions(-) diff --git a/src/color.class.js b/src/color.class.js index daceaa7f..b42699ce 100644 --- a/src/color.class.js +++ b/src/color.class.js @@ -294,6 +294,22 @@ 'yellow': '#FFFF00' }; + /** + * @private + * @param {Number} p + * @param {Number} q + * @param {Number} t + * @return {Number} + */ + function hue2rgb(p, q, t){ + if(t < 0) t += 1; + if(t > 1) t -= 1; + if(t < 1/6) return p + (q - p) * 6 * t; + if(t < 1/2) return q; + if(t < 2/3) return p + (q - p) * (2/3 - t) * 6; + return p; + } + /** * Returns new color object, when given a color in RGB format * @param {String} color ex: rgb(0-255,0-255,0-255) @@ -358,15 +374,6 @@ l = parseFloat(match[3]) / (/%$/.test(match[3]) ? 100 : 1), r, g, b; - function hue2rgb(p, q, t){ - if(t < 0) t += 1; - if(t > 1) t -= 1; - if(t < 1/6) return p + (q - p) * 6 * t; - if(t < 1/2) return q; - if(t < 2/3) return p + (q - p) * (2/3 - t) * 6; - return p; - } - if (s === 0) { r = g = b = l; } diff --git a/src/parser.js b/src/parser.js index 29f9369c..f578444a 100644 --- a/src/parser.js +++ b/src/parser.js @@ -43,6 +43,11 @@ 'transform': 'transformMatrix' }; + var colorAttributes = { + 'stroke': 'strokeOpacity', + 'fill': 'fillOpacity' + }; + function normalizeAttr(attr) { // transform attribute names if (attr in attributesMap) { @@ -84,11 +89,6 @@ * @param {Object} attributes Array of attributes to parse */ function _setStrokeFillOpacity(attributes) { - var colorAttributes = { - 'stroke': 'strokeOpacity', - 'fill': 'fillOpacity' - }; - for (var attr in colorAttributes) { if (!attributes[attr] || typeof attributes[colorAttributes[attr]] === 'undefined') continue; diff --git a/src/text.class.js b/src/text.class.js index 54f1ddbe..066510b5 100644 --- a/src/text.class.js +++ b/src/text.class.js @@ -80,7 +80,7 @@ fontFamily: 'Times New Roman', /** - * Text decoration Possible valus: "", "underline", "overline" or "line-through". + * Text decoration Possible values: "", "underline", "overline" or "line-through". * @type String * @default */ diff --git a/test/unit/color.js b/test/unit/color.js index 381e5e35..d6638b92 100644 --- a/test/unit/color.js +++ b/test/unit/color.js @@ -126,7 +126,7 @@ equal(oColor.toHex(), 'FFFFFF'); }); - test('fromRgb (with withspaces)', function() { + test('fromRgb (with whitespaces)', function() { ok(typeof fabric.Color.fromRgb == 'function'); var originalRgb = 'rgb( 255 , 255 , 255 )'; var oColor = fabric.Color.fromRgb(originalRgb); @@ -167,7 +167,7 @@ equal(oColor.getAlpha(), 0.5, 'alpha should be set properly'); }); - test('fromRgba (with withspaces)', function() { + test('fromRgba (with whitespaces)', function() { var originalRgba = 'rgba( 255 , 255 , 255 , 0.5 )'; oColor = fabric.Color.fromRgba(originalRgba); ok(oColor); @@ -207,6 +207,16 @@ equal(oColor.toHex(), '180637'); }); + test('fromHsl (with whitespaces)', function() { + ok(typeof fabric.Color.fromHsl == 'function'); + var originalHsl = 'hsl( 262 , 80% , 12% )'; + var oColor = fabric.Color.fromHsl(originalHsl); + ok(oColor); + ok(oColor instanceof fabric.Color); + equal(oColor.toHsl(), 'hsl(262,80%,12%)'); + equal(oColor.toHex(), '180637'); + }); + test('fromHsla', function() { ok(typeof fabric.Color.fromHsla == 'function'); var originalHsla = 'hsla(262,80%,12%,0.2)'; @@ -218,6 +228,17 @@ equal(oColor.getAlpha(), 0.2, 'alpha should be set properly'); }); + test('fromHsla (with whitespaces)', function() { + ok(typeof fabric.Color.fromHsla == 'function'); + var originalHsla = 'hsla( 262 , 80% , 12% , 0.2 )'; + var oColor = fabric.Color.fromHsla(originalHsla); + ok(oColor); + ok(oColor instanceof fabric.Color); + equal(oColor.toHsla(), 'hsla(262,80%,12%,0.2)'); + equal(oColor.toHex(), '180637'); + equal(oColor.getAlpha(), 0.2, 'alpha should be set properly'); + }); + test('fromHex', function() { ok(typeof fabric.Color.fromHex == 'function'); var originalHex = 'FF5555';