Convert Figma logo to code with AI

lokesh logocolor-thief

Grab the color palette from an image using just Javascript. Works in the browser and in Node.

12,434
1,311
12,434
81

Top Related Projects

A JavaScript library for binding keyboard combos without the pain of key codes and key combo conflicts.

Extract prominent colors from an image. JS port of Android's Palette.

Fast, small color manipulation and conversion for JavaScript

10,080

JavaScript library for all kinds of color manipulations

Quick Overview

Color Thief is a JavaScript library that extracts the dominant color or a color palette from an image. It uses a fast, quality-driven algorithm to analyze the image and return the most prominent colors, making it useful for dynamic theming, color-based UI adjustments, and image analysis.

Pros

  • Easy to use with a simple API
  • Works with both browser and Node.js environments
  • Supports various image formats (PNG, JPEG, GIF)
  • Lightweight and has no dependencies

Cons

  • May not always accurately represent the perceived dominant color
  • Performance can be slow for large images
  • Limited customization options for color extraction algorithm
  • Requires image to be fully loaded before analysis

Code Examples

Getting the dominant color:

const colorThief = new ColorThief();
const img = document.querySelector('img');
const dominantColor = colorThief.getColor(img);
console.log(dominantColor); // Returns [R, G, B]

Extracting a color palette:

const colorThief = new ColorThief();
const img = document.querySelector('img');
const palette = colorThief.getPalette(img, 5); // Get 5 dominant colors
console.log(palette); // Returns [[R, G, B], [R, G, B], ...]

Using with Node.js:

const ColorThief = require('colorthief');

ColorThief.getColor('path/to/image.jpg')
  .then(color => console.log(color))
  .catch(err => console.log(err));

Getting Started

  1. Install Color Thief:

    npm install colorthief
    
  2. Include the library in your project:

    <script src="node_modules/colorthief/dist/color-thief.min.js"></script>
    
  3. Use Color Thief in your JavaScript:

    const colorThief = new ColorThief();
    const img = document.querySelector('img');
    
    if (img.complete) {
      console.log(colorThief.getColor(img));
    } else {
      img.addEventListener('load', function() {
        console.log(colorThief.getColor(img));
      });
    }
    

This setup allows you to extract colors from images in your web application. Make sure the image is fully loaded before calling Color Thief methods to ensure accurate results.

Competitor Comparisons

A JavaScript library for binding keyboard combos without the pain of key codes and key combo conflicts.

Pros of KeyboardJS

  • Focuses on keyboard event handling and key binding, providing a specialized solution for keyboard interactions
  • Offers a more comprehensive API for managing keyboard shortcuts and combinations
  • Supports complex key sequences and multiple key presses

Cons of KeyboardJS

  • Limited to keyboard-related functionality, unlike Color Thief's broader color extraction capabilities
  • May have a steeper learning curve for developers not familiar with advanced keyboard event handling
  • Potentially unnecessary for projects that don't require extensive keyboard interaction features

Code Comparison

KeyboardJS:

KeyboardJS.bind('ctrl + shift + a', function(e) {
  console.log('Ctrl + Shift + A was pressed');
});

Color Thief:

const colorThief = new ColorThief();
const dominantColor = colorThief.getColor(imageElement);
console.log(dominantColor);

Summary

KeyboardJS is a specialized library for handling keyboard events and creating complex key bindings, while Color Thief focuses on extracting color information from images. KeyboardJS offers more advanced keyboard interaction features but is limited to that specific functionality. Color Thief, on the other hand, provides a simpler API for color-related tasks but lacks keyboard event handling capabilities. The choice between these libraries depends on the specific requirements of your project, whether it's focused on keyboard interactions or color extraction from images.

Extract prominent colors from an image. JS port of Android's Palette.

Pros of Vibrant.js

  • Provides a more diverse color palette, including vibrant, muted, and dark variants
  • Offers better performance for larger images due to its downsampling technique
  • Includes TypeScript support out of the box

Cons of Vibrant.js

  • Less actively maintained compared to Color Thief
  • Slightly more complex API, which may require a steeper learning curve
  • Limited browser support for older versions

Code Comparison

Color Thief:

const colorThief = new ColorThief();
const dominantColor = colorThief.getColor(imageElement);
const palette = colorThief.getPalette(imageElement, 5);

Vibrant.js:

Vibrant.from(imageElement).getPalette((err, palette) => {
  const vibrantColor = palette.Vibrant.hex;
  const mutedColor = palette.Muted.hex;
});

