-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #11: move color utility to separate module.
- Loading branch information
Showing
4 changed files
with
42 additions
and
40 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from typing import Any, List | ||
|
||
from colour import Color | ||
|
||
from roentgen.util import MinMax | ||
|
||
|
||
def is_bright(color: Color) -> bool: | ||
""" | ||
Is color bright enough to have black outline instead of white. | ||
""" | ||
return ( | ||
0.2126 * color.red * 256 + | ||
0.7152 * color.green * 256 + | ||
0.0722 * color.blue * 256 > 200) | ||
|
||
|
||
def get_gradient_color(value: Any, bounds: MinMax, colors: List[Color]): | ||
""" | ||
Get color from the color scale for the value. | ||
:param value: given value (should be in bounds) | ||
:param bounds: maximum and minimum values | ||
:param colors: color scale | ||
""" | ||
color_length: int = len(colors) - 1 | ||
scale = colors + [Color("black")] | ||
|
||
coefficient: float = ( | ||
0 if bounds.max_ == bounds.min_ else | ||
(value - bounds.min_) / (bounds.max_ - bounds.min_)) | ||
coefficient = min(1.0, max(0.0, coefficient)) | ||
m: int = int(coefficient * color_length) | ||
color_coefficient = (coefficient - m / color_length) * color_length | ||
|
||
return Color(rgb=[ | ||
scale[m].rgb[i] + color_coefficient * | ||
(scale[m + 1].rgb[i] - scale[m].rgb[i]) for i in range(3)]) |
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
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
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