Source code for boltz_data.draw.color._ops
import numpy as np
from ._convert import Color, as_rgb_float_color, hex_from_rgb
[docs]
def darken(color: Color, amount: float) -> Color:
"""
Darken a color by the specified amount.
Returns the color in the same format as the input.
Args:
color: Color in any supported format.
amount: Amount to darken (0 = no change, 1 = black).
Returns:
Darkened color in the same format as input.
"""
rgb_float = as_rgb_float_color(color)
darkened = rgb_float * (1 - amount)
match color:
case str():
return hex_from_rgb(darkened)
case np.ndarray():
if color.dtype in (np.int32, np.int64, np.uint8):
return (darkened * 255).astype(color.dtype) # type: ignore[return-value]
return darkened.astype(color.dtype) # type: ignore[return-value]
case list():
if isinstance(color[0], int):
return (darkened * 255).astype(np.uint8).tolist() # type: ignore[no-any-return]
return darkened.tolist() # type: ignore[no-any-return]
case tuple():
if isinstance(color[0], int):
return tuple((darkened * 255).astype(np.uint8).tolist())
return tuple(darkened.tolist())
case _:
return darkened