inkext.css¶
A simple library of functions to parse and format CSS style properties.
- inkext.css.color_to_frgba(color: float | str | Sequence[float] | Sequence[int]) tuple[float, float, float, float]¶
Convert a color to RGBA float form.
All values will be in the range 0.0 - 1.0.
If color is a single numeric value then it is converted to a grayscale RGBA.
- Parameters:
color – A possibly malformed CSS color string or number.
- Returns:
A RGBAColor color in the form #rrggbb. If the CSS color can’t be parsed then black (0.0, 0.0, 0.0, 1.0) is returned.
- inkext.css.csscolor_to_hexrgb(color: float | int | str | Sequence[float] | Sequence[int]) str¶
Return a color in CSS hex form #rrggbb.
If color is a numeric value then it is converted to a grayscale number. If the number is a floating point value between zero and one then it is scaled up to 0-255 grayscale. If the CSS color can’t be parsed then #000000 is returned.
- Parameters:
color – A possibly malformed CSS color string or number.
- Returns:
A CSS hex color in the form #rrggbb.
- inkext.css.csscolor_to_rgb(css_color: str) tuple[int, int, int] | tuple[int, int, int, int]¶
Parse a CSS color property value into an RGB value.
RGB components are integers in the range 0-255.
- Parameters:
css_color – A CSS color property string. I.e. “#ffc0ee” or “white”.
- Returns:
(r, g, b).
- Return type:
A tuple containing the RGB values
- inkext.css.csshex_to_rgb(hex_color: str) tuple[int, int, int] | tuple[int, int, int, int]¶
Convert a CSS hex color property to RGB/RGBA.
- Parameters:
hex_color – A CSS hex property string.
- Returns:
The RGBA value as a tuple of four integers (r, g, b, a) in the range 0-255 if opacity is specified (8 char hex value) otherwise a three-tuple (r, g, b). Returns (0, 0, 0) by default if the hex value can’t be parsed.
- inkext.css.cssrgb_to_rgb(rgb_color: str) tuple[int, int, int] | tuple[int, int, int, int]¶
Convert a CSS rgb or rgba color property to RGB.
- Parameters:
rgb_color – A CSS rgb property string: i.e. rgb(r, g, b).
- Returns:
The RGB value as a tuple or list of three integers plus an optional fourth float value if there is an alpha channel. Returns (0, 0, 0) by default if the hex value can’t be parsed.
- inkext.css.dict_to_inline_style(style_map: dict) str¶
Create an inline style attribute string.
From a dictionary of CSS style properties.
- Parameters:
style_map – A dictionary of CSS style properties.
- Returns:
A string containing inline CSS style properties.
- inkext.css.inline_style_to_dict(inline_style: str) dict¶
Create a dictionary of style properties from an inline style attribute.
- Parameters:
inline_style – A string containing the value of a CSS style attribute.
- Returns:
A dictionary of style properties.
- inkext.css.is_frgba(color: Sequence) bool¶
Return True if this is a RGB/A color spec with float values.
The values should be >= 0.0 and <= 1.0.
This can fail for the colors white and black if they are supposed to be floats in the range (0.0 - 1.0) but are initialized with ints. For example (0, 0, 0) or (1, 1, 1).
- inkext.css.is_irgba(color: Sequence) bool¶
Return True if this is a RGB/A color spec with all integer values.
Normally this would be integers in the range of 0-255. This does not check the range in case the colors will be clipped later.
This can fail for the colors white and black if they are supposed to be floats in the range (0.0 - 1.0) but are initialized with ints. For example (0, 0, 0) or (1, 1, 1).
- inkext.css.parse_channel_value(value: str) int¶
Parse a CSS color channel value.
- Parameters:
value – A valid CSS color channel value string. Can be an integer number or an integer percentage.
- Returns:
An integer value between 0 and 255. Default is 0 if the value isn’t a valid channel value.
- inkext.css.rgba_to_cssa(rgba: Sequence[float | int]) tuple[str, float]¶
Convert RGB[A] values to a CSS hex color and opacity.
Color will be in CSS hex form (ie “#c0c0c0”) and opacity will be a float 0.0 - 1.0.
- inkext.css.to_inline_style(**kwargs) str¶
Create inline style from keyword attributes.