From f268c6e15250f6c14c993bb845b77ffd24a1cf57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Sat, 1 Dec 2012 12:31:22 -0800 Subject: [PATCH] Rename Color#toCssString() to #toCss(), and only include alpha if its < 1. --- src/color/Color.js | 18 ++++++++++-------- src/color/GradientColor.js | 2 +- src/svg/SvgExport.js | 2 +- test/tests/Color.js | 12 ++++++------ 4 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/color/Color.js b/src/color/Color.js index 74a21236..5cb8fc3b 100644 --- a/src/color/Color.js +++ b/src/color/Color.js @@ -371,7 +371,7 @@ var Color = this.Color = Base.extend(new function() { * Called by various setters whenever a color value changes */ _changed: function() { - this._cssString = null; + this._css = null; if (this._owner) this._owner._changed(/*#=*/ Change.STYLE); }, @@ -477,23 +477,25 @@ var Color = this.Color = Base.extend(new function() { /** * @return {String} A css string representation of the color. */ - toCssString: function() { - if (!this._cssString) { + toCss: function() { + if (!this._css) { var color = this.convert('rgb'), alpha = color.getAlpha(), components = [ Math.round(color._red * 255), Math.round(color._green * 255), - Math.round(color._blue * 255), - alpha != null ? alpha : 1 + Math.round(color._blue * 255) ]; - this._cssString = 'rgba(' + components.join(', ') + ')'; + if (alpha != null && alpha < 1) + components.push(alpha); + this._css = (components.length == 4 ? 'rgba(' : 'rgb(') + + components.join(', ') + ')'; } - return this._cssString; + return this._css; }, getCanvasStyle: function() { - return this.toCssString(); + return this.toCss(); } /** diff --git a/src/color/GradientColor.js b/src/color/GradientColor.js index 8ae834f4..76400b85 100644 --- a/src/color/GradientColor.js +++ b/src/color/GradientColor.js @@ -238,7 +238,7 @@ var GradientColor = this.GradientColor = Color.extend(/** @lends GradientColor# } for (var i = 0, l = this.gradient._stops.length; i < l; i++) { var stop = this.gradient._stops[i]; - gradient.addColorStop(stop._rampPoint, stop._color.toCssString()); + gradient.addColorStop(stop._rampPoint, stop._color.toCss()); } return gradient; }, diff --git a/src/svg/SvgExport.js b/src/svg/SvgExport.js index 5acadab8..da5bdd1e 100644 --- a/src/svg/SvgExport.js +++ b/src/svg/SvgExport.js @@ -344,7 +344,7 @@ new function() { attrs[entry.attribute] = value == null ? 'none' : entry.type === 'color' - ? value.toCssString() + ? value.toCss() : entry.type === 'array' ? value.join(',') : entry.type === 'number' diff --git a/test/tests/Color.js b/test/tests/Color.js index 2a95f605..f58debbb 100644 --- a/test/tests/Color.js +++ b/test/tests/Color.js @@ -20,38 +20,38 @@ test('Set named color', function() { var path = new Path(); path.fillColor = 'red'; compareRgbColors(path.fillColor, new RgbColor(1, 0, 0)); - equals(path.fillColor.toCssString(), 'rgba(255, 0, 0, 1)'); + equals(path.fillColor.toCss(), 'rgb(255, 0, 0)'); }); test('Set color to hex', function() { var path = new Path(); path.fillColor = '#ff0000'; compareRgbColors(path.fillColor, new RgbColor(1, 0, 0)); - equals(path.fillColor.toCssString(), 'rgba(255, 0, 0, 1)'); + equals(path.fillColor.toCss(), 'rgb(255, 0, 0)'); var path = new Path(); path.fillColor = '#f00'; compareRgbColors(path.fillColor, new RgbColor(1, 0, 0)); - equals(path.fillColor.toCssString(), 'rgba(255, 0, 0, 1)'); + equals(path.fillColor.toCss(), 'rgb(255, 0, 0)'); }); test('Set color to object', function() { var path = new Path(); path.fillColor = { red: 1, green: 0, blue: 1}; compareRgbColors(path.fillColor, new RgbColor(1, 0, 1)); - equals(path.fillColor.toCssString(), 'rgba(255, 0, 255, 1)'); + equals(path.fillColor.toCss(), 'rgb(255, 0, 255)'); var path = new Path(); path.fillColor = { gray: 0.2 }; compareRgbColors(path.fillColor, new RgbColor(0.8, 0.8, 0.8)); - equals(path.fillColor.toCssString(), 'rgba(204, 204, 204, 1)'); + equals(path.fillColor.toCss(), 'rgb(204, 204, 204)'); }); test('Set color to array', function() { var path = new Path(); path.fillColor = [1, 0, 0]; compareRgbColors(path.fillColor, new RgbColor(1, 0, 0)); - equals(path.fillColor.toCssString(), 'rgba(255, 0, 0, 1)'); + equals(path.fillColor.toCss(), 'rgb(255, 0, 0)'); }); test('Creating colors', function() {