From 4d16ee7cce9c6df94890bcb698ca26e505eec802 Mon Sep 17 00:00:00 2001 From: Cody Schrank Date: Tue, 14 Aug 2018 14:55:24 -0700 Subject: [PATCH 1/6] Can now create a new jimp with the common CSS color format --- jimp.d.ts | 1095 ++++++++++++++++++++++--------------------- src/index.js | 23 +- test/events.test.js | 18 + 3 files changed, 590 insertions(+), 546 deletions(-) diff --git a/jimp.d.ts b/jimp.d.ts index ee8b0b1a1..861491a37 100644 --- a/jimp.d.ts +++ b/jimp.d.ts @@ -1,544 +1,551 @@ -declare namespace Jimp { - type GenericCallback = ( - this: TThis, - err: Error | null, - value: T - ) => U; - type ImageCallback = GenericCallback; - type ColorActionName = - | 'mix' - | 'tint' - | 'shade' - | 'xor' - | 'red' - | 'green' - | 'blue' - | 'hue'; - type ColorAction = { apply: ColorActionName; params: any }; - type BlendMode = { - mode: string; - opacitySource: number; - opacityDest: number; - }; - - type ChangeName = 'background' | 'scan' | 'crop'; - - type ListenableName = - | 'any' - | 'initialized' - | 'before-change' - | 'changed' - | 'before-clone' - | 'cloned' - | ChangeName; - - type ListenerData = T extends 'any' - ? any - : T extends ChangeName - ? { - eventName: 'before-change' | 'changed'; - methodName: T; - [key: string]: any; - } - : { - eventName: T; - methodName: T extends 'initialized' - ? 'constructor' - : T extends 'before-change' | 'changed' - ? ChangeName - : T extends 'before-clone' | 'cloned' ? 'clone' : any; - }; - - type PrintableText = - | string - | { - text: string; - alignmentX: number; - alignmentY: number; - }; - - interface Bitmap { - data: Buffer; - width: number; - height: number; - } - interface RGB { - r: number; - g: number; - b: number; - } - interface RGBA { - r: number; - g: number; - b: number; - a: number; - } - - interface FontChar { - id: number; - x: number; - y: number; - width: number; - height: number; - xoffset: number; - yoffset: number; - xadvance: number; - page: number; - chnl: number; - } - - interface FontInfo { - face: string; - size: number; - bold: number; - italic: number; - charset: string; - unicode: number; - stretchH: number; - smooth: number; - aa: number; - padding: [number, number, number, number]; - spacing: [number, number]; - } - - interface FontCommon { - lineHeight: number; - base: number; - scaleW: number; - scaleH: number; - pages: number; - packed: number; - alphaChnl: number; - redChnl: number; - greenChnl: number; - blueChnl: number; - } - - interface Font { - chars: { - [char: string]: FontChar; - }; - kernings: { - [firstString: string]: { - [secondString: string]: number; - }; - }; - pages: string[]; - common: FontCommon; - info: FontInfo; - } - - class Jimp { - // Constants - static AUTO: -1; - - // supported mime types - static MIME_PNG: 'image/png'; - static MIME_TIFF: 'image/tiff'; - static MIME_JPEG: 'image/jpeg'; - static MIME_JGD: 'image/jgd'; - static MIME_BMP: 'image/bmp'; - static MIME_X_MS_BMP: 'image/x-ms-bmp'; - static MIME_GIF: 'image/gif'; - // PNG filter types - static PNG_FILTER_AUTO: -1; - static PNG_FILTER_NONE: 0; - static PNG_FILTER_SUB: 1; - static PNG_FILTER_UP: 2; - static PNG_FILTER_AVERAGE: 3; - static PNG_FILTER_PATH: 4; - - // resize methods - static RESIZE_NEAREST_NEIGHBOR: 'nearestNeighbor'; - static RESIZE_BILINEAR: 'bilinearInterpolation'; - static RESIZE_BICUBIC: 'bicubicInterpolation'; - static RESIZE_HERMITE: 'hermiteInterpolation'; - static RESIZE_BEZIER: 'bezierInterpolation'; - - // blend modes - static BLEND_SOURCE_OVER: string; - static BLEND_DESTINATION_OVER: string; - static BLEND_MULTIPLY: string; - static BLEND_SCREEN: string; - static BLEND_OVERLAY: string; - static BLEND_DARKEN: string; - static BLEND_LIGHTEN: string; - static BLEND_HARDLIGHT: string; - static BLEND_DIFFERENCE: string; - static BLEND_EXCLUSION: string; - - // Align modes for cover, contain, bit masks - static HORIZONTAL_ALIGN_LEFT: 1; - static HORIZONTAL_ALIGN_CENTER: 2; - static HORIZONTAL_ALIGN_RIGHT: 4; - - static VERTICAL_ALIGN_TOP: 8; - static VERTICAL_ALIGN_MIDDLE: 16; - static VERTICAL_ALIGN_BOTTOM: 32; - - // Font locations - static FONT_SANS_8_BLACK: string; - static FONT_SANS_10_BLACK: string; - static FONT_SANS_12_BLACK: string; - static FONT_SANS_14_BLACK: string; - static FONT_SANS_16_BLACK: string; - static FONT_SANS_32_BLACK: string; - static FONT_SANS_64_BLACK: string; - static FONT_SANS_128_BLACK: string; - - static FONT_SANS_8_WHITE: string; - static FONT_SANS_16_WHITE: string; - static FONT_SANS_32_WHITE: string; - static FONT_SANS_64_WHITE: string; - static FONT_SANS_128_WHITE: string; - - // Edge Handling - static EDGE_EXTEND: 1; - static EDGE_WRAP: 2; - static EDGE_CROP: 3; - - // Properties - bitmap: Bitmap; - - private _quality: number; - private _deflateLevel: number; - private _deflateStrategy: number; - private _filterType: number; - private _rgba: boolean; - private _background: number; - private _originalMime: string; - - // Constructors - constructor(path: string, cb?: Jimp.ImageCallback); - constructor(image: Jimp, cb?: Jimp.ImageCallback); - constructor(data: Buffer, cb?: Jimp.ImageCallback); - constructor(data: Bitmap, cb?: Jimp.ImageCallback); - constructor(w: number, h: number, cb?: Jimp.ImageCallback); - constructor( - w: number, - h: number, - background?: number, - cb?: Jimp.ImageCallback - ); - // For custom constructors when using Jimp.appendConstructorOption - constructor(...args: any[]); - - // Methods - on( - event: T, - cb: (data: ListenerData) => any - ): any; - getHeight(): number; - getWidth(): number; - inspect(): string; - toString(): string; - getMIME(): string; - getExtension(): string; - write(path: string, cb?: Jimp.ImageCallback): this; - writeAsync(path: string): Promise; - deflateLevel(l: number, cb?: Jimp.ImageCallback): this; - deflateStrategy(s: number, cb?: Jimp.ImageCallback): this; - filterType(f: number, cb?: Jimp.ImageCallback): this; - rgba(bool: boolean, cb?: Jimp.ImageCallback): this; - quality(n: number, cb?: Jimp.ImageCallback): this; - getBase64(mime: string, cb: GenericCallback): this; - getBase64Async(mime: string): Promise; - hash(cb?: GenericCallback): this; - hash( - base: number | null | undefined, - cb?: GenericCallback - ): this; - getBuffer(mime: string, cb: GenericCallback): this; - getBufferAsync(mime: string): Promise; - getPixelIndex( - x: number, - y: number, - cb?: GenericCallback - ): this; - getPixelIndex( - x: number, - y: number, - edgeHandling: string, - cb?: GenericCallback - ): this; - getPixelColor( - x: number, - y: number, - cb?: GenericCallback - ): this; - getPixelColour( - x: number, - y: number, - cb?: GenericCallback - ): this; - setPixelColor( - hex: number, - x: number, - y: number, - cb?: Jimp.ImageCallback - ): this; - setPixelColour( - hex: number, - x: number, - y: number, - cb?: Jimp.ImageCallback - ): this; - clone(cb?: Jimp.ImageCallback): this; - cloneQuiet(cb?: Jimp.ImageCallback): this; - background(hex: number, cb?: Jimp.ImageCallback): this; - backgroundQuiet(hex: number, cb?: Jimp.ImageCallback): this; - scan( - x: number, - y: number, - w: number, - h: number, - f: (this: this, x: number, y: number, idx: number) => any, - cb?: Jimp.ImageCallback - ): this; - scanQuiet( - x: number, - y: number, - w: number, - h: number, - f: (this: this, x: number, y: number, idx: number) => any, - cb?: Jimp.ImageCallback - ): this; - crop( - x: number, - y: number, - w: number, - h: number, - cb?: Jimp.ImageCallback - ): this; - cropQuiet( - x: number, - y: number, - w: number, - h: number, - cb?: Jimp.ImageCallback - ): this; - - // Color methods - brightness(val: number, cb?: Jimp.ImageCallback): this; - contrast(val: number, cb?: Jimp.ImageCallback): this; - posterize(n: number, cb?: Jimp.ImageCallback): this; - greyscale(cb?: Jimp.ImageCallback): this; - grayscale(cb?: Jimp.ImageCallback): this; - opacity(f: number, cb?: Jimp.ImageCallback): this; - sepia(cb?: Jimp.ImageCallback): this; - fade(f: number, cb?: Jimp.ImageCallback): this; - convolution(kernel: number[][], cb?: Jimp.ImageCallback): this; - convolution( - kernel: number[][], - edgeHandling: string, - cb?: Jimp.ImageCallback - ): this; - opaque(cb?: Jimp.ImageCallback): this; - pixelate(size: number, cb?: Jimp.ImageCallback): this; - pixelate( - size: number, - x: number, - y: number, - w: number, - h: number, - cb?: Jimp.ImageCallback - ): this; - convolute(kernel: number[][], cb?: Jimp.ImageCallback): this; - convolute( - kernel: number[][], - x: number, - y: number, - w: number, - h: number, - cb?: Jimp.ImageCallback - ): this; - color(actions: ColorAction[], cb?: Jimp.ImageCallback): this; - colour(actions: ColorAction[], cb?: Jimp.ImageCallback): this; - - // Shape methods - rotate(deg: number, cb?: Jimp.ImageCallback): this; - rotate( - deg: number, - mode: string | boolean, - cb?: Jimp.ImageCallback - ): this; - flip( - horizontal: boolean, - vertical: boolean, - cb?: Jimp.ImageCallback - ): this; - mirror( - horizontal: boolean, - vertical: boolean, - cb?: Jimp.ImageCallback - ): this; - resize(w: number, h: number, cb?: Jimp.ImageCallback): this; - resize( - w: number, - h: number, - mode?: string, - cb?: Jimp.ImageCallback - ): this; - cover(w: number, h: number, cb?: Jimp.ImageCallback): this; - cover( - w: number, - h: number, - alignBits?: number, - cb?: Jimp.ImageCallback - ): this; - cover( - w: number, - h: number, - alignBits?: number, - mode?: string, - cb?: Jimp.ImageCallback - ): this; - contain(w: number, h: number, cb?: Jimp.ImageCallback): this; - contain( - w: number, - h: number, - mode?: string, - cb?: Jimp.ImageCallback - ): this; - contain( - w: number, - h: number, - alignBits?: number, - cb?: Jimp.ImageCallback - ): this; - contain( - w: number, - h: number, - alignBits?: number, - mode?: string, - cb?: Jimp.ImageCallback - ): this; - scale(f: number, cb?: Jimp.ImageCallback): this; - scale( - f: number, - mode?: string, - cb?: Jimp.ImageCallback - ): this; - scaleToFit(w: number, h: number, cb?: Jimp.ImageCallback): this; - scaleToFit( - w: number, - h: number, - mode?: string, - cb?: Jimp.ImageCallback - ): this; - displace(map: Jimp, offset: number, cb?: Jimp.ImageCallback): this; - autocrop( - tolerance?: number, - cb?: Jimp.ImageCallback - ): this; - autocrop( - cropOnlyFrames?: boolean, - cb?: Jimp.ImageCallback - ): this; - autocrop( - tolerance?: number, - cropOnlyFrames?: boolean, - cb?: Jimp.ImageCallback - ): this; - - // Text methods - print( - font: Font, - x: number, - y: number, - text: PrintableText, - cb?: Jimp.ImageCallback - ): this; - print( - font: Font, - x: number, - y: number, - text: PrintableText, - maxWidth?: number, - cb?: Jimp.ImageCallback - ): this; - print( - font: Font, - x: number, - y: number, - text: PrintableText, - maxWidth?: number, - maxHeight?: number, - cb?: Jimp.ImageCallback - ): this; - - // Effect methods - blur(r: number, cb?: Jimp.ImageCallback): this; - dither565(cb?: Jimp.ImageCallback): this; - dither16(cb?: Jimp.ImageCallback): this; - histogram(): { r: number[]; g: number[]; b: number[] }; - normalize(cb?: Jimp.ImageCallback): this; - invert(cb?: Jimp.ImageCallback): this; - gaussian(r: number, cb?: Jimp.ImageCallback): this; - composite( - src: Jimp, - x: number, - y: number, - options?: Jimp.BlendMode, - cb?: Jimp.ImageCallback - ): this; - blit(src: Jimp, x: number, y: number, cb?: Jimp.ImageCallback): this; - blit( - src: Jimp, - x: number, - y: number, - srcx: number, - srcy: number, - srcw: number, - srch: number, - cb?: Jimp.ImageCallback - ): this; - mask(src: Jimp, x: number, y: number, cb?: Jimp.ImageCallback): this; - - // Functions - static appendConstructorOption( - name: string, - test: (...args: T) => boolean, - run: ( - this: Jimp, - resolve: (jimp: Jimp) => any, - reject: (reason: Error) => any, - ...args: T - ) => any - ); - static read(path: string): Promise; - static read(image: Jimp): Promise; - static read(data: Buffer): Promise; - static read(w: number, h: number, background?: number): Promise; - static create(path: string): Promise; - static create(image: Jimp): Promise; - static create(data: Buffer): Promise; - static create(w: number, h: number, background?: number): Promise; - static rgbaToInt( - r: number, - g: number, - b: number, - a: number, - cb: GenericCallback - ): number; - static intToRGBA(i: number, cb?: GenericCallback): Jimp.RGBA; - static limit255(n: number): number; - static diff( - img1: Jimp, - img2: Jimp, - threshold?: number - ): { percent: number; image: Jimp }; - static distance(img1: Jimp, img2: Jimp): number; - static colorDiff(rgba1: Jimp.RGB, rgba2: Jimp.RGB): number; - static colorDiff(rgba1: Jimp.RGBA, rgba2: Jimp.RGBA): number; - static loadFont(file: string): Promise; - static loadFont( - file: string, - cb: Jimp.GenericCallback - ): Promise; - } -} - -declare module 'jimp' { - export = Jimp.Jimp; -} +declare namespace Jimp { + type GenericCallback = ( + this: TThis, + err: Error | null, + value: T + ) => U; + type ImageCallback = GenericCallback; + type ColorActionName = + | 'mix' + | 'tint' + | 'shade' + | 'xor' + | 'red' + | 'green' + | 'blue' + | 'hue'; + type ColorAction = { apply: ColorActionName; params: any }; + type BlendMode = { + mode: string; + opacitySource: number; + opacityDest: number; + }; + + type ChangeName = 'background' | 'scan' | 'crop'; + + type ListenableName = + | 'any' + | 'initialized' + | 'before-change' + | 'changed' + | 'before-clone' + | 'cloned' + | ChangeName; + + type ListenerData = T extends 'any' + ? any + : T extends ChangeName + ? { + eventName: 'before-change' | 'changed'; + methodName: T; + [key: string]: any; + } + : { + eventName: T; + methodName: T extends 'initialized' + ? 'constructor' + : T extends 'before-change' | 'changed' + ? ChangeName + : T extends 'before-clone' | 'cloned' ? 'clone' : any; + }; + + type PrintableText = + | string + | { + text: string; + alignmentX: number; + alignmentY: number; + }; + + interface Bitmap { + data: Buffer; + width: number; + height: number; + } + interface RGB { + r: number; + g: number; + b: number; + } + interface RGBA { + r: number; + g: number; + b: number; + a: number; + } + + interface FontChar { + id: number; + x: number; + y: number; + width: number; + height: number; + xoffset: number; + yoffset: number; + xadvance: number; + page: number; + chnl: number; + } + + interface FontInfo { + face: string; + size: number; + bold: number; + italic: number; + charset: string; + unicode: number; + stretchH: number; + smooth: number; + aa: number; + padding: [number, number, number, number]; + spacing: [number, number]; + } + + interface FontCommon { + lineHeight: number; + base: number; + scaleW: number; + scaleH: number; + pages: number; + packed: number; + alphaChnl: number; + redChnl: number; + greenChnl: number; + blueChnl: number; + } + + interface Font { + chars: { + [char: string]: FontChar; + }; + kernings: { + [firstString: string]: { + [secondString: string]: number; + }; + }; + pages: string[]; + common: FontCommon; + info: FontInfo; + } + + class Jimp { + // Constants + static AUTO: -1; + + // supported mime types + static MIME_PNG: 'image/png'; + static MIME_TIFF: 'image/tiff'; + static MIME_JPEG: 'image/jpeg'; + static MIME_JGD: 'image/jgd'; + static MIME_BMP: 'image/bmp'; + static MIME_X_MS_BMP: 'image/x-ms-bmp'; + static MIME_GIF: 'image/gif'; + // PNG filter types + static PNG_FILTER_AUTO: -1; + static PNG_FILTER_NONE: 0; + static PNG_FILTER_SUB: 1; + static PNG_FILTER_UP: 2; + static PNG_FILTER_AVERAGE: 3; + static PNG_FILTER_PATH: 4; + + // resize methods + static RESIZE_NEAREST_NEIGHBOR: 'nearestNeighbor'; + static RESIZE_BILINEAR: 'bilinearInterpolation'; + static RESIZE_BICUBIC: 'bicubicInterpolation'; + static RESIZE_HERMITE: 'hermiteInterpolation'; + static RESIZE_BEZIER: 'bezierInterpolation'; + + // blend modes + static BLEND_SOURCE_OVER: string; + static BLEND_DESTINATION_OVER: string; + static BLEND_MULTIPLY: string; + static BLEND_SCREEN: string; + static BLEND_OVERLAY: string; + static BLEND_DARKEN: string; + static BLEND_LIGHTEN: string; + static BLEND_HARDLIGHT: string; + static BLEND_DIFFERENCE: string; + static BLEND_EXCLUSION: string; + + // Align modes for cover, contain, bit masks + static HORIZONTAL_ALIGN_LEFT: 1; + static HORIZONTAL_ALIGN_CENTER: 2; + static HORIZONTAL_ALIGN_RIGHT: 4; + + static VERTICAL_ALIGN_TOP: 8; + static VERTICAL_ALIGN_MIDDLE: 16; + static VERTICAL_ALIGN_BOTTOM: 32; + + // Font locations + static FONT_SANS_8_BLACK: string; + static FONT_SANS_10_BLACK: string; + static FONT_SANS_12_BLACK: string; + static FONT_SANS_14_BLACK: string; + static FONT_SANS_16_BLACK: string; + static FONT_SANS_32_BLACK: string; + static FONT_SANS_64_BLACK: string; + static FONT_SANS_128_BLACK: string; + + static FONT_SANS_8_WHITE: string; + static FONT_SANS_16_WHITE: string; + static FONT_SANS_32_WHITE: string; + static FONT_SANS_64_WHITE: string; + static FONT_SANS_128_WHITE: string; + + // Edge Handling + static EDGE_EXTEND: 1; + static EDGE_WRAP: 2; + static EDGE_CROP: 3; + + // Properties + bitmap: Bitmap; + + private _quality: number; + private _deflateLevel: number; + private _deflateStrategy: number; + private _filterType: number; + private _rgba: boolean; + private _background: number; + private _originalMime: string; + + // Constructors + constructor(path: string, cb?: Jimp.ImageCallback); + constructor(image: Jimp, cb?: Jimp.ImageCallback); + constructor(data: Buffer, cb?: Jimp.ImageCallback); + constructor(data: Bitmap, cb?: Jimp.ImageCallback); + constructor(w: number, h: number, cb?: Jimp.ImageCallback); + constructor( + w: number, + h: number, + background?: number, + cb?: Jimp.ImageCallback + ); + constructor( + w: number, + h: number, + background?: string, + cb?: Jimp.ImageCallback + ); + // For custom constructors when using Jimp.appendConstructorOption + constructor(...args: any[]); + + // Methods + on( + event: T, + cb: (data: ListenerData) => any + ): any; + getHeight(): number; + getWidth(): number; + inspect(): string; + toString(): string; + getMIME(): string; + getExtension(): string; + write(path: string, cb?: Jimp.ImageCallback): this; + writeAsync(path: string): Promise; + deflateLevel(l: number, cb?: Jimp.ImageCallback): this; + deflateStrategy(s: number, cb?: Jimp.ImageCallback): this; + filterType(f: number, cb?: Jimp.ImageCallback): this; + rgba(bool: boolean, cb?: Jimp.ImageCallback): this; + quality(n: number, cb?: Jimp.ImageCallback): this; + getBase64(mime: string, cb: GenericCallback): this; + getBase64Async(mime: string): Promise; + hash(cb?: GenericCallback): this; + hash( + base: number | null | undefined, + cb?: GenericCallback + ): this; + getBuffer(mime: string, cb: GenericCallback): this; + getBufferAsync(mime: string): Promise; + getPixelIndex( + x: number, + y: number, + cb?: GenericCallback + ): this; + getPixelIndex( + x: number, + y: number, + edgeHandling: string, + cb?: GenericCallback + ): this; + getPixelColor( + x: number, + y: number, + cb?: GenericCallback + ): this; + getPixelColour( + x: number, + y: number, + cb?: GenericCallback + ): this; + setPixelColor( + hex: number, + x: number, + y: number, + cb?: Jimp.ImageCallback + ): this; + setPixelColour( + hex: number, + x: number, + y: number, + cb?: Jimp.ImageCallback + ): this; + clone(cb?: Jimp.ImageCallback): this; + cloneQuiet(cb?: Jimp.ImageCallback): this; + background(hex: number, cb?: Jimp.ImageCallback): this; + backgroundQuiet(hex: number, cb?: Jimp.ImageCallback): this; + scan( + x: number, + y: number, + w: number, + h: number, + f: (this: this, x: number, y: number, idx: number) => any, + cb?: Jimp.ImageCallback + ): this; + scanQuiet( + x: number, + y: number, + w: number, + h: number, + f: (this: this, x: number, y: number, idx: number) => any, + cb?: Jimp.ImageCallback + ): this; + crop( + x: number, + y: number, + w: number, + h: number, + cb?: Jimp.ImageCallback + ): this; + cropQuiet( + x: number, + y: number, + w: number, + h: number, + cb?: Jimp.ImageCallback + ): this; + + // Color methods + brightness(val: number, cb?: Jimp.ImageCallback): this; + contrast(val: number, cb?: Jimp.ImageCallback): this; + posterize(n: number, cb?: Jimp.ImageCallback): this; + greyscale(cb?: Jimp.ImageCallback): this; + grayscale(cb?: Jimp.ImageCallback): this; + opacity(f: number, cb?: Jimp.ImageCallback): this; + sepia(cb?: Jimp.ImageCallback): this; + fade(f: number, cb?: Jimp.ImageCallback): this; + convolution(kernel: number[][], cb?: Jimp.ImageCallback): this; + convolution( + kernel: number[][], + edgeHandling: string, + cb?: Jimp.ImageCallback + ): this; + opaque(cb?: Jimp.ImageCallback): this; + pixelate(size: number, cb?: Jimp.ImageCallback): this; + pixelate( + size: number, + x: number, + y: number, + w: number, + h: number, + cb?: Jimp.ImageCallback + ): this; + convolute(kernel: number[][], cb?: Jimp.ImageCallback): this; + convolute( + kernel: number[][], + x: number, + y: number, + w: number, + h: number, + cb?: Jimp.ImageCallback + ): this; + color(actions: ColorAction[], cb?: Jimp.ImageCallback): this; + colour(actions: ColorAction[], cb?: Jimp.ImageCallback): this; + + // Shape methods + rotate(deg: number, cb?: Jimp.ImageCallback): this; + rotate( + deg: number, + mode: string | boolean, + cb?: Jimp.ImageCallback + ): this; + flip( + horizontal: boolean, + vertical: boolean, + cb?: Jimp.ImageCallback + ): this; + mirror( + horizontal: boolean, + vertical: boolean, + cb?: Jimp.ImageCallback + ): this; + resize(w: number, h: number, cb?: Jimp.ImageCallback): this; + resize( + w: number, + h: number, + mode?: string, + cb?: Jimp.ImageCallback + ): this; + cover(w: number, h: number, cb?: Jimp.ImageCallback): this; + cover( + w: number, + h: number, + alignBits?: number, + cb?: Jimp.ImageCallback + ): this; + cover( + w: number, + h: number, + alignBits?: number, + mode?: string, + cb?: Jimp.ImageCallback + ): this; + contain(w: number, h: number, cb?: Jimp.ImageCallback): this; + contain( + w: number, + h: number, + mode?: string, + cb?: Jimp.ImageCallback + ): this; + contain( + w: number, + h: number, + alignBits?: number, + cb?: Jimp.ImageCallback + ): this; + contain( + w: number, + h: number, + alignBits?: number, + mode?: string, + cb?: Jimp.ImageCallback + ): this; + scale(f: number, cb?: Jimp.ImageCallback): this; + scale( + f: number, + mode?: string, + cb?: Jimp.ImageCallback + ): this; + scaleToFit(w: number, h: number, cb?: Jimp.ImageCallback): this; + scaleToFit( + w: number, + h: number, + mode?: string, + cb?: Jimp.ImageCallback + ): this; + displace(map: Jimp, offset: number, cb?: Jimp.ImageCallback): this; + autocrop( + tolerance?: number, + cb?: Jimp.ImageCallback + ): this; + autocrop( + cropOnlyFrames?: boolean, + cb?: Jimp.ImageCallback + ): this; + autocrop( + tolerance?: number, + cropOnlyFrames?: boolean, + cb?: Jimp.ImageCallback + ): this; + + // Text methods + print( + font: Font, + x: number, + y: number, + text: PrintableText, + cb?: Jimp.ImageCallback + ): this; + print( + font: Font, + x: number, + y: number, + text: PrintableText, + maxWidth?: number, + cb?: Jimp.ImageCallback + ): this; + print( + font: Font, + x: number, + y: number, + text: PrintableText, + maxWidth?: number, + maxHeight?: number, + cb?: Jimp.ImageCallback + ): this; + + // Effect methods + blur(r: number, cb?: Jimp.ImageCallback): this; + dither565(cb?: Jimp.ImageCallback): this; + dither16(cb?: Jimp.ImageCallback): this; + histogram(): { r: number[]; g: number[]; b: number[] }; + normalize(cb?: Jimp.ImageCallback): this; + invert(cb?: Jimp.ImageCallback): this; + gaussian(r: number, cb?: Jimp.ImageCallback): this; + composite( + src: Jimp, + x: number, + y: number, + options?: Jimp.BlendMode, + cb?: Jimp.ImageCallback + ): this; + blit(src: Jimp, x: number, y: number, cb?: Jimp.ImageCallback): this; + blit( + src: Jimp, + x: number, + y: number, + srcx: number, + srcy: number, + srcw: number, + srch: number, + cb?: Jimp.ImageCallback + ): this; + mask(src: Jimp, x: number, y: number, cb?: Jimp.ImageCallback): this; + + // Functions + static appendConstructorOption( + name: string, + test: (...args: T) => boolean, + run: ( + this: Jimp, + resolve: (jimp: Jimp) => any, + reject: (reason: Error) => any, + ...args: T + ) => any + ); + static read(path: string): Promise; + static read(image: Jimp): Promise; + static read(data: Buffer): Promise; + static read(w: number, h: number, background?: number): Promise; + static create(path: string): Promise; + static create(image: Jimp): Promise; + static create(data: Buffer): Promise; + static create(w: number, h: number, background?: number): Promise; + static rgbaToInt( + r: number, + g: number, + b: number, + a: number, + cb: GenericCallback + ): number; + static intToRGBA(i: number, cb?: GenericCallback): Jimp.RGBA; + static cssColorToHex(cssColor: string): number; + static limit255(n: number): number; + static diff( + img1: Jimp, + img2: Jimp, + threshold?: number + ): { percent: number; image: Jimp }; + static distance(img1: Jimp, img2: Jimp): number; + static colorDiff(rgba1: Jimp.RGB, rgba2: Jimp.RGB): number; + static colorDiff(rgba1: Jimp.RGBA, rgba2: Jimp.RGBA): number; + static loadFont(file: string): Promise; + static loadFont( + file: string, + cb: Jimp.GenericCallback + ): Promise; + } +} + +declare module 'jimp' { + export = Jimp.Jimp; +} diff --git a/src/index.js b/src/index.js index 017e5dce6..371a1fe4e 100755 --- a/src/index.js +++ b/src/index.js @@ -6,6 +6,7 @@ import anyBase from 'any-base'; import bMFont from 'load-bmfont'; import MkDirP from 'mkdirp'; import pixelMatch from 'pixelmatch'; +import tinyColor from 'tinycolor2'; import ImagePHash from './modules/phash'; import request from './request'; @@ -240,6 +241,11 @@ class Jimp extends EventEmitter { cb = args[3]; } + if (typeof args[2] === 'string') { + this._background = Jimp.cssColorToHex(args[2]); + cb = args[3]; + } + if (typeof cb === 'undefined') { cb = noop; } @@ -972,6 +978,20 @@ Jimp.intToRGBA = function(i, cb) { return rgba; }; +/** + * Converts a css color (#FFFFFF) to a hex number + * @param {string} cssColor a number + * @returns {number} a hex number representing a color + */ +Jimp.cssColorToHex = function(cssColor) { + cssColor = cssColor || 0; // 0, null, undefined, NaN + + if (typeof cssColor === 'number') return Number(cssColor); + + const color = new tinyColor(cssColor); + return parseInt(color.toHex8(), 16); +}; + /** * Limits a number to between 0 or 255 * @param {number} n a number @@ -1128,8 +1148,7 @@ Jimp.loadFont = function(file, cb) { kernings[firstString] = kernings[firstString] || {}; kernings[firstString][ String.fromCharCode(font.kernings[i].second) - ] = - font.kernings[i].amount; + ] = font.kernings[i].amount; } loadPages(Path.dirname(file), font.pages).then(pages => { diff --git a/test/events.test.js b/test/events.test.js index fe1927b36..b7bc9c51a 100644 --- a/test/events.test.js +++ b/test/events.test.js @@ -20,6 +20,24 @@ describe('Events', () => { }) .on('error', done); }); + + it('initializes with a color', done => { + new Jimp(2, 2, 0xffffffff) + .on('initialized', function() { + this.getPixelColor(1, 1).should.be.equal(0xffffffff); + done(); + }) + .on('error', done); + }); + + it('initializes with a css color', done => { + new Jimp(2, 2, '#FFFFFF') + .on('initialized', function() { + this.getPixelColor(1, 1).should.be.equal(0xffffffff); + done(); + }) + .on('error', done); + }); }); describe('on change', () => { From b9b1908cc1e86795fdd42d95a85a10ec9bd3e77c Mon Sep 17 00:00:00 2001 From: Cody Schrank Date: Tue, 14 Aug 2018 14:57:21 -0700 Subject: [PATCH 2/6] comments --- src/index.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/index.js b/src/index.js index 371a1fe4e..96949c172 100755 --- a/src/index.js +++ b/src/index.js @@ -236,11 +236,13 @@ class Jimp extends EventEmitter { const h = parseInt(args[1], 10); cb = args[2]; + // with a hex color if (typeof args[2] === 'number') { this._background = args[2]; cb = args[3]; } + // with a css color if (typeof args[2] === 'string') { this._background = Jimp.cssColorToHex(args[2]); cb = args[3]; From 48320027e0953f28efdd4c0ff91606b75f4eec52 Mon Sep 17 00:00:00 2001 From: Cody Schrank Date: Tue, 14 Aug 2018 15:08:45 -0700 Subject: [PATCH 3/6] readme --- README.md | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e787701ff..82ea4c6e3 100755 --- a/README.md +++ b/README.md @@ -302,7 +302,7 @@ Jimp.loadFont(Jimp.FONT_SANS_32_BLACK).then(function(font) { }); ``` -Online tools are also available to convert TTF fonts to BMFont format. They can be used to create color font or sprite packs. +Online tools are also available to convert TTF fonts to BMFont format. They can be used to create color font or sprite packs. :star: [Littera](http://kvazars.com/littera/) @@ -504,6 +504,12 @@ Jimp.rgbaToInt(r, g, b, a); // e.g. converts 255, 255, 255, 255 to 0xFFFFFFFF Jimp.intToRGBA(hex); // e.g. converts 0xFFFFFFFF to {r: 255, g: 255, b: 255, a:255} ``` +You can convert a css hex color to its true rgba hexadecimal equivalent: + +```js +Jimp.cssColorToHex(cssColor); // e.g. converts #FF00FF to 0xFF00FFFF +``` + ### Creating new images If you want to begin with an empty Jimp image, you can call the Jimp constructor passing the width and height of the image to create and a Node-style callback: @@ -522,6 +528,14 @@ new Jimp(256, 256, 0xff0000ff, function(err, image) { }); ``` +Or you can use the css hex color format: + +```js +new Jimp(256, 256, '#FF00FF', function(err, image) { + // this image is 256 x 256, every pixel is set to #FF00FF +}); +``` + You can also initialize a new Jimp image with a raw image buffer: ```js From 2c11d304bda3e7e85b87e8d49acfb289a55eb7b1 Mon Sep 17 00:00:00 2001 From: Cody Schrank Date: Tue, 14 Aug 2018 15:54:16 -0700 Subject: [PATCH 4/6] Requested changes and added support for RGB, RGBA, HSL, HSLA, HSV, HSVA, Named --- src/index.js | 5 ++--- test/events.test.js | 46 ++++++++++++++++++++++++++++++++++++++------- 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/src/index.js b/src/index.js index 96949c172..1a1dd52ed 100755 --- a/src/index.js +++ b/src/index.js @@ -981,7 +981,7 @@ Jimp.intToRGBA = function(i, cb) { }; /** - * Converts a css color (#FFFFFF) to a hex number + * Converts a css color (Hex, 8-digit (RGBA) Hex, RGB, RGBA, HSL, HSLA, HSV, HSVA, Named) to a hex number * @param {string} cssColor a number * @returns {number} a hex number representing a color */ @@ -990,8 +990,7 @@ Jimp.cssColorToHex = function(cssColor) { if (typeof cssColor === 'number') return Number(cssColor); - const color = new tinyColor(cssColor); - return parseInt(color.toHex8(), 16); + return parseInt(tinyColor(cssColor).toHex8(), 16); }; /** diff --git a/test/events.test.js b/test/events.test.js index b7bc9c51a..4f0754e0f 100644 --- a/test/events.test.js +++ b/test/events.test.js @@ -30,13 +30,45 @@ describe('Events', () => { .on('error', done); }); - it('initializes with a css color', done => { - new Jimp(2, 2, '#FFFFFF') - .on('initialized', function() { - this.getPixelColor(1, 1).should.be.equal(0xffffffff); - done(); - }) - .on('error', done); + // (Hex, RGB, RGBA, HSL, HSLA, HSV, HSVA, Named) + it('initializes with a css color (Hex)', async () => { + const image = await Jimp.create(2, 2, '#FFFFFF'); + image.getPixelColor(1, 1).should.be.equal(0xffffffff); + }); + + it('initializes with a css color (RGB)', async () => { + const image = await Jimp.create(2, 2, 'rgb (255, 255, 255)'); + image.getPixelColor(1, 1).should.be.equal(0xffffffff); + }); + + it('initializes with a css color (RGBA)', async () => { + const image = await Jimp.create(2, 2, 'rgba (255, 255, 255, 1)'); + image.getPixelColor(1, 1).should.be.equal(0xffffffff); + }); + + it('initializes with a css color (HSL)', async () => { + const image = await Jimp.create(2, 2, 'hsl (100%, 100%, 100%)'); + image.getPixelColor(1, 1).should.be.equal(0xffffffff); + }); + + it('initializes with a css color (HSLA)', async () => { + const image = await Jimp.create(2, 2, 'hsla (100%, 100%, 100%, 1)'); + image.getPixelColor(1, 1).should.be.equal(0xffffffff); + }); + + it('initializes with a css color (HSV)', async () => { + const image = await Jimp.create(2, 2, 'hsv (0%, 0%, 100%)'); + image.getPixelColor(1, 1).should.be.equal(0xffffffff); + }); + + it('initializes with a css color (HSVA)', async () => { + const image = await Jimp.create(2, 2, 'hsva (0%, 0%, 100%, 1)'); + image.getPixelColor(1, 1).should.be.equal(0xffffffff); + }); + + it('initializes with a css color (Named)', async () => { + const image = await Jimp.create(2, 2, 'WHITE'); + image.getPixelColor(1, 1).should.be.equal(0xffffffff); }); }); From 840b666f6bf8ba57588ea73838831c3063b539e1 Mon Sep 17 00:00:00 2001 From: Cody Schrank Date: Tue, 14 Aug 2018 15:56:23 -0700 Subject: [PATCH 5/6] missed one test --- test/events.test.js | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/test/events.test.js b/test/events.test.js index 4f0754e0f..51d108afe 100644 --- a/test/events.test.js +++ b/test/events.test.js @@ -21,16 +21,12 @@ describe('Events', () => { .on('error', done); }); - it('initializes with a color', done => { - new Jimp(2, 2, 0xffffffff) - .on('initialized', function() { - this.getPixelColor(1, 1).should.be.equal(0xffffffff); - done(); - }) - .on('error', done); + // (8bit Hex, Hex, RGB, RGBA, HSL, HSLA, HSV, HSVA, Named) + it('initializes with a 8bit hex color', async () => { + const image = await Jimp.create(2, 2, 0xffffffff); + image.getPixelColor(1, 1).should.be.equal(0xffffffff); }); - // (Hex, RGB, RGBA, HSL, HSLA, HSV, HSVA, Named) it('initializes with a css color (Hex)', async () => { const image = await Jimp.create(2, 2, '#FFFFFF'); image.getPixelColor(1, 1).should.be.equal(0xffffffff); From b1172756f7a9ae9e1ac2bd2215367f52cab97b2f Mon Sep 17 00:00:00 2001 From: Cody Schrank Date: Tue, 14 Aug 2018 16:02:46 -0700 Subject: [PATCH 6/6] possible readme --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 82ea4c6e3..903aabdda 100755 --- a/README.md +++ b/README.md @@ -504,7 +504,7 @@ Jimp.rgbaToInt(r, g, b, a); // e.g. converts 255, 255, 255, 255 to 0xFFFFFFFF Jimp.intToRGBA(hex); // e.g. converts 0xFFFFFFFF to {r: 255, g: 255, b: 255, a:255} ``` -You can convert a css hex color to its true rgba hexadecimal equivalent: +You can convert a css color (Hex, RGB, RGBA, HSL, HSLA, HSV, HSVA, Named) to its hexadecimal equivalent: ```js Jimp.cssColorToHex(cssColor); // e.g. converts #FF00FF to 0xFF00FFFF @@ -528,7 +528,7 @@ new Jimp(256, 256, 0xff0000ff, function(err, image) { }); ``` -Or you can use the css hex color format: +Or you can use a css color format: ```js new Jimp(256, 256, '#FF00FF', function(err, image) {