autohue.js is a lightweight JavaScript library designed to analyze image pixel data and automatically extract key colors — including the primary (dominant) color, secondary color, and edge colors from the top, bottom, left, and right of an image. It was built to help web developers make images and backgrounds visually cohesive without manually choosing background colors for each asset.
What AutoHue.js Does
Automatic Theme Color Extraction
autohue.js scans an image’s pixel data and identifies the primary and secondary colors — the most prominent hues by area — using configurable clustering thresholds. This allows you to dynamically generate UI themes based on the actual content of an image.
Edge Color Detection for Seamless Backgrounds
In addition to theme colors, autohue.js extracts edge colors from each side of the image. These edge colors are ideal for building gradient backgrounds or fluid transitions where the image meets the surrounding layout, creating a visual effect where image content appears to extend into the page with minimal boundaries.
Why Use AutoHue.js?
Enhanced UI Aesthetics
Instead of applying a static background color that might clash with diverse images, autohue.js generates color values that naturally match image content, helping banners and cards appear more integrated.
Dynamic Theming in Responsive Designs
For responsive layouts like wide carousels, where image widths may not fill the entire screen, edge-derived gradients can fill empty spaces smoothly and adaptively, improving visual consistency across devices.
Supports Performance Optimization
By down-sampling images to a configurable maxSize, autohue.js balances performance and extraction accuracy, enabling usage even on large images without significant slowdowns.
Installation and Setup
Install via npm:
pnpm i autohue.js
Then import the library:
import autohue from 'autohue.js';
Practical Usage Example
Use autohue.js to automatically set a background gradient based on edge colors:
autohue(imageUrl, {
threshold: {
primary: 10,
left: 1,
right: 1,
top: 1,
bottom: 1
},
maxSize: 80,
})
.then((result) => {
// Extract colors
const primaryColor = result.primaryColor;
const secondaryColor = result.secondaryColor;
const leftEdge = result.backgroundColor.left;
const rightEdge = result.backgroundColor.right;
// Apply CSS gradient
document.body.style.background = `linear-gradient(to right, ${leftEdge}, ${rightEdge})`;
console.log('Primary:', primaryColor, 'Gradient:', leftEdge, rightEdge);
})
.catch((err) => console.error(err));
This code automatically blends the page background to match the image edges, enhancing visual cohesion.
Common Parameters
| Option | Description |
|---|---|
threshold |
Determines the similarity range for clustering colors. Smaller values yield more distinct clusters; larger values merge similar colors. |
maxSize |
Sets the max sampling size (longest edge) for the image. Lower improves performance; higher improves extraction fidelity. |
Real-World Use Cases
- Adaptive Hero Banners — Automatically generate background fills that match banner images, even when the image does not fill the full layout width.
- E-Commerce Product Pages — Derive background themes from product images to make product displays feel cohesive with surrounding layout.
- Dynamic UI Theming — Customize UI palette based on user-uploaded images, facilitating personalized experiences in portfolios or social apps.
Conclusion
autohue.js excels beyond typical color-extraction scripts by focusing not only on dominant colors, but also on edge and background color extraction tailored for layout blending. With simple integration and performance tuning, it helps developers create visually appealing, adaptive UI themes automatically.