color_from_string

color_from_string#

treescope.formatting_util.color_from_string(key_string: str, lightness: float = 0.785, chroma: float = 0.103, alpha: float = 1.0) str[source]#

Derives a color whose hue is keyed by a string.

The main use for this is to automatically generate a color for the CSS OKLCH color space, which can be used to set a type-dependent color for an object in treescope.

The default values of lightness and chroma are set up to give a consistent range of colors that are all within gamut.

Parameters:
  • key_string – A string that should be hashed to determine a hue.

  • lightness – Lightness argument (0 to 1)

  • chroma – Chroma argument (0 to 0.5 in practice; large values are clipped)

  • alpha – Opacity (0 to 1)

Returns:

A pseudo-uniformly-distributed number between 0 and 360, suitable for use as the third argument to the CSS oklch(...) function.