fdtdx.colors.Color#

class fdtdx.colors.Color(r, g, b)[source]#

Bases: TreeClass

Color representation with multiple format support.

The class contains colors which are from the XKCD color survey: https://xkcd.com/color/rgb.txt and fdtdx implements most of them.

This class represents a color and provides methods to convert between different color formats. Internally, colors are stored as normalized RGB values in the range [0, 1].

Quick Reference#

Attributes

Methods

Attributes#

Color.b: float#

Blue component, normalized to [0, 1]

Type:

b (float)

Color.g: float#

Green component, normalized to [0, 1]

Type:

g (float)

Color.r: float#

Red component, normalized to [0, 1]

Type:

r (float)

Methods#

Color.aset(attr_name, val, create_new_ok=False)#

Sets an attribute of this class. In contrast to the classical .at[].set(), this method updates the class attribute directly and does not only operate on jax pytree leaf nodes. Instead, replaces the full attribute with the new value.

The attribute can either be the attribute name of this class, or for nested classes it can also be the attribute name of a class, which itself is an attribute of this class. The syntax for this operation could look like this: “a->b->[0]->[‘name’]”. Here, the current class has an attribute a, which has an attribute b, which is a list, which we index at index 0, which is an element of type dictionary, which we index using the dictionary key ‘name’.

Note that dictionary keys cannot contain square brackets or single quotes (even if they are escaped).

Parameters:
  • attr_name (str) – Name of attribute to set

  • val (Any) – Value to set the attribute to

  • create_new_ok (bool, optional) – If false (default), throw an error if the attribute does not exist. If true, creates a new attribute if the attribute name does not exist yet.

Returns:

Updated instance with new attribute value

Return type:

Self

classmethod Color.from_hex(hex_string)[source]#

Create a Color from a hexadecimal color string.

Parameters:

hex_string (str) – Hex color string (e.g., “#FF0000”, “FF0000”, “#F00”)

Return type:

Color

Returns:

Color instance

Raises:

ValueError – If hex string is invalid

classmethod Color.from_rgb(r, g, b)[source]#

Create a Color from 8-bit RGB values (0-255).

Parameters:
  • r (int) – Red component (0-255)

  • g (int) – Green component (0-255)

  • b (int) – Blue component (0-255)

Return type:

Color

Returns:

Color instance

Color.get_class_fields()#
Return type:

list[TreeClassField]

Color.get_public_fields()#
Return type:

list[TreeClassField]

Color.to_hex()[source]#

Return color as hexadecimal string.

Return type:

str

Returns:

Hex color string with leading # (e.g., “#FF0000”)

Color.to_mpl()[source]#

Return color in matplotlib-compatible format.

This is an alias for to_rgb_normalized() for clarity when using with matplotlib.

Return type:

tuple[float, float, float]

Returns:

Tuple of (r, g, b) with values in [0, 1]

Color.to_rgb_255()[source]#

Return color as 8-bit RGB tuple (0-255).

Return type:

tuple[int, int, int]

Returns:

Tuple of (r, g, b) with integer values in [0, 255]

Color.to_rgb_normalized()[source]#

Return color as normalized RGB tuple [0, 1].

Return type:

tuple[float, float, float]

Returns:

Tuple of (r, g, b) with values in [0, 1]

If you find any errors in the documentation, please report them in the Github Issues!