Skip to content

Commit 52fad74

Browse files
authored
🤖 Merge PR DefinitelyTyped#73118 Add TypeScript definitions and tests for Color Thief by @sillyangel
1 parent eca5b6a commit 52fad74

File tree

5 files changed

+204
-0
lines changed

5 files changed

+204
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*
2+
!**/*.d.ts
3+
!**/*.d.cts
4+
!**/*.d.mts
5+
!**/*.d.*.ts
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Test file for Color Thief type definitions
2+
// This file should compile without errors when the types are correct
3+
4+
// Import the ColorThief class for testing
5+
import ColorThief from "colorthief";
6+
7+
// Browser version tests
8+
const colorThief = new ColorThief();
9+
const img = document.createElement("img");
10+
11+
// Test getColor method
12+
const dominantColor: ColorThief.RGBColor = colorThief.getColor(img);
13+
const dominantColorWithQuality: ColorThief.RGBColor = colorThief.getColor(img, 5);
14+
15+
// Test getPalette method
16+
const palette: ColorThief.RGBColor[] = colorThief.getPalette(img);
17+
const paletteWithOptions: ColorThief.RGBColor[] = colorThief.getPalette(img, 8, 5);
18+
19+
// Test callback-based methods with proper typing
20+
colorThief.getColorFromUrl(
21+
"https://example.com/image.jpg",
22+
(color: ColorThief.RGBColor, source: HTMLImageElement | string) => {
23+
console.log("Color:", color);
24+
console.log("Source:", source);
25+
},
26+
);
27+
28+
colorThief.getImageData("https://example.com/image.jpg", (dataUrl: string) => {
29+
console.log("Data URL:", dataUrl);
30+
});
31+
32+
colorThief.getColorAsync(
33+
"https://example.com/image.jpg",
34+
(color: ColorThief.RGBColor, source: HTMLImageElement | string) => {
35+
console.log("Async Color:", color);
36+
console.log("Source:", source);
37+
},
38+
);
39+
40+
// Type checking - these should be valid types
41+
const rgbColor: ColorThief.RGBColor = [255, 128, 0];
42+
const options: ColorThief.Options = {
43+
colorCount: 5,
44+
quality: 8,
45+
};
46+
47+
// Callback type checking
48+
const colorCallback: ColorThief.ColorCallback = (color: ColorThief.RGBColor, source: HTMLImageElement | string) => {
49+
const [r, g, b] = color;
50+
console.log(`RGB: ${r}, ${g}, ${b}`);
51+
};
52+
53+
const imageDataCallback: ColorThief.ImageDataCallback = (dataUrl: string) => {
54+
console.log("Received data URL:", dataUrl);
55+
};
56+
57+
// Test array destructuring
58+
const [red, green, blue] = dominantColor;
59+
console.log(`Red: ${red}, Green: ${green}, Blue: ${blue}`);
60+
61+
// Test palette iteration
62+
palette.forEach((color: ColorThief.RGBColor) => {
63+
const [r, g, b] = color;
64+
console.log(`Color: rgb(${r}, ${g}, ${b})`);
65+
});
66+
67+
// Test type constraints
68+
const validColorCount: number = 10; // should be between 2-20
69+
const validQuality: number = 5; // should be >= 1
70+
71+
// Test optional parameters
72+
const colorNoQuality = colorThief.getColor(img);
73+
const paletteNoOptions = colorThief.getPalette(img);
74+
const paletteWithColorCount = colorThief.getPalette(img, 5);
75+
76+
// Test that RGB values are numbers
77+
const testColor: ColorThief.RGBColor = [255, 128, 0];
78+
const rValue: number = testColor[0];
79+
const gValue: number = testColor[1];
80+
const bValue: number = testColor[2];
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/// <reference lib="dom" />
2+
3+
export = ColorThief;
4+
export as namespace ColorThief;
5+
6+
declare namespace ColorThief {
7+
/**
8+
* RGB color tuple representing a color with red, green, and blue values (0-255)
9+
*/
10+
type RGBColor = [number, number, number];
11+
12+
/**
13+
* Options for color extraction methods
14+
*/
15+
interface Options {
16+
/**
17+
* Number of colors to extract for palette (2-20, default: 10)
18+
* For getColor(), this is internally set to 5
19+
*/
20+
colorCount?: number;
21+
22+
/**
23+
* Quality/speed trade-off (1 = highest quality, 10 = default)
24+
* Higher numbers are faster but may miss colors
25+
*/
26+
quality?: number;
27+
}
28+
29+
/**
30+
* Callback function for asynchronous color extraction
31+
*/
32+
type ColorCallback = (color: RGBColor, source: HTMLImageElement | string) => void;
33+
34+
/**
35+
* Callback function for image data retrieval
36+
*/
37+
type ImageDataCallback = (dataUrl: string) => void;
38+
}
39+
40+
/**
41+
* Color Thief - Extract dominant colors from images
42+
*/
43+
declare class ColorThief {
44+
/**
45+
* Extract the dominant color from an image
46+
* @param sourceImage - HTML image element
47+
* @param quality - Quality/speed trade-off (1 = highest quality, 10 = default)
48+
* @returns RGB color array [r, g, b] where values are 0-255
49+
*/
50+
getColor(sourceImage: HTMLImageElement, quality?: number): ColorThief.RGBColor;
51+
52+
/**
53+
* Extract a palette of colors from an image
54+
* @param sourceImage - HTML image element
55+
* @param colorCount - Number of colors to extract (2-20, default: 10)
56+
* @param quality - Quality/speed trade-off (1 = highest quality, 10 = default)
57+
* @returns Array of RGB color arrays [[r, g, b], ...] where values are 0-255
58+
*/
59+
getPalette(sourceImage: HTMLImageElement, colorCount?: number, quality?: number): ColorThief.RGBColor[];
60+
61+
/**
62+
* Extract the dominant color from an image URL (asynchronous)
63+
* @param imageUrl - URL of the image
64+
* @param callback - Callback function to receive the color
65+
* @param quality - Quality/speed trade-off (1 = highest quality, 10 = default)
66+
*/
67+
getColorFromUrl(imageUrl: string, callback: ColorThief.ColorCallback, quality?: number): void;
68+
69+
/**
70+
* Get image data as base64 data URL from a URL
71+
* @param imageUrl - URL of the image
72+
* @param callback - Callback function to receive the data URL
73+
*/
74+
getImageData(imageUrl: string, callback: ColorThief.ImageDataCallback): void;
75+
76+
/**
77+
* Extract the dominant color from an image URL asynchronously with base64 conversion
78+
* @param imageUrl - URL of the image
79+
* @param callback - Callback function to receive the color
80+
* @param quality - Quality/speed trade-off (1 = highest quality, 10 = default)
81+
*/
82+
getColorAsync(imageUrl: string, callback: ColorThief.ColorCallback, quality?: number): void;
83+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"private": true,
3+
"name": "@types/colorthief",
4+
"version": "2.6.9999",
5+
"projects": [
6+
"https://github.com/lokesh/color-thief"
7+
],
8+
"devDependencies": {
9+
"@types/colorthief": "workspace:."
10+
},
11+
"owners": [
12+
{
13+
"name": "sillyangel",
14+
"githubUsername": "sillyangel"
15+
}
16+
]
17+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"compilerOptions": {
3+
"module": "node16",
4+
"lib": [
5+
"es6"
6+
],
7+
"noImplicitAny": true,
8+
"noImplicitThis": true,
9+
"strictFunctionTypes": true,
10+
"strictNullChecks": true,
11+
"types": [],
12+
"noEmit": true,
13+
"forceConsistentCasingInFileNames": true
14+
},
15+
"files": [
16+
"index.d.ts",
17+
"colorthief-tests.ts"
18+
]
19+
}

0 commit comments

Comments
 (0)