mirror of
https://github.com/enzet/map-machine.git
synced 2025-05-20 20:46:24 +02:00
Issue #11: move color utility to separate module.
This commit is contained in:
parent
aee36d0703
commit
557e0d1992
4 changed files with 42 additions and 40 deletions
38
roentgen/color.py
Normal file
38
roentgen/color.py
Normal file
|
@ -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)])
|
|
@ -15,7 +15,8 @@ from roentgen.extract_icon import DEFAULT_SMALL_SHAPE_ID
|
|||
from roentgen.flinger import Flinger
|
||||
from roentgen.osm_reader import Map, OSMMember, OSMRelation, OSMWay, OSMNode, Tagged
|
||||
from roentgen.scheme import IconSet, Scheme
|
||||
from roentgen.util import MinMax, get_gradient_color
|
||||
from roentgen.util import MinMax
|
||||
from roentgen.color import get_gradient_color
|
||||
|
||||
DEBUG: bool = False
|
||||
TIME_COLOR_SCALE: List[Color] = [
|
||||
|
|
|
@ -25,7 +25,8 @@ from roentgen.osm_getter import get_osm
|
|||
from roentgen.osm_reader import Map, OSMReader
|
||||
from roentgen.scheme import Scheme
|
||||
from roentgen.direction import DirectionSet, Sector
|
||||
from roentgen.util import MinMax, is_bright
|
||||
from roentgen.util import MinMax
|
||||
from roentgen.color import is_bright
|
||||
|
||||
ICONS_FILE_NAME: str = "icons/icons.svg"
|
||||
TAGS_FILE_NAME: str = "data/tags.yml"
|
||||
|
|
|
@ -1,8 +1,3 @@
|
|||
from typing import Any, List
|
||||
|
||||
from colour import Color
|
||||
|
||||
|
||||
class MinMax:
|
||||
"""
|
||||
Minimum and maximum.
|
||||
|
@ -29,36 +24,3 @@ class MinMax:
|
|||
Get middle point between minimum and maximum.
|
||||
"""
|
||||
return (self.min_ + self.max_) / 2
|
||||
|
||||
|
||||
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)])
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue