use-image-color
Version:
Grab color palette from any image using React hooks
78 lines (60 loc) • 3 kB
Markdown
Grab color palette from any image using this hook
<p align="center">
<a href="http://hits.dwyl.com/jeffersonlicet/use-image-color"><img src="http://hits.dwyl.com/jeffersonlicet/use-image-color.svg"></a>
<a href="https://www.npmjs.com/package/use-image-color"><img src="https://img.shields.io/npm/v/use-image-color?style=flat-square"></a>
<a href="https://bundlephobia.com/result?p=use-image-color@0.0.7"><img src="https://img.shields.io/bundlephobia/min/use-image-color?style=flat-square"></a>
</p>

It renders your image on a canvas and then generates its palette of colors using color quantization.
This image component renders a placeholder color while your original images are loading.
**Before loading big images (color from smaller images)**

**After loading big images:**

## Live Sandbox demo:
[Live Sandbox Demo](https://codesandbox.io/s/great-glade-sldoe?fontsize=14&hidenavigation=1&theme=dark)

## How does this image component work?
You must provide src (your original image) and a thumbnail (smaller image).
As soon as the thumbnail is loaded the skeleton becomes visible using its dominant color. In the meantime, your original image continues loading, finally, when your original image arrives, it replaces the skeleton.
# Installation:
`npm install use-image-color`
# The hook:
## Usage:
```javascript
import useImageColor from 'use-image-color'
export function Card() {
const { colors } = useImageColor(url, { cors: true, colors: 5 })
return (...);
}
```
```javascript
useImageColor(url: String, options: OptionsObject)
```
| param | description | default |
|------------|--------------------------------------------------------|---------|
| cors | Use CORS. Enable this if you are using external images | false |
| colors | Number of colors to grab | 5 |
| format | Output format: rgb or hex | hex |
| windowSize | Size of window to grab pixels, low values are faster | 50 |
# The component:
## Usage:
```javascript
import { Image } from 'use-image-color'
export function Card() {
return (
<Image src={url} thumbnail={thumbnail} />
);
}
```
| param | description | required |
|------------|--------------------------------------------------------|---------|
| thumbnail | Small version of your image | true |
| src | Original version of your image | true |
| wrapperStyle | Style object to apply to the wrapper of the image and the color | false |
| wrapperClass | Class to apply to the wrapper of the image and the color | false |