Initial commit
This commit is contained in:
commit
e49726988c
5445 files changed
+1089914
No files matched your search
+8
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"extends": "../tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"rootDir": "../src",
|
||||
"module": "nodenext",
|
||||
"moduleResolution": "nodenext"
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"extends": "./build.json",
|
||||
"include": [
|
||||
"../src/**/*.ts",
|
||||
"../src/**/*.cts",
|
||||
"../src/**/*.tsx",
|
||||
"../src/**/*.json"
|
||||
],
|
||||
"exclude": [
|
||||
"../**/*.test.ts",
|
||||
"../src/**/*.mts",
|
||||
"../src/package.json"
|
||||
],
|
||||
"compilerOptions": {
|
||||
"outDir": "../.tshy-build/commonjs"
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"extends": "./build.json",
|
||||
"include": [
|
||||
"../src/**/*.ts",
|
||||
"../src/**/*.mts",
|
||||
"../src/**/*.tsx",
|
||||
"../src/**/*.json"
|
||||
],
|
||||
"exclude": [
|
||||
"../**/*.test.ts",
|
||||
"../src/package.json"
|
||||
],
|
||||
"compilerOptions": {
|
||||
"outDir": "../.tshy-build/esm"
|
||||
}
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
|
||||
|
||||
> @jimp/utils@1.5.0 build /Users/andrewlisowski/Documents/jimp/packages/utils
|
||||
> tshy
|
||||
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
|
||||
|
||||
> @jimp/utils@1.1.2 lint /Users/andrewlisowski/Documents/jimp/packages/utils
|
||||
> eslint .
|
||||
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
|
||||
|
||||
> @jimp/utils@1.1.2 test /Users/andrewlisowski/Documents/jimp/packages/utils
|
||||
> vitest "--watch=false"
|
||||
|
||||
|
||||
[7m[1m[36m RUN [39m[22m[27m [36mv2.0.5[39m [90m/Users/andrewlisowski/Documents/jimp/packages/utils[39m
|
||||
|
||||
[?25l [32m✓[39m [2msrc/[22mindex[2m.test.ts[22m[2m (3)[22m
|
||||
[32m✓[39m colorDiff[2m (3)[22m
|
||||
[32m✓[39m totally opaque (no alpha defined)
|
||||
[32m✓[39m totally transparent
|
||||
[32m✓[39m different alpha
|
||||
[?25l[2K[1A[2K[1A[2K[1A[2K[1A[2K[1A[2K[G [32m✓[39m [2msrc/[22mindex[2m.test.ts[22m[2m (3)[22m
|
||||
[32m✓[39m colorDiff[2m (3)[22m
|
||||
[32m✓[39m totally opaque (no alpha defined)
|
||||
[32m✓[39m totally transparent
|
||||
[32m✓[39m different alpha
|
||||
|
||||
[2m Test Files [22m [1m[32m1 passed[39m[22m[90m (1)[39m
|
||||
[2m Tests [22m [1m[32m3 passed[39m[22m[90m (3)[39m
|
||||
[2m Start at [22m 01:33:25
|
||||
[2m Duration [22m 780ms[2m (transform 280ms, setup 0ms, collect 280ms, tests 4ms, environment 0ms, prepare 106ms)[22m
|
||||
|
||||
[?25h[?25h
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
# v1.4.0 (Sat Sep 07 2024)
|
||||
|
||||
#### 🐛 Bug Fix
|
||||
|
||||
- Bind callback to image instance [#1335](https://github.com/jimp-dev/jimp/pull/1335) ([@hipstersmoothie](https://github.com/hipstersmoothie))
|
||||
|
||||
#### ⚠️ Pushed to `main`
|
||||
|
||||
- fix docs build ([@hipstersmoothie](https://github.com/hipstersmoothie))
|
||||
|
||||
#### Authors: 1
|
||||
|
||||
- Andrew Lisowski ([@hipstersmoothie](https://github.com/hipstersmoothie))
|
||||
|
||||
---
|
||||
|
||||
# v1.1.3 (Mon Sep 02 2024)
|
||||
|
||||
#### 🐛 Bug Fix
|
||||
|
||||
- Update deps [#1319](https://github.com/jimp-dev/jimp/pull/1319) ([@hipstersmoothie](https://github.com/hipstersmoothie))
|
||||
|
||||
#### Authors: 1
|
||||
|
||||
- Andrew Lisowski ([@hipstersmoothie](https://github.com/hipstersmoothie))
|
||||
|
||||
---
|
||||
|
||||
# v1.1.0 (Sun Sep 01 2024)
|
||||
|
||||
#### ⚠️ Pushed to `main`
|
||||
|
||||
- upgrade tshy ([@hipstersmoothie](https://github.com/hipstersmoothie))
|
||||
|
||||
#### Authors: 1
|
||||
|
||||
- Andrew Lisowski ([@hipstersmoothie](https://github.com/hipstersmoothie))
|
||||
|
||||
---
|
||||
|
||||
# v1.0.3 (Sat Aug 31 2024)
|
||||
|
||||
#### 🐛 Bug Fix
|
||||
|
||||
- Fix build [#1303](https://github.com/jimp-dev/jimp/pull/1303) ([@hipstersmoothie](https://github.com/hipstersmoothie))
|
||||
|
||||
#### ⚠️ Pushed to `main`
|
||||
|
||||
- add clean script ([@hipstersmoothie](https://github.com/hipstersmoothie))
|
||||
|
||||
#### Authors: 1
|
||||
|
||||
- Andrew Lisowski ([@hipstersmoothie](https://github.com/hipstersmoothie))
|
||||
|
||||
---
|
||||
|
||||
# v1.0.2 (Sat Aug 31 2024)
|
||||
|
||||
#### ⚠️ Pushed to `main`
|
||||
|
||||
- set side effects ([@hipstersmoothie](https://github.com/hipstersmoothie))
|
||||
- fix versions ([@hipstersmoothie](https://github.com/hipstersmoothie))
|
||||
- add publish config ([@hipstersmoothie](https://github.com/hipstersmoothie))
|
||||
|
||||
#### Authors: 1
|
||||
|
||||
- Andrew Lisowski ([@hipstersmoothie](https://github.com/hipstersmoothie))
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2018 Oliver Moran
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
+85
@@ -0,0 +1,85 @@
|
||||
import { RGBAColor, JimpClass, Bitmap, RGBColor } from "@jimp/types";
|
||||
export declare function clone<I extends JimpClass>(image: I): I;
|
||||
export declare function scan<I extends JimpClass>(image: I, f: (this: I, x: number, y: number, idx: number) => any): I;
|
||||
export declare function scan<I extends {
|
||||
bitmap: Bitmap;
|
||||
}>(image: I, x: number, y: number, w: number, h: number, cb: (x: number, y: number, idx: number) => any): I;
|
||||
export declare function scanIterator<I extends JimpClass>(image: I, x: number, y: number, w: number, h: number): Generator<{
|
||||
x: number;
|
||||
y: number;
|
||||
idx: number;
|
||||
image: I;
|
||||
}, void, unknown>;
|
||||
/**
|
||||
* A helper method that converts RGBA values to a single integer value
|
||||
* @param i A single integer value representing an RGBA colour (e.g. 0xFF0000FF for red)
|
||||
* @returns An object with the properties r, g, b and a representing RGBA values
|
||||
* @example
|
||||
* ```ts
|
||||
* import { intToRGBA } from "@jimp/utils";
|
||||
*
|
||||
* intToRGBA(0xFF0000FF); // { r: 255, g: 0, b: 0, a:255 }
|
||||
* ```
|
||||
*/
|
||||
export declare function intToRGBA(i: number): RGBAColor;
|
||||
/**
|
||||
* A static helper method that converts RGBA values to a single integer value
|
||||
* @param r the red value (0-255)
|
||||
* @param g the green value (0-255)
|
||||
* @param b the blue value (0-255)
|
||||
* @param a the alpha value (0-255)
|
||||
* @example
|
||||
* ```ts
|
||||
* import { rgbaToInt } from "@jimp/utils";
|
||||
*
|
||||
* rgbaToInt(255, 0, 0, 255); // 0xFF0000FF
|
||||
* ```
|
||||
*/
|
||||
export declare function rgbaToInt(r: number, g: number, b: number, a: number): number;
|
||||
/**
|
||||
* Compute color difference
|
||||
* 0 means no difference, 1 means maximum difference.
|
||||
* Both parameters must be an color object `{ r:val, g:val, b:val, a:val }`
|
||||
* Where `a` is optional and `val` is an integer between 0 and 255.
|
||||
* @param rgba1 first color to compare.
|
||||
* @param rgba2 second color to compare.
|
||||
* @returns float between 0 and 1.
|
||||
* @example
|
||||
* ```ts
|
||||
* import { colorDiff } from "@jimp/utils";
|
||||
*
|
||||
* colorDiff(
|
||||
* { r: 255, g: 0, b: 0, a: 0 },
|
||||
* { r: 0, g: 255, b: 0, a: 0 },
|
||||
* ); // 0.5
|
||||
*
|
||||
* colorDiff(
|
||||
* { r: 0, g: 0, b: 0, },
|
||||
* { r: 255, g: 255, b: 255, }
|
||||
* ); // 0.7
|
||||
* ```
|
||||
*/
|
||||
export declare function colorDiff(rgba1: RGBAColor | RGBColor, rgba2: RGBAColor | RGBColor): number;
|
||||
/**
|
||||
* Limits a number to between 0 or 255
|
||||
* @example
|
||||
* ```ts
|
||||
* import { limit255 } from "@jimp/utils";
|
||||
*
|
||||
* limit255(256); // 255
|
||||
* limit255(-1); // 0
|
||||
* ```
|
||||
*/
|
||||
export declare function limit255(n: number): number;
|
||||
/**
|
||||
* Converts a css color (Hex, 8-digit (RGBA) Hex, RGB, RGBA, HSL, HSLA, HSV, HSVA, Named) to a hex number
|
||||
* @returns A hex number representing a color
|
||||
* @example
|
||||
* ```ts
|
||||
* import { cssColorToHex } from "@jimp/utils";
|
||||
*
|
||||
* cssColorToHex("rgba(255, 0, 0, 0.5)"); // "ff000080"
|
||||
* ```
|
||||
*/
|
||||
export declare function cssColorToHex(cssColor: string | number): number;
|
||||
//# sourceMappingURL=index.d.ts.map
|
||||
+1
@@ -0,0 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,EAAE,SAAS,GAAG,QAAQ,EAC3B,KAAK,EAAE,SAAS,GAAG,QAAQ,UAc5B;AAED;;;;;;;;;GASG;AACH,wBAAgB,QAAQ,CAAC,CAAC,EAAE,MAAM,UAKjC;AAED;;;;;;;;;GASG;AACH,wBAAgB,aAAa,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,UAMtD"}
|
||||
+225
@@ -0,0 +1,225 @@
|
||||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.clone = clone;
|
||||
exports.scan = scan;
|
||||
exports.scanIterator = scanIterator;
|
||||
exports.intToRGBA = intToRGBA;
|
||||
exports.rgbaToInt = rgbaToInt;
|
||||
exports.colorDiff = colorDiff;
|
||||
exports.limit255 = limit255;
|
||||
exports.cssColorToHex = cssColorToHex;
|
||||
const tinycolor2_1 = __importDefault(require("tinycolor2"));
|
||||
function clone(image) {
|
||||
const newBitmap = {
|
||||
width: image.bitmap.width,
|
||||
height: image.bitmap.height,
|
||||
data: Buffer.from(image.bitmap.data),
|
||||
};
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
return new image.constructor(newBitmap);
|
||||
}
|
||||
function scan(image,
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
xArg, yArg, wArg, hArg,
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
cbArg) {
|
||||
let x;
|
||||
let y;
|
||||
let w;
|
||||
let h;
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
let cb;
|
||||
if (typeof xArg === "function") {
|
||||
cb = xArg;
|
||||
x = 0;
|
||||
y = 0;
|
||||
w = image.bitmap.width;
|
||||
h = image.bitmap.height;
|
||||
}
|
||||
else {
|
||||
x = xArg;
|
||||
if (typeof yArg !== "number")
|
||||
throw new Error("y must be a number");
|
||||
y = yArg;
|
||||
if (typeof wArg !== "number")
|
||||
throw new Error("w must be a number");
|
||||
w = wArg;
|
||||
if (typeof hArg !== "number")
|
||||
throw new Error("h must be a number");
|
||||
h = hArg;
|
||||
if (typeof cbArg !== "function")
|
||||
throw new Error("cb must be a function");
|
||||
cb = cbArg;
|
||||
}
|
||||
// round input
|
||||
x = Math.round(x);
|
||||
y = Math.round(y);
|
||||
w = Math.round(w);
|
||||
h = Math.round(h);
|
||||
const bound = cb.bind(image);
|
||||
for (let _y = y; _y < y + h; _y++) {
|
||||
for (let _x = x; _x < x + w; _x++) {
|
||||
const idx = (image.bitmap.width * _y + _x) << 2;
|
||||
// Bind the images so this.bitmap works
|
||||
bound(_x, _y, idx);
|
||||
}
|
||||
}
|
||||
return image;
|
||||
}
|
||||
function* scanIterator(image, x, y, w, h) {
|
||||
// round input
|
||||
x = Math.round(x);
|
||||
y = Math.round(y);
|
||||
w = Math.round(w);
|
||||
h = Math.round(h);
|
||||
for (let _y = y; _y < y + h; _y++) {
|
||||
for (let _x = x; _x < x + w; _x++) {
|
||||
const idx = (image.bitmap.width * _y + _x) << 2;
|
||||
yield { x: _x, y: _y, idx, image };
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* A helper method that converts RGBA values to a single integer value
|
||||
* @param i A single integer value representing an RGBA colour (e.g. 0xFF0000FF for red)
|
||||
* @returns An object with the properties r, g, b and a representing RGBA values
|
||||
* @example
|
||||
* ```ts
|
||||
* import { intToRGBA } from "@jimp/utils";
|
||||
*
|
||||
* intToRGBA(0xFF0000FF); // { r: 255, g: 0, b: 0, a:255 }
|
||||
* ```
|
||||
*/
|
||||
function intToRGBA(i) {
|
||||
if (typeof i !== "number") {
|
||||
throw new Error("i must be a number");
|
||||
}
|
||||
const rgba = {
|
||||
r: 0,
|
||||
g: 0,
|
||||
b: 0,
|
||||
a: 0,
|
||||
};
|
||||
rgba.r = Math.floor(i / Math.pow(256, 3));
|
||||
rgba.g = Math.floor((i - rgba.r * Math.pow(256, 3)) / Math.pow(256, 2));
|
||||
rgba.b = Math.floor((i - rgba.r * Math.pow(256, 3) - rgba.g * Math.pow(256, 2)) /
|
||||
Math.pow(256, 1));
|
||||
rgba.a = Math.floor((i -
|
||||
rgba.r * Math.pow(256, 3) -
|
||||
rgba.g * Math.pow(256, 2) -
|
||||
rgba.b * Math.pow(256, 1)) /
|
||||
Math.pow(256, 0));
|
||||
return rgba;
|
||||
}
|
||||
/**
|
||||
* A static helper method that converts RGBA values to a single integer value
|
||||
* @param r the red value (0-255)
|
||||
* @param g the green value (0-255)
|
||||
* @param b the blue value (0-255)
|
||||
* @param a the alpha value (0-255)
|
||||
* @example
|
||||
* ```ts
|
||||
* import { rgbaToInt } from "@jimp/utils";
|
||||
*
|
||||
* rgbaToInt(255, 0, 0, 255); // 0xFF0000FF
|
||||
* ```
|
||||
*/
|
||||
function rgbaToInt(r, g, b, a) {
|
||||
if (typeof r !== "number" ||
|
||||
typeof g !== "number" ||
|
||||
typeof b !== "number" ||
|
||||
typeof a !== "number") {
|
||||
throw new Error("r, g, b and a must be numbers");
|
||||
}
|
||||
if (r < 0 || r > 255) {
|
||||
throw new Error("r must be between 0 and 255");
|
||||
}
|
||||
if (g < 0 || g > 255) {
|
||||
throw new Error("g must be between 0 and 255");
|
||||
}
|
||||
if (b < 0 || b > 255) {
|
||||
throw new Error("b must be between 0 and 255");
|
||||
}
|
||||
if (a < 0 || a > 255) {
|
||||
throw new Error("a must be between 0 and 255");
|
||||
}
|
||||
let i = r & 0xff;
|
||||
i <<= 8;
|
||||
i |= g & 0xff;
|
||||
i <<= 8;
|
||||
i |= b & 0xff;
|
||||
i <<= 8;
|
||||
i |= a & 0xff;
|
||||
// Ensure sign is correct
|
||||
i >>>= 0;
|
||||
return i;
|
||||
}
|
||||
/**
|
||||
* Compute color difference
|
||||
* 0 means no difference, 1 means maximum difference.
|
||||
* Both parameters must be an color object `{ r:val, g:val, b:val, a:val }`
|
||||
* Where `a` is optional and `val` is an integer between 0 and 255.
|
||||
* @param rgba1 first color to compare.
|
||||
* @param rgba2 second color to compare.
|
||||
* @returns float between 0 and 1.
|
||||
* @example
|
||||
* ```ts
|
||||
* import { colorDiff } from "@jimp/utils";
|
||||
*
|
||||
* colorDiff(
|
||||
* { r: 255, g: 0, b: 0, a: 0 },
|
||||
* { r: 0, g: 255, b: 0, a: 0 },
|
||||
* ); // 0.5
|
||||
*
|
||||
* colorDiff(
|
||||
* { r: 0, g: 0, b: 0, },
|
||||
* { r: 255, g: 255, b: 255, }
|
||||
* ); // 0.7
|
||||
* ```
|
||||
*/
|
||||
function colorDiff(rgba1, rgba2) {
|
||||
const sq = (n) => Math.pow(n, 2);
|
||||
const { max } = Math;
|
||||
const maxVal = 255 * 255 * 3;
|
||||
const rgba1A = "a" in rgba1 ? rgba1.a : 255;
|
||||
const rgba2A = "a" in rgba2 ? rgba2.a : 255;
|
||||
return ((max(sq(rgba1.r - rgba2.r), sq(rgba1.r - rgba2.r - rgba1A + rgba2A)) +
|
||||
max(sq(rgba1.g - rgba2.g), sq(rgba1.g - rgba2.g - rgba1A + rgba2A)) +
|
||||
max(sq(rgba1.b - rgba2.b), sq(rgba1.b - rgba2.b - rgba1A + rgba2A))) /
|
||||
maxVal);
|
||||
}
|
||||
/**
|
||||
* Limits a number to between 0 or 255
|
||||
* @example
|
||||
* ```ts
|
||||
* import { limit255 } from "@jimp/utils";
|
||||
*
|
||||
* limit255(256); // 255
|
||||
* limit255(-1); // 0
|
||||
* ```
|
||||
*/
|
||||
function limit255(n) {
|
||||
n = Math.max(n, 0);
|
||||
n = Math.min(n, 255);
|
||||
return n;
|
||||
}
|
||||
/**
|
||||
* Converts a css color (Hex, 8-digit (RGBA) Hex, RGB, RGBA, HSL, HSLA, HSV, HSVA, Named) to a hex number
|
||||
* @returns A hex number representing a color
|
||||
* @example
|
||||
* ```ts
|
||||
* import { cssColorToHex } from "@jimp/utils";
|
||||
*
|
||||
* cssColorToHex("rgba(255, 0, 0, 0.5)"); // "ff000080"
|
||||
* ```
|
||||
*/
|
||||
function cssColorToHex(cssColor) {
|
||||
if (typeof cssColor === "number") {
|
||||
return cssColor;
|
||||
}
|
||||
return parseInt((0, tinycolor2_1.default)(cssColor).toHex8(), 16);
|
||||
}
|
||||
//# sourceMappingURL=index.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;AAGA,sBASC;AAgBD,oBAoDC;AAED,oCAmBC;AAaD,8BA2BC;AAeD,8BAsCC;AAyBD,8BAgBC;AAYD,4BAKC;AAYD,sCAMC;AA7QD,4DAAmC;AAEnC,SAAgB,KAAK,CAAsB,KAAQ;IACjD,MAAM,SAAS,GAAG;QAChB,KAAK,EAAE,KAAK,CAAC,MAAM,CAAC,KAAK;QACzB,MAAM,EAAE,KAAK,CAAC,MAAM,CAAC,MAAM;QAC3B,IAAI,EAAE,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC;KACrC,CAAC;IAEF,8DAA8D;IAC9D,OAAO,IAAK,KAAK,CAAC,WAAmB,CAAC,SAAS,CAAC,CAAC;AACnD,CAAC;AAgBD,SAAgB,IAAI,CAClB,KAAQ;AACR,8DAA8D;AAC9D,IAA2D,EAC3D,IAAa,EACb,IAAa,EACb,IAAa;AACb,8DAA8D;AAC9D,KAAkD;IAElD,IAAI,CAAS,CAAC;IACd,IAAI,CAAS,CAAC;IACd,IAAI,CAAS,CAAC;IACd,IAAI,CAAS,CAAC;IACd,8DAA8D;IAC9D,IAAI,EAA8C,CAAC;IAEnD,IAAI,OAAO,IAAI,KAAK,UAAU,EAAE,CAAC;QAC/B,EAAE,GAAG,IAAI,CAAC;QACV,CAAC,GAAG,CAAC,CAAC;QACN,CAAC,GAAG,CAAC,CAAC;QACN,CAAC,GAAG,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC;QACvB,CAAC,GAAG,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC;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BColor } from "@jimp/types";
|
||||
export declare function clone<I extends JimpClass>(image: I): I;
|
||||
export declare function scan<I extends JimpClass>(image: I, f: (this: I, x: number, y: number, idx: number) => any): I;
|
||||
export declare function scan<I extends {
|
||||
bitmap: Bitmap;
|
||||
}>(image: I, x: number, y: number, w: number, h: number, cb: (x: number, y: number, idx: number) => any): I;
|
||||
export declare function scanIterator<I extends JimpClass>(image: I, x: number, y: number, w: number, h: number): Generator<{
|
||||
x: number;
|
||||
y: number;
|
||||
idx: number;
|
||||
image: I;
|
||||
}, void, unknown>;
|
||||
/**
|
||||
* A helper method that converts RGBA values to a single integer value
|
||||
* @param i A single integer value representing an RGBA colour (e.g. 0xFF0000FF for red)
|
||||
* @returns An object with the properties r, g, b and a representing RGBA values
|
||||
* @example
|
||||
* ```ts
|
||||
* import { intToRGBA } from "@jimp/utils";
|
||||
*
|
||||
* intToRGBA(0xFF0000FF); // { r: 255, g: 0, b: 0, a:255 }
|
||||
* ```
|
||||
*/
|
||||
export declare function intToRGBA(i: number): RGBAColor;
|
||||
/**
|
||||
* A static helper method that converts RGBA values to a single integer value
|
||||
* @param r the red value (0-255)
|
||||
* @param g the green value (0-255)
|
||||
* @param b the blue value (0-255)
|
||||
* @param a the alpha value (0-255)
|
||||
* @example
|
||||
* ```ts
|
||||
* import { rgbaToInt } from "@jimp/utils";
|
||||
*
|
||||
* rgbaToInt(255, 0, 0, 255); // 0xFF0000FF
|
||||
* ```
|
||||
*/
|
||||
export declare function rgbaToInt(r: number, g: number, b: number, a: number): number;
|
||||
/**
|
||||
* Compute color difference
|
||||
* 0 means no difference, 1 means maximum difference.
|
||||
* Both parameters must be an color object `{ r:val, g:val, b:val, a:val }`
|
||||
* Where `a` is optional and `val` is an integer between 0 and 255.
|
||||
* @param rgba1 first color to compare.
|
||||
* @param rgba2 second color to compare.
|
||||
* @returns float between 0 and 1.
|
||||
* @example
|
||||
* ```ts
|
||||
* import { colorDiff } from "@jimp/utils";
|
||||
*
|
||||
* colorDiff(
|
||||
* { r: 255, g: 0, b: 0, a: 0 },
|
||||
* { r: 0, g: 255, b: 0, a: 0 },
|
||||
* ); // 0.5
|
||||
*
|
||||
* colorDiff(
|
||||
* { r: 0, g: 0, b: 0, },
|
||||
* { r: 255, g: 255, b: 255, }
|
||||
* ); // 0.7
|
||||
* ```
|
||||
*/
|
||||
export declare function colorDiff(rgba1: RGBAColor | RGBColor, rgba2: RGBAColor | RGBColor): number;
|
||||
/**
|
||||
* Limits a number to between 0 or 255
|
||||
* @example
|
||||
* ```ts
|
||||
* import { limit255 } from "@jimp/utils";
|
||||
*
|
||||
* limit255(256); // 255
|
||||
* limit255(-1); // 0
|
||||
* ```
|
||||
*/
|
||||
export declare function limit255(n: number): number;
|
||||
/**
|
||||
* Converts a css color (Hex, 8-digit (RGBA) Hex, RGB, RGBA, HSL, HSLA, HSV, HSVA, Named) to a hex number
|
||||
* @returns A hex number representing a color
|
||||
* @example
|
||||
* ```ts
|
||||
* import { cssColorToHex } from "@jimp/utils";
|
||||
*
|
||||
* cssColorToHex("rgba(255, 0, 0, 0.5)"); // "ff000080"
|
||||
* ```
|
||||
*/
|
||||
export declare function cssColorToHex(cssColor: string | number): number;
|
||||
//# sourceMappingURL=index.d.ts.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAGrE,wBAAgB,KAAK,CAAC,CAAC,SAAS,SAAS,EAAE,KAAK,EAAE,CAAC,GAAG,CAAC,CAStD;AAED,wBAAgB,IAAI,CAAC,CAAC,SAAS,SAAS,EACtC,KAAK,EAAE,CAAC,EAER,CAAC,EAAE,CAAC,IAAI,EAAE,CAAC,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,KAAK,GAAG,GACrD,CAAC,CAAC;AACL,wBAAgB,IAAI,CAAC,CAAC,SAAS;IAAE,MAAM,EAAE,MAAM,CAAA;CAAE,EAC/C,KAAK,EAAE,CAAC,EACR,CAAC,EAAE,MAAM,EACT,CAAC,EAAE,MAAM,EACT,CAAC,EAAE,MAAM,EACT,CAAC,EAAE,MAAM,EAET,EAAE,EAAE,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,KAAK,GAAG,GAC7C,CAAC,CAAC;AAuDL,wBAAiB,YAAY,CAAC,CAAC,SAAS,SAAS,EAC/C,KAAK,EAAE,CAAC,EACR,CAAC,EAAE,MAAM,EACT,CAAC,EAAE,MAAM,EACT,CAAC,EAAE,MAAM,EACT,CAAC,EAAE,MAAM;;;;;kBAcV;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,MAAM,aA2BlC;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,UAsCnE;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,SAAS,CACvB,KAAK,EAAE,SAAS,GAAG,QAAQ,EAC3B,KAAK,EAAE,SAAS,GAAG,QAAQ,UAc5B;AAED;;;;;;;;;GASG;AACH,wBAAgB,QAAQ,CAAC,CAAC,EAAE,MAAM,UAKjC;AAED;;;;;;;;;GASG;AACH,wBAAgB,aAAa,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,UAMtD"}
|
||||
+212
@@ -0,0 +1,212 @@
|
||||
import tinyColor from "tinycolor2";
|
||||
export function clone(image) {
|
||||
const newBitmap = {
|
||||
width: image.bitmap.width,
|
||||
height: image.bitmap.height,
|
||||
data: Buffer.from(image.bitmap.data),
|
||||
};
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
return new image.constructor(newBitmap);
|
||||
}
|
||||
export function scan(image,
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
xArg, yArg, wArg, hArg,
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
cbArg) {
|
||||
let x;
|
||||
let y;
|
||||
let w;
|
||||
let h;
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
let cb;
|
||||
if (typeof xArg === "function") {
|
||||
cb = xArg;
|
||||
x = 0;
|
||||
y = 0;
|
||||
w = image.bitmap.width;
|
||||
h = image.bitmap.height;
|
||||
}
|
||||
else {
|
||||
x = xArg;
|
||||
if (typeof yArg !== "number")
|
||||
throw new Error("y must be a number");
|
||||
y = yArg;
|
||||
if (typeof wArg !== "number")
|
||||
throw new Error("w must be a number");
|
||||
w = wArg;
|
||||
if (typeof hArg !== "number")
|
||||
throw new Error("h must be a number");
|
||||
h = hArg;
|
||||
if (typeof cbArg !== "function")
|
||||
throw new Error("cb must be a function");
|
||||
cb = cbArg;
|
||||
}
|
||||
// round input
|
||||
x = Math.round(x);
|
||||
y = Math.round(y);
|
||||
w = Math.round(w);
|
||||
h = Math.round(h);
|
||||
const bound = cb.bind(image);
|
||||
for (let _y = y; _y < y + h; _y++) {
|
||||
for (let _x = x; _x < x + w; _x++) {
|
||||
const idx = (image.bitmap.width * _y + _x) << 2;
|
||||
// Bind the images so this.bitmap works
|
||||
bound(_x, _y, idx);
|
||||
}
|
||||
}
|
||||
return image;
|
||||
}
|
||||
export function* scanIterator(image, x, y, w, h) {
|
||||
// round input
|
||||
x = Math.round(x);
|
||||
y = Math.round(y);
|
||||
w = Math.round(w);
|
||||
h = Math.round(h);
|
||||
for (let _y = y; _y < y + h; _y++) {
|
||||
for (let _x = x; _x < x + w; _x++) {
|
||||
const idx = (image.bitmap.width * _y + _x) << 2;
|
||||
yield { x: _x, y: _y, idx, image };
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* A helper method that converts RGBA values to a single integer value
|
||||
* @param i A single integer value representing an RGBA colour (e.g. 0xFF0000FF for red)
|
||||
* @returns An object with the properties r, g, b and a representing RGBA values
|
||||
* @example
|
||||
* ```ts
|
||||
* import { intToRGBA } from "@jimp/utils";
|
||||
*
|
||||
* intToRGBA(0xFF0000FF); // { r: 255, g: 0, b: 0, a:255 }
|
||||
* ```
|
||||
*/
|
||||
export function intToRGBA(i) {
|
||||
if (typeof i !== "number") {
|
||||
throw new Error("i must be a number");
|
||||
}
|
||||
const rgba = {
|
||||
r: 0,
|
||||
g: 0,
|
||||
b: 0,
|
||||
a: 0,
|
||||
};
|
||||
rgba.r = Math.floor(i / Math.pow(256, 3));
|
||||
rgba.g = Math.floor((i - rgba.r * Math.pow(256, 3)) / Math.pow(256, 2));
|
||||
rgba.b = Math.floor((i - rgba.r * Math.pow(256, 3) - rgba.g * Math.pow(256, 2)) /
|
||||
Math.pow(256, 1));
|
||||
rgba.a = Math.floor((i -
|
||||
rgba.r * Math.pow(256, 3) -
|
||||
rgba.g * Math.pow(256, 2) -
|
||||
rgba.b * Math.pow(256, 1)) /
|
||||
Math.pow(256, 0));
|
||||
return rgba;
|
||||
}
|
||||
/**
|
||||
* A static helper method that converts RGBA values to a single integer value
|
||||
* @param r the red value (0-255)
|
||||
* @param g the green value (0-255)
|
||||
* @param b the blue value (0-255)
|
||||
* @param a the alpha value (0-255)
|
||||
* @example
|
||||
* ```ts
|
||||
* import { rgbaToInt } from "@jimp/utils";
|
||||
*
|
||||
* rgbaToInt(255, 0, 0, 255); // 0xFF0000FF
|
||||
* ```
|
||||
*/
|
||||
export function rgbaToInt(r, g, b, a) {
|
||||
if (typeof r !== "number" ||
|
||||
typeof g !== "number" ||
|
||||
typeof b !== "number" ||
|
||||
typeof a !== "number") {
|
||||
throw new Error("r, g, b and a must be numbers");
|
||||
}
|
||||
if (r < 0 || r > 255) {
|
||||
throw new Error("r must be between 0 and 255");
|
||||
}
|
||||
if (g < 0 || g > 255) {
|
||||
throw new Error("g must be between 0 and 255");
|
||||
}
|
||||
if (b < 0 || b > 255) {
|
||||
throw new Error("b must be between 0 and 255");
|
||||
}
|
||||
if (a < 0 || a > 255) {
|
||||
throw new Error("a must be between 0 and 255");
|
||||
}
|
||||
let i = r & 0xff;
|
||||
i <<= 8;
|
||||
i |= g & 0xff;
|
||||
i <<= 8;
|
||||
i |= b & 0xff;
|
||||
i <<= 8;
|
||||
i |= a & 0xff;
|
||||
// Ensure sign is correct
|
||||
i >>>= 0;
|
||||
return i;
|
||||
}
|
||||
/**
|
||||
* Compute color difference
|
||||
* 0 means no difference, 1 means maximum difference.
|
||||
* Both parameters must be an color object `{ r:val, g:val, b:val, a:val }`
|
||||
* Where `a` is optional and `val` is an integer between 0 and 255.
|
||||
* @param rgba1 first color to compare.
|
||||
* @param rgba2 second color to compare.
|
||||
* @returns float between 0 and 1.
|
||||
* @example
|
||||
* ```ts
|
||||
* import { colorDiff } from "@jimp/utils";
|
||||
*
|
||||
* colorDiff(
|
||||
* { r: 255, g: 0, b: 0, a: 0 },
|
||||
* { r: 0, g: 255, b: 0, a: 0 },
|
||||
* ); // 0.5
|
||||
*
|
||||
* colorDiff(
|
||||
* { r: 0, g: 0, b: 0, },
|
||||
* { r: 255, g: 255, b: 255, }
|
||||
* ); // 0.7
|
||||
* ```
|
||||
*/
|
||||
export function colorDiff(rgba1, rgba2) {
|
||||
const sq = (n) => Math.pow(n, 2);
|
||||
const { max } = Math;
|
||||
const maxVal = 255 * 255 * 3;
|
||||
const rgba1A = "a" in rgba1 ? rgba1.a : 255;
|
||||
const rgba2A = "a" in rgba2 ? rgba2.a : 255;
|
||||
return ((max(sq(rgba1.r - rgba2.r), sq(rgba1.r - rgba2.r - rgba1A + rgba2A)) +
|
||||
max(sq(rgba1.g - rgba2.g), sq(rgba1.g - rgba2.g - rgba1A + rgba2A)) +
|
||||
max(sq(rgba1.b - rgba2.b), sq(rgba1.b - rgba2.b - rgba1A + rgba2A))) /
|
||||
maxVal);
|
||||
}
|
||||
/**
|
||||
* Limits a number to between 0 or 255
|
||||
* @example
|
||||
* ```ts
|
||||
* import { limit255 } from "@jimp/utils";
|
||||
*
|
||||
* limit255(256); // 255
|
||||
* limit255(-1); // 0
|
||||
* ```
|
||||
*/
|
||||
export function limit255(n) {
|
||||
n = Math.max(n, 0);
|
||||
n = Math.min(n, 255);
|
||||
return n;
|
||||
}
|
||||
/**
|
||||
* Converts a css color (Hex, 8-digit (RGBA) Hex, RGB, RGBA, HSL, HSLA, HSV, HSVA, Named) to a hex number
|
||||
* @returns A hex number representing a color
|
||||
* @example
|
||||
* ```ts
|
||||
* import { cssColorToHex } from "@jimp/utils";
|
||||
*
|
||||
* cssColorToHex("rgba(255, 0, 0, 0.5)"); // "ff000080"
|
||||
* ```
|
||||
*/
|
||||
export function cssColorToHex(cssColor) {
|
||||
if (typeof cssColor === "number") {
|
||||
return cssColor;
|
||||
}
|
||||
return parseInt(tinyColor(cssColor).toHex8(), 16);
|
||||
}
|
||||
//# sourceMappingURL=index.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AACA,OAAO,SAAS,MAAM,YAAY,CAAC;AAEnC,MAAM,UAAU,KAAK,CAAsB,KAAQ;IACjD,MAAM,SAAS,GAAG;QAChB,KAAK,EAAE,KAAK,CAAC,MAAM,CAAC,KAAK;QACzB,MAAM,EAAE,KAAK,CAAC,MAAM,CAAC,MAAM;QAC3B,IAAI,EAAE,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC;KACrC,CAAC;IAEF,8DAA8D;IAC9D,OAAO,IAAK,KAAK,CAAC,WAAmB,CAAC,SAAS,CAAC,CAAC;AACnD,CAAC;AAgBD,MAAM,UAAU,IAAI,CAClB,KAAQ;AACR,8DAA8D;AAC9D,IAA2D,EAC3D,IAAa,EACb,IAAa,EACb,IAAa;AACb,8DAA8D;AAC9D,KAAkD;IAElD,IAAI,CAAS,CAAC;I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;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,UAAU,SAAS,CACvB,KAA2B,EAC3B,KAA2B;IAE3B,MAAM,EAAE,GAAG,CAAC,CAAS,EAAE,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;IACzC,MAAM,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;IACrB,MAAM,MAAM,GAAG,GAAG,GAAG,GAAG,GAAG,CAAC,CAAC;IAC7B,MALine truncated
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"type": "module"
|
||||
}
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
import shared from "@jimp/config-eslint/base.js";
|
||||
export default [...shared];
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
{
|
||||
"name": "@jimp/utils",
|
||||
"version": "1.6.0",
|
||||
"repository": "jimp-dev/jimp",
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
},
|
||||
"scripts": {
|
||||
"lint": "eslint .",
|
||||
"test": "vitest",
|
||||
"build": "tshy",
|
||||
"dev": "tshy --watch",
|
||||
"clean": "rm -rf node_modules .tshy .tshy-build dist .turbo"
|
||||
},
|
||||
"author": "Andrew Lisowski <lisowski54@gmail.com>",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@jimp/types": "1.6.0",
|
||||
"tinycolor2": "^1.6.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@jimp/config-eslint": "1.6.0",
|
||||
"@jimp/config-typescript": "1.6.0",
|
||||
"@types/node": "^18.19.48",
|
||||
"@types/tinycolor2": "^1.4.6",
|
||||
"eslint": "^9.9.1",
|
||||
"tshy": "^3.0.2",
|
||||
"typescript": "^5.5.4",
|
||||
"vite-plugin-node-polyfills": "^0.22.0",
|
||||
"vitest": "^2.0.5"
|
||||
},
|
||||
"tshy": {
|
||||
"exclude": [
|
||||
"**/*.test.ts"
|
||||
],
|
||||
"exports": {
|
||||
"./package.json": "./package.json",
|
||||
".": "./src/index.ts"
|
||||
}
|
||||
},
|
||||
"exports": {
|
||||
"./package.json": "./package.json",
|
||||
".": {
|
||||
"import": {
|
||||
"types": "./dist/esm/index.d.ts",
|
||||
"default": "./dist/esm/index.js"
|
||||
},
|
||||
"require": {
|
||||
"types": "./dist/commonjs/index.d.ts",
|
||||
"default": "./dist/commonjs/index.js"
|
||||
}
|
||||
}
|
||||
},
|
||||
"main": "./dist/commonjs/index.js",
|
||||
"types": "./dist/commonjs/index.d.ts",
|
||||
"type": "module",
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
},
|
||||
"sideEffects": false,
|
||||
"module": "./dist/esm/index.js",
|
||||
"gitHead": "c88abe6046dccbdb6e4f5f00c3dd403c81d83515"
|
||||
}
|
||||
+82
@@ -0,0 +1,82 @@
|
||||
import { expect, test, describe } from "vitest";
|
||||
|
||||
import { colorDiff, scan } from "./index.js";
|
||||
import { JimpClass } from "@jimp/types";
|
||||
|
||||
// Convert [0..1] float to a percent value with only one decimal.
|
||||
const pct = (n: number) => ((n * 1000) << 0) / 10;
|
||||
|
||||
describe("colorDiff", () => {
|
||||
test("totally opaque (no alpha defined)", () => {
|
||||
expect(colorDiff({ r: 255, g: 0, b: 0 }, { r: 255, g: 0, b: 0 })).toEqual(
|
||||
0
|
||||
);
|
||||
|
||||
expect(
|
||||
pct(colorDiff({ r: 255, g: 0, b: 0 }, { r: 0, g: 0, b: 0 }))
|
||||
).toEqual(33.3);
|
||||
|
||||
expect(
|
||||
pct(colorDiff({ r: 255, g: 0, b: 0 }, { r: 0, g: 255, b: 0 }))
|
||||
).toEqual(66.6);
|
||||
|
||||
expect(colorDiff({ r: 255, g: 0, b: 0 }, { r: 0, g: 255, b: 255 })).toEqual(
|
||||
1
|
||||
);
|
||||
|
||||
expect(colorDiff({ r: 0, g: 0, b: 0 }, { r: 255, g: 255, b: 255 })).toEqual(
|
||||
1
|
||||
);
|
||||
});
|
||||
|
||||
test("totally transparent", () => {
|
||||
expect(
|
||||
colorDiff({ r: 255, g: 0, b: 0, a: 0 }, { r: 255, g: 0, b: 0, a: 0 })
|
||||
).toEqual(0);
|
||||
|
||||
expect(
|
||||
colorDiff({ r: 0, g: 0, b: 0, a: 0 }, { r: 255, g: 255, b: 255, a: 0 })
|
||||
).toEqual(1);
|
||||
});
|
||||
|
||||
test("different alpha", () => {
|
||||
expect(
|
||||
pct(
|
||||
colorDiff(
|
||||
{ r: 255, g: 0, b: 0, a: 100 },
|
||||
{ r: 255, g: 0, b: 0, a: 150 }
|
||||
)
|
||||
)
|
||||
).toEqual(3.8);
|
||||
|
||||
expect(
|
||||
colorDiff({ r: 0, g: 0, b: 0, a: 0 }, { r: 255, g: 255, b: 255, a: 255 })
|
||||
).toEqual(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe("scan", () => {
|
||||
class TestImage {
|
||||
public bitmap = {
|
||||
height: 3,
|
||||
width: 3,
|
||||
data: Buffer.from([
|
||||
0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00,
|
||||
0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00,
|
||||
]),
|
||||
};
|
||||
}
|
||||
|
||||
const img = new TestImage() as JimpClass;
|
||||
|
||||
test("scan", () => {
|
||||
let count = 0;
|
||||
|
||||
scan(img, function (_, __, idx) {
|
||||
count++;
|
||||
expect(img.bitmap.data[idx]).toBe(this.bitmap.data[idx]);
|
||||
});
|
||||
|
||||
expect(count).toBe(img.bitmap.width * img.bitmap.height);
|
||||
});
|
||||
});
|
||||
+271
@@ -0,0 +1,271 @@
|
||||
import { RGBAColor, JimpClass, Bitmap, RGBColor } from "@jimp/types";
|
||||
import tinyColor from "tinycolor2";
|
||||
|
||||
export function clone<I extends JimpClass>(image: I): I {
|
||||
const newBitmap = {
|
||||
width: image.bitmap.width,
|
||||
height: image.bitmap.height,
|
||||
data: Buffer.from(image.bitmap.data),
|
||||
};
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
return new (image.constructor as any)(newBitmap);
|
||||
}
|
||||
|
||||
export function scan<I extends JimpClass>(
|
||||
image: I,
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
f: (this: I, x: number, y: number, idx: number) => any
|
||||
): I;
|
||||
export function scan<I extends { bitmap: Bitmap }>(
|
||||
image: I,
|
||||
x: number,
|
||||
y: number,
|
||||
w: number,
|
||||
h: number,
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
cb: (x: number, y: number, idx: number) => any
|
||||
): I;
|
||||
export function scan<I extends { bitmap: Bitmap }>(
|
||||
image: I,
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
xArg: number | ((x: number, y: number, idx: number) => any),
|
||||
yArg?: number,
|
||||
wArg?: number,
|
||||
hArg?: number,
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
cbArg?: (x: number, y: number, idx: number) => any
|
||||
): I {
|
||||
let x: number;
|
||||
let y: number;
|
||||
let w: number;
|
||||
let h: number;
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
let cb: (x: number, y: number, idx: number) => any;
|
||||
|
||||
if (typeof xArg === "function") {
|
||||
cb = xArg;
|
||||
x = 0;
|
||||
y = 0;
|
||||
w = image.bitmap.width;
|
||||
h = image.bitmap.height;
|
||||
} else {
|
||||
x = xArg;
|
||||
if (typeof yArg !== "number") throw new Error("y must be a number");
|
||||
y = yArg;
|
||||
if (typeof wArg !== "number") throw new Error("w must be a number");
|
||||
w = wArg;
|
||||
if (typeof hArg !== "number") throw new Error("h must be a number");
|
||||
h = hArg;
|
||||
if (typeof cbArg !== "function") throw new Error("cb must be a function");
|
||||
cb = cbArg;
|
||||
}
|
||||
|
||||
// round input
|
||||
x = Math.round(x);
|
||||
y = Math.round(y);
|
||||
w = Math.round(w);
|
||||
h = Math.round(h);
|
||||
|
||||
const bound = cb.bind(image);
|
||||
|
||||
for (let _y = y; _y < y + h; _y++) {
|
||||
for (let _x = x; _x < x + w; _x++) {
|
||||
const idx = (image.bitmap.width * _y + _x) << 2;
|
||||
// Bind the images so this.bitmap works
|
||||
bound(_x, _y, idx);
|
||||
}
|
||||
}
|
||||
|
||||
return image;
|
||||
}
|
||||
|
||||
export function* scanIterator<I extends JimpClass>(
|
||||
image: I,
|
||||
x: number,
|
||||
y: number,
|
||||
w: number,
|
||||
h: number
|
||||
) {
|
||||
// round input
|
||||
x = Math.round(x);
|
||||
y = Math.round(y);
|
||||
w = Math.round(w);
|
||||
h = Math.round(h);
|
||||
|
||||
for (let _y = y; _y < y + h; _y++) {
|
||||
for (let _x = x; _x < x + w; _x++) {
|
||||
const idx = (image.bitmap.width * _y + _x) << 2;
|
||||
yield { x: _x, y: _y, idx, image };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A helper method that converts RGBA values to a single integer value
|
||||
* @param i A single integer value representing an RGBA colour (e.g. 0xFF0000FF for red)
|
||||
* @returns An object with the properties r, g, b and a representing RGBA values
|
||||
* @example
|
||||
* ```ts
|
||||
* import { intToRGBA } from "@jimp/utils";
|
||||
*
|
||||
* intToRGBA(0xFF0000FF); // { r: 255, g: 0, b: 0, a:255 }
|
||||
* ```
|
||||
*/
|
||||
export function intToRGBA(i: number) {
|
||||
if (typeof i !== "number") {
|
||||
throw new Error("i must be a number");
|
||||
}
|
||||
|
||||
const rgba: RGBAColor = {
|
||||
r: 0,
|
||||
g: 0,
|
||||
b: 0,
|
||||
a: 0,
|
||||
};
|
||||
|
||||
rgba.r = Math.floor(i / Math.pow(256, 3));
|
||||
rgba.g = Math.floor((i - rgba.r * Math.pow(256, 3)) / Math.pow(256, 2));
|
||||
rgba.b = Math.floor(
|
||||
(i - rgba.r * Math.pow(256, 3) - rgba.g * Math.pow(256, 2)) /
|
||||
Math.pow(256, 1)
|
||||
);
|
||||
rgba.a = Math.floor(
|
||||
(i -
|
||||
rgba.r * Math.pow(256, 3) -
|
||||
rgba.g * Math.pow(256, 2) -
|
||||
rgba.b * Math.pow(256, 1)) /
|
||||
Math.pow(256, 0)
|
||||
);
|
||||
|
||||
return rgba;
|
||||
}
|
||||
|
||||
/**
|
||||
* A static helper method that converts RGBA values to a single integer value
|
||||
* @param r the red value (0-255)
|
||||
* @param g the green value (0-255)
|
||||
* @param b the blue value (0-255)
|
||||
* @param a the alpha value (0-255)
|
||||
* @example
|
||||
* ```ts
|
||||
* import { rgbaToInt } from "@jimp/utils";
|
||||
*
|
||||
* rgbaToInt(255, 0, 0, 255); // 0xFF0000FF
|
||||
* ```
|
||||
*/
|
||||
export function rgbaToInt(r: number, g: number, b: number, a: number) {
|
||||
if (
|
||||
typeof r !== "number" ||
|
||||
typeof g !== "number" ||
|
||||
typeof b !== "number" ||
|
||||
typeof a !== "number"
|
||||
) {
|
||||
throw new Error("r, g, b and a must be numbers");
|
||||
}
|
||||
|
||||
if (r < 0 || r > 255) {
|
||||
throw new Error("r must be between 0 and 255");
|
||||
}
|
||||
|
||||
if (g < 0 || g > 255) {
|
||||
throw new Error("g must be between 0 and 255");
|
||||
}
|
||||
|
||||
if (b < 0 || b > 255) {
|
||||
throw new Error("b must be between 0 and 255");
|
||||
}
|
||||
|
||||
if (a < 0 || a > 255) {
|
||||
throw new Error("a must be between 0 and 255");
|
||||
}
|
||||
|
||||
let i = r & 0xff;
|
||||
i <<= 8;
|
||||
i |= g & 0xff;
|
||||
i <<= 8;
|
||||
i |= b & 0xff;
|
||||
i <<= 8;
|
||||
i |= a & 0xff;
|
||||
|
||||
// Ensure sign is correct
|
||||
i >>>= 0;
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute color difference
|
||||
* 0 means no difference, 1 means maximum difference.
|
||||
* Both parameters must be an color object `{ r:val, g:val, b:val, a:val }`
|
||||
* Where `a` is optional and `val` is an integer between 0 and 255.
|
||||
* @param rgba1 first color to compare.
|
||||
* @param rgba2 second color to compare.
|
||||
* @returns float between 0 and 1.
|
||||
* @example
|
||||
* ```ts
|
||||
* import { colorDiff } from "@jimp/utils";
|
||||
*
|
||||
* colorDiff(
|
||||
* { r: 255, g: 0, b: 0, a: 0 },
|
||||
* { r: 0, g: 255, b: 0, a: 0 },
|
||||
* ); // 0.5
|
||||
*
|
||||
* colorDiff(
|
||||
* { r: 0, g: 0, b: 0, },
|
||||
* { r: 255, g: 255, b: 255, }
|
||||
* ); // 0.7
|
||||
* ```
|
||||
*/
|
||||
export function colorDiff(
|
||||
rgba1: RGBAColor | RGBColor,
|
||||
rgba2: RGBAColor | RGBColor
|
||||
) {
|
||||
const sq = (n: number) => Math.pow(n, 2);
|
||||
const { max } = Math;
|
||||
const maxVal = 255 * 255 * 3;
|
||||
const rgba1A = "a" in rgba1 ? rgba1.a : 255;
|
||||
const rgba2A = "a" in rgba2 ? rgba2.a : 255;
|
||||
|
||||
return (
|
||||
(max(sq(rgba1.r - rgba2.r), sq(rgba1.r - rgba2.r - rgba1A + rgba2A)) +
|
||||
max(sq(rgba1.g - rgba2.g), sq(rgba1.g - rgba2.g - rgba1A + rgba2A)) +
|
||||
max(sq(rgba1.b - rgba2.b), sq(rgba1.b - rgba2.b - rgba1A + rgba2A))) /
|
||||
maxVal
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Limits a number to between 0 or 255
|
||||
* @example
|
||||
* ```ts
|
||||
* import { limit255 } from "@jimp/utils";
|
||||
*
|
||||
* limit255(256); // 255
|
||||
* limit255(-1); // 0
|
||||
* ```
|
||||
*/
|
||||
export function limit255(n: number) {
|
||||
n = Math.max(n, 0);
|
||||
n = Math.min(n, 255);
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a css color (Hex, 8-digit (RGBA) Hex, RGB, RGBA, HSL, HSLA, HSV, HSVA, Named) to a hex number
|
||||
* @returns A hex number representing a color
|
||||
* @example
|
||||
* ```ts
|
||||
* import { cssColorToHex } from "@jimp/utils";
|
||||
*
|
||||
* cssColorToHex("rgba(255, 0, 0, 0.5)"); // "ff000080"
|
||||
* ```
|
||||
*/
|
||||
export function cssColorToHex(cssColor: string | number) {
|
||||
if (typeof cssColor === "number") {
|
||||
return cssColor;
|
||||
}
|
||||
|
||||
return parseInt(tinyColor(cssColor).toHex8(), 16);
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"extends": "@jimp/config-typescript/base.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "dist"
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user