Colorful String Utilities

gymcts.colorful_console_utils.rgb_color_sequence(r: int | float, g: int | float, b: int | float, *, format_type: str = 'foreground') str[source]

generates a color-codes, that change the color of text in console outputs.

rgb values must be numbers between 0 and 255 or 0.0 and 1.0.

Parameters:
  • r – red value.

  • g – green value

  • b – blue value

  • format_type – specifies weather the foreground-color or the background-color shall be adjusted. valid options: ‘foreground’,’background’

Returns:

a string that contains the color-codes.

gymcts.colorful_console_utils.wrap_with_color_codes(s: object, /, r: int | float, g: int | float, b: int | float, **kwargs) str[source]

stringify an object and wrap it with console color codes. It adds the color control sequence in front and one at the end that resolves the color again.

rgb values must be numbers between 0 and 255 or 0.0 and 1.0.

Parameters:
  • s – the object to stringify and wrap

  • r – red value.

  • g – green value.

  • b – blue value.

  • kwargs – additional argument for the ‘DisjunctiveGraphJspVisualizer.rgb_color_sequence’-method.

Returns:

gymcts.colorful_console_utils.wrap_evenly_spaced_color(s: Any, n_of_item: int, n_classes: int, c_map='rainbow') str[source]

Wraps a string with a color scale (a matplotlib c_map) based on the n_of_item and n_classes. This function is used to color code the available actions in the MCTS tree visualisation. The children of the MCTS tree are colored based on their action for a clearer visualisation.

Parameters:
  • s – the string (or object) to be wrapped. objects are converted to string (using the __str__ function).

  • n_of_item – the index of the item to be colored. In a mcts tree, this is the (parent-)action of the node.

  • n_classes – the number of classes (or items) to be colored. In a mcts tree, this is the number of available actions.

  • c_map – the colormap to be used (default is ‘rainbow’). The colormap can be any matplotlib colormap, e.g. ‘viridis’, ‘plasma’, ‘inferno’, ‘magma’, ‘cividis’.

Returns:

a string that contains the color-codes (prefix and suffix) and the string s in between.

gymcts.colorful_console_utils.wrap_with_color_scale(s: str, value: float, min_val: float, max_val: float, c_map=None) str[source]

Wraps a string with a color scale (a matplotlib c_map) based on the value, min_val, and max_val.

Parameters:
  • s – the string to be wrapped

  • value – the value to be mapped to a color

  • min_val – the minimum value of the scale

  • max_val – the maximum value of the scale

  • c_map – the colormap to be used (default is ‘rainbow’)

Returns: