-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
125 lines (101 loc) · 3.16 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
from math import radians, degrees
from dataclasses import dataclass
from enum import Enum
class Color:
""" Color class """
red: int
""" the amount of red in the color """
green: int
""" the amount of green in the color """
blue: int
""" the amount of blue in the color """
def __init__(self, red=0, green=0, blue=0):
self.red = red
self.green = green
self.blue = blue
@staticmethod
def from_hex(hex_string: str):
""" Make a color from hex values """
tup = tuple(int(hex_string.lstrip("#")[i:i + 2], 16) for i in (0, 2, 4))
return Color(tup[0], tup[1], tup[2])
def toRGB(self) -> tuple[int, int, int]:
""" Turns RGB value """
return self.red, self.green, self.blue
def toBGR(self) -> tuple[int, int, int]:
""" Turns BGR value """
return self.blue, self.green, self.red
def to_hex(self):
return "{:X}{:X}{:X}".format(self.red, self.green, self.blue)
class Angle:
""" Angle class """
degree: float
""" Angle in degrees unit """
radian: float
""" Angle in radian unit """
def __init__(self, **kwargs):
deg = kwargs.get("degree")
rad = kwargs.get("radian")
if deg is not None: # <<<<<
self.degree = deg
self.radian = radians(deg)
elif rad is not None: # <<<<<
self.radian = rad
self.degree = degrees(rad)
else:
raise ValueError("Please specify either \"degree\" or \"radian\"")
def offset(self, degree: float):
""" Offsets the value of angle """
self.degree += degree
self.radian = radians(self.degree)
return self
def offset_new(self, degree: float):
""" Returns a new angle object with the offset """
return Angle(degree=(self.degree + degree))
class Vec:
"""A vector class"""
x: float
y: float
a: float
b: float
w: float
h: float
def __init__(self, *args):
if len(args) == 1:
x = args[0][0]
y = args[0][1]
else:
x = args[0]
y = args[1]
self.x = x
self.y = y
self.a = x
self.b = y
self.w = x
self.h = y
def sep(self):
return self.x, self.y
def offset(self, *offset):
""" Offsets the position """
self.x += offset[0]
self.y += offset[1]
return self # returning self to allow chaining
def offset_new(self, *offset):
""" Returns a new position object with the offset """
return Vec(self.x + offset[0], self.y + offset[1])
class DisplayMode(Enum):
CORNER = False
""" Render objects from the corner """
CENTER = True
""" Render objects from the center """
class Side:
""" A side of a custom polygon """
position: Vec
""" Position of the side (base point) """
length: float
""" Length of the side """
angle: Angle
""" Rotation of the side """
def __init__(self, position: Vec = Vec(0, 0), angle: Angle = Angle(degree=0), length: float = 0):
self.position = position
self.angle = angle
self.length = length