Skip to content

Commit

Permalink
[patch] Define custom color conversions (#348)
Browse files Browse the repository at this point in the history
* [patch] Define custom color conversions

Instead of depending on matplotlib

* Format black

---------

Co-authored-by: pyiron-runner <[email protected]>
  • Loading branch information
liamhuber and pyiron-runner authored Jun 6, 2024
1 parent ec1a896 commit 8b333bd
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions pyiron_workflow/draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from typing import Literal, Optional, TYPE_CHECKING

import graphviz
from matplotlib.colors import to_hex, to_rgb
from pyiron_snippets.colors import SeabornColors

if TYPE_CHECKING:
Expand Down Expand Up @@ -45,12 +44,23 @@ def reverse_rankdir(rankdir: Literal["LR", "TB"]):
raise ValueError(f"Expected rankdir of 'LR' or 'TB' but got {rankdir}")


def _to_hex(rgb: tuple[int, int, int]) -> str:
"""RGB [0,255] to hex color codes; no alpha values."""
return "#{:02x}{:02x}{:02x}".format(*tuple(int(c) for c in rgb))


def _to_rgb(hex_: str) -> tuple[int, int, int]:
"""Hex to RGB color codes; no alpha values."""
hex_ = hex_.lstrip("#")
return tuple(int(hex_[i : i + 2], 16) for i in (0, 2, 4))


def blend_colours(color_a, color_b, fraction_a=0.5):
"""Blends two hex code colours together"""
return to_hex(
return _to_hex(
tuple(
fraction_a * a + (1 - fraction_a) * b
for (a, b) in zip(to_rgb(color_a), to_rgb(color_b))
for (a, b) in zip(_to_rgb(color_a), _to_rgb(color_b))
)
)

Expand Down

0 comments on commit 8b333bd

Please sign in to comment.