Handle alpha channels correctly in Raster#get/setPixel.

Closes #209.
This commit is contained in:
Jürg Lehni 2013-04-24 16:29:28 -07:00
parent 69512ac396
commit ead3646581
2 changed files with 9 additions and 7 deletions

View file

@ -446,10 +446,11 @@ var Raster = this.Raster = Item.extend(/** @lends Raster# */{
getPixel: function(point) { getPixel: function(point) {
point = Point.read(arguments); point = Point.read(arguments);
var pixels = this.getContext().getImageData(point.x, point.y, 1, 1).data, var pixels = this.getContext().getImageData(point.x, point.y, 1, 1).data,
components = [0, 0, 0, 0]; components = [0, 0, 0];
for (var i = 0; i < 4; i++) for (var i = 0; i < 3; i++)
components[i] = pixels[i] / 255; components[i] = pixels[i] / 255;
return new Color('rgb', components); // Alpha is separate now:
return new Color('rgb', components, pixels[3] / 255);
}, },
/** /**
@ -474,10 +475,11 @@ var Raster = this.Raster = Item.extend(/** @lends Raster# */{
_color = Color.read(arguments); _color = Color.read(arguments);
var ctx = this.getContext(true), var ctx = this.getContext(true),
imageData = ctx.createImageData(1, 1), imageData = ctx.createImageData(1, 1),
components = _color._convert('rgb'),
alpha = _color.getAlpha(); alpha = _color.getAlpha();
imageData.data[0] = _color.getRed() * 255; imageData.data[0] = components[0] * 255;
imageData.data[1] = _color.getGreen() * 255; imageData.data[1] = components[1] * 255;
imageData.data[2] = _color.getBlue() * 255; imageData.data[2] = components[2] * 255;
imageData.data[3] = alpha != null ? alpha * 255 : 255; imageData.data[3] = alpha != null ? alpha * 255 : 255;
ctx.putImageData(imageData, _point.x, _point.y); ctx.putImageData(imageData, _point.x, _point.y);
}, },

View file

@ -609,7 +609,7 @@ var Color = this.Color = Base.extend(new function() {
*/ */
_convert: function(type) { _convert: function(type) {
var converter; var converter;
return this._type == type return this._type === type
? this._components.slice() ? this._components.slice()
: (converter = converters[this._type + '-' + type]) : (converter = converters[this._type + '-' + type])
? converter.apply(this, this._components) ? converter.apply(this, this._components)