vis_tools.Gradient module

Gradient.py

This file contains a class for doing Python-side color gradients. The Gradient class is a gradient with an arbitrary number of color stops. Since Gradient is derived from the Vis-Tools gradient.js, the same style text representations of gradients can be used.

Classes:

  • Gradient - a sampleable color gradient.

Usage:

gradient = Gradient("green@0,[email protected],[email protected],red@1")
color = gradient.sample(0.5)
class vis_tools.Gradient.Gradient(spec='')[source]

Bases: object

Class for sampleable color gradients.

This class lets you create a color gradient with an arbitrary number of color stops on a normalized range from 0 to 1. You then sample the gradient with a normalized value from 0 to 1 to get a color out of the gradient.

This class is based on the Vis-Tools gradient.js. However it does not support the “,r” or “,q<steps>” suffixes supported by gradient.js.

The spec format is:

<color>@0,[<color>@<loc>,…]<color>@1

Where:
  • color - Color objects representing the stop colors

  • loc - Ordered values in range(0, 1) representing the normalized locations of the gradient stops.

Raises:

ValueError – if spec is invalid.

sample(loc0to1)[source]

Sample the gradient to get a color at a particular location.

Returns:

A Color object for the color at the sample point.

Return type:

obj

Parameters:
  • loc0to1 (float) – A normalized value in the range(0, 1) at which

  • color. (point to sample the gradient) –