UNPKG

image-js

Version:

Image processing and manipulation in JavaScript

43 lines (42 loc) 1.25 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = rgba; var _Image = _interopRequireDefault(require("../Image")); var _model = require("../model/model"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } /** * Make a copy of the current image and convert to RGBA * The source image must have RGB color model, 8 or 16 bits. * This method will add an alpha channel if required. In this case * the value for the alpha channel will be 100% opacity. * @memberof Image * @instance * @return {Image} - New image in RGB color model with alpha channel * @example * var rgbaImage = image.rgba(); */ function rgba() { this.checkProcessable('rgba', { bitDepth: [8, 16], alpha: [0, 1], colorModel: [_model.RGB] }); if (this.colorModel === _model.RGB && this.alpha) { return this.clone(); } let newImage = _Image.default.createFrom(this, { alpha: 1 }); let ptr = 0; let data = this.data; for (let i = 0; i < data.length; i += this.channels) { newImage.data[ptr++] = data[i]; newImage.data[ptr++] = data[i + 1]; newImage.data[ptr++] = data[i + 2]; newImage.data[ptr++] = this.maxValue; ptr++; } return newImage; }