@@ -585,7 +585,7 @@ def invert(self, with_alpha=False) -> Self:
585585 new [- 1 ] = alpha
586586 return self ._construct_from_space (new )
587587
588- def interpolate (self , other : ManimColor , alpha : float ) -> Self :
588+ def interpolate (self , other : Self , alpha : float ) -> Self :
589589 """Interpolates between the current and the given ManimColor an returns the interpolated color
590590
591591 Parameters
@@ -606,6 +606,99 @@ def interpolate(self, other: ManimColor, alpha: float) -> Self:
606606 self ._internal_space * (1 - alpha ) + other ._internal_space * alpha
607607 )
608608
609+ def darker (self , blend : float = 0.2 ) -> Self :
610+ """Returns a new color that is darker than the current color, i.e.
611+ interpolated with black. The opacity is unchanged.
612+
613+ Parameters
614+ ----------
615+ blend : float, optional
616+ The blend ratio for the interpolation, from 0 (the current color
617+ unchanged) to 1 (pure black). By default 0.2 which results in a
618+ slightly darker color
619+
620+ Returns
621+ -------
622+ ManimColor
623+ The darker ManimColor
624+
625+ See Also
626+ --------
627+ :meth:`lighter`
628+ """
629+ from manim .utils .color .manim_colors import BLACK
630+
631+ alpha = self ._internal_space [3 ]
632+ black = self ._from_internal (BLACK ._internal_value )
633+ return self .interpolate (black , blend ).opacity (alpha )
634+
635+ def lighter (self , blend : float = 0.2 ) -> Self :
636+ """Returns a new color that is lighter than the current color, i.e.
637+ interpolated with white. The opacity is unchanged.
638+
639+ Parameters
640+ ----------
641+ blend : float, optional
642+ The blend ratio for the interpolation, from 0 (the current color
643+ unchanged) to 1 (pure white). By default 0.2 which results in a
644+ slightly lighter color
645+
646+ Returns
647+ -------
648+ ManimColor
649+ The lighter ManimColor
650+
651+ See Also
652+ --------
653+ :meth:`darker`
654+ """
655+ from manim .utils .color .manim_colors import WHITE
656+
657+ alpha = self ._internal_space [3 ]
658+ white = self ._from_internal (WHITE ._internal_value )
659+ return self .interpolate (white , blend ).opacity (alpha )
660+
661+ def contrasting (
662+ self ,
663+ threshold : float = 0.5 ,
664+ light : Self | None = None ,
665+ dark : Self | None = None ,
666+ ) -> Self :
667+ """Returns one of two colors, light or dark (by default white or black),
668+ that contrasts with the current color (depending on its luminance).
669+ This is typically used to set text in a contrasting color that ensures
670+ it is readable against a background of the current color.
671+
672+ Parameters
673+ ----------
674+ threshold : float, optional
675+ The luminance threshold that dictates whether the current color is
676+ considered light or dark (and thus whether to return the dark or
677+ light color, respectively), by default 0.5
678+ light : ManimColor, optional
679+ The light color to return if the current color is considered dark,
680+ by default pure white
681+ dark : ManimColor, optional
682+ The dark color to return if the current color is considered light,
683+ by default pure black
684+
685+ Returns
686+ -------
687+ ManimColor
688+ The contrasting ManimColor
689+ """
690+ from manim .utils .color .manim_colors import BLACK , WHITE
691+
692+ luminance , _ , _ = colorsys .rgb_to_yiq (* self .to_rgb ())
693+ if luminance < threshold :
694+ if light is not None :
695+ return light
696+ return self ._from_internal (WHITE ._internal_value )
697+ else :
698+ if dark is not None :
699+ return dark
700+ return self ._from_internal (BLACK ._internal_value )
701+
609702 def opacity (self , opacity : float ) -> Self :
610703 """Creates a new ManimColor with the given opacity and the same color value as before
611704
@@ -1282,7 +1375,7 @@ def color_gradient(
12821375
12831376
12841377def interpolate_color (
1285- color1 : ManimColorT , color2 : ManimColor , alpha : float
1378+ color1 : ManimColorT , color2 : ManimColorT , alpha : float
12861379) -> ManimColorT :
12871380 """Standalone function to interpolate two ManimColors and get the result refer to :meth:`interpolate` in :class:`ManimColor`
12881381
0 commit comments