UNPKG

avecolor

Version:

A lightweight JavaScript utility to extract the average color from any image. Supports HEX or RGB output.

165 lines (115 loc) โ€ข 3.43 kB
# ๐ŸŽจ avecolor A lightweight JavaScript + TypeScript utility to extract the **average (dominant)** color from any image โ€” perfect for generating matching backgrounds, borders, or shadows automatically in your apps. --- ## ๐Ÿš€ Features - โšก Fast and simple โ€” pure JavaScript, no dependencies - ๐ŸŽจ Supports **HEX** or **RGBA** formats - ๐Ÿงฉ Optional settings: `opacity`, `skipWhite`, and `format` - ๐Ÿง  Works with **React**, **Vanilla JS**, and **TypeScript** - ๐Ÿ”’ Handles CORS and transparent pixels --- ![screenshot](/screenshot.jpg) ## ๐Ÿ“ฆ Installation Install via npm: ```bash npm install avecolor ``` or via yarn: ```bash yarn add avecolor ``` --- ## ๐Ÿ“˜ Usage (JavaScript) ```js import { getAverageColor } from "avecolor"; async function applyBackground() { const color = await getAverageColor("/images/example.jpg"); document.body.style.backgroundColor = color; } applyBackground(); ``` --- ## โš›๏ธ Usage (React + TypeScript Example) ```tsx // image card component import { useEffect, useState } from "react"; import { getAverageColor } from "avecolor"; interface ImageCardProps { src: string; alt: string; } export default function ImageCard({ src, alt = "" }: ImageCardProps) { const [color, setColor] = useState("#000000"); useEffect(() => { let isMounted = true; getAverageColor(src, { skipWhite: true, format: "hex" }) .then((c) => isMounted && setColor(c)) .catch((err) => console.error(`Failed to get average color for ${src}:`, err) ); return () => { isMounted = false; }; }, [src]); return ( <div style={{ boxShadow: `0 8px 80px -4px ${color}`, transition: "box-shadow 0.3s ease", borderRadius: "16px", overflow: "hidden", }} > <img style={{ width: "100%", height: "auto", display: "block" }} src={src} alt={alt} /> </div> ); } ``` ```tsx // parent component import ImageCard from "./components/ImageCard"; import photo1 from "./assets/image1.jpg"; <div className="max-w-100"> <ImageCard src={photo1} alt="cover" /> </div>; ``` --- ## ๐Ÿง  Options | Option | Type | Default | Description | | ----------- | ------------------ | ------- | -------------------------- | | `format` | `"hex"` or `"rgb"` | `"hex"` | Output color format | | `opacity` | `number` | `1` | Opacity from 0 โ†’ 1 | | `skipWhite` | `boolean` | `false` | Ignore nearly white pixels | --- ## ๐Ÿงฉ Example Outputs ```js await getAverageColor("image.jpg"); // "#9e6df2" await getAverageColor("image.jpg", { format: "rgb", opacity: 0.6 }); // "rgba(158, 109, 242, 0.6)" ``` --- ## ๐Ÿงฑ Use Cases - Auto-generate **backgrounds** that match images - Create **themed cards** or **image shadows** - Dynamic **UI color adaptation** (like Spotify or YouTube thumbnails) --- ## ๐Ÿงฐ TypeScript Support Type definitions are included out of the box โ€” no setup required. ```ts import { getAverageColor } from "avecolor"; ``` --- ## ๐Ÿท๏ธ Keywords `color` ยท `average color` ยท `dominant color` ยท `image color` ยท `background generator` ยท `color extractor` ยท `react color util` ยท `style helper` --- ## ๐Ÿง‘โ€๐Ÿ’ป Author **Milad Gharibi** [GitHub Profile](https://github.com/micodex) --- ## ๐Ÿ“„ License MIT License ยฉ 2025 Milad Gharibi - micodex