Clean up internal properties and beans in Color related classes.

This commit is contained in:
Jürg Lehni 2011-03-05 00:16:26 +00:00
parent 44ab8ed3a0
commit 1d866ecb6c
4 changed files with 41 additions and 39 deletions

View file

@ -12,12 +12,11 @@ var Color = this.Color = Base.extend({
* All colors of the different subclasses support alpha values. * All colors of the different subclasses support alpha values.
*/ */
getAlpha: function() { getAlpha: function() {
return this._alpha; return this._alpha != null ? this._alpha : 1;
}, },
setAlpha: function(alpha) { setAlpha: function(alpha) {
if (this._alpha == null || alpha == -1) this._alpha = -1; if (alpha < 0) this._alpha = 0;
else if (this._alpha < 0) this._alpha = 0;
else if (alpha > 1) this._alpha = 1; else if (alpha > 1) this._alpha = 1;
else this._alpha = alpha; else this._alpha = alpha;
this._cssString = null; this._cssString = null;
@ -29,7 +28,7 @@ var Color = this.Color = Base.extend({
* @return {@true if the color has an alpha value} * @return {@true if the color has an alpha value}
*/ */
hasAlpha: function() { hasAlpha: function() {
return this._alpha != -1; return this._alpha != null;
}, },
getCanvasStyle: function() { getCanvasStyle: function() {

View file

@ -6,10 +6,9 @@ var GrayColor = this.GrayColor = Color.extend({
var arg = arguments[0]; var arg = arguments[0];
if (typeof arg == 'number') { if (typeof arg == 'number') {
this._gray = arg; this._gray = arg;
this.alpha = arg.alpha ? arg.alpha : -1;
} else if (arg instanceof Color) { } else if (arg instanceof Color) {
this._gray = arg.gray; this._gray = arg.getGray();
this.alpha = arg.alpha; this._alpha = arg.getAlpha();
} }
} }
}, },
@ -44,25 +43,25 @@ var GrayColor = this.GrayColor = Color.extend({
*/ */
equals: function(color) { equals: function(color) {
if (color instanceof GrayColor) { if (color instanceof GrayColor) {
return this.gray == color.gray && return this._gray == color._gray &&
this.alpha == color.alpha; this._alpha == color._alpha;
} }
return false; return false;
}, },
toString: function() { toString: function() {
return '{ gray: ' + this.gray return '{ gray: ' + this._gray
+ (this.alpha != -1 ? ', alpha: ' + this.alpha : '') + (this._alpha != null ? ', alpha: ' + this._alpha : '')
+ ' }'; + ' }';
}, },
toCssString: function() { toCssString: function() {
if (!this._cssString) { if (!this._cssString) {
var component = Math.round((1 - this.gray) * 255) + ','; var component = Math.round((1 - this._gray) * 255) + ',';
this._cssString = 'rgba(' this._cssString = 'rgba('
+ component + component + component + component + component + component
+ (this.alpha != -1 ? this.alpha : 1) + (this._alpha != null ? this.alpha : 1)
+ ')'; + ')';
} }
return this._cssString; return this._cssString;
} }

View file

@ -84,26 +84,29 @@ var RGBColor = this.RGBColor = Color.extend(new function() {
this._red = components[0]; this._red = components[0];
this._green = components[1]; this._green = components[1];
this._blue = components[2]; this._blue = components[2];
this.alpha = -1; this._alpha = null;
} else if (Base.isArray(arg)) { } else if (Base.isArray(arg)) {
this._red = arg[0]; this._red = arg[0];
this._green = arg[1]; this._green = arg[1];
this._blue = arg[2]; this._blue = arg[2];
this.alpha = arg.length > 3 ? arg[3] : -1; this._alpha = arg.length > 3 ? arg[3] : null;
} else if (arg.red !== undefined) { } else if (arg.red !== undefined) {
// TODO: If beans are not activated, this won't copy from
// an existing color. OK?
this._red = arg.red; this._red = arg.red;
this._blue = arg.blue; this._blue = arg.blue;
this._green = arg.green; this._green = arg.green;
this.alpha = arg.alpha ? arg.alpha : -1; this._alpha = arg.alpha ? arg.alpha : null;
} else if (arg.gray !== undefined) { } else if (arg.gray !== undefined) {
// TODO: Shouldn't this follow the NTSC convention as well?
this._red = this._green = this._blue = 1 - arg.gray; this._red = this._green = this._blue = 1 - arg.gray;
this.alpha = arg.alpha ? arg.alpha : -1; this._alpha = arg.alpha ? arg.alpha : null;
}; };
} else if (arguments.length >= 3) { } else if (arguments.length >= 3) {
this._red = arguments[0]; this._red = arguments[0];
this._green = arguments[1]; this._green = arguments[1];
this._blue = arguments[2]; this._blue = arguments[2];
this.alpha = arguments.length > 3 ? arguments[3] : -1; this._alpha = arguments.length > 3 ? arguments[3] : null;
} }
}, },
@ -177,29 +180,29 @@ var RGBColor = this.RGBColor = Color.extend(new function() {
*/ */
equals: function(color) { equals: function(color) {
if (color instanceof RGBColor) { if (color instanceof RGBColor) {
return this.red == color.red && return this._red == color._red &&
this.green == color.green && this._green == color._green &&
this.blue == color.blue && this._blue == color._blue &&
this.alpha == color.alpha; this._alpha == color._alpha;
} }
return false; return false;
}, },
toString: function() { toString: function() {
return '{ red: ' + this.red return '{ red: ' + this._red
+ ', green: ' + this.green + ', green: ' + this._green
+ ', blue: ' + this.blue + ', blue: ' + this._blue
+ (this.alpha != -1 ? ', alpha: ' + this.alpha : '') + (this._alpha != null ? ', alpha: ' + this._alpha : '')
+ ' }'; + ' }';
}, },
toCssString: function() { toCssString: function() {
if (!this._cssString) { if (!this._cssString) {
this._cssString = 'rgba(' this._cssString = 'rgba('
+ (Math.round(this.red * 255)) + ', ' + (Math.round(this._red * 255)) + ', '
+ (Math.round(this.green * 255)) + ', ' + (Math.round(this._green * 255)) + ', '
+ (Math.round(this.blue * 255)) + ', ' + (Math.round(this._blue * 255)) + ', '
+ (this.alpha != -1 ? this.alpha : 1) + (this._alpha != null ? this._alpha : 1)
+ ')'; + ')';
} }
return this._cssString; return this._cssString;

View file

@ -111,12 +111,13 @@ var Raster = this.Raster = Item.extend({
// TODO: setPixel(point, color) // TODO: setPixel(point, color)
setPixel: function(x, y, color) { setPixel: function(x, y, color) {
color = Color.read(arguments, 2); color = Color.read(arguments, 2);
var ctx = this.context; var ctx = this.context,
var imageData = ctx.getImageData(x, y, 1, 1); imageData = ctx.getImageData(x, y, 1, 1),
imageData.data[0] = color.red * 255; alpha = color.getAlpha();
imageData.data[1] = color.green * 255; imageData.data[0] = color.getRed() * 255;
imageData.data[2] = color.blue * 255; imageData.data[1] = color.getGreen() * 255;
imageData.data[3] = color.alpha != -1 ? color.alpha * 255 : 255; imageData.data[2] = color.getBlue() * 255;
imageData.data[3] = alpha != null ? alpha * 255 : 255;
ctx.putImageData(imageData, x, y); ctx.putImageData(imageData, x, y);
}, },