mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-03 19:45:44 -05:00
Clean up internal properties and beans in Color related classes.
This commit is contained in:
parent
44ab8ed3a0
commit
1d866ecb6c
4 changed files with 41 additions and 39 deletions
|
@ -12,12 +12,11 @@ var Color = this.Color = Base.extend({
|
|||
* All colors of the different subclasses support alpha values.
|
||||
*/
|
||||
getAlpha: function() {
|
||||
return this._alpha;
|
||||
return this._alpha != null ? this._alpha : 1;
|
||||
},
|
||||
|
||||
setAlpha: function(alpha) {
|
||||
if (this._alpha == null || alpha == -1) this._alpha = -1;
|
||||
else if (this._alpha < 0) this._alpha = 0;
|
||||
if (alpha < 0) this._alpha = 0;
|
||||
else if (alpha > 1) this._alpha = 1;
|
||||
else this._alpha = alpha;
|
||||
this._cssString = null;
|
||||
|
@ -29,7 +28,7 @@ var Color = this.Color = Base.extend({
|
|||
* @return {@true if the color has an alpha value}
|
||||
*/
|
||||
hasAlpha: function() {
|
||||
return this._alpha != -1;
|
||||
return this._alpha != null;
|
||||
},
|
||||
|
||||
getCanvasStyle: function() {
|
||||
|
|
|
@ -6,10 +6,9 @@ var GrayColor = this.GrayColor = Color.extend({
|
|||
var arg = arguments[0];
|
||||
if (typeof arg == 'number') {
|
||||
this._gray = arg;
|
||||
this.alpha = arg.alpha ? arg.alpha : -1;
|
||||
} else if (arg instanceof Color) {
|
||||
this._gray = arg.gray;
|
||||
this.alpha = arg.alpha;
|
||||
this._gray = arg.getGray();
|
||||
this._alpha = arg.getAlpha();
|
||||
}
|
||||
}
|
||||
},
|
||||
|
@ -44,25 +43,25 @@ var GrayColor = this.GrayColor = Color.extend({
|
|||
*/
|
||||
equals: function(color) {
|
||||
if (color instanceof GrayColor) {
|
||||
return this.gray == color.gray &&
|
||||
this.alpha == color.alpha;
|
||||
return this._gray == color._gray &&
|
||||
this._alpha == color._alpha;
|
||||
}
|
||||
return false;
|
||||
},
|
||||
|
||||
toString: function() {
|
||||
return '{ gray: ' + this.gray
|
||||
+ (this.alpha != -1 ? ', alpha: ' + this.alpha : '')
|
||||
+ ' }';
|
||||
return '{ gray: ' + this._gray
|
||||
+ (this._alpha != null ? ', alpha: ' + this._alpha : '')
|
||||
+ ' }';
|
||||
},
|
||||
|
||||
toCssString: function() {
|
||||
if (!this._cssString) {
|
||||
var component = Math.round((1 - this.gray) * 255) + ',';
|
||||
var component = Math.round((1 - this._gray) * 255) + ',';
|
||||
this._cssString = 'rgba('
|
||||
+ component + component + component
|
||||
+ (this.alpha != -1 ? this.alpha : 1)
|
||||
+ ')';
|
||||
+ component + component + component
|
||||
+ (this._alpha != null ? this.alpha : 1)
|
||||
+ ')';
|
||||
}
|
||||
return this._cssString;
|
||||
}
|
||||
|
|
|
@ -84,26 +84,29 @@ var RGBColor = this.RGBColor = Color.extend(new function() {
|
|||
this._red = components[0];
|
||||
this._green = components[1];
|
||||
this._blue = components[2];
|
||||
this.alpha = -1;
|
||||
this._alpha = null;
|
||||
} else if (Base.isArray(arg)) {
|
||||
this._red = arg[0];
|
||||
this._green = arg[1];
|
||||
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) {
|
||||
// TODO: If beans are not activated, this won't copy from
|
||||
// an existing color. OK?
|
||||
this._red = arg.red;
|
||||
this._blue = arg.blue;
|
||||
this._green = arg.green;
|
||||
this.alpha = arg.alpha ? arg.alpha : -1;
|
||||
this._alpha = arg.alpha ? arg.alpha : null;
|
||||
} 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.alpha = arg.alpha ? arg.alpha : -1;
|
||||
this._alpha = arg.alpha ? arg.alpha : null;
|
||||
};
|
||||
} else if (arguments.length >= 3) {
|
||||
this._red = arguments[0];
|
||||
this._green = arguments[1];
|
||||
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) {
|
||||
if (color instanceof RGBColor) {
|
||||
return this.red == color.red &&
|
||||
this.green == color.green &&
|
||||
this.blue == color.blue &&
|
||||
this.alpha == color.alpha;
|
||||
return this._red == color._red &&
|
||||
this._green == color._green &&
|
||||
this._blue == color._blue &&
|
||||
this._alpha == color._alpha;
|
||||
}
|
||||
return false;
|
||||
},
|
||||
|
||||
toString: function() {
|
||||
return '{ red: ' + this.red
|
||||
+ ', green: ' + this.green
|
||||
+ ', blue: ' + this.blue
|
||||
+ (this.alpha != -1 ? ', alpha: ' + this.alpha : '')
|
||||
return '{ red: ' + this._red
|
||||
+ ', green: ' + this._green
|
||||
+ ', blue: ' + this._blue
|
||||
+ (this._alpha != null ? ', alpha: ' + this._alpha : '')
|
||||
+ ' }';
|
||||
},
|
||||
|
||||
toCssString: function() {
|
||||
if (!this._cssString) {
|
||||
this._cssString = 'rgba('
|
||||
+ (Math.round(this.red * 255)) + ', '
|
||||
+ (Math.round(this.green * 255)) + ', '
|
||||
+ (Math.round(this.blue * 255)) + ', '
|
||||
+ (this.alpha != -1 ? this.alpha : 1)
|
||||
+ (Math.round(this._red * 255)) + ', '
|
||||
+ (Math.round(this._green * 255)) + ', '
|
||||
+ (Math.round(this._blue * 255)) + ', '
|
||||
+ (this._alpha != null ? this._alpha : 1)
|
||||
+ ')';
|
||||
}
|
||||
return this._cssString;
|
||||
|
|
|
@ -111,12 +111,13 @@ var Raster = this.Raster = Item.extend({
|
|||
// TODO: setPixel(point, color)
|
||||
setPixel: function(x, y, color) {
|
||||
color = Color.read(arguments, 2);
|
||||
var ctx = this.context;
|
||||
var imageData = ctx.getImageData(x, y, 1, 1);
|
||||
imageData.data[0] = color.red * 255;
|
||||
imageData.data[1] = color.green * 255;
|
||||
imageData.data[2] = color.blue * 255;
|
||||
imageData.data[3] = color.alpha != -1 ? color.alpha * 255 : 255;
|
||||
var ctx = this.context,
|
||||
imageData = ctx.getImageData(x, y, 1, 1),
|
||||
alpha = color.getAlpha();
|
||||
imageData.data[0] = color.getRed() * 255;
|
||||
imageData.data[1] = color.getGreen() * 255;
|
||||
imageData.data[2] = color.getBlue() * 255;
|
||||
imageData.data[3] = alpha != null ? alpha * 255 : 255;
|
||||
ctx.putImageData(imageData, x, y);
|
||||
},
|
||||
|
||||
|
|
Loading…
Reference in a new issue