Both libraries aim to extract dominant colors from images, but they differ in their approach and output. Color Thief focuses on providing a simple, straightforward color extraction, while Vibrant.js offers a more comprehensive color palette with various color types. The choice between the two depends on the specific needs of your project, such as the desired color variety, performance requirements, and target browser support.

Fast, small color manipulation and conversion for JavaScript

Pros of TinyColor

  • More comprehensive color manipulation functions (e.g., lighten, darken, complement)
  • Supports a wider range of color formats (RGB, HSL, HSV, HEX)
  • Smaller file size and faster performance for color operations

Cons of TinyColor

  • Does not extract colors from images (Color Thief's primary function)
  • Less focused on color palette generation
  • May require additional processing for image-based color operations

Code Comparison

TinyColor:

var color = tinycolor("red");
console.log(color.toHexString()); // "#ff0000"
console.log(color.lighten(20).toHexString()); // "#ff6666"

Color Thief:

var colorThief = new ColorThief();
var dominantColor = colorThief.getColor(sourceImage);
console.log(dominantColor); // [r, g, b]

Summary

TinyColor is a versatile color manipulation library, offering a wide range of color operations and format support. It excels in programmatic color handling but lacks image color extraction capabilities. Color Thief, on the other hand, specializes in extracting color palettes from images, making it more suitable for image-based color analysis. The choice between the two depends on whether the primary need is color manipulation (TinyColor) or image color extraction (Color Thief).

10,080

JavaScript library for all kinds of color manipulations

Pros of chroma.js

  • More comprehensive color manipulation library with a wide range of functions
  • Supports various color spaces and color scales
  • Offers advanced features like color interpolation and color harmonies

Cons of chroma.js

  • Larger file size and potentially more complex to use for simple tasks
  • May have a steeper learning curve for beginners
  • Focuses on color manipulation rather than color extraction from images

Code Comparison

color-thief:

const colorThief = new ColorThief();
const dominantColor = colorThief.getColor(imageElement);
console.log(dominantColor); // [r, g, b]

chroma.js:

const color = chroma('#ff0000');
const darkerColor = color.darken();
console.log(darkerColor.hex()); // '#cc0000'

Summary

Color Thief is primarily focused on extracting colors from images, making it ideal for tasks like generating color palettes from uploaded images or analyzing dominant colors in photographs. It's lightweight and easy to use for these specific tasks.

Chroma.js, on the other hand, is a more comprehensive color manipulation library. It offers a wide range of functions for working with colors, including color space conversions, color scales, and advanced color operations. While it doesn't focus on color extraction from images, it provides powerful tools for manipulating and analyzing colors in various contexts.

The choice between these libraries depends on the specific requirements of your project. If you need to extract colors from images, Color Thief is the better option. For more advanced color manipulation and analysis, Chroma.js would be the preferred choice.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Color Thief

Grab the color palette from an image using just Javascript.Works in the browser and in Node.

View the demo page for examples, API docs, and more.


Contributing

Project structure

  • build/ - Simple script that copies and renames files into the /dist folder.
  • cypress/ - Browsers tests.
  • dist/ - Generated distribution files created by microbundle package and a couple of files copied via build script.
  • examples/ - CSS, JS, and Images for the index.html example page.
  • src/color-thief-node.js - Source for the Node (commonjs) compatible version of the script.
  • src/color-thief.js - Source for the browser (ES6, AMD, Global var) compatible version of the script.
  • src/core.js - Functions shared between the node and browser versions of the script.
  • test/ - Node integration tests. Uses Chai.
  • index.html - Example page.

Running tests

There are two sets of tests:

  1. Browser tests run with Cypress
  2. Node tests run with Karma and utilizing Mocha

To run both the browser and Node tests:

  • npm run dev to start local server.
  • npm run test

To run just the browser tests with the Cypress UI:

  • npm run dev to start local server
  • npm run test:browser

To run just the Node tests:

  • npm run test:node

Adding tests

  • Update cypress/test-pages/index.html as needed or create a new test page if you need new examples.
  • Add new tests in cypress/integration/apis_spec.js

Making a new release

  • Merge dev into master
  • Pull down master
  • Update version number in src/color-thief.js and package.json
  • Run npm run build
  • Commit and push built files back up to master
  • Create a new Github release along with tag. Naming convention for both v2.8.1
  • npm publish

NPM DownloadsLast 30 Days