npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

avg-color

v1.0.3

Published

calculate average color of image

Downloads

110

Readme

Avg Color

A library for calculating the average color of an image. Easily determine the dominant color tone of a graphic file with a simple API.

Version License TypeScript

🚀 Installation

npm install avg-color

📖 Usage

Basic Example

import { avc } from 'avg-color';

const imageUrl = 'https://example.com/image.jpg';

avc(imageUrl)
  .then((color) => {
    console.log('Average color:', color); // rgb(123, 45, 67)
  })
  .catch((error) => {
    console.error('Error:', error);
  });

Using async/await

import { avc } from 'avg-color';

async function getAverageColor(imageUrl) {
  try {
    const color = await avc(imageUrl);
    console.log('Average color:', color);
    return color;
  } catch (error) {
    console.error('Error calculating average color:', error);
  }
}

Using in React

import { useState, useEffect } from 'react';
import { avc } from 'avg-color';

function ImageComponent({ imageUrl }) {
  const [avgColor, setAvgColor] = useState(null);

  useEffect(() => {
    avc(imageUrl).then(setAvgColor).catch(console.error);
  }, [imageUrl]);

  return (
    <div style={{ backgroundColor: avgColor || '#ccc' }}>
      <img src={imageUrl} alt="Image" />
      {avgColor && <p>Average color: {avgColor}</p>}
    </div>
  );
}

Using in Next.js

'use client';

import { useState, useEffect } from 'react';
import { avc } from 'avg-color';

export default function Home() {
  const [color, setColor] = useState(null);
  const imageUrl = '/api/image?name=example.jpg';

  useEffect(() => {
    avc(imageUrl)
      .then((calculatedColor) => {
        setColor(calculatedColor);
      })
      .catch((error) => {
        console.error('Error:', error);
      });
  }, []);

  return (
    <div>
      <button style={{ backgroundColor: color || '#6b7280' }}>
        Button with dynamic color
      </button>
    </div>
  );
}

🎯 Features

  • ✨ Simple and intuitive API
  • 🚀 Lightweight library with no dependencies
  • 📦 TypeScript support out of the box
  • 🌐 Works in the browser (uses Canvas API)
  • 🎨 Returns color in RGB format: rgb(r, g, b)

📝 API

avc(imageUrl: string): Promise<string>

Calculates the average color of an image.

Parameters:

  • imageUrl (string) - URL of the image to analyze

Returns:

  • Promise<string> - A promise that resolves with a color in rgb(r, g, b) format

Errors:

  • Throws an error if the image failed to load
  • Throws an error if the 2D Canvas context could not be obtained

🔧 Requirements

  • Browser with Canvas API support
  • ES6+ module support

📸 Usage Examples

Dynamic Button Color Change

import { avc } from 'avg-color';

const imageUrl = '/path/to/image.jpg';
const button = document.querySelector('#myButton');

avc(imageUrl).then((color) => {
  button.style.backgroundColor = color;
});

Creating a Color Palette

import { avc } from 'avg-color';

const images = ['image1.jpg', 'image2.jpg', 'image3.jpg'];
const colors = await Promise.all(images.map((img) => avc(`/images/${img}`)));

console.log('Color palette:', colors);

🖼️ Demo

Below are screenshots and a video demonstration of the library in action:

Screenshots

Demo 1 Example of using the library in a web application

Demo 2 Dynamic button color change based on the average color of the image

Demo 3 Application interface showing the calculated color

Video Demonstration

Video Demo

🤝 Contributing

We welcome contributions to the project! Please read CONTRIBUTING.md for more information.

📄 License

This project is licensed under the MIT License. See the LICENSE file for details.

🔗 Links

👤 Author

DotikDeveloper

🙏 Acknowledgments

Thank you to everyone who uses and contributes to this library!


⭐ If you like this project, please give it a star on GitHub!