Color

Functions

detroit.color(format: str) RGB | HSL | None

Parses the specified CSS Color specifier string, returning an RGB or HSL color. If the specifier was not valid, None is returned.

Parameters:

format (str) – Specifier

Returns:

Formatted color

Return type:

RGB | HSL | None

Examples

>>> d3.color("#2e65ffff")
RGB(r=46, g=101, b=255, opacity=1.0)
>>> d3.color("rgb(108, 10, 204)")
RGB(r=108, g=10, b=204, opacity=1.0)
>>> d3.color("hsl(210.4, 90%, 70%)")
HSL(h=210.4, s=0.9, l=0.7, opacity=1.0)
>>> d3.color("bad") is None
True
detroit.rgb(specifier: str) RGB
detroit.rgb(r: int, g: int, b: int) RGB
detroit.rgb(r: int, g: int, b: int, opacity: float) RGB

Builds a new RGB color.

Parameters:
  • specifier (str) – String which represents a color

  • r (int) – Red value between 0 and 255

  • g (int) – Green value between 0 and 255

  • b (int) – Blue value between 0 and 255

  • opacity (float) – Opacity value between 0 and 1

Returns:

RGB object

Return type:

RGB

Examples

>>> d3.rgb("#2e65ffff")
RGB(r=46, g=101, b=255, opacity=1.0)
>>> d3.rgb(128, 250, 102, 0.2)
RGB(r=128, g=250, b=102, opacity=0.2)
detroit.hsl(specifier: str) HSL
detroit.hsl(h: float, s: float, l: float) HSL
detroit.hsl(h: float, s: float, l: float, opacity: float) HSL

Build a new HSL color.

Parameters:
  • specifier (str) – String which represents a color

  • h (float) – Hue channel value between 0 and 360

  • s (float) – Saturation channel value between 0 and 1

  • l (float) – Lightness channel value between 0 and 1

  • opacity (float) – Opacity value between 0 and 1

Returns:

HSL object

Return type:

HSL

Examples

>>> d3.hsl("#2e65ffff")
HSL(h=224.21052631578948, s=1.0, l=0.5901960784313726, opacity=1.0)
>>> d3.hsl(210.4, 0.9, 0.7, 0.8)
HSL(h=210.4, s=0.9, l=0.7, opacity=0.8)
detroit.cubehelix(specifier: str) Cubehelix
detroit.cubehelix(h: float, s: float, l: float) Cubehelix
detroit.cubehelix(h: float, s: float, l: float, opacity: float) Cubehelix

Build a new Cubehelix color

Parameters:
  • specifier (str) – String which represents a color

  • h (float) – Hue channel value between 0 and 360

  • s (float) – Saturation channel value between 0 and 1

  • l (float) – Lightness channel value between 0 and 1

  • opacity (float) – Opacity value between 0 and 1

Returns:

Cubehelix object

Return type:

Cubehelix

Examples

>>> d3.cubehelix("#2e65ffff")
Cubehelix(h=222.45389380826435, s=1.3363126255145055, l=0.3978037691833379, opacity=1.0)
>>> d3.cubehelix(210.4, 0.9, 0.7, 0.8)
Cubehelix(h=210.4, s=0.9, l=0.7, opacity=0.8)
detroit.lab(specifier: str) LAB
detroit.lab(l: float, a: float, b: float) LAB
detroit.lab(l: float, a: float, b: float, opacity: float) LAB

Builds a new LAB color.

Parameters:
  • specifier (str) – String which represents a color

  • l (float) – L* channel value between 0 and 100

  • a (float) – a* channel value between -128 and 127

  • b (float) – b* channel value between -128 and 127

  • opacity (float) – Opacity value between 0 and 1

Returns:

LAB object

Return type:

LAB

Examples

>>> d3.lab("#2e65ffff")
LAB(l=46.97291122702186, a=27.037050613700043, b=-83.1712687492733, opacity=1.0)
>>> d3.lab(78.25, -17.71, 76.47, 0.8)
LAB(l=78.25, a=-17.71, b=76.47, opacity=0.8)
detroit.gray(l: float) LAB
detroit.gray(l: float, opacity: float) gray

Builds a new LAB color on gray gradient.

Parameters:
  • l (float) – L* channel value between 0 and 100

  • opacity (float) – Opacity value between 0 and 1

Returns:

LAB object

Return type:

LAB

Examples

>>> d3.gray(78.25, 0.8)
LAB(l=78.25, a=0.0, b=0.0, opacity=0.8)
detroit.hcl(specifier: str) HCL
detroit.hcl(h: float, c: float, l: float) HCL
detroit.hcl(h: float, c: float, l: float, opacity: float) HCL

