27
27
of any additions to this parser TL,DR: Dev life will be easier if it is ensured that commas in colour schemas
28
28
are inside of parentheses, brackets, or curly braces (like "rgb(20, 20, 20)")"""
29
29
30
- import pygame
30
+ import enum
31
31
from typing import (
32
32
Callable ,
33
33
Union ,
34
- Iterable ,
35
34
TypeVar ,
36
35
Optional ,
37
36
List ,
41
40
TypedDict ,
42
41
)
43
42
44
- import enum
43
+ import pygame
44
+
45
45
from pygame_gui .core .colour_gradient import ColourGradient
46
46
from pygame_gui .core .utility import premul_col
47
47
from pygame_gui ._constants import _namedColours
@@ -61,6 +61,13 @@ class NumParserType(enum.Enum):
61
61
62
62
63
63
def is_num_str (string : str ) -> bool :
64
+ """
65
+ Check if a string converts to a number.
66
+
67
+ :param string: the string to check
68
+
69
+ :return: A boolean, True if the string converts to a number, False otherwise
70
+ """
64
71
try :
65
72
float (string )
66
73
return True
@@ -69,14 +76,36 @@ def is_num_str(string: str) -> bool:
69
76
70
77
71
78
def is_int_str (string : str ) -> bool :
79
+ """
80
+ Check if a string converts to a round number.
81
+
82
+ :param string: the string to check
83
+
84
+ :return: A boolean, True if the string converts to a non-fractional number, False otherwise
85
+ """
72
86
return is_num_str (string ) and "." not in string
73
87
74
88
75
89
def is_float_str (string : str ):
90
+ """
91
+ Check if a string converts to a fractional number.
92
+
93
+ :param string: the string to check
94
+
95
+ :return: A boolean, True if the string converts to a fractional number, False otherwise
96
+ """
76
97
return is_num_str (string ) and "." in string
77
98
78
99
79
100
def is_degree_string (strdata : str ) -> bool :
101
+ """
102
+ Check if a string converts to a degree. This means that it either ends 'deg' and the rest is an integer number
103
+ between -360 and 360; or it is just an integer number between -360 and 360.
104
+
105
+ :param strdata: the string to check
106
+
107
+ :return: A boolean, True if the string converts to a degree, False otherwise
108
+ """
80
109
if strdata != "" :
81
110
if strdata .endswith ("deg" ) and len (strdata ) > 3 :
82
111
if is_int_str (strdata [:- 3 ]):
@@ -89,30 +118,73 @@ def is_degree_string(strdata: str) -> bool:
89
118
90
119
91
120
def parse_degree_string (strdata : str ) -> int :
121
+ """
122
+ Converts a string to a degree integer. This means that it either ends 'deg' and the rest is an integer number
123
+ between -360 and 360; or it is just an integer number between -360 and 360.
124
+
125
+ Should be used with 'is_degree_string()' to check for validity.
126
+
127
+ :param strdata: the string to parse.
128
+
129
+ :return: an integer between -360 and 360
130
+ """
92
131
if strdata != "" :
93
132
return int (strdata [:- 3 ]) if strdata .endswith ("deg" ) else int (strdata )
133
+ return 0
94
134
95
135
96
136
def is_percentage_string (strdata : str ) -> bool :
137
+ """
138
+ Check if a string converts to a percentage. This means that it either ends '%' and the rest is an integer number;
139
+ or it is a float number between 0.0 and 1.0.
140
+
141
+ :param strdata: the string to check
142
+
143
+ :return: A boolean, True if the string converts to a percentage, False otherwise
144
+ """
97
145
if strdata != "" :
98
146
if strdata [- 1 ] == "%" and is_int_str (strdata [:- 1 ]):
99
147
return True
100
148
101
149
if is_float_str (strdata ):
102
150
float_value = float (strdata )
103
- return 0 <= float_value <= 1
151
+ return 0.0 <= float_value <= 1.0
104
152
return False
105
153
106
154
107
155
def parse_percentage_string (strdata : str ) -> float :
156
+ """
157
+ returns a percentage float value between 0.0 and 1.0 parsed from the input string.
158
+
159
+ Should be used in conjunction with is_percentage_string()
160
+
161
+ :param strdata: the string to parse
162
+
163
+ :return: a float between 0.0 and 1.0
164
+ """
108
165
return float (strdata ) if is_float_str (strdata ) else float (strdata [:- 1 ]) / 100
109
166
110
167
111
168
def is_u8_string (strdata : str ) -> bool :
169
+ """
170
+ Checks if the string can be converted to an integer between 0 and 255.
171
+
172
+ :param strdata: the string to parse
173
+ :return: a boolean - True if the string can convert to an integer between 0 and 255
174
+ """
112
175
return is_int_str (strdata ) and 0 <= int (strdata ) <= 255
113
176
114
177
115
178
def parse_u8_string (strdata : str ) -> int :
179
+ """
180
+ Parse the input string to an integer (between 0 and 255).
181
+
182
+ Should be used in conjunction with is_u8_string()
183
+
184
+ :param strdata: the string to parse
185
+
186
+ :return: an integer between 0 and 255
187
+ """
116
188
return int (strdata )
117
189
118
190
@@ -121,6 +193,11 @@ def parse_u8_string(strdata: str) -> int:
121
193
122
194
123
195
class ColourValueParserData (TypedDict ):
196
+ """
197
+ A class that groups together parsers and validators for turning strings into numerical values for colours.
198
+
199
+ """
200
+
124
201
validator : ColourValueValidator
125
202
parser : ColourValueParser
126
203
@@ -137,9 +214,9 @@ class ColourValueParserData(TypedDict):
137
214
},
138
215
NumParserType .FLOAT : {
139
216
"validator" : is_float_str ,
140
- "parser" : lambda string : float ( string ) ,
217
+ "parser" : float ,
141
218
},
142
- NumParserType .INT : {"validator" : is_int_str , "parser" : lambda string : int ( string ) },
219
+ NumParserType .INT : {"validator" : is_int_str , "parser" : int },
143
220
}
144
221
"""A mapping for each NumParserType to its corresponding validator and parser for strings of that type's
145
222
specification"""
@@ -669,8 +746,7 @@ def get_commas_outside_enclosing_glyphs(strdata: str) -> List[int]:
669
746
opening_stack = []
670
747
comma_indices_outside_parentheses : List [int ] = []
671
748
672
- for i in range (len (strdata )):
673
- ch = strdata [i ]
749
+ for i , ch in enumerate (strdata ):
674
750
if ch == "," and not opening_stack :
675
751
comma_indices_outside_parentheses .append (i )
676
752
if ch in glyphs .values ():
@@ -720,9 +796,9 @@ def split_string_at_indices(
720
796
721
797
splits = []
722
798
last = 0
723
- for i in range ( len ( indices )) :
724
- splits .append (strdata [last : indices [ i ] ])
725
- last = indices [ i ] + 1
799
+ for index in indices :
800
+ splits .append (strdata [last : index ])
801
+ last = index + 1
726
802
if last >= len (strdata ):
727
803
return splits
728
804
splits .append (strdata [last :])
0 commit comments