0.1.3 • Published 3 years ago

@rtcoder/dominant-color v0.1.3

Weekly downloads
-
License
ISC
Repository
github
Last release
3 years ago

Dominant color

Get dominant color from images using only JavaScript

Installation

npm i @rtcoder/dominant-color

Usage

import {getDominantColor} from "@rtcoder/dominant-color";

const img = document.querySelector('img');

getDominantColor(img, {
    downScaleFactor: 1,
    skipPixels: 0,
    colorsPaletteLength: 5,
    paletteWithCountOfOccurrences: false,
    colorFormat: 'rgb',
    callback: (color, palette) => {
       // your code here
    }
});

Config options

NameTypeDefault valueDescription
downScaleFactornumber1factor of scale down of image, recommend to use for many large images
skipPixelsnumber0when larger than 0, skips every n pixels of while determine dominant color, recommend to use for large images
colorsPaletteLengthnumber5length of returned palette of colors
paletteWithCountOfOccurrencesbooleanfalsedetermines whether to return colors with the number of occurrences
colorFormatstring'rgb'defines the format of the returned colors, available values are 'rgb', 'hsl' and 'hex'
callbackfunctionempty functioncallback function

Interfaces

type ColorFormat = 'rgb' | 'hsl' | 'hex';  

interface PrimaryColor {  
  color: string;  
  count: number;  
}

interface DominantColorOptions {  
  downScaleFactor: number;  
  skipPixels: number;  
  colorsPaletteLength: number;  
  paletteWithCountOfOccurrences: boolean;  
  colorFormat: ColorFormat;  
  callback: DominantColorCallback;  
}  

type DominantColorCallback = (dominant: string, colorsPalette: (string[]) | (PrimaryColor[])) => void;

function getDominantColor(element: HTMLImageElement, options: Partial<DominantColorOptions>): void
0.1.3

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago