Repository: ManimCommunity/manim
File: manim/utils/color/core.py (Python)

Code before:

    Returns
    -------
    ManimColor
        A random :class:`ManimColor`.
    """
    return RandomColorGenerator().next()


class RandomColorGenerator:
    """
    A generator for producing random colors from a given list of Manim colors,
    optionally in a reproducible sequence using a seed value.

    When initialized with a specific seed, this class produces a deterministic
    sequence of `ManimColor` instances. If no seed is provided, the selection is
    non-deterministic using Python’s global random state.

    Parameters
    ----------
    seed : int | None, optional
        A seed value to initialize the internal random number generator.
        If None (default), colors are chosen using the global random state.

    sample_colors : list[ManimColor], optional
        A custom list of Manim colors to sample from. Defaults to the full Manim color palette.

    Examples
    --------
    Without a seed (non-deterministic):
    >>> from manim import RandomColorGenerator, ManimColor, RED, GREEN, BLUE
    >>> rnd = RandomColorGenerator()
    >>> isinstance(rnd.next(), ManimColor)
    True

    With a seed (deterministic sequence):
    >>> rnd = RandomColorGenerator(42)
    >>> rnd.next()
    ManimColor('#ECE7E2')
    >>> rnd.next()
    ManimColor('#BBBBBB')
    >>> rnd.next()
    ManimColor('#BBBBBB')

    Re-initializing with the same seed gives the same sequence:
    >>> rnd2 = RandomColorGenerator(42)
    >>> rnd2.next()
    ManimColor('#ECE7E2')
    >>> rnd2.next()
    ManimColor('#BBBBBB')
    >>> rnd2.next()

Review comment (suggestion):
```suggestion
        A custom list of Manim colors to sample from. Defaults to the full Manim
        color palette.
```

Code after:
    -------
    ManimColor
        A random :class:`ManimColor`.
    """
    return RandomColorGenerator._random_color()


class RandomColorGenerator:
    _singleton: RandomColorGenerator | None = None
    """A generator for producing random colors from a given list of Manim colors,
    optionally in a reproducible sequence using a seed value.

    When initialized with a specific seed, this class produces a deterministic
    sequence of :class:`.ManimColor` instances. If no seed is provided, the selection is
    non-deterministic using Python’s global random state.

    Parameters
    ----------
    seed
        A seed value to initialize the internal random number generator.
        If ``None`` (the default), colors are chosen using the global random state.

    sample_colors
        A custom list of Manim colors to sample from. Defaults to the full Manim
        color palette.

    Examples
    --------
    Without a seed (non-deterministic)::

        >>> from manim import RandomColorGenerator, ManimColor, RED, GREEN, BLUE
        >>> rnd = RandomColorGenerator()
        >>> isinstance(rnd.next(), ManimColor)
        True

    With a seed (deterministic sequence)::

        >>> rnd = RandomColorGenerator(42)
        >>> rnd.next()
        ManimColor('#ECE7E2')
        >>> rnd.next()
        ManimColor('#BBBBBB')
        >>> rnd.next()
        ManimColor('#BBBBBB')

    Re-initializing with the same seed gives the same sequence::

        >>> rnd2 = RandomColorGenerator(42)
        >>> rnd2.next()
        ManimColor('#ECE7E2')
        >>> rnd2.next()