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

color-thief-react

v2.1.0

Published

A React component with hooks for Color Thief. Grab color palette from an image with javascript

Downloads

71,663

Readme

Color Thief (React)

🎨 A React component with hooks for Color Thief. Grab color palette from an image with javascript Use the official Lokesh's color-thief.

npm (scoped) Build Status codecov semantic-release GitHub

Do you like?

Please, consider supporting my work as a lot of effort takes place to create this component! Thanks a lot.

Demo

See a real demo in the Codesandbox

Install

npm i -S color-thief-react
yarn add color-thief-react

Basic Usage

import Color from 'color-thief-react';
// In your render...
<Color src={IMAGE_URL}>
  {({ data, loading, error }) => (
    <div style={{ color: data }}>
      Text with the predominant color
    </div>
  )}
</Color>

API

Color

Return the predominant color of the image. You can use a React component or hook. Both have the same props.

src: Required. Link of the image


format: Format of the response. Can be rgbString, rgbArray, hslString, hslArray, hex or a CSS keyword. Default is rgbString

Color conversion is made possible by the color-convert package.

Examples

rgbString: 'rgb(89, 197, 182)' rgbArray: [89, 197, 182] hslString: 'hsl(172, 48%, 56%)' hslArray: [172, 48, 56] hex: '#59c5b6' keyword: 'mediumaquamarine'

CSS Keywords keyword: Color keywords are case-insensitive identifiers that represent a specific color, such as red, blue, black, or lightseagreen.

Keywords are matched to the closest color. See this page on Web Colors for a complete list of colors that can be returned with the keyword color format.


crossOrigin: Tag cross-origin for image


quality: Quality 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. Read more in https://lokeshdhakar.com/projects/color-thief/

Usage

import { useColor } from 'color-thief-react'

const { data, loading, error } = useColor(src, format, { crossOrigin, quality})

<div style={{ color: data }}>
  Text with the predominant color
</div>
import Color from 'color-thief-react';
// In your render...
<Color src={IMAGE_URL} format="hex">
  {({ data, loading, error }) => (
    <div style={{ color: data }}>
      Text with the predominant color
    </div>
  )}
</Color>

Palette

Return a palette of colors based on the predominance of colors on the image. You can use a React component or hook. Both have the same props.

src: Required. Link of the image

colorCount: Count of colors of the palette. Default is 2

format: Format of the response. See the format section in the Color chapter for a detailed API.

crossOrigin: Tag cross-origin for image

quality: Default is 10. Quality 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. Read more in https://lokeshdhakar.com/projects/color-thief/

import { Palette } from 'color-thief-react';
// In your render...
<Palette src={IMAGE_URL} colorCount={2}>
  {({ data, loading, error }) => (
    <div style={{ color: data[0], backgroundColor: data[1] }}>
      Text with the predominant color
    </div>
  )}
</Palette>
import { usePalette } from 'color-thief-react'

const { data, loading, error } = usePalette(src, colorCount, format, { crossOrigin, quality})

<div style={{ color: data[0], backgroundPalette: data[1] }}>
  Text with the predominant color
</div>