inkext.css

A simple library of functions to parse and format CSS style properties.

inkext.css.csscolor_to_cssrgb(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 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

See:

https://developer.mozilla.org/en-US/docs/Web/CSS/color

inkext.css.csshex_to_rgb(hex_color: str) tuple[int, int, int]

Convert a CSS hex color property to RGB.

Parameters:

hex_color – A CSS hex property string.

Returns:

(r, g, b). Returns (0, 0, 0) by default if the hex value can’t be parsed.

Return type:

The RGB value as a tuple of three integers

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.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.

inkext.css.to_inline_style(**kwargs) str

Create inline style from keyword attributes.