Skip to content

Commit e514f52

Browse files
committed
TypeScript: take advantage of color dependency types
1 parent bffd885 commit e514f52

File tree

4 files changed

+17
-27
lines changed

4 files changed

+17
-27
lines changed

lib/index.d.ts

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
/// <reference types="node" />
2929

3030
import type { Duplex } from 'node:stream';
31+
import { ColorLike } from '@img/colour';
3132

3233
//#region Constructor functions
3334

@@ -234,7 +235,7 @@ declare namespace sharp {
234235
* @param tint Parsed by the color module.
235236
* @returns A sharp instance that can be used to chain operations
236237
*/
237-
tint(tint: Colour | Color): Sharp;
238+
tint(tint: ColorLike): Sharp;
238239

239240
/**
240241
* Convert to 8-bit greyscale; 256 shades of grey.
@@ -1014,7 +1015,7 @@ declare namespace sharp {
10141015
/** @deprecated Use {@link SharpOptions.tiff} instead */
10151016
subifd?: number | undefined;
10161017
/** @deprecated Use {@link SharpOptions.pdf} instead */
1017-
pdfBackground?: Colour | Color | undefined;
1018+
pdfBackground?: ColorLike | undefined;
10181019
/** @deprecated Use {@link SharpOptions.openSlide} instead */
10191020
level?: number | undefined;
10201021
/** Set to `true` to read all frames/pages of an animated image (equivalent of setting `pages` to `-1`). (optional, default false) */
@@ -1073,7 +1074,7 @@ declare namespace sharp {
10731074
/** Number of bands, 3 for RGB, 4 for RGBA */
10741075
channels: CreateChannels;
10751076
/** Parsed by the [color](https://www.npmjs.org/package/color) module to extract values for red, green, blue and alpha. */
1076-
background: Colour | Color;
1077+
background: ColorLike;
10771078
/** Describes a noise to be created. */
10781079
noise?: Noise | undefined;
10791080
/** The height of each page/frame for animated images, must be an integral factor of the overall image height. */
@@ -1120,7 +1121,7 @@ declare namespace sharp {
11201121
/** Space between images, in pixels. */
11211122
shim?: number | undefined;
11221123
/** Background colour. */
1123-
background?: Colour | Color | undefined;
1124+
background?: ColorLike | undefined;
11241125
/** Horizontal alignment. */
11251126
halign?: HorizontalAlignment | undefined;
11261127
/** Vertical alignment. */
@@ -1141,7 +1142,7 @@ declare namespace sharp {
11411142

11421143
interface PdfInputOptions {
11431144
/** Background colour to use when PDF is partially transparent. Requires the use of a globally-installed libvips compiled with support for PDFium, Poppler, ImageMagick or GraphicsMagick. */
1144-
background?: Colour | Color | undefined;
1145+
background?: ColorLike | undefined;
11451146
}
11461147

11471148
interface OpenSlideInputOptions {
@@ -1510,7 +1511,7 @@ declare namespace sharp {
15101511

15111512
interface RotateOptions {
15121513
/** parsed by the color module to extract values for red, green, blue and alpha. (optional, default "#000000") */
1513-
background?: Colour | Color | undefined;
1514+
background?: ColorLike | undefined;
15141515
}
15151516

15161517
type Precision = 'integer' | 'float' | 'approximate';
@@ -1526,7 +1527,7 @@ declare namespace sharp {
15261527

15271528
interface FlattenOptions {
15281529
/** background colour, parsed by the color module, defaults to black. (optional, default {r:0,g:0,b:0}) */
1529-
background?: Colour | Color | undefined;
1530+
background?: ColorLike | undefined;
15301531
}
15311532

15321533
interface NegateOptions {
@@ -1551,7 +1552,7 @@ declare namespace sharp {
15511552
/** Position, gravity or strategy to use when fit is cover or contain. (optional, default 'centre') */
15521553
position?: number | string | undefined;
15531554
/** Background colour when using a fit of contain, parsed by the color module, defaults to black without transparency. (optional, default {r:0,g:0,b:0,alpha:1}) */
1554-
background?: Colour | Color | undefined;
1555+
background?: ColorLike | undefined;
15551556
/** The kernel to use for image reduction. (optional, default 'lanczos3') */
15561557
kernel?: keyof KernelEnum | undefined;
15571558
/** Do not enlarge if the width or height are already less than the specified dimensions, equivalent to GraphicsMagick's > geometry option. (optional, default false) */
@@ -1594,14 +1595,14 @@ declare namespace sharp {
15941595
/** single pixel count to right edge (optional, default 0) */
15951596
right?: number | undefined;
15961597
/** background colour, parsed by the color module, defaults to black without transparency. (optional, default {r:0,g:0,b:0,alpha:1}) */
1597-
background?: Colour | Color | undefined;
1598+
background?: ColorLike | undefined;
15981599
/** how the extension is done, one of: "background", "copy", "repeat", "mirror" (optional, default `'background'`) */
15991600
extendWith?: ExtendWith | undefined;
16001601
}
16011602

16021603
interface TrimOptions {
16031604
/** Background colour, parsed by the color module, defaults to that of the top-left pixel. (optional) */
1604-
background?: Colour | Color | undefined;
1605+
background?: ColorLike | undefined;
16051606
/** Allowed difference from the above colour, a positive number. (optional, default 10) */
16061607
threshold?: number | undefined;
16071608
/** Does the input more closely resemble line art (e.g. vector) rather than being photographic? (optional, default false) */
@@ -1617,15 +1618,8 @@ declare namespace sharp {
16171618
/** 1 for grayscale, 2 for grayscale + alpha, 3 for sRGB, 4 for CMYK or RGBA */
16181619
type Channels = 1 | 2 | 3 | 4;
16191620

1620-
interface RGBA {
1621-
r?: number | undefined;
1622-
g?: number | undefined;
1623-
b?: number | undefined;
1624-
alpha?: number | undefined;
1625-
}
1626-
1627-
type Colour = string | RGBA;
1628-
type Color = Colour;
1621+
type Colour = ColorLike;
1622+
type Color = ColorLike;
16291623

16301624
interface Kernel {
16311625
/** width of the kernel in pixels. */
@@ -1691,7 +1685,7 @@ declare namespace sharp {
16911685
/** Tile angle of rotation, must be a multiple of 90. (optional, default 0) */
16921686
angle?: number | undefined;
16931687
/** background colour, parsed by the color module, defaults to white without transparency. (optional, default {r:255,g:255,b:255,alpha:1}) */
1694-
background?: string | RGBA | undefined;
1688+
background?: ColorLike | undefined;
16951689
/** How deep to make the pyramid, possible values are "onepixel", "onetile" or "one" (default based on layout) */
16961690
depth?: string | undefined;
16971691
/** Threshold to skip tile generation, a value 0 - 255 for 8-bit images or 0 - 65535 for 16-bit images */

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@
140140
"vips"
141141
],
142142
"dependencies": {
143-
"@img/colour": "^1.0.0",
143+
"@img/colour": "^1.1.0",
144144
"detect-libc": "^2.1.2",
145145
"semver": "^7.7.4"
146146
},

test/types/sharp.test-d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// biome-ignore-all lint/correctness/noUnusedFunctionParameters: types only test file
22
// biome-ignore-all lint/correctness/noUnusedVariables: types only test file
33

4-
import sharp = require('../../');
4+
import sharp from '../../';
55

66
import { createReadStream, createWriteStream } from 'node:fs';
77

@@ -726,7 +726,7 @@ sharp(input).composite([
726726

727727
// Support format-specific input options
728728
const colour: sharp.Colour = '#fff';
729-
const color: sharp.Color = '#fff';
729+
const color: sharp.Color = { l: 1, a: 2, b: 3 };
730730
sharp({ pdf: { background: colour } });
731731
sharp({ pdf: { background: color } });
732732
sharp({ pdfBackground: colour }); // Deprecated

test/types/tsconfig.json

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)