Show detected tape colors in UI

This commit is contained in:
Mikei386
2026-06-22 14:41:12 +02:00
parent e880260aa5
commit d00ce6fe90
8 changed files with 244 additions and 14 deletions
+85 -7
View File
@@ -375,7 +375,76 @@ def should_render_copies_as_strip(design: dict[str, Any], copies: int) -> bool:
return str(design.get("cutMode", "auto")) in {"chain", "cutmark-chain"}
def detect_tape_width() -> tuple[float | None, str]:
COLOR_HEX_BY_NAME = {
"white": "#ffffff",
"black": "#111111",
"red": "#d92d20",
"blue": "#2563eb",
"yellow": "#facc15",
"green": "#16a34a",
"clear": "#f8fafc",
"transparent": "#f8fafc",
"silver": "#c0c0c0",
"gold": "#d4af37",
"matte white": "#f5f5f0",
"matte black": "#1f1f1f",
}
COLOR_NAME_DE = {
"white": "Weiss",
"black": "Schwarz",
"red": "Rot",
"blue": "Blau",
"yellow": "Gelb",
"green": "Gruen",
"clear": "Transparent",
"transparent": "Transparent",
"silver": "Silber",
"gold": "Gold",
"matte white": "Matt weiss",
"matte black": "Matt schwarz",
}
MEDIA_TYPE_DE = {
"laminated tape": "Laminiertes Band",
"non-laminated tape": "Nicht laminiertes Band",
"fabric tape": "Textilband",
"heat shrink tube": "Schrumpfschlauch",
}
def color_info_from_line(output: str, key: str) -> dict[str, str] | None:
match = re.search(rf"^{re.escape(key)}\s*=\s*([0-9a-fA-F]+)(?:\s*\(([^)]+)\))?", output, re.MULTILINE)
if not match:
return None
code = match.group(1)
raw_name = (match.group(2) or f"Code {code}").strip()
normalized = raw_name.lower()
return {
"code": code,
"name": COLOR_NAME_DE.get(normalized, raw_name),
"rawName": raw_name,
"css": COLOR_HEX_BY_NAME.get(normalized, "#d1d5db"),
}
def media_type_from_output(output: str) -> dict[str, str] | None:
match = re.search(r"^media type\s*=\s*([0-9a-fA-F]+)(?:\s*\(([^)]+)\))?", output, re.MULTILINE)
if not match:
return None
code = match.group(1)
raw_name = (match.group(2) or f"Code {code}").strip()
normalized = raw_name.lower()
return {
"code": code,
"name": MEDIA_TYPE_DE.get(normalized, raw_name),
"rawName": raw_name,
}
def detect_tape_info() -> dict[str, Any]:
result = subprocess.run(
["ptouch-print", "--info"],
capture_output=True,
@@ -387,6 +456,7 @@ def detect_tape_width() -> tuple[float | None, str]:
if result.returncode != 0:
raise RuntimeError(format_print_error(result))
width = None
candidates: list[float] = []
for pattern in [
r"(?:tape|media|band|width|breite|size|groesse|printing)[^\n:=-]*[:=-]?\s*(\d+(?:[.,]\d+)?)\s*mm",
@@ -397,8 +467,16 @@ def detect_tape_width() -> tuple[float | None, str]:
for supported in TAPE_WIDTHS_MM:
if any(abs(candidate - supported) < 0.26 for candidate in candidates):
return supported, output
return None, output
width = supported
break
return {
"width": width,
"mediaType": media_type_from_output(output),
"tapeColor": color_info_from_line(output, "tape color"),
"textColor": color_info_from_line(output, "text color"),
"raw": output,
}
def format_print_error(result: subprocess.CompletedProcess[str]) -> str:
@@ -503,10 +581,10 @@ def api_print():
@app.get("/api/tape-info")
def api_tape_info():
try:
width, raw = detect_tape_width()
if width is None:
return jsonify({"ok": False, "error": "Bandbreite konnte nicht erkannt werden.", "raw": raw}), 400
return jsonify({"ok": True, "width": width, "raw": raw})
info = detect_tape_info()
if info["width"] is None:
return jsonify({"ok": False, "error": "Bandbreite konnte nicht erkannt werden.", **info}), 400
return jsonify({"ok": True, **info})
except Exception as exc:
return jsonify({"ok": False, "error": str(exc)}), 400