Various improvements to Color caching mechanisms.

This commit is contained in:
Jürg Lehni 2013-04-19 12:51:53 -07:00
parent 9efbc288a1
commit bb546decae

View file

@ -571,8 +571,7 @@ var Color = this.Color = Base.extend(new function() {
* Called by various setters whenever a color value changes * Called by various setters whenever a color value changes
*/ */
_changed: function() { _changed: function() {
this._css = null; this._canvasStyle = null;
this._canvasGradient = null;
if (this._owner) if (this._owner)
this._owner._changed(/*#=*/ Change.STYLE); this._owner._changed(/*#=*/ Change.STYLE);
}, },
@ -706,33 +705,26 @@ var Color = this.Color = Base.extend(new function() {
* @return {String} A css string representation of the color. * @return {String} A css string representation of the color.
*/ */
toCss: function(noAlpha) { toCss: function(noAlpha) {
var css = this._css; var components = this._convert('rgb'),
// Only cache _css value if we're not ommiting alpha, as required alpha = noAlpha || this._alpha == null ? 1 : this._alpha;
// by SVG export. components = [
if (!css || noAlpha) { Math.round(components[0] * 255),
var components = this._convert('rgb'), Math.round(components[1] * 255),
alpha = noAlpha || this._alpha == null ? 1 : this._alpha; Math.round(components[2] * 255)
components = [ ];
Math.round(components[0] * 255), if (alpha < 1)
Math.round(components[1] * 255), components.push(alpha);
Math.round(components[2] * 255) return (components.length == 4 ? 'rgba(' : 'rgb(')
]; + components.join(', ') + ')';
if (alpha < 1)
components.push(alpha);
var css = (components.length == 4 ? 'rgba(' : 'rgb(')
+ components.join(', ') + ')';
if (!noAlpha)
this._css = css;
}
return css;
}, },
toCanvasStyle: function(ctx) { toCanvasStyle: function(ctx) {
if (this._canvasStyle)
return this._canvasStyle;
// Normal colors are simply represented by their css string.
if (this._type !== 'gradient') if (this._type !== 'gradient')
return this.toCss(); return this._canvasStyle = this.toCss();
// Gradient code form here onwards, incudling caching // Gradient code form here onwards
if (this._canvasGradient)
return this._canvasGradient;
var components = this._components, var components = this._components,
gradient = components[0], gradient = components[0],
stops = gradient._stops, stops = gradient._stops,
@ -756,9 +748,10 @@ var Color = this.Color = Base.extend(new function() {
} }
for (var i = 0, l = stops.length; i < l; i++) { for (var i = 0, l = stops.length; i < l; i++) {
var stop = stops[i]; var stop = stops[i];
canvasGradient.addColorStop(stop._rampPoint, stop._color.toCss()); canvasGradient.addColorStop(stop._rampPoint,
stop._color.toCanvasStyle());
} }
return this._canvasGradient = canvasGradient; return this._canvasStyle = canvasGradient;
}, },
/** /**
@ -773,6 +766,7 @@ var Color = this.Color = Base.extend(new function() {
var point = components[i]; var point = components[i];
matrix._transformPoint(point, point, true); matrix._transformPoint(point, point, true);
} }
this._changed();
} }
}, },