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 🙏

© 2026 – Pkg Stats / Ryan Hefner

pixel-colors

v1.1.0

Published

A high-performance, deterministic color extraction library for React. Use K-Means clustering to find dominant color palettes from any image with zero external dependencies.

Downloads

387

Readme

pixel-colors

A high-performance, deterministic color extraction library for React. Use K-Means clustering to find dominant color palettes from any image with zero external dependencies.

License: MIT Bundle Size TypeScript


Key Features

  • Deterministic K-Means: Unlike other libraries that use random seeding, our algorithm uses a strided initialization for consistent palettes every time.
  • Performance Optimized: Automatically downsamples large images on an offscreen canvas to ensure extraction happens in milliseconds.
  • Strictly Typed: Built with `noUncheckedIndexedAccess` and `verbatimModuleSyntax`—no "possibly undefined" runtime errors.
  • SSR Safe: Internal guards for `window` and `document` make it compatible with Next.js, Remix, and Bun.

Installation

bun add pixel-colors
# or
npm install pixel-colors

Usage

Basic Example

The `usePixelColors` hook handles image loading, canvas drawing, and color clustering in a single line.

import { usePixelColors } from "pixel-colors";

function ProfileCard({ userImage }) {
  const { palette, loading, error } = usePixelColors(userImage, 5);

  if (loading) return <SkeletonPalette />;

  return (
    <div style={{ borderColor: palette[0] }}>
      {palette.map((color) => (
        <Swatch key={color} color={color} />
      ))}
    </div>
  );
}

Logic-Only (Vanilla JS/TS)

You can use the core extraction logic without React:

import { extractColors } from "pixel-colors";

const img = document.getElementById("my-image") as HTMLImageElement;
const colors = extractColors(img, 6);
// Returns ['rgb(25, 45, 60)', ...]

Technical Details

Downsampling

To prevent the main thread from locking up on 4K images, the library resizes the input to a maximum of 100x100px before processing. This reduces the pixel data by ~99% while maintaining the integrity of the dominant color clusters.

Algorithm

  1. Staging: Image is drawn to an offscreen canvas.
  2. Sampling: Pixels are converted to an `RGB` coordinate space.
  3. Clustering: K-Means groups pixels into `k` clusters using Euclidean distance.
  4. Convergence: The algorithm iterates until centroids stabilize or max iterations (10) are reached.

Configuration (TSConfig)

This library is built with modern TypeScript standards. For the best experience, we recommend these settings:

{
  "compilerOptions": {
    "moduleResolution": "bundler",
    "strict": true,
    "noUncheckedIndexedAccess": true
  }
}

Contributing

Contributions are welcome. Please ensure that any new hooks or logic maintain the zero-dependency goal and pass the existing `bun test` suite.

License

MIT © maynkudu