UNPKG

konva

Version:

HTML5 2d canvas library.

50 lines (49 loc) 1.33 kB
import { Factory } from "../Factory.js"; import { Node } from "../Node.js"; import { RGBComponent } from "../Validators.js"; export const RGBA = function (imageData) { const data = imageData.data, nPixels = data.length, red = this.red(), green = this.green(), blue = this.blue(), alpha = this.alpha(); for (let i = 0; i < nPixels; i += 4) { const ia = 1 - alpha; data[i] = red * alpha + data[i] * ia; data[i + 1] = green * alpha + data[i + 1] * ia; data[i + 2] = blue * alpha + data[i + 2] * ia; } }; Factory.addGetterSetter(Node, 'red', 0, function (val) { this._filterUpToDate = false; if (val > 255) { return 255; } else if (val < 0) { return 0; } else { return Math.round(val); } }); Factory.addGetterSetter(Node, 'green', 0, function (val) { this._filterUpToDate = false; if (val > 255) { return 255; } else if (val < 0) { return 0; } else { return Math.round(val); } }); Factory.addGetterSetter(Node, 'blue', 0, RGBComponent, Factory.afterSetFilter); Factory.addGetterSetter(Node, 'alpha', 1, function (val) { this._filterUpToDate = false; if (val > 1) { return 1; } else if (val < 0) { return 0; } else { return val; } });