Skip to content

ImageColor

ImageColor.getrgb(color) parses a color string into an 8-bit sRGB tuple. An explicit alpha component produces an RGBA tuple. getcolor(color, mode) converts that result to a pixel in 1, L, LA, RGB, RGBA, CMYK, I, or F mode. Alpha is preserved for LA and RGBA; other modes discard it. Grayscale uses the same integer luminance calculation as Pillow.

from blanket import ImageColor

assert ImageColor.getrgb("rebeccapurple") == (102, 51, 153)
assert ImageColor.getrgb("rgb(100% 0 50% / 50%)") == (255, 0, 128, 128)
assert ImageColor.getcolor("hsl(120deg 100% 50%)", "RGBA") == (0, 255, 0, 255)

Color syntax

The parser supports the absolute color literals in CSS Color 4:

Form Supported syntax
Names All named colors, including rebeccapurple, plus transparent
Hexadecimal #rgb, #rgba, #rrggbb, #rrggbbaa
RGB rgb() and rgba(), comma or space separators, numbers or percentages
HSL hsl() and hsla(), comma or space separators
HWB hwb(hue white black)
Lab lab(), lch(), oklab(), oklch()
Predefined spaces color(space c1 c2 c3)

Predefined spaces are srgb, srgb-linear, display-p3, display-p3-linear, a98-rgb, prophoto-rgb, rec2020, xyz, xyz-d50, and xyz-d65. xyz aliases xyz-d65. Rec.2020 uses the display-referred gamma 2.4 transfer function specified by CSS Color 4.

Modern forms accept / alpha, decimal and exponent notation, mixed numeric and percentage components, and none for missing components. Missing components become zero when producing a pixel. Hue accepts degrees (with or without deg), grad, rad, and turn, including negative angles. Color strings are case insensitive and allow surrounding whitespace. Alpha and RGB components are clamped to their legal ranges. Colors outside the sRGB gamut use Oklab chroma reduction with CSS local MINDE gamut mapping, then round to 8-bit channels. No external Python dependencies are required.

For Pillow compatibility, hsv() and hsb() remain supported, and comma-separated rgba() with four integer components retains byte alpha: rgba(1, 2, 3, 4) means (1, 2, 3, 4). Use / 1, / 100%, or a decimal alpha such as 1.0 for CSS opacity semantics.

This is a pixel color parser, with no document or system theme context. Contextual keywords such as currentcolor and system colors raise ValueError, as do CSS expressions (calc(), var()), relative colors, and stylesheet token escapes. Pass a resolved color literal instead. Inputs longer than 1024 characters are rejected.

The same parser handles color strings in image construction, drawing, palette helpers, and ImageOps fills.

Reference

getrgb

getrgb(color: str) -> tuple[int, ...]

Convert an absolute CSS color literal to an 8-bit sRGB or RGBA tuple.

Supports CSS4 functions and predefined spaces, plus Pillow's HSV and integer-alpha RGBA extensions. Contextual CSS values require resolution by the caller. Out-of-gamut colors use CSS local MINDE gamut mapping.

getcolor

getcolor(color: str, mode: str) -> int | tuple[int, ...]

Convert a CSS color string to a pixel value for mode.