npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

use-color-thief

v1.3.2

Published

A react hook version of the package Color Thief, which generates color palettes from images

Downloads

496

Readme

useColorThief

A React Hook wrapper for the package color-thief made by lokesh

npm version npm downloads bundle size license

example

Requirements

To use this package you must have at least React version 16.8.0 as that is when hooks were first added.

Installation

npm i -S use-color-thief

Example

import useColorThief from 'use-color-thief';
import { useEffect } from 'react';

const source = 'https://source.unsplash.com/random/1280x720';

const MyComponent = () => {
  const { color, palette } = useColorThief(source, {
    format: 'hex',
    colorCount: 10,
    quality: 10,
  });

  useEffect(() => {
    console.log('Color:', color);
    console.log('Palette:', palette);
  }, [palette, color]);

  return <div>{color}</div>;
};

export default MyComponent;

API

const { color, palette } = useColorThief(source, {
  format: 'rgb',
  colorCount: 10,
  quality: 10,
});

Parameters

source

Type: String or React Ref

The image to grab the primary color and palette from. The source can be one of 3 options:

  • Url — An http url to pull an image from
    • e.g. https://www.example.com/my-image.png
  • Base 64 Data Url — A data url representation of the image
    • e.g. data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAS...
  • React Ref — A react ref created either through useRef or createRef (most likely useRef) that is attached to an img element in your jsx
    • e.g.
const imgRef = useRef();

const output = useColorThief(imgRef);

return <img ref={imgRef} src="https://www.example.com/my-image.png" />;

options

Type: object

The optional parameters you can pass to modify the output of the hook.

options.format

Type: string — Default: 'rgb'

The format option changes the format of the color and palette outputs. You pass either rgb or hex.

options.quality

Type: number — Default: 10

An optional argument that must be an Integer of value 1 or greater, and defaults to 10. The number determines how many pixels are skipped before the next one is sampled. We rarely need to sample every single pixel in the image to get good results. The bigger the number, the faster a value will be returned. This field applies to both returns, color and palette.

options.colorCount

Type: number — Default: 10

The number of colors to return for the palette field.

Output

This hook returns an object containing two fields, color and palette.

output.color

Type: Array[Integer] or String — Default: null

The dominant color from the image. If nothing or rgb is passed for options.format, this value will be returned as an array of three integers representing red, green, and blue values. If hex is passed for options.format the color will be returned as a string in hex format, #RRGGBB.

output.palette

Type: Array[Array[Integer]] or Array[String] - Default: null

A palette from the image by clustering similar colors. The palette is returned as an array containing colors, each color matching the same format as the output.color value returned.

Demos

Each demo grabs a random image each time, so reload to see a different example output