Builds a new HCL color.

Parameters:
  • specifier (str) – String which represents a color

  • h (float) – Hue channel value

  • c (float) – Chroma channel value

  • l (float) – Luminance channel value

  • opacity (float) – Opacity value between 0 and 1

Returns:

HCL object

Return type:

HCL

Examples

>>> d3.hcl("#2e65ffff")
HCL(h=288.00814189783785, c=87.45548611294561, l=46.97291122702186, opacity=1.0)
>>> d3.hcl(60, 83.82, 43.14, 0.8)
HCL(h=60.0, c=83.82, l=43.14, opacity=0.8)
detroit.lch(specifier: str) HCL
detroit.lch(l: float, c: float, h: float) HCL
detroit.lch(l: float, c: float, h: float, opacity: float) HCL

Builds a new HCL color from LCH format.

Parameters:
  • specifier (str) – String which represents a color

  • l (float) – Luminance channel value

  • c (float) – Chroma channel value

  • h (float) – Hue channel value

  • opacity (float) – Opacity value between 0 and 1

Returns:

HCL object

Return type:

HCL

Examples

>>> d3.lch("#2e65ffff")
HCL(h=288.00814189783785, c=87.45548611294561, l=46.97291122702186, opacity=1.0)
>>> d3.lch(43.14, 83.82, 60, 0.8)
HCL(h=60.0, c=83.82, l=43.14, opacity=0.8)

Classes

class detroit.color.color.Color

Color base class used by RGB, HSL, Cubehelix, LAB and HCL.

format_hex() str

Returns the color formatted as hex color.

Returns:

Hex color

Return type:

str

Examples

>>> d3.rgb(128, 250, 102, 0.2).format_hex()
'#80fa66'
format_hex_8() str

Returns the color formatted as hex color with alpha channel (opacity).

Returns:

Hex color

Return type:

str

Examples

>>> d3.rgb(128, 250, 102, 0.2).format_hex_8()
'#80fa6633'
format_hsl() str

Returns the color formatted as HSL color.

Returns:

HSL color

Return type:

str

Examples

>>> d3.rgb(128, 250, 102, 0.2).format_hsl()
'hsla(109.45945945945947, 93.67088607594937%, 69.01960784313725%, 0.2)'
format_rgb() str

Returns the color formatted as RGB color.

Returns:

RGB color

Return type:

str

Examples

>>> d3.rgb(128, 250, 102, 0.2).format_rgb()
'rgba(128, 250, 102, 0.2)'
class detroit.color.color.RGB(r: int, g: int, b: int, opacity: float = 1)

RGB color format

Parameters:
  • r (int) – Red channel value

  • g (int) – Green channel value

  • b (int) – Blue channel value

  • opacity (float) – Opacity value

brighter(k: float | None = None) RGB

Returns a brighter copy of this color. For example, if k is 1, steelblue in RGB color space becomes rgb(100, 186, 255). The parameter k controls how much brighter the returned color should be (in arbitrary units); if k is not specified, it defaults to 1. The behavior of this method is dependent on the implementing color space.

Parameters:

k (float | None) – Brightness coefficient

Returns:

Brighter RGB

Return type:

RGB

darker(k: float | None = None) RGB

Returns a darker copy of this color. For example, if k is 1, steelblue in RGB color space becomes rgb(49, 91, 126). The parameter k controls how much darker the returned color should be (in arbitrary units); if k is not specified, it defaults to 1. The behavior of this method is dependent on the implementing color space.

Parameters:

k (float | None) – Darkness coefficient

Returns:

Darker RGB

Return type:

RGB

rgb() RGB

Returns the RGB equivalent of this color

Returns:

RGB color format

Return type:

RGB

clamp() RGB

Returns a new RGB color where the r, g, and b channels are clamped to the range [0, 255] and rounded to the nearest integer value, and the opacity is clamped to the range [0, 1].

Returns:

Clamped color

Return type:

RGB

displayable() bool

Returns True if and only if the color is displayable on standard hardware. For example, this returns false for an RGB color if any channel value is less than zero or greater than 255 when rounded, or if the opacity is not in the range [0, 1].

Returns:

Is displayable

Return type:

bool

format_hex() str

Returns a hexadecimal string representing this color in RGB space, such as #4682b4. If this color is not displayable, a suitable displayable color is returned instead. For example, RGB channel values greater than 255 are clamped to 255.

Returns:

Hex color representation

Return type:

str

format_hex_8() str

Returns a hexadecimal string representing this color in RGBA space, such as #4682b4cc. If this color is not displayable, a suitable displayable color is returned instead. For example, RGB channel values greater than 255 are clamped to 255.

Returns:

Hex 8 color representation

Return type:

str

format_rgb() str

Returns a string representing this color according to the CSS Object Model specification, such as rgb(247, 234, 186) or rgba(247, 234, 186, 0.2). If this color is not displayable, a suitable displayable color is returned instead by clamping RGB channel values to the interval [0, 255].

Returns:

RGB color representation

Return type:

str

class detroit.color.color.HSL(h: float, s: float, l: float, opacity: float = 1.0)

HSL color format

Parameters:
  • h (float) – Hue channel value

  • s (float) – Saturation channel value

  • l (float) – Lightness channel value

  • opacity (float) – Opacity value

brighter(k: float | None = None) HSL

Returns a brighter copy of this color. For example, if k is 1, steelblue in RGB color space becomes rgb(100, 186, 255). The parameter k controls how much brighter the returned color should be (in arbitrary units); if k is not specified, it defaults to 1. The behavior of this method is dependent on the implementing color space.

Parameters:

k (float | None) – Brightness coefficient

Returns:

Brighter HSL

Return type:

HSL

darker(k: float | None = None) HSL

Returns a darker copy of this color. For example, if k is 1, steelblue in RGB color space becomes rgb(49, 91, 126). The parameter k controls how much darker the returned color should be (in arbitrary units); if k is not specified, it defaults to 1. The behavior of this method is dependent on the implementing color space.

Parameters:

k (float | None) – Darkness coefficient

Returns:

Darker HSL

Return type:

HSL

rgb() RGB

Returns the RGB equivalent of this color

Returns:

RGB color format

Return type:

RGB

clamp() HSL

Returns a new HSL color where the h channel is clamped to the range [0, 360), and the s, l, and opacity channels are clamped to the range [0, 1].

Returns:

Clamped color

Return type:

HSL

displayable() bool

Returns True if and only if the color is displayable on standard hardware. For example, this returns false for an HSL color if any channel value are in the range [0, 1] or if the opacity is not in the range [0, 1].

Returns:

Is displayable

Return type:

bool

format_hsl() str

Returns the color formatted as HSL color.

Returns:

HSL color

Return type:

str

Examples

>>> d3.rgb(128, 250, 102, 0.2).format_hsl()
'hsla(109.45945945945947, 93.67088607594937%, 69.01960784313725%, 0.2)'
class detroit.color.cubehelix.Cubehelix(h: float, s: float, l: float, opacity: float = 1.0)

Cubehelix color format

Parameters:
  • h (float) – Hue channel value

  • s (float) – Saturation channel value

  • l (float) – Lightness channel value

  • opacity (float) – Opacity value

brighter(k: float | None = None) Cubehelix

Returns a brighter copy of this color.

Parameters:

k (float | None) – Brightness coefficient

Returns:

Brighter Cubehelix

Return type:

Cubehelix

darker(k: float | None = None) Cubehelix

Returns a darker copy of this color.

Parameters:

k (float | None) – Darkness coefficient

Returns:

Darker Cubehelix

Return type:

Cubehelix

rgb() RGB

Returns the RGB equivalent of this color

Returns:

RGB color format

Return type:

RGB

class detroit.color.lab.LAB(l: float, a: float, b: float, opacity: float = 1.0)

LAB color format

Parameters:
  • l (float) – L* channel value

  • a (float) – a* channel value

  • b (float) – b* channel value

  • opacity (float) – Opacity value

brighter(k: float | None = None) LAB

Returns a brighter copy of this color.

Parameters:

k (float | None) – Brightness coefficient

Returns:

Brighter LAB

Return type:

LAB

darker(k: float | None = None) LAB

Returns a darker copy of this color.

Parameters:

k (float | None) – Brightness coefficient

Returns:

Brighter LAB

Return type:

LAB

rgb() RGB

Returns the RGB equivalent of this color

Returns:

RGB color format

Return type:

RGB

class detroit.color.lab.HCL(h: float, c: float, l: float, opacity: float = 1.0)

HCL color format

Parameters:
  • h (float) – Hue channel value

  • c (float) – Chroma channel value

  • l (float) – Luminance channel value

  • opacity (float) – Opacity value

brighter(k: float | None = None) HCL

Returns a brighter copy of this color.

Parameters:

k (float | None) – Brightness coefficient

Returns:

Brighter HCL

Return type:

HCL

darker(k: float | None = None) HCL

Returns a darker copy of this color.

Parameters:

k (float | None) – Brightness coefficient

Returns:

Brighter HCL

Return type:

HCL

rgb() RGB

Returns the RGB equivalent of this color

Returns:

RGB color format

Return type:

RGB