|
| 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]; |
0 commit comments