UNPKG

imagerot

Version:

A lightweight, cross-environment image library for applying unique effects via raw image buffers.

40 lines (39 loc) 2.15 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.rgbshift = void 0; const rgbshift = (_a, ...args_1) => __awaiter(void 0, [_a, ...args_1], void 0, function* ({ data, width, height }, options = {}) { var _b, _c, _d; const redShift = (_b = options.redShift) !== null && _b !== void 0 ? _b : { x: 5, y: 0 }; const greenShift = (_c = options.greenShift) !== null && _c !== void 0 ? _c : { x: -5, y: 0 }; const blueShift = (_d = options.blueShift) !== null && _d !== void 0 ? _d : { x: 0, y: 5 }; const tempData = new Uint8Array(data); // Copy to avoid overwriting during shifts for (let y = 0; y < height; y++) { for (let x = 0; x < width; x++) { const idx = (y * width + x) * 4; // Shift red const rx = (x + redShift.x + width) % width; const ry = (y + redShift.y + height) % height; data[idx] = tempData[(ry * width + rx) * 4]; // Shift green const gx = (x + greenShift.x + width) % width; const gy = (y + greenShift.y + height) % height; data[idx + 1] = tempData[(gy * width + gx) * 4 + 1]; // Shift blue const bx = (x + blueShift.x + width) % width; const by = (y + blueShift.y + height) % height; data[idx + 2] = tempData[(by * width + bx) * 4 + 2]; // Alpha unchanged } } return data; }); exports.rgbshift = rgbshift;