Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions types/colorthief/colorthief-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@
// Import the ColorThief class for testing
import ColorThief from "colorthief";

// Node.js version tests
const imgPath = "rainbow.png";

// Test getColor method
const dominantColorNode: Promise<ColorThief.RGBColor> = ColorThief.getColor(imgPath);
const dominantColorWithQualityNode: Promise<ColorThief.RGBColor> = ColorThief.getColor(imgPath, 5);

// Test getPalette method
const paletteNode: Promise<ColorThief.RGBColor[]> = ColorThief.getPalette(imgPath);
const paletteWithOptionsNode: Promise<ColorThief.RGBColor[]> = ColorThief.getPalette(imgPath, 8, 5);

// Browser version tests
const colorThief = new ColorThief();
const img = document.createElement("img");
Expand Down
33 changes: 33 additions & 0 deletions types/colorthief/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/// <reference lib="dom" />
/// <reference types="node" />

export = ColorThief;
export as namespace ColorThief;
Expand Down Expand Up @@ -41,6 +42,38 @@ declare namespace ColorThief {
* Color Thief - Extract dominant colors from images
*/
declare class ColorThief {
// --------------------------------------------------------
// Node.js usage (Static methods returning Promises)
// --------------------------------------------------------

/**
* Extract the dominant color from an image (Node.js)
* @param sourceImage - Path to the image file, or Buffer containing image
* @param quality - Quality/speed trade-off (1 = highest quality, 10 = default)
* @returns Promise resolving to RGB color array [r, g, b] where values are 0-255
*/
static getColor(
sourceImage: Buffer | Uint8Array | ArrayBuffer | string,
quality?: number,
): Promise<ColorThief.RGBColor>;

/**
* Extract a palette of colors from an image (Node.js)
* @param sourceImage - Path to the image file, or Buffer containing image
* @param colorCount - Number of colors to extract (2-20, default: 10)
* @param quality - Quality/speed trade-off (1 = highest quality, 10 = default)
* @returns Promise resolving to Array of RGB color arrays [[r, g, b], ...] where values are 0-255
*/
static getPalette(
sourceImage: Buffer | Uint8Array | ArrayBuffer | string,
colorCount?: number,
quality?: number,
): Promise<ColorThief.RGBColor[]>;

// --------------------------------------------------------
// Browser usage (Instance methods)
// --------------------------------------------------------

/**
* Extract the dominant color from an image
* @param sourceImage - HTML image element
Expand Down
3 changes: 3 additions & 0 deletions types/colorthief/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
"projects": [
"https://github.com/lokesh/color-thief"
],
"dependencies": {
"@types/node": "*"
},
"devDependencies": {
"@types/colorthief": "workspace:."
},
Expand Down