-
Notifications
You must be signed in to change notification settings - Fork 368
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add utility to
rr.components.Color
to generate colors from any stri…
…ng (and use it in the air traffic data example) (#8458) ### What It's some time nice to log some color information in multiple entities to make it easier to relate them visually. This PR adds a `rr.components.Color.from_str()` utility that does exactly that: generate a nice color randomly picked up based on the provided string. This PR also updates the air traffic data example so the barometric traces have matching colors with the map data. --------- Co-authored-by: Clement Rey <[email protected]>
- Loading branch information
Showing
3 changed files
with
49 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from __future__ import annotations | ||
|
||
import colorsys | ||
import math | ||
from typing import TYPE_CHECKING | ||
|
||
if TYPE_CHECKING: | ||
from . import Color | ||
|
||
_GOLDEN_RATIO = (math.sqrt(5.0) - 1.0) / 2.0 | ||
|
||
|
||
class ColorExt: | ||
"""Extension for [Color][rerun.components.Color].""" | ||
|
||
@staticmethod | ||
def from_string(s: str) -> Color: | ||
""" | ||
Generate a random yet deterministic color based on a string. | ||
The color is guaranteed to be identical for the same input string. | ||
""" | ||
|
||
from . import Color | ||
|
||
# adapted from egui::PlotUi | ||
hue = (hash(s) & 0xFFFF) / 2**16 * _GOLDEN_RATIO | ||
return Color([round(comp * 255) for comp in colorsys.hsv_to_rgb(hue, 0.85, 0.5)]) |