diff --git a/README.md b/README.md index e787701ff..903aabdda 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 color (Hex, RGB, RGBA, HSL, HSLA, HSV, HSVA, Named) to its 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 a css 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 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..1a1dd52ed 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'; @@ -235,11 +236,18 @@ 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]; + } + if (typeof cb === 'undefined') { cb = noop; } @@ -972,6 +980,19 @@ Jimp.intToRGBA = function(i, cb) { return rgba; }; +/** + * 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 + */ +Jimp.cssColorToHex = function(cssColor) { + cssColor = cssColor || 0; // 0, null, undefined, NaN + + if (typeof cssColor === 'number') return Number(cssColor); + + return parseInt(tinyColor(cssColor).toHex8(), 16); +}; + /** * Limits a number to between 0 or 255 * @param {number} n a number @@ -1128,8 +1149,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..51d108afe 100644 --- a/test/events.test.js +++ b/test/events.test.js @@ -20,6 +20,52 @@ describe('Events', () => { }) .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); + }); + + 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); + }); }); describe('on change', () => {