shades.utils

utils

contains general purpose functions for use within or outside module

 1"""
 2utils
 3
 4contains general purpose functions for use within or outside module
 5"""
 6from typing import Tuple, Union
 7from random import randint
 8
 9
10def color_clamp(color: Tuple[int, int, int]) -> Tuple[int, int, int]:
11    """
12    Ensures a three part iterable is a properly formatted color
13    (i.e. all numbers between 0 and 255)
14    """
15    clamped_color = [max(min(int(i), 255), 0) for i in color]
16    return tuple(clamped_color)
17
18
19def distance_between_points(xy1: Tuple[int, int], xy2: Tuple[int, int]) -> float:
20    """
21    Returns the euclidean distance between two points.
22    https://en.wikipedia.org/wiki/Euclidean_distance
23    """
24    return (((xy1[0] - xy2[0]) ** 2) + ((xy1[1] - xy2[1]) ** 2)) ** 0.5
25
26
27def randomly_shift_point(
28        xy_coords: Tuple[int, int],
29        movement_range: Union[Tuple[int, int], Tuple[Tuple[int, int], Tuple[int, int]]],
30    ) -> Tuple[int, int]:
31    """
32    Randomly shifts a point within defined range
33
34    movement range of form:
35    (min amount, max amount)
36
37    you can give two movement ranges for:
38    [(min amount on x axis, max amount on x axis),
39    (min amount on y axis, max amount on y axis)]
40    or just one, if you want equal ranges
41    """
42    if type(movement_range[0]) not in [list, tuple]:
43        movement_range = [movement_range, movement_range]
44
45    shifted_xy = [
46        xy_coords[i] +
47        randint(movement_range[i][0], movement_range[i][1])
48        for i in range(2)
49    ]
50    return tuple(shifted_xy)
def color_clamp(color: Tuple[int, int, int]) -> Tuple[int, int, int]:
11def color_clamp(color: Tuple[int, int, int]) -> Tuple[int, int, int]:
12    """
13    Ensures a three part iterable is a properly formatted color
14    (i.e. all numbers between 0 and 255)
15    """
16    clamped_color = [max(min(int(i), 255), 0) for i in color]
17    return tuple(clamped_color)

Ensures a three part iterable is a properly formatted color (i.e. all numbers between 0 and 255)

def distance_between_points(xy1: Tuple[int, int], xy2: Tuple[int, int]) -> float:
20def distance_between_points(xy1: Tuple[int, int], xy2: Tuple[int, int]) -> float:
21    """
22    Returns the euclidean distance between two points.
23    https://en.wikipedia.org/wiki/Euclidean_distance
24    """
25    return (((xy1[0] - xy2[0]) ** 2) + ((xy1[1] - xy2[1]) ** 2)) ** 0.5

Returns the euclidean distance between two points. https://en.wikipedia.org/wiki/Euclidean_distance

def randomly_shift_point( xy_coords: Tuple[int, int], movement_range: Union[Tuple[int, int], Tuple[Tuple[int, int], Tuple[int, int]]]) -> Tuple[int, int]:
28def randomly_shift_point(
29        xy_coords: Tuple[int, int],
30        movement_range: Union[Tuple[int, int], Tuple[Tuple[int, int], Tuple[int, int]]],
31    ) -> Tuple[int, int]:
32    """
33    Randomly shifts a point within defined range
34
35    movement range of form:
36    (min amount, max amount)
37
38    you can give two movement ranges for:
39    [(min amount on x axis, max amount on x axis),
40    (min amount on y axis, max amount on y axis)]
41    or just one, if you want equal ranges
42    """
43    if type(movement_range[0]) not in [list, tuple]:
44        movement_range = [movement_range, movement_range]
45
46    shifted_xy = [
47        xy_coords[i] +
48        randint(movement_range[i][0], movement_range[i][1])
49        for i in range(2)
50    ]
51    return tuple(shifted_xy)

Randomly shifts a point within defined range

movement range of form: (min amount, max amount)

you can give two movement ranges for: [(min amount on x axis, max amount on x axis), (min amount on y axis, max amount on y axis)] or just one, if you want equal ranges