Skip to main content

Extract the rendered color palette from any website as a typed result.

Project description

colorsense

Extract the rendered color palette of any website as a structured, typed Python object.

CI PyPI Python License: MIT

colorsense renders a page in a headless browser, harvests its design tokens and computed element colors, and classifies them into a 60/30/10 palette — primary, secondary, accent, and neutrals, each with ranked, scored candidates. The result is a frozen Pydantic model, ready for downstream consumers (including AI models) that need to understand a site's color identity.

import asyncio
from colorsense import PaletteRole, Theme, analyze

result = asyncio.run(analyze("https://example.com"))
primary = result.themes[Theme.light].roles.mapping[PaletteRole.primary]
print(primary[0].color.hex)  # ranked candidates; empty tuple when none detected

Installation

pip install colorsense
playwright install chromium

Requires Python 3.12+. Rendering uses headless Chromium via Playwright; the browser binary is not a pip dependency, so run playwright install chromium once after installing (on Linux, also playwright install-deps chromium for the OS libraries).

Quick start

analyze is async — await it from an event loop, or wrap it with asyncio.run:

import asyncio
from colorsense import analyze, PaletteRole

result = asyncio.run(analyze("https://example.com"))

for theme, palette in result.themes.items():
    candidates = palette.roles.mapping[PaletteRole.primary]
    if candidates:  # every role is present; empty tuple when none detected
        best = candidates[0]
        print(theme, best.color.hex, best.probability)

Each role — primary, secondary, accent, neutral_light, neutral_dark — maps to a probability-ranked tuple of candidates; take [0] for the best pick. Inside an async application (e.g. a FastAPI endpoint), just result = await analyze(url).

See the usage guide for the full result schema, options, and fetch policy.

Command line

pip install colorsense also ships a colorsense command, so a first look needs no code:

colorsense https://example.com
colorsense https://example.com --dark --json > palette.json

The default output is a human-readable palette summary; --json emits the full AnalysisResult schema. All flags are documented in the usage guide.

Features

  • 60/30/10 palette classification — five roles, each with ranked candidates carrying a confidence (probability) and page-area dominance (area).
  • Typed, serializable resultsanalyze returns a frozen Pydantic AnalysisResult; result.model_dump_json() round-trips.
  • OKLCH out of the box — every Color carries an sRGB hex plus cached OKLCH coordinates, so you can derive theme-matched tints, shades, and contrast without re-parsing hex strings.
  • Light and dark themes — opt into dark mode rendering; near-identical light/dark renders collapse to a single reported theme.
  • Declared vs. rendered reconciliation — reports the site's CSS custom properties and discrepancies between what is declared and what is actually used.
  • Status-color filtering — success/error/warning colors are detected and kept out of the palette, so an error banner never masquerades as a brand accent.
  • Polite, controllable fetching — configurable User-Agent, robots.txt gate with Crawl-delay support, per-host rate limiting, render caching, and a per-request egress filter that gates every browser request and the policy's own robots.txt fetch.
  • Server-grade guard rails — a built-in private-network egress filter (block_private_networks) covering browser requests and the robots fetch alike, plus opt-in bounds on render concurrency (max_concurrent_renders) and total call time (max_total_seconds), and a browser launch-arg pass-through (browser_args) for e.g. capping the V8 heap per renderer.
  • Async-native and concurrent — themes render concurrently in one shared browser; CPU work is offloaded so the event loop stays responsive.

How it compares

Most palette tools quantize the pixels of an image or screenshot and return dominant colors with no semantics. colorsense renders the live page, reads computed styles and declared design tokens, and classifies colors into 60/30/10 roles with confidence scores — you get "this is the brand accent", not "this orange is common". If you just need dominant colors from an image, an image-quantization tool is simpler and the right choice.

Security

colorsense fetches and fully renders third-party pages. If untrusted or user-supplied URLs can reach analyze from a server, treat it as an SSRF surface: validate hosts before calling, and use PolitenessPolicy(request_filter=block_private_networks()) to gate every request the rendered page makes — and the policy's own robots.txt fetch, including its redirect hops; bound abuse with max_concurrent_renders and max_total_seconds. The threat model and required controls are documented in SECURITY.mdread it before exposing analyze to untrusted input.

Examples

The examples/ directory has two runnable starting points: quickstart.py for trusted, hardcoded URLs, and webservice/ — a FastAPI service that is a reference implementation of the SECURITY.md controls for untrusted, user-supplied URLs.

Documentation

Support & license

Bug reports and feature requests: GitHub Issues. Want to contribute? Start with CONTRIBUTING.md. Licensed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

colorsense-0.3.0.tar.gz (163.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

colorsense-0.3.0-py3-none-any.whl (92.5 kB view details)

Uploaded Python 3

File details

Details for the file colorsense-0.3.0.tar.gz.

File metadata

  • Download URL: colorsense-0.3.0.tar.gz
  • Upload date:
  • Size: 163.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for colorsense-0.3.0.tar.gz
Algorithm Hash digest
SHA256 c4fb100486cefa0419a73d725bd33141328df11181f44099d8600ee4858b9aa9
MD5 f8de3a47cb35d1d4b5c0ddf54b182d61
BLAKE2b-256 0595eced215bdc58fb6dab5d7125d745aea63133f4f83856183999637e58c7c9

See more details on using hashes here.

Provenance

The following attestation bundles were made for colorsense-0.3.0.tar.gz:

Publisher: publish.yml on cassidyhhaas/colorsense

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file colorsense-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: colorsense-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 92.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for colorsense-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 19c15478af1cd90314f2030f13df241cb97370807ead65529e9ec783d2c6fc60
MD5 a275b0ed883661792d946941db094b54
BLAKE2b-256 e3d1b29ae4e8290dd420cd03a81a4940b2b97433126d8f1d770cdae821a38788

See more details on using hashes here.

Provenance

The following attestation bundles were made for colorsense-0.3.0-py3-none-any.whl:

Publisher: publish.yml on cassidyhhaas/colorsense

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page