From 776caac7f58bd3e1978c6fe38d06d45de476f565 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20B=C5=82a=C5=BCejewicz=20=28Peter=20Blazejewicz=29?= Date: Sat, 11 Apr 2020 02:08:56 +0200 Subject: [PATCH] feat(png-img): type definition for v2.3 (#43750) - declaration file - tests https://github.com/gemini-testing/png-img#api Thanks! --- types/png-img/index.d.ts | 83 ++++++++++++++++++++++++++++++++++ types/png-img/png-img-tests.ts | 20 ++++++++ types/png-img/tsconfig.json | 23 ++++++++++ types/png-img/tslint.json | 1 + 4 files changed, 127 insertions(+) create mode 100644 types/png-img/index.d.ts create mode 100644 types/png-img/png-img-tests.ts create mode 100644 types/png-img/tsconfig.json create mode 100644 types/png-img/tslint.json diff --git a/types/png-img/index.d.ts b/types/png-img/index.d.ts new file mode 100644 index 0000000000..60f1698765 --- /dev/null +++ b/types/png-img/index.d.ts @@ -0,0 +1,83 @@ +// Type definitions for png-img 2.3 +// Project: https://github.com/gemini-testing/png-img +// Definitions by: Piotr Błażejewicz +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// + +declare class PngImg { + /** + * Create PngImg object from passed buffer with image + */ + constructor(buffer: Buffer); + + /** + * Get image size as an object. + */ + size(): Size; + + /** + * Get pixel color and alpha. + */ + get(x: number, y: number): Color; + + /** + * Fill region with passed color. Modifies current image. + * Color can be {r,g,b,a} object or as a '#XXXXXX' string + */ + fill(offsetX: number, offsetY: number, width: number, height: number, color: Color | string): this; + + /** + * Same as fill(x, y, 1, 1, color) + * (shorthand) + */ + set(x: number, y: number, color: Color | string): this; + + /** + * Crop image. Modifies current image. + * Throws if new image is not inside the current image. + */ + crop(offsetX: number, offsetY: number, width: number, height: number): this; + + /** + * Sets new image size. Modifies current image. + * If new size is less or equal than current size, than crop will be performed. + */ + setSize(width: number, height: number): this; + + /** + * Inserts image into specified place. + */ + insert(img: PngImg, offsetX: number, offsetY: number): this; + + /** + * Rotates image 90 degrees clockwise + */ + rotateRight(): this; + + /** + * Rotates image 90 degress counterclockwise + */ + rotateLeft(): this; + + /** + * Save image to file. Asynchronous operation. + * @param file - path to file to save image + * @param callback - will be called after save operation finish or on error + */ + save(file: string, callback: (error: Error) => void): void; +} + +interface Size { + width: number; + height: number; +} + +interface Color { + r: number; + g: number; + b: number; + a: number; +} + +export = PngImg; diff --git a/types/png-img/png-img-tests.ts b/types/png-img/png-img-tests.ts new file mode 100644 index 0000000000..64f00c0564 --- /dev/null +++ b/types/png-img/png-img-tests.ts @@ -0,0 +1,20 @@ +import fs = require('fs'); +import PngImg = require('png-img'); + +const buf = fs.readFileSync('path/to/img.png'); +const img = new PngImg(buf); +const otherImg = new PngImg(buf); +img.size(); // $ExpectType Size +img.get(0, 0); // $ExpectType Color +img.fill(0, 0, 16, 16, '#00ffFF') // fill with cyan + .fill(16, 16, 16, 16, { r: 0, g: 255, b: 255, a: 127 }); +img.crop(0, 0, 16, 16).crop(8, 8, 8, 8); +img.setSize(img.size().width, img.size().height + otherImg.size().height).insert(otherImg, 0, img.size().height); + +img.save('path/to/file.png', error => { + if (error) { + console.error('Error:', error); + } else { + console.log('OK'); + } +}); diff --git a/types/png-img/tsconfig.json b/types/png-img/tsconfig.json new file mode 100644 index 0000000000..7635a72d8d --- /dev/null +++ b/types/png-img/tsconfig.json @@ -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", + "png-img-tests.ts" + ] +} diff --git a/types/png-img/tslint.json b/types/png-img/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/png-img/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }