lokesh/color-thief — 13.7k★ trên GitHub (TypeScript). Grab the color palette from an image using just Javascript. Works in the browser and in Node.
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
CLI — colorthief photo.jpg with JSON, CSS, and ANSI output
Zero runtime dependencies
API at a Glance
Function
Description
getColorSync(source, options?)
Dominant color (sync, browser only)
getPaletteSync(source, options?)
Color palette (sync, browser only)
getSwatchesSync(source, options?)
Semantic swatches (sync, browser only)
getColor(source, options?)
Dominant color (async, browser + Node.js)
getPalette(source, options?)
Color palette (async, browser + Node.js)
getSwatches(source, options?)
Semantic swatches (async, browser + Node.js)
getPaletteProgressive(source, options?)
3-pass progressive palette (async generator)
observe(source, options)
Watch a source and emit palette updates (browser only)
createColor(r, g, b, population)
Build a Color object from RGB values
Options
Option
Default
Description
colorCount
10
Number of palette colors (2–20)
quality
10
Sampling rate (1 = every pixel, 10 = every 10th)
colorSpace
'oklch'
Quantization space: 'rgb' or 'oklch'
region
—
Sample a sub-rectangle: { x, y, width, height } as 0–1 fractions
gamut
'srgb'
Output gamut: 'srgb', 'display-p3', or 'auto' (browser only)
worker
false
Deprecated — ignored as of v3, removed in v4. See Web Workers
signal
—
AbortSignal to cancel extraction
ignoreWhite
true
Skip white pixels
Color Object
Property / Method
Returns
.rgb()
{ r, g, b }
.hex()
'#ff8000'
.hsl()
{ h, s, l }
.oklch()
{ l, c, h }
.css(format?)
'rgb(255, 128, 0)', 'hsl(…)', or 'oklch(…)' — a P3 color's default .css() is color(display-p3 …)
.array()
[r, g, b]
.gamut
'srgb' or 'display-p3'
.toString()
Hex string (works in template literals)
.textColor
'#ffffff' or '#000000'
.isDark / .isLight
Boolean
.contrast
{ white, black, foreground } — WCAG ratios
.population
Raw pixel count
.proportion
0–1 share of total
Region extraction
Pass a region to sample only part of the image. Coordinates are fractions of
the image size (0–1) measured from the top-left, so a region is
resolution-independent — the same values work for a thumbnail and the full-size
original.
// Colors from the bottom third — e.g. for a gradient that overlaps the
// bottom of the image
const palette = await getPalette(img, {
region: { x: 0, y: 0.66, width: 1, height: 0.34 },
});
// Center crop
const color = await getColor(img, {
region: { x: 0.25, y: 0.25, width: 0.5, height: 0.5 },
});
Works everywhere an option object does: getColor, getPalette, getSwatches,
getPaletteProgressive, the *Sync functions, observe(), and the CLI — in
both browser and Node. A region that runs past the right or bottom edge is
clamped to the image; out-of-range or zero-sized values throw.
Accepts HTMLImageElement, HTMLCanvasElement, HTMLVideoElement, ImageData, ImageBitmap, and OffscreenCanvas.
Wide-gamut (Display P3)
By default colors are read and reported in sRGB. For P3-tagged / wide-gamut images, pass gamut to preserve the extra saturation:
// Force P3: read the image through a P3 canvas, report P3 colors
const palette = await getPalette(img, { gamut: 'display-p3' });
palette[0].css(); // 'color(display-p3 0.92 0.2 0.14)'
palette[0].gamut; // 'display-p3'
// Auto: report P3 only when the image actually uses out-of-sRGB colors,
// otherwise behave exactly like sRGB
const auto = await getPalette(img, { gamut: 'auto' });
.rgb(), .array(), and .hex() always return sRGB (gamut-mapped when the color is P3), so existing rgb(...) strings keep working. The wide-gamut values live in .css() and .oklch(); use .rgb('display-p3') for the raw P3 components. Falls back to sRGB where P3 canvas support is unavailable. Node output is sRGB for now.
Live extraction with observe()
import { observe } from 'colorthief';
// Watch a video and update ambient lighting as it plays
const controller = observe(videoElement, {
throttle: 200, // ms between updates
colorCount: 5,
onChange(palette) {
updateAmbientBackground(palette);
},
});
// Stop when done
controller.stop();
Works with <video>, <canvas>, and <img> elements. For images, it uses a MutationObserver to detect src changes. For video and canvas, it polls using requestAnimationFrame with throttle.
Web Workers
To keep extraction off the main thread, run Color Thief inside your own worker and hand it an ImageBitmap. Bitmaps are transferable, so the pixels move without being copied, and decoding, sampling, and quantization all happen off-thread.
Deprecated: the worker: true option is ignored as of v3 and will be removed in v4, along with the isWorkerSupported, extractInWorker, and terminateWorker exports from colorthief/internals. It only moved quantization off-thread while leaving decoding, pixel sampling, and the structured clone of the pixel array on the main thread — and serializing that array cost several times more than the quantization it saved. It also quantized in RGB while the default path uses OKLCH, so worker: true returned different colors. Calls that pass it now go through the normal pipeline and log a one-time warning.
Accepts file paths and Buffers. Uses sharp for image decoding.
CLI
Quick start
npx colorthief-cli photo.jpg
The colorthief-cli package bundles everything needed (including sharp for image
decoding), so it works immediately with no extra setup.
Commands
# Dominant color
colorthief-cli photo.jpg
# Color palette
colorthief-cli palette photo.jpg
# Semantic swatches
colorthief-cli swatches photo.jpg
Output formats
# Default: ANSI color swatches
colorthief-cli photo.jpg
# ▇▇ #e84393
# JSON with full color data
colorthief-cli photo.jpg --json
# CSS custom properties
colorthief-cli palette photo.jpg --css
# :root {
# --color-1: #e84393;
# --color-2: #6c5ce7;
# }
Options
colorthief-cli palette photo.jpg --count 5 # Number of colors (2-20)
colorthief-cli photo.jpg --quality 1 # Sampling quality (1=best)
colorthief-cli photo.jpg --color-space rgb # Color space (rgb or oklch)
colorthief-cli photo.jpg --region 0,0.66,1,0.34 # Sample the bottom third
--region takes x,y,width,height as fractions of the image size (0–1), the
same as the region option.
Stdin is supported — use - or pipe directly:
cat photo.jpg | colorthief-cli -
Multiple files are supported. Output is prefixed with filenames, and --json wraps
results in an object keyed by filename.
Note: If you already have colorthief and sharp installed in a project, you
can also use colorthief directly as the command name (without the -cli suffix).
Links
Demo page & live examples
Changelog
Roadmap
GitHub
npm
Contributing
npm run build # Build all dist formats
npm run test # Run all tests (Mocha + Cypress)
npm run test:node # Node tests only
npm run test:browser # Browser tests (requires npm run dev)
npm run dev # Start local server on port 8080
Releasing
# 1. Make sure you're on master with a clean working tree
git status
# 2. Add the new version's entry to CHANGELOG.md and commit it
# 3. Run the full test suite
npm run build
npm run test:node
npm run test:browser # requires npm run dev in another terminal
# 4. Preview what will be published
npm pack --dry-run
# 5. Tag and publish
npm version <major|minor|patch> # bumps version, creates git tag
npm publish # builds via prepublishOnly, then publishes
git push && git push --tags
Trang TopGit này là một snapshot — tab "Readme" hiển thị nguyên văn README của repo (đã bỏ link, giữ ảnh). Repo GitHub ở github.com/lokesh/color-thief là nguồn chính thức.
lokesh/color-thief có bao nhiêu sao?
lokesh/color-thief có 13.7k sao GitHub — tải lại trang để xem số mới nhất, hoặc xem trực tiếp github.com/lokesh/color-thief. TopGit phản chiếu số sao của GitHub nhưng không cam kết đến từng phút.
lokesh/color-thief có phải mã nguồn mở không?
Có — lokesh/color-thief phát hành theo license MIT, nghĩa là mã nguồn mở để đọc, fork và (tùy license) tái sử dụng. Mã: github.com/lokesh/color-thief.
lokesh/color-thief có tag gì không?
Bản đồng bộ chưa ghi nhận topic GitHub nào cho lokesh/color-thief. GitHub topics hiển thị ở thanh bên phải trang repo — đó là nơi đáng kiểm tra nhất.
lokesh/color-thief có trang demo không?
Dự án có trang chủ ở https://lokeshdhakar.com/projects/color-thief/. Tab "Readme" ở trang này thường có ảnh chụp và hướng dẫn bắt đầu nhanh.
lokesh/color-thief còn đang phát triển không?
Commit gần nhất trên lokesh/color-thief là 3 tháng trước (theo timestamp GitHub). Repo có 1.3k fork — một chỉ báo về mức độ quan tâm của cộng đồng.
Đọc đầy đủ README ở tab phía trên.
Chưa chắc color-thief có hợp với bạn?
Để ChatGPT, Claude hoặc Perplexity tìm hiểu giúp — bấm bên dưới và xem AI nói gì về color-thief.