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
Markdown
# ๐จ 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

## ๐ฆ 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