util module

class squaremap_combine.util.Color(red: int, green: int, blue: int, alpha: int = 255)

Represents a 24-bit color.

Format strings available:

Specifier

Output

{Color(255, 0, 255):x}

ff00ff

{Color(255, 0, 255):rgb}

(255, 0, 255)

{Color(255, 0, 255):rgba}

(255, 0, 255, 255)

__init__(red: int, green: int, blue: int, alpha: int = 255) None
as_hex(*, prefix: bool = True) str

Converts this color to an 8-character hexcode string, with leading # by default.

Parameters:

prefix – Whether to include # at the beginning of the string.

as_rgb() tuple[int, int, int]

Converts this color to a three-integer tuple representing its RGB values.

as_rgba() tuple[int, int, int, int]

Converts this color to a four-integer tuple representing its RGBA values.

copy() Color

Returns a new Color instance with the same channel values as this instance.

static ensure_hex_format(hexcode: str) str | None

Checks whether the given string is a valid 6 or 8 character hexcode, and returns the string if so, returning None if invalid. A 3 or 6-character hexcode will be converted to 8 by this function.

classmethod from_hex(hex_string: str) Self

Creates a Color instance from the a hexcode string. String must be either 3, 6, or 8 characters long. If 3 characters are used, they are doubled to create a 6-character hexcode to be used instead. The last 2 characters of an 8-character hexcode are used for the alpha value. Any 6-character hexcode will have the resulting color’s alpha assumed to be 255.

classmethod from_name(name: NamedColorHex | str, alpha: int | None = None) Self

Returns a Color instance created from hexcode found in the NamedColorHex enum.

Parameters:

alpha – An alpha value for the resulting color. If not None, this value will override the hexcode’s alpha value.

Raises:

ValueError – Raised if the given name does not exist as a NamedColorHex key.

classmethod from_str(s: str) Self

Attempts to create a Color instance with the given string, trying the following methods until one succeeds:

class squaremap_combine.util.ImplementableJSONEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)

Extended JSON encoder that attempts to call a __json__ method on the object being serialized, falling back on default JSONEncoder behavior otherwise.

default(o: Any) Any

Implement this method in a subclass such that it returns a serializable object for o, or calls the base implementation (to raise a TypeError).

For example, to support arbitrary iterators, you could implement default like this:

def default(self, o):
    try:
        iterable = iter(o)
    except TypeError:
        pass
    else:
        return list(iterable)
    # Let the base class default method raise the TypeError
    return super().default(o)
squaremap_combine.util.coerce_to(val: A | B, cls: type[B], coerce_fn: Callable[[A], B] | None = None) B

Returns val if val is an instance of cls, otherwise calls coerce_fn on val and returns the result. If coerce_fn is None, cls will be used as the callable.

squaremap_combine.util.draw_corners(img: Image | str | Path, *, length: int = 8, fill: Color | str = 'red') Image

Loads an image, either as an existing Image object or from a path, and draws lines of length pixels for each corner of the image, returning the edited image.

Note

Note that if given an Image object, the image will be edited in-place due to how Pillow’s ImageDraw.Draw method works.

squaremap_combine.util.snap_num(num: int | float, mult: int, snap_fn: Callable[[int | float], int]) int

Snaps the given num to the smallest or largest (depending on the outcome of snap_fn) multiple. of mult.