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.
- 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
Noneif invalid. A 3 or 6-character hexcode will be converted to 8 by this function.
- classmethod from_hex(hex_string: str) Self¶
Creates a
Colorinstance 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
Colorinstance created from hexcode found in theNamedColorHexenum.- 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
namedoes not exist as aNamedColorHexkey.
- classmethod from_str(s: str) Self¶
Attempts to create a
Colorinstance 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 aTypeError).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
valifvalis an instance ofcls, otherwise callscoerce_fnonvaland returns the result. Ifcoerce_fnisNone,clswill 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
Imageobject or from a path, and draws lines oflengthpixels for each corner of the image, returning the edited image.Note
Note that if given an
Imageobject, the image will be edited in-place due to how Pillow’sImageDraw.Drawmethod works.
- squaremap_combine.util.snap_num(num: int | float, mult: int, snap_fn: Callable[[int | float], int]) int¶
Snaps the given
numto the smallest or largest (depending on the outcome ofsnap_fn) multiple. ofmult.