xulbux 1.7.0__tar.gz → 1.7.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of xulbux might be problematic. Click here for more details.
- {xulbux-1.7.0/src/xulbux.egg-info → xulbux-1.7.2}/PKG-INFO +36 -34
- {xulbux-1.7.0 → xulbux-1.7.2}/README.md +35 -33
- {xulbux-1.7.0 → xulbux-1.7.2}/pyproject.toml +1 -1
- {xulbux-1.7.0 → xulbux-1.7.2}/src/xulbux/__init__.py +1 -1
- {xulbux-1.7.0 → xulbux-1.7.2}/src/xulbux/_consts_.py +2 -4
- {xulbux-1.7.0 → xulbux-1.7.2}/src/xulbux/xx_code.py +1 -1
- {xulbux-1.7.0 → xulbux-1.7.2}/src/xulbux/xx_color.py +45 -33
- {xulbux-1.7.0 → xulbux-1.7.2}/src/xulbux/xx_console.py +151 -52
- {xulbux-1.7.0 → xulbux-1.7.2}/src/xulbux/xx_format_codes.py +66 -36
- {xulbux-1.7.0 → xulbux-1.7.2}/src/xulbux/xx_regex.py +9 -9
- {xulbux-1.7.0 → xulbux-1.7.2/src/xulbux.egg-info}/PKG-INFO +36 -34
- {xulbux-1.7.0 → xulbux-1.7.2}/tests/test_console.py +1 -1
- {xulbux-1.7.0 → xulbux-1.7.2}/tests/test_format_codes.py +4 -4
- {xulbux-1.7.0 → xulbux-1.7.2}/LICENSE +0 -0
- {xulbux-1.7.0 → xulbux-1.7.2}/setup.cfg +0 -0
- {xulbux-1.7.0 → xulbux-1.7.2}/src/xulbux/_cli_.py +0 -0
- {xulbux-1.7.0 → xulbux-1.7.2}/src/xulbux/xx_data.py +0 -0
- {xulbux-1.7.0 → xulbux-1.7.2}/src/xulbux/xx_env_path.py +0 -0
- {xulbux-1.7.0 → xulbux-1.7.2}/src/xulbux/xx_file.py +0 -0
- {xulbux-1.7.0 → xulbux-1.7.2}/src/xulbux/xx_json.py +0 -0
- {xulbux-1.7.0 → xulbux-1.7.2}/src/xulbux/xx_path.py +0 -0
- {xulbux-1.7.0 → xulbux-1.7.2}/src/xulbux/xx_string.py +0 -0
- {xulbux-1.7.0 → xulbux-1.7.2}/src/xulbux/xx_system.py +0 -0
- {xulbux-1.7.0 → xulbux-1.7.2}/src/xulbux.egg-info/SOURCES.txt +0 -0
- {xulbux-1.7.0 → xulbux-1.7.2}/src/xulbux.egg-info/dependency_links.txt +0 -0
- {xulbux-1.7.0 → xulbux-1.7.2}/src/xulbux.egg-info/entry_points.txt +0 -0
- {xulbux-1.7.0 → xulbux-1.7.2}/src/xulbux.egg-info/requires.txt +0 -0
- {xulbux-1.7.0 → xulbux-1.7.2}/src/xulbux.egg-info/top_level.txt +0 -0
- {xulbux-1.7.0 → xulbux-1.7.2}/tests/test_code.py +0 -0
- {xulbux-1.7.0 → xulbux-1.7.2}/tests/test_color.py +0 -0
- {xulbux-1.7.0 → xulbux-1.7.2}/tests/test_color_types.py +0 -0
- {xulbux-1.7.0 → xulbux-1.7.2}/tests/test_data.py +0 -0
- {xulbux-1.7.0 → xulbux-1.7.2}/tests/test_env_path.py +0 -0
- {xulbux-1.7.0 → xulbux-1.7.2}/tests/test_file.py +0 -0
- {xulbux-1.7.0 → xulbux-1.7.2}/tests/test_json.py +0 -0
- {xulbux-1.7.0 → xulbux-1.7.2}/tests/test_path.py +0 -0
- {xulbux-1.7.0 → xulbux-1.7.2}/tests/test_string.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: xulbux
|
|
3
|
-
Version: 1.7.
|
|
3
|
+
Version: 1.7.2
|
|
4
4
|
Summary: A Python library which includes lots of helpful classes, types and functions aiming to make common programming tasks simpler.
|
|
5
5
|
Author-email: XulbuX <xulbux.real@gmail.com>
|
|
6
6
|
License-Expression: MIT
|
|
@@ -34,14 +34,14 @@ Requires-Dist: flake8>=6.1.0; extra == "dev"
|
|
|
34
34
|
Requires-Dist: flake8-pyproject>=1.2.3; extra == "dev"
|
|
35
35
|
Dynamic: license-file
|
|
36
36
|
|
|
37
|
-
# **$\
|
|
37
|
+
# **$\Huge\textsf{XulbuX}$**
|
|
38
38
|
|
|
39
|
-
|
|
39
|
+
**XulbuX** is library that contains many useful classes, types, and functions,
|
|
40
40
|
ranging from console logging and working with colors to file management and system operations.
|
|
41
41
|
The library is designed to simplify common programming tasks and improve code readability through its collection of tools.
|
|
42
42
|
|
|
43
|
-
For precise information about the library, see the library's [wiki page](https://github.com/XulbuX/PythonLibraryXulbuX/wiki).<br>
|
|
44
|
-
For the libraries latest changes and updates, see the [change log](https://github.com/XulbuX/PythonLibraryXulbuX/blob/main/CHANGELOG.md).
|
|
43
|
+
For precise information about the library, see the library's [**wiki page**](https://github.com/XulbuX/PythonLibraryXulbuX/wiki).<br>
|
|
44
|
+
For the libraries latest changes and updates, see the [**change log**](https://github.com/XulbuX/PythonLibraryXulbuX/blob/main/CHANGELOG.md).
|
|
45
45
|
|
|
46
46
|
<br>
|
|
47
47
|
|
|
@@ -81,20 +81,20 @@ from xulbux import rgba, hsla, hexa
|
|
|
81
81
|
|
|
82
82
|
## Modules
|
|
83
83
|
|
|
84
|
-
| Module
|
|
85
|
-
|
|
|
86
|
-
| [](https://github.com/XulbuX/PythonLibraryXulbuX/wiki/xx_code)
|
|
87
|
-
| [](https://github.com/XulbuX/PythonLibraryXulbuX/wiki/xx_color)
|
|
88
|
-
| [](https://github.com/XulbuX/PythonLibraryXulbuX/wiki/xx_console)
|
|
89
|
-
| [](https://github.com/XulbuX/PythonLibraryXulbuX/wiki/xx_data)
|
|
84
|
+
| Module | Short Description |
|
|
85
|
+
| :--------------------------------------------------------------------------------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------- |
|
|
86
|
+
| [](https://github.com/XulbuX/PythonLibraryXulbuX/wiki/xx_code) | advanced code-string operations (*changing the indent, finding function calls, ...*) |
|
|
87
|
+
| [](https://github.com/XulbuX/PythonLibraryXulbuX/wiki/xx_color) | everything around colors (*converting, blending, searching colors in strings, ...*) |
|
|
88
|
+
| [](https://github.com/XulbuX/PythonLibraryXulbuX/wiki/xx_console) | advanced actions related to the console (*pretty logging, advanced inputs, ...*) |
|
|
89
|
+
| [](https://github.com/XulbuX/PythonLibraryXulbuX/wiki/xx_data) | advanced operations with data structures (*compare, generate path ID's, pretty print/format, ...*) |
|
|
90
90
|
| [](https://github.com/XulbuX/PythonLibraryXulbuX/wiki/xx_env_path) | getting and editing the PATH variable (*get paths, check for paths, add paths, ...*) |
|
|
91
|
-
| [](https://github.com/XulbuX/PythonLibraryXulbuX/wiki/xx_file)
|
|
91
|
+
| [](https://github.com/XulbuX/PythonLibraryXulbuX/wiki/xx_file) | advanced working with files (*create files, rename file-extensions, ...*) |
|
|
92
92
|
| [](https://github.com/XulbuX/PythonLibraryXulbuX/wiki/xx_format_codes) | easy pretty printing with custom format codes (*print, inputs, custom format codes to ANSI, ...*) |
|
|
93
|
-
| [](https://github.com/XulbuX/PythonLibraryXulbuX/wiki/xx_json)
|
|
94
|
-
| [](https://github.com/XulbuX/PythonLibraryXulbuX/wiki/xx_path)
|
|
95
|
-
| 
|
|
96
|
-
| [](https://github.com/XulbuX/PythonLibraryXulbuX/wiki/xx_string)
|
|
97
|
-
| 
|
|
93
|
+
| [](https://github.com/XulbuX/PythonLibraryXulbuX/wiki/xx_json) | advanced working with json files (*read, create, update, ...*) |
|
|
94
|
+
| [](https://github.com/XulbuX/PythonLibraryXulbuX/wiki/xx_path) | advanced path operations (*get paths, smart-extend relative paths, delete paths, ...*) |
|
|
95
|
+
|  | generated regex pattern-templates (*match bracket- and quote pairs, match colors, ...*) |
|
|
96
|
+
| [](https://github.com/XulbuX/PythonLibraryXulbuX/wiki/xx_string) | helpful actions when working with strings. (*normalize, escape, decompose, ...*) |
|
|
97
|
+
|  | advanced system actions (*restart with message, check installed Python libs, ...*) |
|
|
98
98
|
|
|
99
99
|
<br>
|
|
100
100
|
|
|
@@ -111,15 +111,15 @@ def main() -> None:
|
|
|
111
111
|
|
|
112
112
|
# LET THE USER ENTER A HEXA COLOR IN ANY HEXA FORMAT
|
|
113
113
|
input_clr = FormatCodes.input(
|
|
114
|
-
|
|
114
|
+
"\n[b](Enter a HEXA color in any format) [dim](>) "
|
|
115
115
|
)
|
|
116
116
|
|
|
117
117
|
# ANNOUNCE INDEXING THE INPUT COLOR
|
|
118
118
|
Console.log(
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
119
|
+
"INDEX",
|
|
120
|
+
"Indexing the input HEXA color...",
|
|
121
|
+
start="\n",
|
|
122
|
+
title_bg_color=COLOR.blue,
|
|
123
123
|
)
|
|
124
124
|
|
|
125
125
|
try:
|
|
@@ -129,16 +129,16 @@ def main() -> None:
|
|
|
129
129
|
except ValueError:
|
|
130
130
|
# ANNOUNCE THE ERROR AND EXIT THE PROGRAM
|
|
131
131
|
Console.fail(
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
132
|
+
"The input HEXA color is invalid.",
|
|
133
|
+
end="\n\n",
|
|
134
|
+
exit=True,
|
|
135
135
|
)
|
|
136
136
|
|
|
137
137
|
# ANNOUNCE STARTING THE CONVERSION
|
|
138
138
|
Console.log(
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
139
|
+
"CONVERT",
|
|
140
|
+
"Converting the HEXA color into different types...",
|
|
141
|
+
title_bg_color=COLOR.tangerine,
|
|
142
142
|
)
|
|
143
143
|
|
|
144
144
|
# CONVERT THE HEXA COLOR INTO THE TWO OTHER COLOR TYPES
|
|
@@ -147,14 +147,16 @@ def main() -> None:
|
|
|
147
147
|
|
|
148
148
|
# ANNOUNCE THE SUCCESSFUL CONVERSION
|
|
149
149
|
Console.done(
|
|
150
|
-
|
|
151
|
-
|
|
150
|
+
"Successfully converted color into different types.",
|
|
151
|
+
end="\n\n",
|
|
152
152
|
)
|
|
153
153
|
|
|
154
154
|
# PRETTY PRINT THE COLOR IN DIFFERENT TYPES
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
155
|
+
Console.log_box_bordered(
|
|
156
|
+
f"[b](HEXA:) [i|white]({hexa_color})",
|
|
157
|
+
f"[b](RGBA:) [i|white]({rgba_color})",
|
|
158
|
+
f"[b](HSLA:) [i|white]({hsla_color})",
|
|
159
|
+
)
|
|
158
160
|
|
|
159
161
|
|
|
160
162
|
if __name__ == "__main__":
|
|
@@ -165,4 +167,4 @@ if __name__ == "__main__":
|
|
|
165
167
|
<br>
|
|
166
168
|
|
|
167
169
|
--------------------------------------------------------------
|
|
168
|
-
[View this library on PyPI](https://pypi.org/project/XulbuX/)
|
|
170
|
+
[View this library on **PyPI**](https://pypi.org/project/XulbuX/)
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
# **$\
|
|
1
|
+
# **$\Huge\textsf{XulbuX}$**
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
**XulbuX** is library that contains many useful classes, types, and functions,
|
|
4
4
|
ranging from console logging and working with colors to file management and system operations.
|
|
5
5
|
The library is designed to simplify common programming tasks and improve code readability through its collection of tools.
|
|
6
6
|
|
|
7
|
-
For precise information about the library, see the library's [wiki page](https://github.com/XulbuX/PythonLibraryXulbuX/wiki).<br>
|
|
8
|
-
For the libraries latest changes and updates, see the [change log](https://github.com/XulbuX/PythonLibraryXulbuX/blob/main/CHANGELOG.md).
|
|
7
|
+
For precise information about the library, see the library's [**wiki page**](https://github.com/XulbuX/PythonLibraryXulbuX/wiki).<br>
|
|
8
|
+
For the libraries latest changes and updates, see the [**change log**](https://github.com/XulbuX/PythonLibraryXulbuX/blob/main/CHANGELOG.md).
|
|
9
9
|
|
|
10
10
|
<br>
|
|
11
11
|
|
|
@@ -45,20 +45,20 @@ from xulbux import rgba, hsla, hexa
|
|
|
45
45
|
|
|
46
46
|
## Modules
|
|
47
47
|
|
|
48
|
-
| Module
|
|
49
|
-
|
|
|
50
|
-
| [](https://github.com/XulbuX/PythonLibraryXulbuX/wiki/xx_code)
|
|
51
|
-
| [](https://github.com/XulbuX/PythonLibraryXulbuX/wiki/xx_color)
|
|
52
|
-
| [](https://github.com/XulbuX/PythonLibraryXulbuX/wiki/xx_console)
|
|
53
|
-
| [](https://github.com/XulbuX/PythonLibraryXulbuX/wiki/xx_data)
|
|
48
|
+
| Module | Short Description |
|
|
49
|
+
| :--------------------------------------------------------------------------------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------- |
|
|
50
|
+
| [](https://github.com/XulbuX/PythonLibraryXulbuX/wiki/xx_code) | advanced code-string operations (*changing the indent, finding function calls, ...*) |
|
|
51
|
+
| [](https://github.com/XulbuX/PythonLibraryXulbuX/wiki/xx_color) | everything around colors (*converting, blending, searching colors in strings, ...*) |
|
|
52
|
+
| [](https://github.com/XulbuX/PythonLibraryXulbuX/wiki/xx_console) | advanced actions related to the console (*pretty logging, advanced inputs, ...*) |
|
|
53
|
+
| [](https://github.com/XulbuX/PythonLibraryXulbuX/wiki/xx_data) | advanced operations with data structures (*compare, generate path ID's, pretty print/format, ...*) |
|
|
54
54
|
| [](https://github.com/XulbuX/PythonLibraryXulbuX/wiki/xx_env_path) | getting and editing the PATH variable (*get paths, check for paths, add paths, ...*) |
|
|
55
|
-
| [](https://github.com/XulbuX/PythonLibraryXulbuX/wiki/xx_file)
|
|
55
|
+
| [](https://github.com/XulbuX/PythonLibraryXulbuX/wiki/xx_file) | advanced working with files (*create files, rename file-extensions, ...*) |
|
|
56
56
|
| [](https://github.com/XulbuX/PythonLibraryXulbuX/wiki/xx_format_codes) | easy pretty printing with custom format codes (*print, inputs, custom format codes to ANSI, ...*) |
|
|
57
|
-
| [](https://github.com/XulbuX/PythonLibraryXulbuX/wiki/xx_json)
|
|
58
|
-
| [](https://github.com/XulbuX/PythonLibraryXulbuX/wiki/xx_path)
|
|
59
|
-
| 
|
|
60
|
-
| [](https://github.com/XulbuX/PythonLibraryXulbuX/wiki/xx_string)
|
|
61
|
-
| 
|
|
57
|
+
| [](https://github.com/XulbuX/PythonLibraryXulbuX/wiki/xx_json) | advanced working with json files (*read, create, update, ...*) |
|
|
58
|
+
| [](https://github.com/XulbuX/PythonLibraryXulbuX/wiki/xx_path) | advanced path operations (*get paths, smart-extend relative paths, delete paths, ...*) |
|
|
59
|
+
|  | generated regex pattern-templates (*match bracket- and quote pairs, match colors, ...*) |
|
|
60
|
+
| [](https://github.com/XulbuX/PythonLibraryXulbuX/wiki/xx_string) | helpful actions when working with strings. (*normalize, escape, decompose, ...*) |
|
|
61
|
+
|  | advanced system actions (*restart with message, check installed Python libs, ...*) |
|
|
62
62
|
|
|
63
63
|
<br>
|
|
64
64
|
|
|
@@ -75,15 +75,15 @@ def main() -> None:
|
|
|
75
75
|
|
|
76
76
|
# LET THE USER ENTER A HEXA COLOR IN ANY HEXA FORMAT
|
|
77
77
|
input_clr = FormatCodes.input(
|
|
78
|
-
|
|
78
|
+
"\n[b](Enter a HEXA color in any format) [dim](>) "
|
|
79
79
|
)
|
|
80
80
|
|
|
81
81
|
# ANNOUNCE INDEXING THE INPUT COLOR
|
|
82
82
|
Console.log(
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
83
|
+
"INDEX",
|
|
84
|
+
"Indexing the input HEXA color...",
|
|
85
|
+
start="\n",
|
|
86
|
+
title_bg_color=COLOR.blue,
|
|
87
87
|
)
|
|
88
88
|
|
|
89
89
|
try:
|
|
@@ -93,16 +93,16 @@ def main() -> None:
|
|
|
93
93
|
except ValueError:
|
|
94
94
|
# ANNOUNCE THE ERROR AND EXIT THE PROGRAM
|
|
95
95
|
Console.fail(
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
96
|
+
"The input HEXA color is invalid.",
|
|
97
|
+
end="\n\n",
|
|
98
|
+
exit=True,
|
|
99
99
|
)
|
|
100
100
|
|
|
101
101
|
# ANNOUNCE STARTING THE CONVERSION
|
|
102
102
|
Console.log(
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
103
|
+
"CONVERT",
|
|
104
|
+
"Converting the HEXA color into different types...",
|
|
105
|
+
title_bg_color=COLOR.tangerine,
|
|
106
106
|
)
|
|
107
107
|
|
|
108
108
|
# CONVERT THE HEXA COLOR INTO THE TWO OTHER COLOR TYPES
|
|
@@ -111,14 +111,16 @@ def main() -> None:
|
|
|
111
111
|
|
|
112
112
|
# ANNOUNCE THE SUCCESSFUL CONVERSION
|
|
113
113
|
Console.done(
|
|
114
|
-
|
|
115
|
-
|
|
114
|
+
"Successfully converted color into different types.",
|
|
115
|
+
end="\n\n",
|
|
116
116
|
)
|
|
117
117
|
|
|
118
118
|
# PRETTY PRINT THE COLOR IN DIFFERENT TYPES
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
119
|
+
Console.log_box_bordered(
|
|
120
|
+
f"[b](HEXA:) [i|white]({hexa_color})",
|
|
121
|
+
f"[b](RGBA:) [i|white]({rgba_color})",
|
|
122
|
+
f"[b](HSLA:) [i|white]({hsla_color})",
|
|
123
|
+
)
|
|
122
124
|
|
|
123
125
|
|
|
124
126
|
if __name__ == "__main__":
|
|
@@ -129,4 +131,4 @@ if __name__ == "__main__":
|
|
|
129
131
|
<br>
|
|
130
132
|
|
|
131
133
|
--------------------------------------------------------------
|
|
132
|
-
[View this library on PyPI](https://pypi.org/project/XulbuX/)
|
|
134
|
+
[View this library on **PyPI**](https://pypi.org/project/XulbuX/)
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "xulbux"
|
|
7
|
-
version = "1.7.
|
|
7
|
+
version = "1.7.2"
|
|
8
8
|
authors = [{ name = "XulbuX", email = "xulbux.real@gmail.com" }]
|
|
9
9
|
description = "A Python library which includes lots of helpful classes, types and functions aiming to make common programming tasks simpler."
|
|
10
10
|
readme = "README.md"
|
|
@@ -94,8 +94,6 @@ class ANSI:
|
|
|
94
94
|
"""The separator between ANSI escape sequence parts."""
|
|
95
95
|
END = end = "m"
|
|
96
96
|
"""The end of an ANSI escape sequence."""
|
|
97
|
-
default_color_modifiers: dict[str, str] = {"lighten": "+l", "darken": "-d"}
|
|
98
|
-
"""Characters to modify the lightness of the default color with."""
|
|
99
97
|
|
|
100
98
|
@classmethod
|
|
101
99
|
def seq(cls, parts: int = 1) -> FormattableString:
|
|
@@ -107,7 +105,7 @@ class ANSI:
|
|
|
107
105
|
seq_bg_color: FormattableString = CHAR + START + "48" + SEP + "2" + SEP + "{}" + SEP + "{}" + SEP + "{}" + END
|
|
108
106
|
"""The ANSI escape sequence for setting the background RGB color."""
|
|
109
107
|
|
|
110
|
-
color_map:
|
|
108
|
+
color_map: tuple[str, ...] = (
|
|
111
109
|
########### DEFAULT CONSOLE COLOR NAMES ############
|
|
112
110
|
"black",
|
|
113
111
|
"red",
|
|
@@ -117,7 +115,7 @@ class ANSI:
|
|
|
117
115
|
"magenta",
|
|
118
116
|
"cyan",
|
|
119
117
|
"white",
|
|
120
|
-
|
|
118
|
+
)
|
|
121
119
|
"""The console default color names."""
|
|
122
120
|
|
|
123
121
|
codes_map: dict[str | tuple[str, ...], int] = {
|
|
@@ -53,7 +53,7 @@ class Code:
|
|
|
53
53
|
return list(Data.remove_duplicates(funcs + nested_func_calls))
|
|
54
54
|
|
|
55
55
|
@staticmethod
|
|
56
|
-
def is_js(code: str, funcs: list = ["__", "$t", "$lang"]) -> bool:
|
|
56
|
+
def is_js(code: str, funcs: list[str] = ["__", "$t", "$lang"]) -> bool:
|
|
57
57
|
"""Will check if the code is very likely to be JavaScript."""
|
|
58
58
|
if not code or len(code.strip()) < 3:
|
|
59
59
|
return False
|
|
@@ -33,7 +33,7 @@ The `Color` class, which contains all sorts of different color-related methods:
|
|
|
33
33
|
|
|
34
34
|
from .xx_regex import Regex
|
|
35
35
|
|
|
36
|
-
from typing import Annotated, TypeAlias, Iterator, Optional, Literal, Union
|
|
36
|
+
from typing import Annotated, TypeAlias, Iterator, Optional, Literal, Union, Any, cast
|
|
37
37
|
import re as _re
|
|
38
38
|
|
|
39
39
|
|
|
@@ -42,6 +42,10 @@ Int_0_255 = Annotated[int, "An integer value between 0 and 255, inclusive."]
|
|
|
42
42
|
Int_0_360 = Annotated[int, "An integer value between 0 and 360, inclusive."]
|
|
43
43
|
Float_0_1 = Annotated[float, "A float value between 0.0 and 1.0, inclusive."]
|
|
44
44
|
|
|
45
|
+
AnyRgba: TypeAlias = Any
|
|
46
|
+
AnyHsla: TypeAlias = Any
|
|
47
|
+
AnyHexa: TypeAlias = Any
|
|
48
|
+
|
|
45
49
|
Rgba: TypeAlias = Union[
|
|
46
50
|
tuple[Int_0_255, Int_0_255, Int_0_255],
|
|
47
51
|
tuple[Int_0_255, Int_0_255, Int_0_255, Float_0_1],
|
|
@@ -178,7 +182,7 @@ class rgba:
|
|
|
178
182
|
self.a = 1 - self.a
|
|
179
183
|
return rgba(self.r, self.g, self.b, self.a, _validate=False)
|
|
180
184
|
|
|
181
|
-
def grayscale(self, method:
|
|
185
|
+
def grayscale(self, method: Literal["wcag2", "wcag3", "simple", "bt601"] = "wcag2") -> "rgba":
|
|
182
186
|
"""Converts the color to grayscale using the luminance formula.\n
|
|
183
187
|
------------------------------------------------------------------
|
|
184
188
|
The `method` is the luminance calculation method to use:
|
|
@@ -387,7 +391,7 @@ class hsla:
|
|
|
387
391
|
self.a = 1 - self.a
|
|
388
392
|
return hsla(self.h, self.s, self.l, self.a, _validate=False)
|
|
389
393
|
|
|
390
|
-
def grayscale(self, method:
|
|
394
|
+
def grayscale(self, method: Literal["wcag2", "wcag3", "simple", "bt601"] = "wcag2") -> "hsla":
|
|
391
395
|
"""Converts the color to grayscale using the luminance formula.\n
|
|
392
396
|
------------------------------------------------------------------
|
|
393
397
|
The `method` is the luminance calculation method to use:
|
|
@@ -496,7 +500,7 @@ class hexa:
|
|
|
496
500
|
self.b: int
|
|
497
501
|
self.a: Optional[float]
|
|
498
502
|
if all(x is not None for x in (_r, _g, _b)):
|
|
499
|
-
self.r, self.g, self.b, self.a = _r, _g, _b, _a
|
|
503
|
+
self.r, self.g, self.b, self.a = cast(int, _r), cast(int, _g), cast(int, _b), _a
|
|
500
504
|
return
|
|
501
505
|
if isinstance(color, hexa):
|
|
502
506
|
raise ValueError("Color is already a hexa() color")
|
|
@@ -628,7 +632,7 @@ class hexa:
|
|
|
628
632
|
self.a = 1 - self.a
|
|
629
633
|
return hexa("", self.r, self.g, self.b, self.a)
|
|
630
634
|
|
|
631
|
-
def grayscale(self, method:
|
|
635
|
+
def grayscale(self, method: Literal["wcag2", "wcag3", "simple", "bt601"] = "wcag2") -> "hexa":
|
|
632
636
|
"""Converts the color to grayscale using the luminance formula.\n
|
|
633
637
|
------------------------------------------------------------------
|
|
634
638
|
The `method` is the luminance calculation method to use:
|
|
@@ -677,7 +681,7 @@ class hexa:
|
|
|
677
681
|
class Color:
|
|
678
682
|
|
|
679
683
|
@staticmethod
|
|
680
|
-
def is_valid_rgba(color:
|
|
684
|
+
def is_valid_rgba(color: AnyRgba, allow_alpha: bool = True) -> bool:
|
|
681
685
|
try:
|
|
682
686
|
if isinstance(color, rgba):
|
|
683
687
|
return True
|
|
@@ -685,7 +689,7 @@ class Color:
|
|
|
685
689
|
if allow_alpha and Color.has_alpha(color):
|
|
686
690
|
return (
|
|
687
691
|
0 <= color[0] <= 255 and 0 <= color[1] <= 255 and 0 <= color[2] <= 255
|
|
688
|
-
and (0 <= color[3] <= 1 or color[3] is None)
|
|
692
|
+
and (0 <= color[3] <= 1 or color[3] is None)
|
|
689
693
|
)
|
|
690
694
|
elif len(color) == 3:
|
|
691
695
|
return 0 <= color[0] <= 255 and 0 <= color[1] <= 255 and 0 <= color[2] <= 255
|
|
@@ -708,7 +712,7 @@ class Color:
|
|
|
708
712
|
return False
|
|
709
713
|
|
|
710
714
|
@staticmethod
|
|
711
|
-
def is_valid_hsla(color:
|
|
715
|
+
def is_valid_hsla(color: AnyHsla, allow_alpha: bool = True) -> bool:
|
|
712
716
|
try:
|
|
713
717
|
if isinstance(color, hsla):
|
|
714
718
|
return True
|
|
@@ -716,7 +720,7 @@ class Color:
|
|
|
716
720
|
if allow_alpha and Color.has_alpha(color):
|
|
717
721
|
return (
|
|
718
722
|
0 <= color[0] <= 360 and 0 <= color[1] <= 100 and 0 <= color[2] <= 100
|
|
719
|
-
and (0 <= color[3] <= 1 or color[3] is None)
|
|
723
|
+
and (0 <= color[3] <= 1 or color[3] is None)
|
|
720
724
|
)
|
|
721
725
|
elif len(color) == 3:
|
|
722
726
|
return 0 <= color[0] <= 360 and 0 <= color[1] <= 100 and 0 <= color[2] <= 100
|
|
@@ -734,12 +738,13 @@ class Color:
|
|
|
734
738
|
return False
|
|
735
739
|
elif isinstance(color, str):
|
|
736
740
|
return bool(_re.fullmatch(Regex.hsla_str(allow_alpha=allow_alpha), color))
|
|
741
|
+
return False
|
|
737
742
|
except Exception:
|
|
738
743
|
return False
|
|
739
744
|
|
|
740
745
|
@staticmethod
|
|
741
746
|
def is_valid_hexa(
|
|
742
|
-
color:
|
|
747
|
+
color: AnyHexa,
|
|
743
748
|
allow_alpha: bool = True,
|
|
744
749
|
get_prefix: bool = False,
|
|
745
750
|
) -> bool | tuple[bool, Optional[Literal['#', '0x']]]:
|
|
@@ -754,14 +759,15 @@ class Color:
|
|
|
754
759
|
(color[2:], "0x") if color.startswith("0x") else (color, None))
|
|
755
760
|
return ((bool(_re.fullmatch(Regex.hexa_str(allow_alpha=allow_alpha), color)),
|
|
756
761
|
prefix) if get_prefix else bool(_re.fullmatch(Regex.hexa_str(allow_alpha=allow_alpha), color)))
|
|
762
|
+
return False
|
|
757
763
|
except Exception:
|
|
758
764
|
return (False, None) if get_prefix else False
|
|
759
765
|
|
|
760
766
|
@staticmethod
|
|
761
|
-
def is_valid(color:
|
|
767
|
+
def is_valid(color: AnyRgba | AnyHsla | AnyHexa, allow_alpha: bool = True) -> bool:
|
|
762
768
|
return bool(
|
|
763
|
-
Color.is_valid_rgba(color, allow_alpha) or Color.is_valid_hsla(color, allow_alpha)
|
|
764
|
-
or Color.is_valid_hexa(color, allow_alpha)
|
|
769
|
+
Color.is_valid_rgba(color, allow_alpha) or Color.is_valid_hsla(color, allow_alpha)
|
|
770
|
+
or Color.is_valid_hexa(color, allow_alpha)
|
|
765
771
|
)
|
|
766
772
|
|
|
767
773
|
@staticmethod
|
|
@@ -772,7 +778,7 @@ class Color:
|
|
|
772
778
|
Returns `True` if the color has an alpha channel and `False` otherwise."""
|
|
773
779
|
if isinstance(color, (rgba, hsla, hexa)):
|
|
774
780
|
return color.has_alpha()
|
|
775
|
-
if Color.is_valid_hexa(color):
|
|
781
|
+
if Color.is_valid_hexa(color):
|
|
776
782
|
if isinstance(color, str):
|
|
777
783
|
if color.startswith("#"):
|
|
778
784
|
color = color[1:]
|
|
@@ -791,11 +797,11 @@ class Color:
|
|
|
791
797
|
"""Will try to convert any color type to a color of type RGBA."""
|
|
792
798
|
if isinstance(color, (hsla, hexa)):
|
|
793
799
|
return color.to_rgba()
|
|
794
|
-
elif Color.is_valid_hsla(color):
|
|
800
|
+
elif Color.is_valid_hsla(color):
|
|
795
801
|
return hsla(*color, _validate=False).to_rgba() # type: ignore[not-iterable]
|
|
796
|
-
elif Color.is_valid_hexa(color):
|
|
797
|
-
return hexa(color).to_rgba()
|
|
798
|
-
elif Color.is_valid_rgba(color):
|
|
802
|
+
elif Color.is_valid_hexa(color):
|
|
803
|
+
return hexa(cast(str | int, color)).to_rgba()
|
|
804
|
+
elif Color.is_valid_rgba(color):
|
|
799
805
|
return color if isinstance(color, rgba) else (rgba(*color, _validate=False)) # type: ignore[not-iterable]
|
|
800
806
|
raise ValueError(f"Invalid color format '{color}'")
|
|
801
807
|
|
|
@@ -804,11 +810,11 @@ class Color:
|
|
|
804
810
|
"""Will try to convert any color type to a color of type HSLA."""
|
|
805
811
|
if isinstance(color, (rgba, hexa)):
|
|
806
812
|
return color.to_hsla()
|
|
807
|
-
elif Color.is_valid_rgba(color):
|
|
813
|
+
elif Color.is_valid_rgba(color):
|
|
808
814
|
return rgba(*color, _validate=False).to_hsla() # type: ignore[not-iterable]
|
|
809
|
-
elif Color.is_valid_hexa(color):
|
|
810
|
-
return hexa(color).to_hsla()
|
|
811
|
-
elif Color.is_valid_hsla(color):
|
|
815
|
+
elif Color.is_valid_hexa(color):
|
|
816
|
+
return hexa(cast(str | int, color)).to_hsla()
|
|
817
|
+
elif Color.is_valid_hsla(color):
|
|
812
818
|
return color if isinstance(color, hsla) else (hsla(*color, _validate=False)) # type: ignore[not-iterable]
|
|
813
819
|
raise ValueError(f"Invalid color format '{color}'")
|
|
814
820
|
|
|
@@ -817,12 +823,12 @@ class Color:
|
|
|
817
823
|
"""Will try to convert any color type to a color of type HEXA."""
|
|
818
824
|
if isinstance(color, (rgba, hsla)):
|
|
819
825
|
return color.to_hexa()
|
|
820
|
-
elif Color.is_valid_rgba(color):
|
|
826
|
+
elif Color.is_valid_rgba(color):
|
|
821
827
|
return rgba(*color, _validate=False).to_hexa() # type: ignore[not-iterable]
|
|
822
|
-
elif Color.is_valid_hsla(color):
|
|
828
|
+
elif Color.is_valid_hsla(color):
|
|
823
829
|
return hsla(*color, _validate=False).to_hexa() # type: ignore[not-iterable]
|
|
824
|
-
elif Color.is_valid_hexa(color):
|
|
825
|
-
return color if isinstance(color, hexa) else hexa(color)
|
|
830
|
+
elif Color.is_valid_hexa(color):
|
|
831
|
+
return color if isinstance(color, hexa) else hexa(cast(str | int, color))
|
|
826
832
|
raise ValueError(f"Invalid color format '{color}'")
|
|
827
833
|
|
|
828
834
|
@staticmethod
|
|
@@ -919,7 +925,13 @@ class Color:
|
|
|
919
925
|
raise ValueError(f"Invalid HEX integer '0x{hex_str}': expected in range [0x000000, 0xFFFFFF]")
|
|
920
926
|
|
|
921
927
|
@staticmethod
|
|
922
|
-
def luminance(
|
|
928
|
+
def luminance(
|
|
929
|
+
r: int,
|
|
930
|
+
g: int,
|
|
931
|
+
b: int,
|
|
932
|
+
output_type: Optional[type] = None,
|
|
933
|
+
method: Literal["wcag2", "wcag3", "simple", "bt601"] = "wcag2",
|
|
934
|
+
) -> int | float:
|
|
923
935
|
"""Calculates the relative luminance of a color according to various standards.\n
|
|
924
936
|
----------------------------------------------------------------------------------
|
|
925
937
|
The `output_type` controls the range of the returned luminance value:
|
|
@@ -963,7 +975,7 @@ class Color:
|
|
|
963
975
|
|
|
964
976
|
@staticmethod
|
|
965
977
|
def text_color_for_on_bg(text_bg_color: Rgba | Hexa) -> rgba | hexa | int:
|
|
966
|
-
was_hexa, was_int = Color.is_valid_hexa(text_bg_color), isinstance(text_bg_color, int)
|
|
978
|
+
was_hexa, was_int = Color.is_valid_hexa(text_bg_color), isinstance(text_bg_color, int)
|
|
967
979
|
text_bg_color = Color.to_rgba(text_bg_color)
|
|
968
980
|
brightness = 0.2126 * text_bg_color[0] + 0.7152 * text_bg_color[1] + 0.0722 * text_bg_color[2]
|
|
969
981
|
return (((0xFFFFFF if was_int else hexa("", 255, 255, 255)) if was_hexa else rgba(255, 255, 255, _validate=False))
|
|
@@ -978,22 +990,22 @@ class Color:
|
|
|
978
990
|
- lightness_change (float): float between -1.0 (darken by `100%`) and 1.0 (lighten by `100%`)\n
|
|
979
991
|
-----------------------------------------------------------------------------------------------------
|
|
980
992
|
returns (rgba|hexa): the adjusted color in the format of the input color"""
|
|
981
|
-
was_hexa = Color.is_valid_hexa(color)
|
|
982
|
-
_color: hsla = Color.to_hsla(color)
|
|
993
|
+
was_hexa = Color.is_valid_hexa(color)
|
|
994
|
+
_color: hsla = Color.to_hsla(color)
|
|
983
995
|
h, s, l, a = (int(_color[0]), int(_color[1]), int(_color[2]), _color[3] if Color.has_alpha(_color) else None)
|
|
984
996
|
l = int(max(0, min(100, l + lightness_change * 100)))
|
|
985
997
|
return hsla(h, s, l, a, _validate=False).to_hexa() if was_hexa else hsla(h, s, l, a, _validate=False).to_rgba()
|
|
986
998
|
|
|
987
999
|
@staticmethod
|
|
988
|
-
def adjust_saturation(color: Rgba |
|
|
1000
|
+
def adjust_saturation(color: Rgba | Hexa, saturation_change: float) -> rgba | hexa:
|
|
989
1001
|
"""In- or decrease the saturation of the input color.\n
|
|
990
1002
|
-----------------------------------------------------------------------------------------------------------
|
|
991
1003
|
- color (rgba|hexa): HEX or RGBA color
|
|
992
1004
|
- saturation_change (float): float between -1.0 (saturate by `100%`) and 1.0 (desaturate by `100%`)\n
|
|
993
1005
|
-----------------------------------------------------------------------------------------------------------
|
|
994
1006
|
returns (rgba|hexa): the adjusted color in the format of the input color"""
|
|
995
|
-
was_hexa = Color.is_valid_hexa(color)
|
|
996
|
-
_color: hsla = Color.to_hsla(color)
|
|
1007
|
+
was_hexa = Color.is_valid_hexa(color)
|
|
1008
|
+
_color: hsla = Color.to_hsla(color)
|
|
997
1009
|
h, s, l, a = (int(_color[0]), int(_color[1]), int(_color[2]), _color[3] if Color.has_alpha(_color) else None)
|
|
998
1010
|
s = int(max(0, min(100, s + saturation_change * 100)))
|
|
999
1011
|
return hsla(h, s, l, a, _validate=False).to_hexa() if was_hexa else hsla(h, s, l, a, _validate=False).to_rgba()
|