quantumgui 1.0.0__py3-none-any.whl
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.
- quantumgui/__init__.py +9 -0
- quantumgui/__main__.py +6 -0
- quantumgui/cli.py +341 -0
- quantumgui/core/__init__.py +39 -0
- quantumgui/core/animation.py +221 -0
- quantumgui/core/audit.py +323 -0
- quantumgui/core/context.py +54 -0
- quantumgui/core/data_source.py +426 -0
- quantumgui/core/draw.py +428 -0
- quantumgui/core/enterprise.py +402 -0
- quantumgui/core/enterprise_app.py +419 -0
- quantumgui/core/events.py +34 -0
- quantumgui/core/geometry.py +108 -0
- quantumgui/core/native_dialogs.py +276 -0
- quantumgui/core/pdf_report.py +390 -0
- quantumgui/core/rules.py +284 -0
- quantumgui/core/state.py +42 -0
- quantumgui/core/task_runner.py +181 -0
- quantumgui/core/widget.py +498 -0
- quantumgui/core/window.py +920 -0
- quantumgui/core/xlsx_export.py +145 -0
- quantumgui/py.typed +0 -0
- quantumgui/theme/__init__.py +32 -0
- quantumgui/theme/hct.py +191 -0
- quantumgui/theme/theme.py +93 -0
- quantumgui/theme/tokens.py +423 -0
- quantumgui/ui.py +2142 -0
- quantumgui/widgets/__init__.py +168 -0
- quantumgui/widgets/advanced.py +499 -0
- quantumgui/widgets/button.py +224 -0
- quantumgui/widgets/charts.py +625 -0
- quantumgui/widgets/charts_bi.py +528 -0
- quantumgui/widgets/charts_extra.py +430 -0
- quantumgui/widgets/charts_pro.py +568 -0
- quantumgui/widgets/checkbox.py +199 -0
- quantumgui/widgets/chip.py +219 -0
- quantumgui/widgets/containers.py +321 -0
- quantumgui/widgets/data_controls.py +487 -0
- quantumgui/widgets/data_grid.py +956 -0
- quantumgui/widgets/data_table.py +551 -0
- quantumgui/widgets/date_picker.py +407 -0
- quantumgui/widgets/display_media.py +372 -0
- quantumgui/widgets/display_pro.py +407 -0
- quantumgui/widgets/drawer.py +252 -0
- quantumgui/widgets/enterprise_controls.py +934 -0
- quantumgui/widgets/fab.py +261 -0
- quantumgui/widgets/forms_pro.py +703 -0
- quantumgui/widgets/forms_pro_media.py +587 -0
- quantumgui/widgets/inputs.py +870 -0
- quantumgui/widgets/layout_pro.py +162 -0
- quantumgui/widgets/list_view.py +307 -0
- quantumgui/widgets/masked_input.py +131 -0
- quantumgui/widgets/master_detail.py +155 -0
- quantumgui/widgets/navigation.py +382 -0
- quantumgui/widgets/overlays.py +355 -0
- quantumgui/widgets/productivity.py +500 -0
- quantumgui/widgets/productivity_boards.py +544 -0
- quantumgui/widgets/radio.py +209 -0
- quantumgui/widgets/scroll_view.py +129 -0
- quantumgui/widgets/select.py +373 -0
- quantumgui/widgets/sheets.py +249 -0
- quantumgui/widgets/slider.py +209 -0
- quantumgui/widgets/switch.py +196 -0
- quantumgui/widgets/tabs.py +175 -0
- quantumgui/widgets/text.py +101 -0
- quantumgui/widgets/text_field.py +490 -0
- quantumgui/widgets/time_picker.py +499 -0
- quantumgui-1.0.0.dist-info/METADATA +137 -0
- quantumgui-1.0.0.dist-info/RECORD +73 -0
- quantumgui-1.0.0.dist-info/WHEEL +5 -0
- quantumgui-1.0.0.dist-info/entry_points.txt +2 -0
- quantumgui-1.0.0.dist-info/licenses/LICENSE +32 -0
- quantumgui-1.0.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
"""Zero-dependency native Excel `.xlsx` (OOXML) exporter using Python stdlib `zipfile`.
|
|
2
|
+
|
|
3
|
+
Produces genuine `.xlsx` workbooks readable by Microsoft Excel, LibreOffice Calc, and Google Sheets
|
|
4
|
+
with bold headers, true numeric & boolean cell types, auto-sized columns, and optional totals row.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import zipfile
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
from typing import Any, Sequence
|
|
12
|
+
from xml.sax.saxutils import escape
|
|
13
|
+
|
|
14
|
+
_CONTENT_TYPES_XML = """<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
|
15
|
+
<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
|
|
16
|
+
<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>
|
|
17
|
+
<Default Extension="xml" ContentType="application/xml"/>
|
|
18
|
+
<Override PartName="/xl/workbook.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"/>
|
|
19
|
+
<Override PartName="/xl/worksheets/sheet1.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/>
|
|
20
|
+
<Override PartName="/xl/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml"/>
|
|
21
|
+
</Types>"""
|
|
22
|
+
|
|
23
|
+
_RELS_XML = """<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
|
24
|
+
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
|
|
25
|
+
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/>
|
|
26
|
+
</Relationships>"""
|
|
27
|
+
|
|
28
|
+
_WB_RELS_XML = """<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
|
29
|
+
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
|
|
30
|
+
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet1.xml"/>
|
|
31
|
+
<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/>
|
|
32
|
+
</Relationships>"""
|
|
33
|
+
|
|
34
|
+
_STYLES_XML = """<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
|
35
|
+
<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
|
|
36
|
+
<fonts count="2">
|
|
37
|
+
<font><sz val="11"/><name val="Calibri"/></font>
|
|
38
|
+
<font><b/><sz val="11"/><name val="Calibri"/></font>
|
|
39
|
+
</fonts>
|
|
40
|
+
<fills count="3">
|
|
41
|
+
<fill><patternFill patternType="none"/></fill>
|
|
42
|
+
<fill><patternFill patternType="gray125"/></fill>
|
|
43
|
+
<fill><patternFill patternType="solid"><fgColor rgb="FFEADDFF"/></patternFill></fill>
|
|
44
|
+
</fills>
|
|
45
|
+
<borders count="1"><border><left/><right/><top/><bottom/><diagonal/></border></borders>
|
|
46
|
+
<cellStyleXfs count="1"><xf numFmtId="0" fontId="0" fillId="0" borderId="0"/></cellStyleXfs>
|
|
47
|
+
<cellXfs count="2">
|
|
48
|
+
<xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0"/>
|
|
49
|
+
<xf numFmtId="0" fontId="1" fillId="2" borderId="0" xfId="0" applyFont="1" applyFill="1"/>
|
|
50
|
+
</cellXfs>
|
|
51
|
+
</styleSheet>"""
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def _col_letter(idx: int) -> str:
|
|
55
|
+
"""Convert 0-based column index to Excel column letter (0->A, 25->Z, 26->AA)."""
|
|
56
|
+
out = ""
|
|
57
|
+
n = idx + 1
|
|
58
|
+
while n > 0:
|
|
59
|
+
n, rem = divmod(n - 1, 26)
|
|
60
|
+
out = chr(65 + rem) + out
|
|
61
|
+
return out
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def _cell_xml(row_num: int, col_idx: int, value: Any, *, style: int = 0) -> str:
|
|
65
|
+
ref = f"{_col_letter(col_idx)}{row_num}"
|
|
66
|
+
s_attr = f' s="{style}"' if style else ""
|
|
67
|
+
if value is None or value == "":
|
|
68
|
+
return f'<c r="{ref}"{s_attr}/>'
|
|
69
|
+
if isinstance(value, bool):
|
|
70
|
+
return f'<c r="{ref}" t="b"{s_attr}><v>{1 if value else 0}</v></c>'
|
|
71
|
+
if isinstance(value, (int, float)):
|
|
72
|
+
return f'<c r="{ref}"{s_attr}><v>{value}</v></c>'
|
|
73
|
+
txt = escape(str(value))
|
|
74
|
+
return f'<c r="{ref}" t="inlineStr"{s_attr}><is><t>{txt}</t></is></c>'
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def export_xlsx(
|
|
78
|
+
path: str | Path,
|
|
79
|
+
headers: Sequence[str],
|
|
80
|
+
rows: Sequence[Sequence[Any]],
|
|
81
|
+
*,
|
|
82
|
+
sheet_name: str = "Datos",
|
|
83
|
+
totals: Sequence[Any] | None = None,
|
|
84
|
+
) -> Path:
|
|
85
|
+
"""Export tabular headers and rows directly into a native Excel `.xlsx` workbook."""
|
|
86
|
+
dest = Path(path)
|
|
87
|
+
dest.parent.mkdir(parents=True, exist_ok=True)
|
|
88
|
+
|
|
89
|
+
safe_sheet = escape((sheet_name or "Datos")[:31].replace("/", "-").replace("\\", "-"))
|
|
90
|
+
wb_xml = (
|
|
91
|
+
'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'
|
|
92
|
+
'<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" '
|
|
93
|
+
'xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">'
|
|
94
|
+
f'<sheets><sheet name="{safe_sheet}" sheetId="1" r:id="rId1"/></sheets>'
|
|
95
|
+
"</workbook>"
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
n_cols = max(len(headers), max((len(r) for r in rows), default=0))
|
|
99
|
+
col_widths = [max(10, len(str(headers[i])) + 4) if i < len(headers) else 12 for i in range(n_cols)]
|
|
100
|
+
for r in rows[:200]:
|
|
101
|
+
for ci, v in enumerate(r):
|
|
102
|
+
if ci < n_cols and v is not None:
|
|
103
|
+
col_widths[ci] = min(48, max(col_widths[ci], len(str(v)) + 3))
|
|
104
|
+
|
|
105
|
+
cols_xml = ""
|
|
106
|
+
if n_cols > 0:
|
|
107
|
+
cols_parts = [f'<col min="{i + 1}" max="{i + 1}" width="{col_widths[i]}" customWidth="1"/>' for i in range(n_cols)]
|
|
108
|
+
cols_xml = f"<cols>{''.join(cols_parts)}</cols>"
|
|
109
|
+
|
|
110
|
+
sheet_rows: list[str] = []
|
|
111
|
+
# Header row (row 1, bold + tinted fill)
|
|
112
|
+
if headers:
|
|
113
|
+
cells = "".join(_cell_xml(1, ci, h, style=1) for ci, h in enumerate(headers))
|
|
114
|
+
sheet_rows.append(f'<row r="1">{cells}</row>')
|
|
115
|
+
row_num = 2
|
|
116
|
+
else:
|
|
117
|
+
row_num = 1
|
|
118
|
+
|
|
119
|
+
# Data rows
|
|
120
|
+
for row in rows:
|
|
121
|
+
cells = "".join(_cell_xml(row_num, ci, v) for ci, v in enumerate(row))
|
|
122
|
+
sheet_rows.append(f'<row r="{row_num}">{cells}</row>')
|
|
123
|
+
row_num += 1
|
|
124
|
+
|
|
125
|
+
# Optional totals footer row
|
|
126
|
+
if totals:
|
|
127
|
+
cells = "".join(_cell_xml(row_num, ci, v, style=1) for ci, v in enumerate(totals))
|
|
128
|
+
sheet_rows.append(f'<row r="{row_num}">{cells}</row>')
|
|
129
|
+
|
|
130
|
+
sheet_xml = (
|
|
131
|
+
'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'
|
|
132
|
+
'<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">'
|
|
133
|
+
f"{cols_xml}<sheetData>{''.join(sheet_rows)}</sheetData>"
|
|
134
|
+
"</worksheet>"
|
|
135
|
+
)
|
|
136
|
+
|
|
137
|
+
with zipfile.ZipFile(dest, "w", compression=zipfile.ZIP_DEFLATED) as zf:
|
|
138
|
+
zf.writestr("[Content_Types].xml", _CONTENT_TYPES_XML)
|
|
139
|
+
zf.writestr("_rels/.rels", _RELS_XML)
|
|
140
|
+
zf.writestr("xl/workbook.xml", wb_xml)
|
|
141
|
+
zf.writestr("xl/_rels/workbook.xml.rels", _WB_RELS_XML)
|
|
142
|
+
zf.writestr("xl/styles.xml", _STYLES_XML)
|
|
143
|
+
zf.writestr("xl/worksheets/sheet1.xml", sheet_xml)
|
|
144
|
+
|
|
145
|
+
return dest
|
quantumgui/py.typed
ADDED
|
File without changes
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
from .hct import Color, CorePalette, TonalPalette
|
|
2
|
+
from .theme import Theme, ThemeMode
|
|
3
|
+
from .tokens import (
|
|
4
|
+
ELEVATION_DP,
|
|
5
|
+
ELEVATION_TINT,
|
|
6
|
+
ColorScheme,
|
|
7
|
+
ContrastLevel,
|
|
8
|
+
ShapeScale,
|
|
9
|
+
ShapeToken,
|
|
10
|
+
StateLayers,
|
|
11
|
+
TextStyle,
|
|
12
|
+
TypographyRole,
|
|
13
|
+
TypographyScale,
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
__all__ = [
|
|
17
|
+
"Color",
|
|
18
|
+
"ColorScheme",
|
|
19
|
+
"ContrastLevel",
|
|
20
|
+
"CorePalette",
|
|
21
|
+
"ELEVATION_DP",
|
|
22
|
+
"ELEVATION_TINT",
|
|
23
|
+
"ShapeScale",
|
|
24
|
+
"ShapeToken",
|
|
25
|
+
"StateLayers",
|
|
26
|
+
"TextStyle",
|
|
27
|
+
"Theme",
|
|
28
|
+
"ThemeMode",
|
|
29
|
+
"TonalPalette",
|
|
30
|
+
"TypographyRole",
|
|
31
|
+
"TypographyScale",
|
|
32
|
+
]
|
quantumgui/theme/hct.py
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
"""Material Design 3 perceptual color space (HCT-inspired L*Ch) & Tonal Palettes.
|
|
2
|
+
|
|
3
|
+
Generates full Material Design 3 tonal palettes (tones 0..100) from any hex seed
|
|
4
|
+
color with perceptually uniform lightness (CIE L*) and gamut-mapped chroma.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import math
|
|
10
|
+
from dataclasses import dataclass
|
|
11
|
+
from functools import lru_cache
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@dataclass(frozen=True, slots=True)
|
|
15
|
+
class Color:
|
|
16
|
+
"""Immutable RGBA color with 0..255 integer channels."""
|
|
17
|
+
|
|
18
|
+
r: int
|
|
19
|
+
g: int
|
|
20
|
+
b: int
|
|
21
|
+
a: int = 255
|
|
22
|
+
|
|
23
|
+
@classmethod
|
|
24
|
+
def from_hex(cls, hex_str: str) -> Color:
|
|
25
|
+
s = hex_str.strip().lstrip("#")
|
|
26
|
+
if len(s) == 3:
|
|
27
|
+
s = "".join(c * 2 for c in s)
|
|
28
|
+
if len(s) == 6:
|
|
29
|
+
return cls(int(s[0:2], 16), int(s[2:4], 16), int(s[4:6], 16), 255)
|
|
30
|
+
if len(s) == 8:
|
|
31
|
+
return cls(int(s[0:2], 16), int(s[2:4], 16), int(s[4:6], 16), int(s[6:8], 16))
|
|
32
|
+
raise ValueError(f"Invalid hex color: {hex_str!r}")
|
|
33
|
+
|
|
34
|
+
def to_hex(self, include_alpha: bool = False) -> str:
|
|
35
|
+
if include_alpha:
|
|
36
|
+
return f"#{self.r:02X}{self.g:02X}{self.b:02X}{self.a:02X}"
|
|
37
|
+
return f"#{self.r:02X}{self.g:02X}{self.b:02X}"
|
|
38
|
+
|
|
39
|
+
def with_alpha(self, alpha: float) -> Color:
|
|
40
|
+
"""Return copy with alpha in [0.0, 1.0]."""
|
|
41
|
+
a = max(0, min(255, round(alpha * 255)))
|
|
42
|
+
return Color(self.r, self.g, self.b, a)
|
|
43
|
+
|
|
44
|
+
def blend(self, overlay: Color, factor: float) -> Color:
|
|
45
|
+
"""Linearly blend `overlay` onto `self` by `factor` in [0.0, 1.0]."""
|
|
46
|
+
t = max(0.0, min(1.0, factor))
|
|
47
|
+
return Color(
|
|
48
|
+
round(self.r + (overlay.r - self.r) * t),
|
|
49
|
+
round(self.g + (overlay.g - self.g) * t),
|
|
50
|
+
round(self.b + (overlay.b - self.b) * t),
|
|
51
|
+
self.a,
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
def to_skia(self) -> int:
|
|
55
|
+
"""Pack as Skia ARGB 32-bit integer (skia.ColorSetARGB)."""
|
|
56
|
+
return ((self.a & 0xFF) << 24) | ((self.r & 0xFF) << 16) | ((self.g & 0xFF) << 8) | (self.b & 0xFF)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
# --- CIE Lab / LCh <-> sRGB helpers (D65 illuminant) ---
|
|
60
|
+
|
|
61
|
+
_XN, _YN, _ZN = 0.95047, 1.00000, 1.08883
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def _srgb_to_linear(c: float) -> float:
|
|
65
|
+
return c / 12.92 if c <= 0.04045 else ((c + 0.055) / 1.055) ** 2.4
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def _linear_to_srgb(c: float) -> float:
|
|
69
|
+
if c <= 0.0031308:
|
|
70
|
+
return 12.92 * c
|
|
71
|
+
return 1.055 * (c ** (1.0 / 2.4)) - 0.055
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def _lab_f(t: float) -> float:
|
|
75
|
+
return t ** (1.0 / 3.0) if t > 0.008856 else (7.787 * t) + (16.0 / 116.0)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def _lab_f_inv(t: float) -> float:
|
|
79
|
+
t3 = t * t * t
|
|
80
|
+
return t3 if t3 > 0.008856 else (t - 16.0 / 116.0) / 7.787
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def color_to_lch(color: Color) -> tuple[float, float, float]:
|
|
84
|
+
"""Convert sRGB Color to (L* [0..100], C [0..~130], H [0..360))."""
|
|
85
|
+
r = _srgb_to_linear(color.r / 255.0)
|
|
86
|
+
g = _srgb_to_linear(color.g / 255.0)
|
|
87
|
+
b = _srgb_to_linear(color.b / 255.0)
|
|
88
|
+
|
|
89
|
+
x = (0.4124564 * r + 0.3575761 * g + 0.1804375 * b) / _XN
|
|
90
|
+
y = (0.2126729 * r + 0.7151522 * g + 0.0721750 * b) / _YN
|
|
91
|
+
z = (0.0193339 * r + 0.1191920 * g + 0.9503041 * b) / _ZN
|
|
92
|
+
|
|
93
|
+
fx, fy, fz = _lab_f(x), _lab_f(y), _lab_f(z)
|
|
94
|
+
l_star = 116.0 * fy - 16.0
|
|
95
|
+
a_star = 500.0 * (fx - fy)
|
|
96
|
+
b_star = 200.0 * (fy - fz)
|
|
97
|
+
|
|
98
|
+
chroma = math.hypot(a_star, b_star)
|
|
99
|
+
hue = math.degrees(math.atan2(b_star, a_star)) % 360.0
|
|
100
|
+
return (l_star, chroma, hue)
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def _lch_to_linear_rgb(l_star: float, chroma: float, hue_deg: float) -> tuple[float, float, float]:
|
|
104
|
+
h_rad = math.radians(hue_deg)
|
|
105
|
+
a_star = chroma * math.cos(h_rad)
|
|
106
|
+
b_star = chroma * math.sin(h_rad)
|
|
107
|
+
|
|
108
|
+
fy = (l_star + 16.0) / 116.0
|
|
109
|
+
fx = fy + (a_star / 500.0)
|
|
110
|
+
fz = fy - (b_star / 200.0)
|
|
111
|
+
|
|
112
|
+
x = _XN * _lab_f_inv(fx)
|
|
113
|
+
y = _YN * _lab_f_inv(fy)
|
|
114
|
+
z = _ZN * _lab_f_inv(fz)
|
|
115
|
+
|
|
116
|
+
r = 3.2404542 * x - 1.5371385 * y - 0.4985314 * z
|
|
117
|
+
g = -0.9692660 * x + 1.8760108 * y + 0.0415560 * z
|
|
118
|
+
b = 0.0556434 * x - 0.2040259 * y + 1.0572252 * z
|
|
119
|
+
return (r, g, b)
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def _in_srgb_gamut(r: float, g: float, b: float) -> bool:
|
|
123
|
+
eps = -1e-4
|
|
124
|
+
return (eps <= r <= 1.0 + 1e-4) and (eps <= g <= 1.0 + 1e-4) and (eps <= b <= 1.0 + 1e-4)
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
@lru_cache(maxsize=2048)
|
|
128
|
+
def lch_to_color(tone: float, chroma: float, hue: float) -> Color:
|
|
129
|
+
"""Convert perceptual (Tone=L*, Chroma, Hue) to gamut-mapped sRGB Color."""
|
|
130
|
+
tone = max(0.0, min(100.0, float(tone)))
|
|
131
|
+
hue = float(hue) % 360.0
|
|
132
|
+
chroma = max(0.0, float(chroma))
|
|
133
|
+
|
|
134
|
+
if tone <= 0.01:
|
|
135
|
+
return Color(0, 0, 0)
|
|
136
|
+
if tone >= 99.99:
|
|
137
|
+
return Color(255, 255, 255)
|
|
138
|
+
|
|
139
|
+
r, g, b = _lch_to_linear_rgb(tone, chroma, hue)
|
|
140
|
+
if not _in_srgb_gamut(r, g, b):
|
|
141
|
+
# Binary-search the maximum chroma that stays inside sRGB at this exact tone & hue
|
|
142
|
+
lo, hi = 0.0, chroma
|
|
143
|
+
for _ in range(18):
|
|
144
|
+
mid = (lo + hi) * 0.5
|
|
145
|
+
mr, mg, mb = _lch_to_linear_rgb(tone, mid, hue)
|
|
146
|
+
if _in_srgb_gamut(mr, mg, mb):
|
|
147
|
+
lo = mid
|
|
148
|
+
else:
|
|
149
|
+
hi = mid
|
|
150
|
+
r, g, b = _lch_to_linear_rgb(tone, lo, hue)
|
|
151
|
+
|
|
152
|
+
sr = max(0, min(255, round(_linear_to_srgb(max(0.0, min(1.0, r))) * 255)))
|
|
153
|
+
sg = max(0, min(255, round(_linear_to_srgb(max(0.0, min(1.0, g))) * 255)))
|
|
154
|
+
sb = max(0, min(255, round(_linear_to_srgb(max(0.0, min(1.0, b))) * 255)))
|
|
155
|
+
return Color(sr, sg, sb)
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
class TonalPalette:
|
|
159
|
+
"""A Material Design 3 Tonal Palette with fixed Hue and Chroma."""
|
|
160
|
+
|
|
161
|
+
def __init__(self, hue: float, chroma: float) -> None:
|
|
162
|
+
self.hue = hue % 360.0
|
|
163
|
+
self.chroma = max(0.0, chroma)
|
|
164
|
+
|
|
165
|
+
def tone(self, t: int | float) -> Color:
|
|
166
|
+
return lch_to_color(round(float(t), 2), round(self.chroma, 2), round(self.hue, 2))
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
@dataclass(frozen=True)
|
|
170
|
+
class CorePalette:
|
|
171
|
+
"""The 6 official Material Design 3 tonal palettes derived from a seed color."""
|
|
172
|
+
|
|
173
|
+
primary: TonalPalette
|
|
174
|
+
secondary: TonalPalette
|
|
175
|
+
tertiary: TonalPalette
|
|
176
|
+
neutral: TonalPalette
|
|
177
|
+
neutral_variant: TonalPalette
|
|
178
|
+
error: TonalPalette
|
|
179
|
+
|
|
180
|
+
@classmethod
|
|
181
|
+
def from_seed(cls, seed: str | Color) -> CorePalette:
|
|
182
|
+
color = Color.from_hex(seed) if isinstance(seed, str) else seed
|
|
183
|
+
_, chroma, hue = color_to_lch(color)
|
|
184
|
+
return cls(
|
|
185
|
+
primary=TonalPalette(hue, max(48.0, chroma)),
|
|
186
|
+
secondary=TonalPalette(hue, 16.0),
|
|
187
|
+
tertiary=TonalPalette((hue + 60.0) % 360.0, 24.0),
|
|
188
|
+
neutral=TonalPalette(hue, 4.0),
|
|
189
|
+
neutral_variant=TonalPalette(hue, 8.0),
|
|
190
|
+
error=TonalPalette(25.0, 84.0),
|
|
191
|
+
)
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"""Material Design 3 Theme manager with dynamic seed & light/dark switching."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import Callable, Literal
|
|
6
|
+
|
|
7
|
+
from .hct import Color, CorePalette
|
|
8
|
+
from .tokens import ColorScheme, ContrastLevel, ShapeScale, StateLayers, TypographyScale
|
|
9
|
+
|
|
10
|
+
ThemeMode = Literal["light", "dark"]
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class Theme:
|
|
14
|
+
"""Reactive Material Design 3 Theme.
|
|
15
|
+
|
|
16
|
+
Changing `seed`, `mode`, or `contrast` regenerates the entire M3 ColorScheme
|
|
17
|
+
and notifies attached windows to repaint immediately.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
def __init__(
|
|
21
|
+
self,
|
|
22
|
+
seed: str | Color = "#6750A4",
|
|
23
|
+
mode: ThemeMode = "light",
|
|
24
|
+
contrast: ContrastLevel = "standard",
|
|
25
|
+
font_family: str = "Segoe UI",
|
|
26
|
+
) -> None:
|
|
27
|
+
self._seed: Color = Color.from_hex(seed) if isinstance(seed, str) else seed
|
|
28
|
+
self._mode: ThemeMode = mode
|
|
29
|
+
self._contrast: ContrastLevel = contrast
|
|
30
|
+
self.font_family: str = font_family
|
|
31
|
+
self.typography: TypographyScale = TypographyScale()
|
|
32
|
+
self.shape: ShapeScale = ShapeScale()
|
|
33
|
+
self.state_layers: StateLayers = StateLayers()
|
|
34
|
+
self._listeners: list[Callable[[], None]] = []
|
|
35
|
+
self._rebuild()
|
|
36
|
+
|
|
37
|
+
def _rebuild(self) -> None:
|
|
38
|
+
self.palette = CorePalette.from_seed(self._seed)
|
|
39
|
+
self.colors = (
|
|
40
|
+
ColorScheme.dark(self.palette, contrast=self._contrast)
|
|
41
|
+
if self._mode == "dark"
|
|
42
|
+
else ColorScheme.light(self.palette, contrast=self._contrast)
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
@property
|
|
46
|
+
def contrast(self) -> ContrastLevel:
|
|
47
|
+
return self._contrast
|
|
48
|
+
|
|
49
|
+
@contrast.setter
|
|
50
|
+
def contrast(self, value: ContrastLevel) -> None:
|
|
51
|
+
if value not in ("standard", "medium", "high"):
|
|
52
|
+
raise ValueError(f"contrast must be 'standard', 'medium', or 'high', got {value!r}")
|
|
53
|
+
if value != self._contrast:
|
|
54
|
+
self._contrast = value
|
|
55
|
+
self._rebuild()
|
|
56
|
+
self._notify()
|
|
57
|
+
|
|
58
|
+
@property
|
|
59
|
+
def seed(self) -> Color:
|
|
60
|
+
return self._seed
|
|
61
|
+
|
|
62
|
+
@seed.setter
|
|
63
|
+
def seed(self, value: str | Color) -> None:
|
|
64
|
+
new_seed = Color.from_hex(value) if isinstance(value, str) else value
|
|
65
|
+
if new_seed != self._seed:
|
|
66
|
+
self._seed = new_seed
|
|
67
|
+
self._rebuild()
|
|
68
|
+
self._notify()
|
|
69
|
+
|
|
70
|
+
@property
|
|
71
|
+
def mode(self) -> ThemeMode:
|
|
72
|
+
return self._mode
|
|
73
|
+
|
|
74
|
+
@mode.setter
|
|
75
|
+
def mode(self, value: ThemeMode) -> None:
|
|
76
|
+
if value not in ("light", "dark"):
|
|
77
|
+
raise ValueError(f"mode must be 'light' or 'dark', got {value!r}")
|
|
78
|
+
if value != self._mode:
|
|
79
|
+
self._mode = value
|
|
80
|
+
self._rebuild()
|
|
81
|
+
self._notify()
|
|
82
|
+
|
|
83
|
+
def toggle_mode(self) -> ThemeMode:
|
|
84
|
+
self.mode = "dark" if self._mode == "light" else "light"
|
|
85
|
+
return self._mode
|
|
86
|
+
|
|
87
|
+
def subscribe(self, callback: Callable[[], None]) -> None:
|
|
88
|
+
if callback not in self._listeners:
|
|
89
|
+
self._listeners.append(callback)
|
|
90
|
+
|
|
91
|
+
def _notify(self) -> None:
|
|
92
|
+
for cb in list(self._listeners):
|
|
93
|
+
cb()
|