Skip to content

dev-pengi/dominated-color

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

dominated-color

dominated-color is a simple module that allows you to detect the dominant color of an image using the node-vibrant package.

Installation

To install dominated-color, use npm:

npm install dominated-color

Usage

const { detectDominantColor } = require('dominated-color');

async function example() {
  const imagePath = '/path/to/your/image.jpg';

  try {
    const dominantColor = await detectDominantColor(imagePath);
    console.log('Dominant color:', dominantColor);
  } catch (error) {
    console.error('Error:', error);
  }
}

example();

Function Signature

detectDominantColor(imagePath: string, format?: 'hex' | 'rgb'): Promise<string | number[] | null>
  • imagePath (required): The path to the image file.
  • format (optional, default: hex): The format of the dominant color. Possible values: hex or rgb.
  • The function returns a promise that resolves to the dominant color in the specified format (hex or RGB array), or null if an error occurs.

Examples

Example 1: Detect Dominant Color as Hex

const { detectDominantColor } = require('dominated-color');

async function example() {
  const imagePath = '/path/to/your/image.jpg';

  try {
    const dominantColor = await detectDominantColor(imagePath);
    console.log('Dominant color (hex):', dominantColor);
  } catch (error) {
    console.error('Error:', error);
  }
}

example();

Example 2: Detect Dominant Color as RGB

const { detectDominantColor } = require('dominated-color');

async function example() {
  const imagePath = '/path/to/your/image.jpg';

  try {
    const dominantColor = await detectDominantColor(imagePath, 'rgb');
    console.log('Dominant color (RGB):', dominantColor);
  } catch (error) {
    console.error('Error:', error);
  }
}

example();