mirror of
https://github.com/enzet/map-machine.git
synced 2025-05-24 22:46:22 +02:00
Rename Röntgen to Map Machine.
This commit is contained in:
parent
38c4e00de3
commit
2bd89a6539
57 changed files with 252 additions and 236 deletions
53
map_machine/color.py
Normal file
53
map_machine/color.py
Normal file
|
@ -0,0 +1,53 @@
|
|||
"""
|
||||
Color utility.
|
||||
"""
|
||||
from typing import Any
|
||||
|
||||
from colour import Color
|
||||
|
||||
from map_machine.util import MinMax
|
||||
|
||||
__author__ = "Sergey Vartanov"
|
||||
__email__ = "me@enzet.ru"
|
||||
|
||||
|
||||
def is_bright(color: Color) -> bool:
|
||||
"""
|
||||
Check whether color bright enough to have black outline instead of white.
|
||||
"""
|
||||
return (
|
||||
0.2126 * color.red + 0.7152 * color.green + 0.0722 * color.blue
|
||||
> 0.78125
|
||||
)
|
||||
|
||||
|
||||
def get_gradient_color(
|
||||
value: Any, bounds: MinMax, colors: list[Color]
|
||||
) -> 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: list[Color] = colors + [Color("black")]
|
||||
|
||||
range_coefficient: float = (
|
||||
0 if bounds.is_empty() else (value - bounds.min_) / bounds.delta()
|
||||
)
|
||||
# If value is out of range, set it to boundary value.
|
||||
range_coefficient = min(1.0, max(0.0, range_coefficient))
|
||||
index: int = int(range_coefficient * color_length)
|
||||
coefficient: float = (
|
||||
range_coefficient - index / color_length
|
||||
) * color_length
|
||||
|
||||
return Color(
|
||||
rgb=[
|
||||
scale[index].rgb[i]
|
||||
+ coefficient * (scale[index + 1].rgb[i] - scale[index].rgb[i])
|
||||
for i in range(3)
|
||||
]
|
||||
)
|
Loading…
Add table
Add a link
Reference in a new issue