Add types for quantize (#65669)

This commit is contained in:
matiand
2023-06-06 02:12:45 +02:00
committed by GitHub
parent 09239fd9de
commit fa492f10c7
4 changed files with 123 additions and 0 deletions

62
types/quantize/index.d.ts vendored Normal file
View File

@@ -0,0 +1,62 @@
// Type definitions for quantize 1.0
// Project: https://github.com/olivierlesnicki/quantize
// Definitions by: matiand <https://github.com/matiand>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
declare namespace quantize {
type RgbPixel = [number, number, number];
interface PriorityQueue {
debug: () => ColorObject[];
map: <U>(callback: (item: ColorObject, index: number) => U) => U[];
peek: (index?: number) => ColorObject;
pop: () => ColorObject;
push: (item: ColorObject) => void;
size: () => number;
}
interface VBox {
r1: number;
r2: number;
g1: number;
g2: number;
b1: number;
b2: number;
histo: number[];
avg: (recalculate?: boolean) => RgbPixel;
contains: (pixel: RgbPixel) => boolean;
copy: () => VBox;
count: () => number;
volume: (recalculate?: boolean) => number;
}
interface ColorObject {
color: RgbPixel;
vbox: VBox;
}
interface ColorMap {
vboxes: PriorityQueue;
/**
* Maps the pixel from source image to the closest palette color
*/
map: (pixel: RgbPixel) => RgbPixel;
nearest: (pixel: RgbPixel) => RgbPixel;
/**
* Returns the palette as an array of RgbPixel
* @returns RgbPixel[][]
*/
palette: () => RgbPixel[];
push: (vbox: VBox) => void;
/**
* Returns the size of the palette
*/
size: () => number;
}
}
declare function quantize(pixels: quantize.RgbPixel[], colorCount: number): quantize.ColorMap | false;
export = quantize;

View File

@@ -0,0 +1,37 @@
import quantize = require('quantize');
// $ExpectType false | ColorMap
const colorMap = quantize(
[
[255, 0, 0],
[0, 255, 0],
[0, 0, 255],
],
2,
);
if (colorMap !== false) {
// $ExpectType RgbPixel[]
colorMap.palette();
// $ExpectType number
colorMap.size();
// $ExpectType RgbPixel
colorMap.map([100, 100, 100]);
// $ExpectType RgbPixel
colorMap.nearest([200, 100, 0]);
// $ExpectType void
colorMap.push(colorMap.vboxes.peek().vbox);
}
// @ts-expect-error
quantize([[255, 0]], 2);
// @ts-expect-error
quantize([[255, 0, 0]]);
// @ts-expect-error
quantize([[255, 0, 0]], '2');

View File

@@ -0,0 +1,23 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"quantize-tests.ts"
]
}

View File

@@ -0,0 +1 @@
{ "extends": "@definitelytyped/dtslint/dt.json" }