A lightweight JavaScript utility for extracting colors from images using various sampling methods. Perfect for getting color schemes, dominant colors, or quick color analysis from images.
- 🚀 Multiple sampling methods for different use cases
- 📦 Zero dependencies
- 🎨 Returns RGB color values
- 💻 Works in browser environments
- ⚡ Fast and efficient processing
- 🔄 Async/Promise-based API
npm install get-some-pixel-colorsimport { analyzeImageColor } from "get-some-pixel-colors";
// Using with an image element
const imgElement = document.querySelector("img");
const color = await analyzeImageColor(imgElement, "dominant");
console.log(color); // { r: 255, g: 128, b: 0 }
// Using with an image URL
import { getImageColor } from "get-some-pixel-colors";
const color = await getImageColor("path/to/image.jpg", "firstRow");const imgElement = document.querySelector("img");
const color = await analyzeImageColor(imgElement, "dominant");
document.body.style.backgroundColor = `rgb(${color.r}, ${color.g}, ${color.b})`;firstRow: Samples the first row of pixels (fastest)- Best for headers or banners where top color is most important
topLeft: Samples the top-left corner (10x10 pixels)- Good for thumbnails or icons
fourCorners: Samples all four corners (5x5 pixels each)- Better overall color representation without full analysis
dominant: Full image analysis for dominant color (most accurate)- Best when accuracy is more important than speed
Analyzes an image element using the specified method.
imageElement: HTML Image Elementmethod: String (optional, defaults to 'firstRow')- 'firstRow'
- 'topLeft'
- 'fourCorners'
- 'dominant'
Promise resolving to an object with RGB values:
{
r: number, // Red (0-255)
g: number, // Green (0-255)
b: number // Blue (0-255)
}Same as analyzeImageColor but accepts an image URL instead of an element.
Works in all modern browsers that support:
- Canvas API
- Promises
- Async/Await
try {
const color = await getImageColor("path/to/image.jpg", "dominant");
console.log(color);
} catch (error) {
console.error("Error analyzing image:", error);
}firstRowandtopLeftmethods are fastestdominantmethod may be slower on large images- Consider using
fourCornersfor a good balance of speed and accuracy
Contributions are welcome! Please feel free to submit a Pull Request.
MIT