一个从图像中提取颜色的 React 组件
react-color-extractor is a React component that extracts colors from an image.
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.
Design systems
Creative coding
Creating advanced color tools
npm install react-color-extractoror if you use yarn
yarn add react-color-extractorThis package also depends on React, so make sure you've it installed.
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 (
)
}
}Checkout the demo on CodeSandbox
Check out the examples folder.
react-color-extractor can be used in two different ways.
console.log(colors)}>
Check out this example.
src prop"
getColors={colors => console.log(colors)}
/>Check out this example.
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.
getColors(colors: Array | 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
this.setState({ colors: colors })} />rgbtype: boolean
When set to true, produces the color in rgb format. By default, colors produced are in hex format
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.
this.setState({ hasError: true , msg: error })}>srctype: string
src prop accepts a remote or local image url, or a blob url.
console.log(colors)}
/>maxColorstype: number
maxColors prop accepts a number for amount of colors in palette from which swatches will be generated.
...} maxColors={128} />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//react-color-extractor
cd react-color-extractor
yarnRun flow type checker using yarn flow
Run yarn build:component to build the source code.
暂无开放 Issues,或尚未同步最近议题。