diff --git a/types/identicon.js/identicon.js-tests.ts b/types/identicon.js/identicon.js-tests.ts new file mode 100644 index 0000000000..4c233912c7 --- /dev/null +++ b/types/identicon.js/identicon.js-tests.ts @@ -0,0 +1,16 @@ +import Identicon, { Svg } from "identicon.js"; + +// create an identicon with only a hash and a size +new Identicon("d3b07384d113edec49eaa6238ad5ff00", 420).toString(); + +// create an identicon with a hash and other options and get the svg +const svg = new Identicon("d3b07384d113edec49eaa6238ad5ff00", { + background: [255, 255, 255, 255], + foreground: [0, 0, 0, 0], + margin: 0.05, + size: 64, + format: "svg", +}).image() as Svg; +svg.getDump(); +// or get the base64 encoded svg +svg.getBase64(); diff --git a/types/identicon.js/index.d.ts b/types/identicon.js/index.d.ts new file mode 100644 index 0000000000..499a1f1f5c --- /dev/null +++ b/types/identicon.js/index.d.ts @@ -0,0 +1,129 @@ +// Type definitions for identicon.js 2.3 +// Project: https://github.com/stewartlord/identicon.js +// Definitions by: D0miH +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +export type Color = [number, number, number, number]; + +export interface IdenticonOptions { + background?: Color; + foreground?: Color; + margin?: number; + size?: number; + format?: "svg" | "png"; +} + +export interface PNGlib { + width: number; + height: number; + depth: number; + + /** + * Returns the index of a given pixel in the image data array. + * @param x The given x coordinate of the pixel. + * @param y The given y coordinate of the pixel. + */ + index(x: number, y: number): number; + + /** + * Returns the image as a base64 encoded string. + */ + getBase64(): string; + + /** + * Returns the png as a string. + */ + getDump(): string; +} + +export interface Svg { + size: number; + foreground: Color; + background: Color; + rectangles: [ + { + x: number; + y: number; + width: number; + height: number; + color: Color; + } + ]; + + /** + * Returns a string with the structure 'rgb(r, g, b, a)'. + * @param red + * @param green + * @param blue + * @param alpha + */ + color(red: number, green: number, blue: number, alpha: number): string; + + /** + * Returns the Svg as string. + */ + getDump(): string; + + /** + * Returns the Svg as a base64 encoded string. + */ + getBase64(): string; +} + +export default class Identicon { + hash: string; + foreground: Color; + background: Color; + size: number; + format: "svg" | "png"; + margin: number; + + constructor(hash: string, size?: number); + constructor(hash: string, options: IdenticonOptions); + + /** + * Returns a new blank image as Svg or PNGlib. + */ + image(): Svg | PNGlib; + + /** + * Returns a new image as Svg or PNGlib with the identicon applied. + */ + render(): Svg | PNGlib; + + /** + * Places a rectangle at the given position with the given width, height and color in the image. + * @param x The x coordinate. + * @param y The y coordinate + * @param w The width. + * @param h The height. + * @param color The color. + * @param image The image. + */ + rectangle( + x: number, + y: number, + w: number, + h: number, + color: Color, + image: Svg | PNGlib + ): void; + + /** + * Converts from hsl to rgb. + * @param h hue + * @param s saturation + * @param b brightness + */ + hsl2rgb(h: number, s: number, b: number): [number, number, number]; + + /** + * Returns the image data as a string. + */ + toString(): string; + + /** + * Returns true if the identicon is a Svg. + */ + isSvg(): boolean; +} diff --git a/types/identicon.js/tsconfig.json b/types/identicon.js/tsconfig.json new file mode 100644 index 0000000000..c679002ee1 --- /dev/null +++ b/types/identicon.js/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "identicon.js-tests.ts" + ] +} diff --git a/types/identicon.js/tslint.json b/types/identicon.js/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/identicon.js/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }