A modern, fast JavaScript library for extracting dominant colors and color palettes from images. Built with ES6+ features and optimized for modern browsers.
*With additional upload functionality, so now it provides you the possibility to try this plugin to see if it fits your needs. -Aleks
See a Demo | Read more on Lokesh's blog | Try the Modern Version
- π¨ Advanced Algorithm: Uses modified median cut quantization (MMCQ) for accurate color extraction
- β‘ Modern Performance: Built with ES6+ features, optimized for speed
- π± Responsive: Works on desktop, tablet, and mobile devices
- π Easy Integration: Simple API that works with any image source
- π Dark Mode: Automatic dark mode support
- βΏ Accessible: Built with accessibility in mind
- π¦ Tree Shakable: ES modules for optimal bundle size
npm install color-thief-modernimport { ColorThief } from 'color-thief-modern';
// Get dominant color
const dominantColor = ColorThief.getDominantColor(imageElement);
// Returns: [r, g, b] array
// Get color palette
const palette = ColorThief.getPalette(imageElement, 8);
// Returns: [[r,g,b], [r,g,b], ...] array
// Convert to hex
const hexColor = ColorThief.rgbToHex(dominantColor);
// Returns: "#ff5733"
// Convert to HSL
const hslColor = ColorThief.rgbToHsl(dominantColor);
// Returns: [h, s, l] array<!DOCTYPE html>
<html>
<head>
<script type="module">
import { ColorThief } from './path/to/color-thief.js';
const img = document.getElementById('myImage');
img.addEventListener('load', function () {
const dominantColor = ColorThief.getDominantColor(this);
console.log('Dominant color:', dominantColor);
});
</script>
</head>
<body>
<img id="myImage" src="image.jpg" crossorigin="anonymous" />
</body>
</html>Returns the dominant color from an image. Uses the median cut algorithm provided by quantize.js to cluster similar colors and return the base color from the largest cluster.
Parameters:
image(HTMLImageElement|HTMLCanvasElement): The source imagequality(number, optional): Quality/speed trade-off (1-10, default: 10)
Returns: number[]|null - RGB array [r, g, b] or null if failed
Returns a color palette from an image. Uses the median cut algorithm provided by quantize.js to cluster similar colors.
Parameters:
image(HTMLImageElement|HTMLCanvasElement): The source imagecolorCount(number, optional): Number of colors to extract (2-20, default: 10)quality(number, optional): Quality/speed trade-off (1-10, default: 10)
Returns: number[][]|null - Array of RGB arrays [[r,g,b], ...] or null if failed
The original function names are still available as named exports:
import { getDominantColor, createPalette } from 'color-thief-modern';
// Original API still works
getDominantColor(sourceImage); // Returns [num, num, num]
createPalette(sourceImage, colorCount); // Returns [[num, num, num], ...]Returns the average color from an image.
Parameters:
image(HTMLImageElement|HTMLCanvasElement): The source imagesampleSize(number, optional): Pixel sampling rate (default: 10)
Returns: number[]|null - RGB array [r, g, b] or null if failed
Converts RGB array to hex string.
Parameters:
rgb(number[]): RGB array [r, g, b]
Returns: string - Hex color string (e.g., "#ff5733")
Converts RGB array to HSL array.
Parameters:
rgb(number[]): RGB array [r, g, b]
Returns: number[] - HSL array [h, s, l]
- Node.js 16+
- npm or yarn
# Clone the repository
git clone https://github.com/achudars/color-thief.git
cd color-thief
# Install dependencies
npm install
# Start development server
npm run dev
# Build for production
npm run build
# Run linting
npm run lint
# Format code
npm run formatβββ src/
β βββ color-thief.js # Main library
β βββ quantize.js # Color quantization algorithm
β βββ main.js # Demo application
β βββ styles.css # Modern CSS styles
βββ public/ # Static assets
βββ dist/ # Built files
βββ index.html # Modern demo page
βββ package.json
βββ vite.config.js # Vite configuration
βββ eslint.config.js # ESLint configuration
βββ .prettierrc # Prettier configuration
- β ES6+ classes and modules
- β Async/await and Promises
- β Optional chaining and nullish coalescing
- β Template literals and destructuring
- β Arrow functions and const/let
- β Improved algorithm efficiency
- β Better memory management
- β Reduced DOM manipulation
- β Tree-shakable modules
- β TypeScript-like JSDoc annotations
- β Modern build system (Vite)
- β ESLint and Prettier configuration
- β Better error handling and validation
- β Drag & drop file upload
- β Click to copy colors
- β Responsive design
- β Dark mode support
- β Accessibility improvements
- Chrome 61+
- Firefox 60+
- Safari 12+
- Edge 79+
By Lokesh Dhakar | lokeshdhakar.com | twitter.com/lokeshdhakar
Modernized by Aleksandrs Cudars with additional upload functionality
Thanks to Nick Rabinowitz for creating quantize.js which provides the MMCQ algorithm, jfsiii for a large number of code improvements, and others for submitting issues and fixes.
Licensed under the Creative Commons Attribution 2.5 License
- β Free for use in both personal and commercial projects
- β Attribution required: Leave author name, author homepage link, and the license info intact