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

react-extract-colors

v1.0.3

Published

A react component to extract dominant colors from an image

Downloads

54

Readme

Using npm
npm install react-extract-colors
Using yarn
yarn add react-extract-colors
Using pnpm
pnpm add react-extract-colors

Once the package is installed, you can import the library using import approach:

import { useExtractColor } from "react-extract-colors";
  • mainly supported image formats: (png, jpg, jpeg, svg, gif and more...)

When you have an image and you want to extract the dominant color, you can use the hook. The hook returns an object containing various properties.

If you only need the dominant color, you can use the dominantColor property.

You can also get the darker and lighter variants to create a grandient color.

import { useExtractColor } from "react-extract-colors";

const image = "https://picsum.photos/id/237/200/300";

const {
  colors,
  dominantColor,
  darkerColor,
  lighterColor,
  loading,
  error,
} = useExtractColor(image);
Properties 📊

| Property | Description | | :-------- | :------------------------- | | colors | An array containing the top dominant colors | | dominantColor | The dominant color of the image | | darkerColor | A darker variant of the dominant color | | lighterColor | A lighter variant of the dominant color | | loading | Indicates whether the image is loading | | error | Indicates whether the image has an error |

Explore a basic example to understand its usage, you can utilize it in various ways.

// import the hook
import { useExtractColor } from "react-extract-colors";

const image = "https://picsum.photos/id/237/200/300";

const App = () => {
    // Use the hook to extract the dominant color
  const { dominantColor, darkerColor, lighterColor } = useExtractColor(image);

  return (
    // set a linear gradient with colors extracted
    <div style={{ backgroundColor: `linear-gradient(45deg, ${dominantColor}, ${darkerColor}, ${lighterColor})` }}>
      <h1>Extract Color</h1>
      <img src={image} alt="random image" width="200" height="300" />
    </div>
  );
};

export default App;

You can also pass settings to the hook, to customize the extraction process.

Note: passing settings to the hook is an optional step.

// import the hook
import { useExtractColor } from "react-extract-colors";

const image = "https://picsum.photos/id/237/200/300";

const App = () => {
    // Use the hook to extract the dominant color
  const { colors } = useExtractColor(image, {
    maxColors: 3,
    format: "hex",
    maxSize: 200,
  })

  const [color1, color2, color3] = colors

  return (
    // set the background color to the dominant color
    <div style={{ border: `1px solid ${color1}` }}>
      <h1 style={{ color: color3 }}>Extract Color</h1>
      <img src={image} alt="random image" width="200" height="300" />
    </div>
  );
}

export default App;

maxColors:

The maxColors parameter determines the number of colors to be included in the colors array. When you provide an image to the hook, it extracts and counts the colors, returning the most dominant ones based on the specified limit.

Note: While you can retrieve more colors than the recommended limit, use this feature judiciously as its effectiveness varies with each image.

format:

You can specify the color format using one of the following options: rgba, rgb, hex, hsl, or hsv.

maxSize:

The maxSize is the size at which the image will be processed to extract colors. The smaller the size, the faster the processing may be, but it affects color accuracy to some extent. If you need more precision, you can set a higher value at the expense of sacrificing some speed.

| Parameter | Type | Description | Options (recommended) | | :-------- | :------- | :------------------------- | :------- | | maxColors | number | Number of colors to get in the colors array default: 5 | 0-100 | | format | string | Format to get the colors default: rgba | rgba rgb hex hsl hsv | | maxSize | number | Size to extract the colors default: 10 | 0-500 |

The React Extract Colors hook was created by Jesús Aguilar

Licensed under the MIT License - see the LICENSE.md file for details.