Skip to content

Instantly share code, notes, and snippets.

@apcamargo
Last active July 29, 2024 08:07
Show Gist options
  • Save apcamargo/6afbd008d97019e9a37a9ca01ca5a778 to your computer and use it in GitHub Desktop.
Save apcamargo/6afbd008d97019e9a37a9ca01ca5a778 to your computer and use it in GitHub Desktop.
from coloraide import Color
def lighten(
color: Color,
amount: float,
) -> Color:
"""
Lighten a color by a given amount.
"""
space = color.space()
color = color.convert("oklch")
l = min(color["l"], 1.0)
color["l"] = l * (1 + amount)
return color.convert(space)
def darken(
color: Color,
amount: float,
) -> Color:
"""
Darken a color by a given amount.
"""
space = color.space()
color = color.convert("oklch")
l = max(color["l"], 0.0)
color["l"] = l * (1 - amount)
return color.convert(space)
def saturate(
color: Color,
amount: float,
) -> Color:
"""
Saturate a color by a given amount.
"""
space = color.space()
color = color.convert("oklch")
s = min(color["c"], 1.0)
color["c"] = s * (1 + amount)
return color.convert(space)
def desaturate(
color: Color,
amount: float,
) -> Color:
"""
Desaturate a color by a given amount.
"""
space = color.space()
color = color.convert("oklch")
s = max(color["c"], 0.0)
color["c"] = max(s * (1 - amount), 1e-6)
return color.convert(space)
c = Color("#F4CC2D")
c = desaturate(c, 0.25)
print(c.to_string(hex=True))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment