Skip to content

Utils

This page documents the public utilities provided by mini-svg.

They are useful in any level (basic/intermediate/advanced) and include:

  • COLORS: immutable color constants (attribute access like COLORS.YELLOW)
  • PALETTES: immutable color palettes for generative art (e.g. PALETTES.WARHOL)
  • Global default style: set_style(...) / get_style(...)
  • Color helpers: rgb, hex_to_rgb, gray_255, gray_percent, hsl
  • Palette helper: palette_pick(...)

Public API

mini_svg.style

mini_svg/style.py

Public style utilities for the mini-svg project by :hagane.

This module is part of the public API and is meant to be documented.

It provides:

Colors
  • rgb(), hex_to_rgb()
  • gray_255(), gray_percent()
  • hsl()
  • COLORS (immutable constants)
Palettes
  • PALETTES (immutable tuples of colors)
  • palette_pick()
Global defaults
  • set_style(...)
  • get_style(field)

The default style is used by drawing functions when style parameters are omitted (passed as None).

rgb

rgb(r: int, g: int, b: int) -> str

Convert RGB components (0..255) to a hex SVG color string.

Parameters:

  • r (int) –
    Red, green and blue components as integers in the range 0..255.
    
  • g (int) –
    Red, green and blue components as integers in the range 0..255.
    
  • b (int) –
    Red, green and blue components as integers in the range 0..255.
    

Returns:

  • str

    A hex color string like "#ff8800", suitable for SVG fill or stroke.

Examples:

import mini_svg as minisvg
color = minisvg.rgb(255, 136, 0)
# "#ff8800"

hex_to_rgb

hex_to_rgb(hex_color: str) -> Tuple[int, int, int]

Convert a hex color string "#rrggbb" to an (r, g, b) tuple.

Parameters:

  • hex_color (str) –
    A hex color string like "#ff8800".
    

Returns:

  • Tuple[int, int, int]

    (r, g, b) tuple with integers in the range 0..255.

Raises:

  • ValueError

    If hex_color is not in the form "#rrggbb".

Examples:

import mini_svg as minisvg
r, g, b = minisvg.hex_to_rgb("#ff8800")

gray_255

gray_255(v: int) -> str

Create a grayscale color from an integer intensity (0..255).

Parameters:

  • v (int) –
    Intensity from 0 (black) to 255 (white).
    

Returns:

  • str

    A hex grayscale color string like "#808080".

gray_percent

gray_percent(p: float) -> str

Create a grayscale color from a percentage in the range 0.0..1.0.

Parameters:

  • p (float) –
    0.0 produces black, 1.0 produces white.
    

Returns:

  • str

    A hex grayscale color string.

hsl

hsl(h: float, s: float, l: float) -> str

Convert HSL (Hue, Saturation, Lightness) to a hex SVG color string.

Parameters:

  • h (float) –
    Hue in degrees (any value; wraps around 360).
    
  • s (float) –
    Saturation in the range 0.0..1.0.
    
  • l (float) –
    Lightness in the range 0.0..1.0.
    

Returns:

  • str

    A hex color string like "#33aaff".

Notes

This is very convenient for generative art: sweep hue in a loop.

Examples:

import mini_svg as minisvg
for i in range(10):
        c = minisvg.hsl(i * 36, 0.8, 0.5)

set_style

set_style(
    *,
    stroke: Optional[str] = None,
    fill: Optional[str] = None,
    stroke_width: Optional[int] = None,
    opacity: Optional[float] = None,
    rotation_deg: Optional[float] = None,
    font_size: Optional[int] = None,
) -> None

Set global default drawing style.

Any parameter left as None remains unchanged.

Parameters:

  • stroke (Optional[str], default: None ) –
    Default stroke (outline) color.
    
  • fill (Optional[str], default: None ) –
    Default fill color.
    
  • stroke_width (Optional[int], default: None ) –
    Default stroke width (integer pixels).
    
  • opacity (Optional[float], default: None ) –
    Default opacity (0.0..1.0).
    
  • rotation_deg (Optional[float], default: None ) –
    Default rotation angle in degrees (used by polygons/stars).
    
  • font_size (Optional[int], default: None ) –
    Default font size (used by text()).
    
Notes

Drawing functions use these defaults when their style parameters are omitted.

Examples:

import mini_svg as minisvg
minisvg.set_style(stroke=minisvg.COLORS.DARK_GRAY, stroke_width=2, opacity=0.9)

get_style

get_style(field: str) -> object

Get one global default style field by name.

Parameters:

  • field (str) –
    Name of the field.
    

Returns:

  • object

    The current value.

Raises:

  • KeyError

    If field does not exist.

Examples:

import mini_svg as minisvg
current = minisvg.get_style("stroke_width")

palette_pick

palette_pick(palette: Tuple[str, ...], i: int) -> str

Pick a color from a palette using wrap-around indexing.

Parameters:

  • palette (Tuple[str, ...]) –
    A palette tuple, for example `PALETTES.WARHOL`.
    
  • i (int) –
    Any integer index. The palette wraps around automatically.
    

Returns:

  • str

    A color string.

Examples:

import mini_svg as minisvg
p = minisvg.PALETTES.WARHOL
c0 = minisvg.palette_pick(p, 0)
c1 = minisvg.palette_pick(p, 999)  # wraps around