Color Thief
by Lokesh Dhakar Grab the color palette from an image using just Javascript.
Works in the browser and in Node.
Grab the color palette from an image using just Javascript.
Works in the browser and in Node.
The Color Thief package includes multiple distribution files to support different environments and build processes. Here is the list of all the files in the /dist
folder and what formats they support:
$ npm i --save colorthief
const ColorThief = require('colorthief');
getColor()
and getPalette()
methods return a Promise when used in Node. const img = resolve(process.cwd(), 'rainbow.png');
ColorThief.getColor(img)
.then(color => { console.log(color) })
.catch(err => { console.log(err) })
ColorThief.getPalette(img, 5)
.then(palette => { console.log(palette) })
.catch(err => { console.log(err) })
There are multiples ways to install Color Thief when using it in the browser:
$ npm i --save colorthief
<script src="https://cdnjs.cloudflare.com/ajax/libs/color-thief/2.3.0/color-thief.umd.js"></script>
<script src="node_modules/colorthief/dist/color-thief.umd.js"></script>
<script>
const colorThief = new ColorThief();
const img = document.querySelector('img');
// Make sure image is finished loading
if (img.complete) {
colorThief.getColor(img);
} else {
image.addEventListener('load', function() {
colorThief.getColor(img);
});
}
</script>
<script type="module" src="app.js"></script>
import ColorThief from './node_modules/colorthief/dist/color-thief.mjs'
const colorThief = new ColorThief();
const img = document.querySelector('img');
if (img.complete) {
colorThief.getColor(img);
} else {
image.addEventListener('load', function() {
colorThief.getColor(img);
});
}
/dist/color-thief.umd.js
file uses the UMD (Universal Module Definition) format. This includes RequireJS AMD support.All questions are about the usage of Color Thief in the browser, not Node.
Yes. If you see an error that reads: Cannot read property '0' of null
, it is likely that the image had not finished loading when you passed it to getColor()
or getPalette()
.
// RISKY 🙀
const colorThief = new ColorThief();
const img = document.querySelector('img');
colorThief.getColor(img);
// BETTER 👍
const colorThief = new ColorThief();
const img = document.querySelector('img');
if (img.complete) {
colorThief.getColor(img);
} else {
image.addEventListener('load', function() {
colorThief.getColor(img);
});
}
If you are testing the script locally in a web browser, you might see the following error: Access to script at 'file://...' from origin 'null' has been blocked by CORS policy
. This is because the browser restricts direct access to the filesystem.
To get around this, you can set up a minimal server to host the files. One option is http-server. To run this on demand without installing it as a project dependency, you can use the npx
command:
$ npx http-server
Now you can visit http://localhost:8080 to view your server.
Yes. If you are seeing an error that reads: The canvas has been tainted by cross-origin data.
, then you are running into a cross-origin resouce sharing (CORS) issue. Check the following two items:
access-control-allow-origin: *
. <img src="https://aws.com/s3/image.jpg" crossorigin="anonymous" />
If you're dynamically adding the image element, you can do the following:
const colorThief = new ColorThief();
const img = new Image();
img.addEventListener('load', function() {
colorThief.getColor(img);
});
img.crossOrigin = 'Anonymous';
img.src = 'https://aws.com/s3/image.jpeg'
You can use a proxy server that pulls down the image and returns it with a more liberal CORS policy. Below you can see an example of this with an image that is proxied through a public Google server. If you don't own the proxy server, just know, that you are at their mercy. But this is handy in a pinch for testing.
const colorThief = new ColorThief();
const img = new Image();
img.addEventListener('load', function() {
colorThief.getColor(img);
});
let imageURL = 'https://i.pinimg.com/564x/3b/b4/9b/3bb49bd2a7f86e29b5670da1fe03fee4.jpg';
let googleProxyURL = 'https://images1-focus-opensocial.googleusercontent.com/gadgets/proxy?container=focus&refresh=2592000&url=';
img.crossOrigin = 'Anonymous';
img.src = googleProxyURL + encodeURIComponent(imageURL);
The ColorThief methods return colors as an array of three integers representing red, green, and blue values. If you are looking to use the output in CSS, you can do that using the rgb format: color: rgb(102, 51, 153)
. If you do need to convert to hex, you can use the following function:
const rgbToHex = (r, g, b) => '#' + [r, g, b].map(x => {
const hex = x.toString(16)
return hex.length === 1 ? '0' + hex : hex
}).join('')
rgbToHex(102, 51, 153); // #663399
Follow the steps below to get help. Make sure you have read the documentation on this page first.
color-thief
tag to make it easier to find.Color Thief is licensed under the MIT License.
Github provides a nice summary of the license:
"A short and simple permissive license with conditions only requiring preservation of copyright and license notices. Licensed works, modifications, and larger works may be distributed under different terms and without source code."