lokesh / color-thief

Grab the color palette from an image using just Javascript. Works in the browser and in Node.
https://lokeshdhakar.com/projects/color-thief/
MIT License
12.67k stars 1.31k forks source link

typescript declaration file #188

Closed nblthree closed 4 years ago

nblthree commented 4 years ago

Try npm install @types/colorthief if it exists or add a new declaration (.d.ts) file containing declare module 'colorthief';

unfortunately it doesn't exist

grahamking commented 3 years ago

Here's a colorthief.d.ts that works for me. I put it in src/types and typescript picked it up automatically.

declare module 'colorthief' {
  type Color = [number, number, number];
  export default class ColorThief {
    getColor: (img: HTMLImageElement | null) => Color;
    getPalette: (img: HTMLImageElement | null) => Color[];
  }
}
HeroadZ commented 2 years ago

version with optional parameters:

declare module 'colorthief' {
    export type RGBColor = [number, number, number];
    export default class ColorThief {
        getColor: (img: HTMLImageElement | null, quality: number=10) => RGBColor;
        getPalette: (img: HTMLImageElement | null, colorCount: number=10, quality: number=10) => RGBColor[];
    }
}
NatoBoram commented 2 years ago

getPalette can return null

https://github.com/lokesh/color-thief/blob/4dc3bb0b43250f0d006278998aa91c4156f77cc3/src/color-thief.js#L96

cabello commented 10 months ago

This is what I've been using for over 4 months, create a file colorthief.d.ts in your source directory, a d.ts file only contains type definitions:

declare module "colorthief" {
  export type RGBColor = [number, number, number]
  export default class ColorThief {
    getColor: (
      img: HTMLImageElement | null,
      quality: number = 10,
    ) => RGBColor | null

    getPalette: (
      img: HTMLImageElement | null,
      colorCount: number = 10,
      quality: number = 10,
    ) => RGBColor[] | null
  }
}
Fasteroid commented 1 month ago

Why closed the issue? These are great type definitions here but I'd really love if they actually existed on the real package!