Style Module
The style module contains regular expressions and functions for parsing and composing style strings.
Style string definition: https://datatracker.ietf.org/doc/id/draft-slevinski-formal-signwriting-10.html#name-styling-string
- sutton_signwriting_core.style.style_parse(style_string)
Parse a style string to a structured dictionary.
- Parameters:
style_string (str) – a style string
- Returns:
Dictionary with style elements
- Return type:
Example
>>> style_parse('-CP10G_blue_D_red,Cyan_') {'colorize': True, 'padding': 10, 'background': 'blue', 'detail': ['red', 'Cyan']}
- sutton_signwriting_core.style.style_compose(style_dict)
Function to compose style string from dictionary.
- Parameters:
style_dict (StyleObject) – a dictionary of style elements with keys: - ‘colorize’: Optional[bool] - ‘padding’: Optional[int] - ‘background’: Optional[str] - ‘detail’: Optional[List[str]] - ‘zoom’: Optional[Union[float, str]] - ‘detailsym’: Optional[List[Dict]] (each with ‘index’: int, ‘detail’: List[str]) - ‘classes’: Optional[str] - ‘id’: Optional[str]
- Returns:
style string
- Return type:
str | None
Example
>>> style_compose({ ... 'colorize': True, ... 'padding': 10, ... 'background': 'blue', ... 'detail': ['red', 'Cyan'], ... 'zoom': 1.1, ... 'detailsym': [ ... {'index': 1, 'detail': ['#ff0', 'green']} ... ] ... }) '-CP10GblueDred,CyanZ1.1-D01#ff0,green_'
- sutton_signwriting_core.style.style_rgb_to_hex(rgb, tolerance=0.0)
Function to convert rgb color to hex or “transparent” if below tolerance.
- Parameters:
rgb (str) – an rgb color
tolerance (float) – max alpha for full transparency (default: 0)
- Returns:
a hex color or “transparent”
- Return type:
str
Examples
>>> style_rgb_to_hex("rgb(255,255,255)") 'ffffff' >>> style_rgb_to_hex("rgba(255,255,255,0.5)",0.5) 'transparent'
- sutton_signwriting_core.style.style_rgba_to_hex(color, background)
Function to merge color with background based on alpha transparency
- Parameters:
color (str) – an rgba color
background (str) – an rgba background color
- Returns:
a hex color or “transparent”
- Return type:
str
Example
>>> style_rgba_to_hex("rgba(255,255,255,0.5)","rgb(0,0,0)") '7f7f7f'
- sutton_signwriting_core.style.style_merge(style1, style2)
Function to merge style dictionaries.
- Parameters:
style1 (StyleObject | None) – a style dictionary
style2 (StyleObject | None) – a style dictionary
- Returns:
a style dictionary
- Return type:
Example
>>> style_merge({'colorize': True}, {'zoom': 2}) {'colorize': True, 'zoom': 2}