pygfx.utils.Color

class pygfx.utils.Color(*args)

Bases: object

A representation of color (in the sRGB colorspace).

Internally the color is stored using 4 32-bit floats (rgba). It can be instantiated in a variety of ways. E.g. by providing the color components as values between 0 and 1:

  • Color(r, g, b, a) providing rgba values.

  • Color(r, g, b) providing rgb, alpha is 1.

  • Color(gray, a) grayscale intensity and alpha.

  • Color(gray) grayscale intensity.

The above variations can also be supplied as a single tuple/list, or anything that:

  • Color((r, g, b)).

Named colors:

  • Color(“red”) base color names.

  • Color(“cornflowerblue”) CSS color names.

  • Color(“m”) Matlab color chars.

Hex colors:

  • Color(“#ff0000”) the common hex format.

  • Color(“#ff0000ff”) the hex format that includes alpha.

  • Color(“#ff0”) the short form hex format.

  • Color(“#ff0f”) the short form hex format that includes alpha.

CSS color functions:

  • Color(“rgb(255, 0, 0)”) or Color(“rgb(100%, 0%, 0%)”).

  • Color(“rgba(255, 0, 0, 1.0)”) or Color(“rgba(100%, 0%, 0%, 100%)”).

  • Color(“hsv(0.333, 1, 0.5)”) or Color(“hsv(120deg, 100%, 50%)”).

  • Color(“hsva(0.333, 1, 0.5, 1.0)”) or Color(“hsva(120deg, 100%, 50%, 100%)”).

  • Color(“hsl(0.333, 1, 0.5)”) or Color(“hsl(120deg, 100%, 50%)”).

  • Color(“hsla(0.333, 1, 0.5, 1.0)”) or Color(“hsla(120deg, 100%, 50%, 100%)”).

  • Color(“hsluv(0.333, 0.5, 0.5)”) or Color(“hsluv(120deg, 50%, 50%)”).

  • Color(“hsluva(0.333, 0.5, 0.5, 1.0)”) or Color(“hsluva(120deg, 50%, 50%, 100%)”).

Parameters:

args (tuple, int, str) – The color specification. Check the docstring of this function for details on available format options.

property rgba

The RGBA tuple (values between 0 and 1).

property rgb

The RGB tuple (values between 0 and 1).

property r

The red value.

property g

The green value.

property b

The blue value.

property a

The alpha (transparency) value, between 0 and 1.

property gray

Return the grayscale intensity.

property hex

The CSS hex string, e.g. “#00ff00”. The alpha channel is ignored. Values are clipped to 00 an ff.

property hexa

The hex string including alpha, e.g. “#00ff00ff”. Values are clipped to 00 an ff.

property css

The CSS color string, e.g. “rgba(0,255,0,0.5)”.

clip()

Return a new Color with the values clipped between 0 and 1.

classmethod from_physical(r, g, b, a=1)

Create a Color object from a color in the physical colorspace.

With the physical colorspace we mean what is sometimes called Linear-sRGB. It has the same gamut as sRGB, but where sRGB is linear w.r.t. human perception, Linear-sRGB is linear w.r.t. lumen and photon counts. Calculations on colors in pygfx’s shaders are done in the physical colorspace.

to_physical()

Get the color represented in the physical colorspace, as 3 floats.

classmethod from_hsv(hue, saturation, value, alpha=1)

Create a Color object from a color in the HSV (a.k.a. HSB) colorspace.

HSV stands for hue, saturation, value (aka brightness). The hue component indicates the color tone. Values go from red (0) to green (0.333) to blue (0.666) and back to red (1). The satutation indicates vividness, with 0 meaning gray and 1 meaning the primary color. The value/brightness indicates goes from 0 (black) to 1 (white).

The alpha channel is optional and defaults to 1.

to_hsv()

Get the color represented in the HSV colorspace, as 3 floats.

to_hsva()

Get the color represented in the HSV colorspace, as 4 floats.

classmethod from_hsl(hue, saturation, lightness, alpha=1)

Create a Color object from a color in the HSL colorspace.

The HSL colorspace is similar to the HSV colorspace, except the “value” is replaced with “lightness”. This lightness scales the color differently, e.g. a lightness of 1 always represents full white.

The alpha channel is optional and defaults to 1.

to_hsl()

Get the color represented in the HSL colorspace, as 3 floats.

to_hsla()

Get the color represented in the HSL colorspace, as 4 floats.

classmethod from_hsluv(hue, saturation, lightness, alpha=1)

Create a Color object from a color in the HSLuv colorspace.

HSLuv is a human-friendly alternative to HSL. The hue component works the same as HSL/HSV, going from red (0) through green (0.333) and blue (0.666) back to red (1). The saturation ranges from 0 (grayscale) to 1 (pure color). The lightness ranges from 0 (black) to 1 (white). Unlike HSL/HSV, HSLuv provides perceptually uniform brightness and saturation.

The alpha channel is optional and defaults to 1.

to_hsluv()

Get the color represented in the HSLuv colorspace, as 3 floats.

to_hsluva()

Get the color represented in the HSLuv colorspace, as 4 floats.

lerp(target, t)

Linear interpolate from source color towards target color with factor t in RGBA space.

Parameters:
  • target (Color) – The target color to interpolate towards

  • t (float) – Interpolation factor between 0 and 1

Returns:

The interpolated color

Return type:

Color

lerp_in_hue(target, t, colorspace='hsluv')

Linear interpolate from source color towards target color with factor t in specified colorspace.

Parameters:
  • target (Color) – The target color to interpolate towards

  • t (float) – Interpolation factor between 0 and 1

  • colorspace (str) – The colorspace to interpolate in. One of “hsl”, “hsluv”, “hsv”. Default is “hsluv”

Returns:

The interpolated color

Return type:

Color

lighter(factor=0.5, colorspace='hsluv')

Make the color lighter by the given factor.

Parameters:
  • factor (float) – Factor to lighten by, between 0 and 1. Default is 0.5

  • colorspace (str) – The colorspace to use, one of “hsl”, “hsluv”, or “hsv”. Default is “hsluv”

Returns:

A lighter version of the color

Return type:

Color

darker(factor=0.5, colorspace='hsluv')

Make the color darker by the given factor.

Parameters:
  • factor (float) – Factor to darken by, between 0 and 1. Default is 0.5

  • colorspace (str) – The colorspace to use, one of “hsl”, “hsluv”, or “hsv”. Default is “hsluv”

Returns:

A darker version of the color

Return type:

Color

Examples

Simple Custom Object

Simple Custom Object

Custom Object

Custom Object

Labelled Image Grid

Labelled Image Grid

Toon Rendering 2

Toon Rendering 2

Pytorch Integration

Pytorch Integration