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

chroma-extractor

v1.0.5

Published

JS/React package used to extract chroma values from different image file types.

Downloads

5

Readme

useChromaExtractor

useChromaExtractor is a custom React hook that provides functionality to extract prominent colors, average color, and a color palette from an image file. This hook uses the color.js and extract-colors packages to perform the color extraction operations.

Installation

To use useChromaExtractor, you need to install the library using the following command: npm install chroma-extractor.js Or you can clone or download this project and copy the useChromaExtractor.js file into your React project.

Usage

Import the Hook: import useChromaExtractor from './path/to/useChromaExtractor';

Use the Hook in a Component:

import React, { useState } from 'react';

import useChromaExtractor from './path/to/useChromaExtractor';

function ColorExtractorComponent() {

    const { chroma, extractProminent, extractAverage, extractPalette } = useChromaExtractor();
    const [selectedFile, setSelectedFile] = useState(null);

    const handleFileChange = (event) => {
        setSelectedFile(event.target.files[0]);
    };

    const handleExtractColors = () => {
        if (selectedFile) {
            extractProminent(selectedFile);
            extractAverage(selectedFile);
            extractPalette(selectedFile);
        }
    };

    return (
        <div>
            <input type="file" onChange={handleFileChange} />
            <button onClick={handleExtractColors}>Extract Colors</button>
            <div>
                <h3>Prominent Color:</h3>
                <p>{chroma.prominent}</p>
                <h3>Average Color:</h3>
                <p>{chroma.average}</p>
                <h3>Color Palette:</h3>
                <ul>
                    {chroma.palette.map((color, index) => (
                        <li key={index} style={{ backgroundColor: color }}>{color}</li>
                    ))}
                </ul>
            </div>
        </div>
    );
}

Hook API

useChromaExtractor provides the following functions:

extractProminent(img, format='hex'): Extracts the most prominent color from an image. The format can be 'hex' or 'rgb'.

extractAverage(img, format='hex'): Extracts the average color from an image. The format can be 'hex' or 'rgb'.

extractPalette(img, format='hex'): Extracts the color palette from an image. The format can be 'hex' or 'rgb'.

Returned State

The hook returns a chroma object with the following fields:

chroma.prominent: The extracted prominent color from the image.

chroma.average: The extracted average color from the image.

chroma.palette: The extracted color palette from the image.

Full Example

import React, { useState } from 'react';
import useChromaExtractor from './path/to/useChromaExtractor';

function ColorExtractorComponent() {
    const { chroma, extractProminent, extractAverage, extractPalette } = useChromaExtractor();
    const [selectedFile, setSelectedFile] = useState(null);

    const handleFileChange = (event) => {
        setSelectedFile(event.target.files[0]);
    };

    const handleExtractColors = () => {
        if (selectedFile) {
            extractProminent(selectedFile);
            extractAverage(selectedFile);
            extractPalette(selectedFile);
        }
    };

    return (
        <div>
            <input type="file" onChange={handleFileChange} />
            <button onClick={handleExtractColors}>Extract Colors</button>
            <div>
                <h3>Prominent Color:</h3>
                <p>{chroma.prominent}</p>
                <h3>Average Color:</h3>
                <p>{chroma.average}</p>
                <h3>Color Palette:</h3>
                <ul>
                    {chroma.palette.map((color, index) => (
                        <li key={index} style={{ backgroundColor: color }}>{color}</li>
                    ))}
                </ul>
            </div>
        </div>
    );
}

export default ColorExtractorComponent;

Contributing

Contributions are welcome! If you have any improvements or corrections, feel free to open a pull request or submit an issue.

License

This project is licensed under the MIT License. See the LICENSE file for more details.