Coder Social home page Coder Social logo

nitin42 / react-color-extractor Goto Github PK

View Code? Open in Web Editor NEW
367.0 7.0 30.0 42.09 MB

A React component which extracts colors from an image

Home Page: https://react-color-extractor.surge.sh/

JavaScript 86.25% CSS 13.75%
colors react hsl rgb hex converter image

react-color-extractor's Introduction

react-color-extractor

size version

What

react-color-extractor is a React component that extracts colors from an image.

Motivation

This is one of the tools that I am using in creative coding. I was learning color theory and wanted a React based library to extract a collection of swatches from an image. The extracted colors then can be used to create interesting gradient patterns, loading designs with identical color scheme or crafting a symmetric color scheme across a system.

Use cases

  • Design systems

  • Creative coding

  • Creating advanced color tools

Install

npm install react-color-extractor

or if you use yarn

yarn add react-color-extractor

This package also depends on React, so make sure you've it installed.

Example

import React from 'react'
import { ColorExtractor } from 'react-color-extractor'

class App extends React.Component {
  state = { colors: [] }

  renderSwatches = () => {
    const { colors } = this.state

    return colors.map((color, id) => {
      return (
        <div
          key={id}
          style={{
            backgroundColor: color,
            width: 100,
            height: 100
          }}
        />
      )
    })
  }

  getColors = colors =>
    this.setState(state => ({ colors: [...state.colors, ...colors] }))

  render() {
    return (
      <div>
        <ColorExtractor getColors={this.getColors}>
          <img
            src="https://i.imgur.com/OCyjHNF.jpg"
            style={{ width: 700, height: 500 }}
          />
        </ColorExtractor>
        <div
          style={{
            marginTop: 20,
            display: 'flex',
            justifyContent: 'center'
          }}
        >
          {this.renderSwatches()}
        </div>
      </div>
    )
  }
}

Checkout the demo on CodeSandbox

Edit k3wj33kzw3

Examples

Check out the examples folder.

Usage

react-color-extractor can be used in two different ways.

  • With image element as children
<ColorExtractor getColors={colors => console.log(colors)}>
  <img src="..." alt="..." style={{...}} />
</ColorExtractor>

Check out this example.

  • Passing a local or remote image, or a blob url via src prop
<ColorExtractor
  src="<local-or-remote-image-url-or-blob-url>"
  getColors={colors => console.log(colors)}
/>

Check out this example.

Using remote images

In development, make sure that you've configured proxy settings in your server config when you are using the remote images, otherwise you might run into CORS issue. You can also use this chrome extension to tackle CORS issue.

API

<ColorExtractor /> props

getColors

(colors: Array<Array<number> | string>) => void

getColors callback is invoked with an array of colors, either in hex or rgb format once the image is done processing. Use this callback to update the state with the colors array

<ColorExtractor getColors={colors => this.setState({ colors: colors })} />

rgb

type: boolean

When set to true, produces the color in rgb format. By default, colors produced are in hex format

<ColorExtractor rgb getColors={colors => console.log(colors)} />

This will log colors in rgb format

onError

(error: Object) => void

onError callback is invoked if there is any issue with processing the image.

<ColorExtractor onError={error => this.setState({ hasError: true , msg: error })}>

src

type: string

src prop accepts a remote or local image url, or a blob url.

<ColorExtractor
  src="https://i.imgur.com/OCyjHNF.jpg"
  getColors={colors => console.log(colors)}
/>

maxColors

type: number

maxColors prop accepts a number for amount of colors in palette from which swatches will be generated.

<ColorExtractor src="..." getColors={colors => ...} maxColors={128} />

Contributing

If you like to contribute to this project, then follow the below instructions to setup the project locally on your machine.

git clone https://github.com/<your_username_here>/react-color-extractor

cd react-color-extractor

yarn

Type checking

Run flow type checker using yarn flow

Building the source code

Run yarn build:component to build the source code.

TODO

  • Add quantizers

react-color-extractor's People

Contributors

nitin42 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

react-color-extractor's Issues

maxColors does not limit the number of colors

The prop called maxColors does not limit the number of colors from the element. No matter if i add 4 (as number) or 10 (as number) it is still the same 6 different colors coming out.

Cannot load buffer as an image in browser

I'm using image generated on the fly, and it doesn't work. Is there a solution ?

const logo = "http://www.edgebase.com/companies/amazon/logo?width=80&height=80"

<ColorExtractor src={logo} getColors={colors => console.log(colors)} onError={error => console.log(error)} />

Cannot load buffer as an image in browser
    at BrowserImage../node_modules/node-vibrant/lib/image/browser.js.BrowserImage.load (browser.js:64)
    at ColorExtractor._this.useDefaultImageClass (react-color-extractor.es.js:74)
    at react-color-extractor.es.js:67

```

CORS Problem

having cors problem, and "chrome extension" shouldnt be the fix, because my end users wont have any extensions to see my website.

what could be the solve?

Some images don't process?

I was hoping that I could use this component to extract colors from movie posters sent from the movie database api but none of the jpg images will work in the code. I have been trying with the code from the example and am able to get a lot of my own images when trying images from the web it doesn't work. Am I doing something wrong or are there limitations that i am not seeing?
thank you

How to use colorExtractor src on local files?

hello i have a react project im working on ( link )
where my website gets a random image from a folder in my repo.
i use that image path inside a component which gets displayed on the page totally fine.

                          <ProgressiveImage
                                    className={"colorSrcImg"}
                                    alt={"color source image"}
                                    src={colorSrcImg}
                                />

where the value of colorSrcImg is a string like so: /static/media/R-4452671-1365273054-3687.jpeg.b837e956.jpg
but when i try to use that same src value for ColorExtractor like so:


                                <ColorExtractor
                                    onError={error => console.log(error)}
                                    src={colorSrcImgThumbnail}
                                    getColors={colors => console.log(colors)}
                                />

i get an error in my web console:

index.js:1350 Please provide an image url using the 'src' prop or wrap an image element under the <ColorExtractor /> component. Check out the docs for more info - https://goo.gl/rMZ5L7

am I loading my image wrong? will this /static/media/ filepath work? In my repo the images are hosted?

my file being mentioned is here:
https://github.com/MartinBarker/react-auth-app/blob/colorExtractor/src/Components/MainSidebar.js#L90

Can't Extract colour from my image

Here's the code, it does not return any color palette but it works when using some image that I saved from google.

`
import { ColorExtractor } from 'react-color-extractor';

return (
<ColorExtractor getColors={colors => this.setState({ colors })}>
<img src='https://cms.dmpcdn.com/dev_sportschedule/2019/07/26/816e2b70-da46-4c2e-98f8-3e621dafc4a6_original.jpg' style={{ width: 700, height: 500 }} />

)
`
Could you help me find the solutions to this issue
and if you have any suggestion please tell me
Thank you in advance
N.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.