flashforge-python-api 1.6.0__py3-none-any.whl → 3.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.
Files changed (70) hide show
  1. flashforge/__init__.py +337 -256
  2. flashforge/_removed.py +65 -0
  3. flashforge/api/__init__.py +12 -18
  4. flashforge/api/constants/__init__.py +11 -11
  5. flashforge/api/constants/commands.py +18 -15
  6. flashforge/api/constants/endpoints.py +11 -11
  7. flashforge/api/misc/__init__.py +8 -8
  8. flashforge/api/network/__init__.py +11 -11
  9. flashforge/api/network/utils.py +71 -71
  10. flashforge/backends/__init__.py +80 -0
  11. flashforge/backends/connect.py +275 -0
  12. flashforge/backends/creator5_backend.py +191 -0
  13. flashforge/backends/dual_api_backend.py +170 -0
  14. flashforge/backends/dual_api_models.py +145 -0
  15. flashforge/backends/http_core.py +431 -0
  16. flashforge/backends/http_printer_backend.py +254 -0
  17. flashforge/backends/legacy_backend.py +436 -0
  18. flashforge/backends/printer_backend.py +217 -0
  19. flashforge/backends/types.py +125 -0
  20. flashforge/capabilities/__init__.py +98 -0
  21. flashforge/capabilities/models.py +67 -0
  22. flashforge/capabilities/palettes/__init__.py +28 -0
  23. flashforge/capabilities/palettes/ad5x_palette.py +70 -0
  24. flashforge/capabilities/palettes/creator5_palette.py +78 -0
  25. flashforge/capabilities/palettes/palette.py +54 -0
  26. flashforge/{api/controls/creator5_palette.py → capabilities/palettes/palette_snap.py} +226 -258
  27. flashforge/capabilities/printer_capabilities.py +145 -0
  28. flashforge/capabilities/registry.py +125 -0
  29. flashforge/capabilities/resolve_model.py +61 -0
  30. flashforge/discovery/__init__.py +39 -39
  31. flashforge/discovery/discovery.py +730 -763
  32. flashforge/exceptions.py +90 -51
  33. flashforge/models/__init__.py +52 -55
  34. flashforge/models/machine_info.py +540 -543
  35. flashforge/{api/controls/info.py → models/machine_info_parser.py} +35 -242
  36. flashforge/models/responses.py +119 -254
  37. flashforge/status/__init__.py +43 -0
  38. flashforge/status/printer_snapshot.py +396 -0
  39. flashforge/tcp/__init__.py +55 -54
  40. flashforge/tcp/a3_client.py +427 -427
  41. flashforge/tcp/a4_client.py +304 -304
  42. flashforge/tcp/ff_client.py +609 -590
  43. flashforge/tcp/gcode/__init__.py +15 -13
  44. flashforge/tcp/gcode/a3_gcode_controller.py +109 -109
  45. flashforge/tcp/gcode/gcode_controller.py +292 -308
  46. flashforge/tcp/gcode/gcodes.py +81 -99
  47. flashforge/tcp/gcode/legacy_gcode_controller.py +56 -0
  48. flashforge/tcp/parsers/__init__.py +27 -27
  49. flashforge/tcp/parsers/endstop_status.py +248 -248
  50. flashforge/tcp/parsers/location_info.py +83 -83
  51. flashforge/tcp/parsers/print_status.py +180 -180
  52. flashforge/tcp/parsers/printer_info.py +146 -146
  53. flashforge/tcp/parsers/temp_info.py +218 -218
  54. flashforge/tcp/parsers/thumbnail_info.py +274 -274
  55. flashforge/tcp/tcp_client.py +52 -12
  56. flashforge/threemf/__init__.py +40 -40
  57. flashforge/threemf/parser.py +560 -560
  58. flashforge/threemf/warnings.py +58 -58
  59. {flashforge_python_api-1.6.0.dist-info → flashforge_python_api-3.0.0.dist-info}/METADATA +46 -47
  60. flashforge_python_api-3.0.0.dist-info/RECORD +69 -0
  61. {flashforge_python_api-1.6.0.dist-info → flashforge_python_api-3.0.0.dist-info}/WHEEL +1 -1
  62. {flashforge_python_api-1.6.0.dist-info → flashforge_python_api-3.0.0.dist-info}/licenses/LICENSE +21 -21
  63. flashforge/api/controls/__init__.py +0 -17
  64. flashforge/api/controls/control.py +0 -413
  65. flashforge/api/controls/files.py +0 -210
  66. flashforge/api/controls/job_control.py +0 -896
  67. flashforge/api/controls/temp_control.py +0 -309
  68. flashforge/client.py +0 -609
  69. flashforge_python_api-1.6.0.dist-info/RECORD +0 -52
  70. {flashforge_python_api-1.6.0.dist-info → flashforge_python_api-3.0.0.dist-info}/entry_points.txt +0 -0
@@ -0,0 +1,125 @@
1
+ """
2
+ Parameter types for the printer backends.
3
+
4
+ These are outbound models: request data this library builds. They keep
5
+ ``extra="forbid"`` and range checks, so a typo or an out-of-range value fails before
6
+ anything reaches the printer.
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ import re
12
+ from enum import IntEnum
13
+ from typing import Literal
14
+
15
+ from pydantic import BaseModel, ConfigDict, Field, field_validator
16
+
17
+ _HEX = re.compile(r"^#[0-9A-Fa-f]{6}$")
18
+
19
+
20
+ class SlotAction(IntEnum):
21
+ """AD5X material-station slot operation, carrying the ``ms_cmd`` action code."""
22
+
23
+ LOAD = 0
24
+ UNLOAD = 1
25
+ CANCEL = 2
26
+
27
+
28
+ #: Air filtration mode (Adventurer 5M Pro).
29
+ FiltrationMode = Literal["internal", "external", "off"]
30
+
31
+
32
+ class MaterialMapping(BaseModel):
33
+ """
34
+ Maps one tool of a sliced file to one material-station slot.
35
+ The same shape on the AD5X and the Creator 5 series.
36
+ """
37
+
38
+ model_config = ConfigDict(extra="forbid", populate_by_name=True)
39
+
40
+ #: Tool index from the slicer, 0-based (0-3).
41
+ tool_id: int = Field(ge=0, le=3, alias="toolId")
42
+ #: The printer's native slot ID, 1-based (1-4). Do not convert it to 0-based.
43
+ slot_id: int = Field(ge=1, le=4, alias="slotId")
44
+ #: Material name, for example "PLA". Not empty.
45
+ material_name: str = Field(alias="materialName")
46
+ #: Tool color from the slicer, "#RRGGBB".
47
+ tool_material_color: str = Field(alias="toolMaterialColor")
48
+ #: Color of the material in the slot, "#RRGGBB".
49
+ slot_material_color: str = Field(alias="slotMaterialColor")
50
+
51
+ @field_validator("tool_material_color", "slot_material_color")
52
+ @classmethod
53
+ def _validate_hex(cls, value: str) -> str:
54
+ if not _HEX.match(value):
55
+ raise ValueError(f"Invalid hex color format: {value}")
56
+ return value
57
+
58
+ def to_wire(self) -> dict[str, object]:
59
+ """The camelCase dict the printer expects."""
60
+ return {
61
+ "toolId": self.tool_id,
62
+ "slotId": self.slot_id,
63
+ "materialName": self.material_name,
64
+ "toolMaterialColor": self.tool_material_color,
65
+ "slotMaterialColor": self.slot_material_color,
66
+ }
67
+
68
+
69
+ class UploadJobParams(BaseModel):
70
+ """Upload a file to the printer and, as an option, start it."""
71
+
72
+ model_config = ConfigDict(extra="forbid")
73
+
74
+ #: Local path of the .gcode or .3mf file.
75
+ file_path: str
76
+ #: Start the print when the upload completes.
77
+ start_print: bool
78
+ #: Level the bed before the print.
79
+ leveling_before_print: bool
80
+ #: Flow calibration before the print (AD5X, Creator 5).
81
+ flow_calibration: bool = False
82
+ #: First-layer inspection (AD5X only).
83
+ first_layer_inspection: bool = False
84
+ #: Record a time-lapse video (AD5X, Creator 5).
85
+ time_lapse_video: bool = False
86
+ #: Material mappings (AD5X, Creator 5). On the AD5X a non-empty list uses the station.
87
+ #: On the Creator 5 they are sent only with ``start_print=True``: the firmware keeps upload
88
+ #: mappings until the next print ends. Prefer ``start_local_job`` with mappings.
89
+ material_mappings: list[MaterialMapping] = Field(default_factory=list)
90
+
91
+
92
+ class LocalJobParams(BaseModel):
93
+ """Start a print of a file that is already on the printer."""
94
+
95
+ model_config = ConfigDict(extra="forbid")
96
+
97
+ #: File name on the printer.
98
+ file_name: str
99
+ #: Level the bed before the print.
100
+ leveling_before_print: bool
101
+ #: Flow calibration (AD5X, Creator 5).
102
+ flow_calibration: bool = False
103
+ #: Record a time-lapse video (AD5X, Creator 5).
104
+ time_lapse_video: bool = False
105
+ #: Material mappings (AD5X, Creator 5). On the AD5X a non-empty list starts a multi-color
106
+ #: print; an empty list starts a single-color print. On the Creator 5 give one mapping per
107
+ #: tool; mappings take effect only for a .3mf file.
108
+ material_mappings: list[MaterialMapping] = Field(default_factory=list)
109
+
110
+
111
+ class PrintOverrides(BaseModel):
112
+ """Changes to the active print. Leave a field as None to keep its current value."""
113
+
114
+ model_config = ConfigDict(extra="forbid")
115
+
116
+ #: Print speed in percent. The 5M series accepts 50-150.
117
+ print_speed: int | None = None
118
+ #: Z offset in mm, relative to the job start offset (not cumulative), -5.0 to +5.0.
119
+ z_offset: float | None = None
120
+ #: Chamber fan speed in percent.
121
+ chamber_fan_speed: int | None = None
122
+ #: Cooling fan speed in percent.
123
+ cooling_fan_speed: int | None = None
124
+ #: Left cooling fan speed in percent. AD5X only.
125
+ left_cooling_fan_speed: int | None = None
@@ -0,0 +1,98 @@
1
+ """
2
+ Model registry, the single PID table, model resolution, capabilities and palettes.
3
+ """
4
+
5
+ from __future__ import annotations
6
+
7
+ from .models import (
8
+ ALL_MODEL_IDS,
9
+ MODEL_PID_MAP,
10
+ BackendFamily,
11
+ PrinterModelId,
12
+ model_from_pid,
13
+ )
14
+ from .palettes import (
15
+ AD5X_FILAMENT_PALETTE,
16
+ AD5X_MATERIALS,
17
+ AD5X_PALETTE,
18
+ CREATOR5_FILAMENT_PALETTE,
19
+ CREATOR5_MATERIALS,
20
+ CREATOR5_PALETTE,
21
+ Palette,
22
+ PaletteColor,
23
+ snap_to_ad5x_palette,
24
+ snap_to_creator5_palette,
25
+ )
26
+ from .printer_capabilities import (
27
+ FeatureSupport,
28
+ LedSupport,
29
+ MaterialStationSupport,
30
+ PrinterCapabilities,
31
+ PrinterReport,
32
+ resolve_capabilities,
33
+ )
34
+ from .registry import FIELD_TRUST, MODEL_REGISTRY, ModelSpec, TrustSource, get_model_spec
35
+ from .resolve_model import model_from_machine_type, resolve_model
36
+
37
+
38
+ def get_model_palette(model: PrinterModelId) -> tuple[PaletteColor, ...] | None:
39
+ """The firmware palette of a model, or None when it has no material station."""
40
+ if model == PrinterModelId.AD5X:
41
+ return AD5X_PALETTE
42
+ if model in (PrinterModelId.CREATOR_5, PrinterModelId.CREATOR_5_PRO):
43
+ return CREATOR5_PALETTE
44
+ return None
45
+
46
+
47
+ def get_palette_for_model(model: PrinterModelId | str | None) -> Palette:
48
+ """
49
+ The filament palette a client UI uses for a printer: the Creator 5 palette for the Creator 5
50
+ series, else the AD5X palette (also the default while the model is not known).
51
+ """
52
+ if model in (PrinterModelId.CREATOR_5, PrinterModelId.CREATOR_5_PRO):
53
+ return CREATOR5_FILAMENT_PALETTE
54
+ return AD5X_FILAMENT_PALETTE
55
+
56
+
57
+ def snap_to_model_palette(model: PrinterModelId, hex_str: str) -> PaletteColor | None:
58
+ """Snaps a color to the model's palette (CIEDE2000). None when the model has no palette."""
59
+ if model == PrinterModelId.AD5X:
60
+ return snap_to_ad5x_palette(hex_str)
61
+ if model in (PrinterModelId.CREATOR_5, PrinterModelId.CREATOR_5_PRO):
62
+ return snap_to_creator5_palette(hex_str)
63
+ return None
64
+
65
+
66
+ __all__ = [
67
+ "ALL_MODEL_IDS",
68
+ "AD5X_FILAMENT_PALETTE",
69
+ "AD5X_MATERIALS",
70
+ "AD5X_PALETTE",
71
+ "BackendFamily",
72
+ "CREATOR5_FILAMENT_PALETTE",
73
+ "CREATOR5_MATERIALS",
74
+ "CREATOR5_PALETTE",
75
+ "FIELD_TRUST",
76
+ "FeatureSupport",
77
+ "LedSupport",
78
+ "MODEL_PID_MAP",
79
+ "MODEL_REGISTRY",
80
+ "MaterialStationSupport",
81
+ "ModelSpec",
82
+ "PaletteColor",
83
+ "PrinterCapabilities",
84
+ "PrinterModelId",
85
+ "PrinterReport",
86
+ "TrustSource",
87
+ "Palette",
88
+ "get_model_palette",
89
+ "get_palette_for_model",
90
+ "get_model_spec",
91
+ "model_from_machine_type",
92
+ "model_from_pid",
93
+ "resolve_capabilities",
94
+ "resolve_model",
95
+ "snap_to_ad5x_palette",
96
+ "snap_to_creator5_palette",
97
+ "snap_to_model_palette",
98
+ ]
@@ -0,0 +1,67 @@
1
+ """
2
+ Printer model identifiers, backend families and the single product-ID table.
3
+
4
+ The product ID (PID) is the one number that names a printer model. It is the same
5
+ value in the UDP discovery packet (USB product ID) and in the ``/detail`` ``pid``
6
+ field. This module is the only place in the library where PIDs are listed.
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ from enum import StrEnum
12
+
13
+
14
+ class PrinterModelId(StrEnum):
15
+ """Stable, kebab-case model identifier. The values match the TypeScript and Kotlin ports."""
16
+
17
+ ADVENTURER_5M = "adventurer-5m"
18
+ ADVENTURER_5M_PRO = "adventurer-5m-pro"
19
+ AD5X = "ad5x"
20
+ CREATOR_5 = "creator-5"
21
+ CREATOR_5_PRO = "creator-5-pro"
22
+ ADVENTURER_4 = "adventurer-4"
23
+ ADVENTURER_3 = "adventurer-3"
24
+ GENERIC_LEGACY = "generic-legacy"
25
+ UNKNOWN = "unknown"
26
+
27
+
28
+ class BackendFamily(StrEnum):
29
+ """
30
+ Printer family. A family shares one backend.
31
+
32
+ - ``dual-api``: 5M, 5M Pro and AD5X. HTTP API (port 8898) plus the TCP command channel (8899).
33
+ - ``http-only``: Creator 5 and Creator 5 Pro. HTTP API only. The printer runs no TCP server.
34
+ - ``legacy``: Adventurer 3, Adventurer 4 and other TCP-only printers.
35
+ - ``unknown``: model not recognised.
36
+
37
+ Named ``BackendFamily`` (``PrinterFamily`` in the TypeScript port) because
38
+ :class:`flashforge.threemf.PrinterFamily` already exists for 3MF files.
39
+ """
40
+
41
+ DUAL_API = "dual-api"
42
+ HTTP_ONLY = "http-only"
43
+ LEGACY = "legacy"
44
+ UNKNOWN = "unknown"
45
+
46
+
47
+ #: Every model the library knows, in a fixed order.
48
+ ALL_MODEL_IDS: tuple[PrinterModelId, ...] = tuple(PrinterModelId)
49
+
50
+ #: The one PID table. Several PIDs can map to one model (Adventurer 4 Lite and Pro).
51
+ MODEL_PID_MAP: dict[int, PrinterModelId] = {
52
+ 35: PrinterModelId.ADVENTURER_5M, # 0x0023
53
+ 36: PrinterModelId.ADVENTURER_5M_PRO, # 0x0024
54
+ 38: PrinterModelId.AD5X, # 0x0026
55
+ 40: PrinterModelId.CREATOR_5, # 0x0028
56
+ 41: PrinterModelId.CREATOR_5_PRO, # 0x0029
57
+ 8: PrinterModelId.ADVENTURER_3, # 0x0008
58
+ 22: PrinterModelId.ADVENTURER_4, # 0x0016 (Adventurer 4 Lite)
59
+ 30: PrinterModelId.ADVENTURER_4, # 0x001e (Adventurer 4 Pro)
60
+ }
61
+
62
+
63
+ def model_from_pid(pid: int | None) -> PrinterModelId | None:
64
+ """Looks up a model from a PID. Returns None for a PID the library does not know."""
65
+ if pid is None:
66
+ return None
67
+ return MODEL_PID_MAP.get(pid)
@@ -0,0 +1,28 @@
1
+ """Per-model firmware color palettes (AD5X, Creator 5 series)."""
2
+
3
+ from .ad5x_palette import AD5X_MATERIALS, AD5X_PALETTE, snap_to_ad5x_palette
4
+ from .creator5_palette import CREATOR5_MATERIALS, CREATOR5_PALETTE, snap_to_creator5_palette
5
+ from .palette import Palette
6
+ from .palette_snap import PaletteColor, build_palette_lab, hex_to_rgb, snap_to_palette
7
+
8
+ #: The AD5X filament palette for client UIs: 24 colors, 14 materials.
9
+ AD5X_FILAMENT_PALETTE = Palette(AD5X_PALETTE, AD5X_MATERIALS)
10
+
11
+ #: The Creator 5 / Creator 5 Pro filament palette for client UIs: 24 colors, 21 materials.
12
+ CREATOR5_FILAMENT_PALETTE = Palette(CREATOR5_PALETTE, CREATOR5_MATERIALS)
13
+
14
+ __all__ = [
15
+ "AD5X_FILAMENT_PALETTE",
16
+ "AD5X_MATERIALS",
17
+ "AD5X_PALETTE",
18
+ "CREATOR5_FILAMENT_PALETTE",
19
+ "CREATOR5_MATERIALS",
20
+ "CREATOR5_PALETTE",
21
+ "Palette",
22
+ "PaletteColor",
23
+ "build_palette_lab",
24
+ "hex_to_rgb",
25
+ "snap_to_ad5x_palette",
26
+ "snap_to_creator5_palette",
27
+ "snap_to_palette",
28
+ ]
@@ -0,0 +1,70 @@
1
+ """
2
+ AD5X material-station slot color palette.
3
+
4
+ The AD5X draws a slot color icon only when ``rgb`` is an exact, case-sensitive match
5
+ with one of these 24 firmware strings, sent as uppercase ``#RRGGBB`` (the ``#`` is
6
+ part of the wire value). The printer also stores and reports back the value it was
7
+ sent, so an off-palette or ``#``-less value breaks later material mappings.
8
+ """
9
+
10
+ from __future__ import annotations
11
+
12
+ from .palette_snap import PaletteColor, build_palette_lab, snap_to_palette
13
+
14
+ #: The firmware's 24-entry UI palette. Index 0 (White) is the no-match fallback.
15
+ AD5X_PALETTE: tuple[PaletteColor, ...] = (
16
+ PaletteColor(0, "White", "#FFFFFF"),
17
+ PaletteColor(1, "Yellow", "#FEF043"),
18
+ PaletteColor(2, "Light Green", "#DCF478"),
19
+ PaletteColor(3, "Green", "#0ACC38"),
20
+ PaletteColor(4, "Dark Green", "#067749"),
21
+ PaletteColor(5, "Teal", "#0C6283"),
22
+ PaletteColor(6, "Cyan", "#0DE2A0"),
23
+ PaletteColor(7, "Light Blue", "#75D9F3"),
24
+ PaletteColor(8, "Blue", "#45A8F9"),
25
+ PaletteColor(9, "Dark Blue", "#2750E0"),
26
+ PaletteColor(10, "Purple", "#46328E"),
27
+ PaletteColor(11, "Violet", "#A03CF7"),
28
+ PaletteColor(12, "Magenta", "#F330F9"),
29
+ PaletteColor(13, "Pink", "#D4B0DC"),
30
+ PaletteColor(14, "Coral", "#F95D73"),
31
+ PaletteColor(15, "Red", "#F72224"),
32
+ PaletteColor(16, "Brown", "#7C4B00"),
33
+ PaletteColor(17, "Orange", "#F98D33"),
34
+ PaletteColor(18, "Cream", "#FDEBD5"),
35
+ PaletteColor(19, "Tan", "#D3C4A3"),
36
+ PaletteColor(20, "Dark Brown", "#AF7836"),
37
+ PaletteColor(21, "Gray", "#898989"),
38
+ PaletteColor(22, "Light Gray", "#BCBCBC"),
39
+ PaletteColor(23, "Black", "#161616"),
40
+ )
41
+
42
+ #: The 14 material names the AD5X material-station UI renders.
43
+ AD5X_MATERIALS: tuple[str, ...] = (
44
+ "PLA",
45
+ "PLA-CF",
46
+ "PETG",
47
+ "PETG-CF",
48
+ "ABS",
49
+ "TPU",
50
+ "SILK",
51
+ "PA",
52
+ "PA-CF",
53
+ "PAHT-CF",
54
+ "PC",
55
+ "PC-ABS",
56
+ "PET-CF",
57
+ "PPS-CF",
58
+ )
59
+
60
+ _PALETTE_LAB = build_palette_lab(AD5X_PALETTE)
61
+
62
+
63
+ def snap_to_ad5x_palette(hex_str: str) -> PaletteColor:
64
+ """
65
+ Snaps a hex color to the nearest AD5X palette entry (CIEDE2000).
66
+
67
+ The returned ``hex`` is always uppercase ``#RRGGBB``. Unparseable input falls
68
+ back to White with a warning.
69
+ """
70
+ return snap_to_palette(hex_str, _PALETTE_LAB, "snap_to_ad5x_palette")
@@ -0,0 +1,78 @@
1
+ """
2
+ Creator 5 / Creator 5 Pro material-station slot color palette.
3
+
4
+ The Creator 5 ``msConfig_cmd`` renders a color icon only when ``rgb`` is an exact,
5
+ case-sensitive match with one of these 24 firmware strings (verified in firmware
6
+ 1.9.2). These values differ from the AD5X palette (Blue is ``#4CAAF8`` here and
7
+ ``#45A8F9`` on the AD5X).
8
+ """
9
+
10
+ from __future__ import annotations
11
+
12
+ from .palette_snap import PaletteColor, build_palette_lab, snap_to_palette
13
+
14
+ #: The firmware's 24-entry UI palette. Index 0 (White) is the no-match fallback.
15
+ CREATOR5_PALETTE: tuple[PaletteColor, ...] = (
16
+ PaletteColor(0, "White", "#FFFFFF"),
17
+ PaletteColor(1, "Yellow", "#FFF245"),
18
+ PaletteColor(2, "Light Green", "#DEF578"),
19
+ PaletteColor(3, "Green", "#21CC3D"),
20
+ PaletteColor(4, "Dark Green", "#167A4B"),
21
+ PaletteColor(5, "Teal", "#156682"),
22
+ PaletteColor(6, "Cyan", "#24E4A0"),
23
+ PaletteColor(7, "Light Blue", "#7BD9F0"),
24
+ PaletteColor(8, "Blue", "#4CAAF8"),
25
+ PaletteColor(9, "Dark Blue", "#2E54DD"),
26
+ PaletteColor(10, "Purple", "#48358C"),
27
+ PaletteColor(11, "Violet", "#A341F7"),
28
+ PaletteColor(12, "Magenta", "#F435F6"),
29
+ PaletteColor(13, "Pink", "#D5B4DE"),
30
+ PaletteColor(14, "Coral", "#FA6173"),
31
+ PaletteColor(15, "Red", "#F82D29"),
32
+ PaletteColor(16, "Brown", "#805003"),
33
+ PaletteColor(17, "Orange", "#F9903B"),
34
+ PaletteColor(18, "Cream", "#FCEBD7"),
35
+ PaletteColor(19, "Tan", "#D5C5A1"),
36
+ PaletteColor(20, "Dark Brown", "#B17C38"),
37
+ PaletteColor(21, "Gray", "#8C8C89"),
38
+ PaletteColor(22, "Light Gray", "#BEBEBE"),
39
+ PaletteColor(23, "Black", "#1B1B1B"),
40
+ )
41
+
42
+ #: The 21 material names the Creator 5 material-station UI renders, in firmware order. New vs
43
+ #: the AD5X: ASA, S-PAHT, S-Multi, HIPS, PVA and three TPU durometers.
44
+ CREATOR5_MATERIALS: tuple[str, ...] = (
45
+ "PLA",
46
+ "PETG",
47
+ "PLA-CF",
48
+ "PETG-CF",
49
+ "ABS",
50
+ "ASA",
51
+ "SILK",
52
+ "PET-CF",
53
+ "PAHT-CF",
54
+ "S-PAHT",
55
+ "S-Multi",
56
+ "PA-CF",
57
+ "HIPS",
58
+ "PVA",
59
+ "TPU-90A",
60
+ "TPU-95A",
61
+ "TPU-64D",
62
+ "PC",
63
+ "PA",
64
+ "PC-ABS",
65
+ "PPS-CF",
66
+ )
67
+
68
+ _PALETTE_LAB = build_palette_lab(CREATOR5_PALETTE)
69
+
70
+
71
+ def snap_to_creator5_palette(hex_str: str) -> PaletteColor:
72
+ """
73
+ Snaps a hex color to the nearest Creator 5 palette entry (CIEDE2000).
74
+
75
+ The returned ``hex`` is always uppercase ``#RRGGBB`` and an exact firmware match.
76
+ Unparseable input falls back to White with a warning.
77
+ """
78
+ return snap_to_palette(hex_str, _PALETTE_LAB, "snap_to_creator5_palette")
@@ -0,0 +1,54 @@
1
+ """
2
+ A model's filament palette for client UIs: the colors and materials its firmware UI renders,
3
+ plus nearest-match helpers that snap arbitrary input (a Spoolman spool, a slicer color) onto
4
+ them. Same API as ``Palette`` in ff-api (TypeScript) and ff-5mp-api-kt.
5
+ """
6
+
7
+ from __future__ import annotations
8
+
9
+ import re
10
+ from collections.abc import Sequence
11
+
12
+ from .palette_snap import PaletteColor, _delta_e_2000, _hex_to_rgb, _rgb_to_lab, build_palette_lab
13
+
14
+
15
+ def _normalize_material(raw: str) -> str:
16
+ """Upper case, letters and digits only: "pla cf" and "PLA-CF" compare equal."""
17
+ return re.sub(r"[^A-Z0-9]", "", raw.upper())
18
+
19
+
20
+ class Palette:
21
+ """A model's fixed filament palette: the colors and materials its UI renders."""
22
+
23
+ def __init__(self, colors: Sequence[PaletteColor], materials: Sequence[str]) -> None:
24
+ #: The colors this model's UI renders, in firmware order.
25
+ self.colors: tuple[PaletteColor, ...] = tuple(colors)
26
+ #: The materials this model's UI renders, in firmware order.
27
+ self.materials: tuple[str, ...] = tuple(materials)
28
+ self._lab = build_palette_lab(self.colors)
29
+ self._material_norm = {_normalize_material(m): m for m in self.materials}
30
+
31
+ def nearest_color(self, hex_str: str | None) -> PaletteColor | None:
32
+ """
33
+ The nearest palette color (CIEDE2000) to a hex color: ``#RRGGBB``, ``RRGGBB``,
34
+ ``#RRGGBBAA`` (alpha ignored) or ``#RGB``. None when the value cannot be parsed.
35
+ """
36
+ if not isinstance(hex_str, str):
37
+ return None
38
+ rgb = _hex_to_rgb(hex_str)
39
+ if rgb is None:
40
+ return None
41
+ target = _rgb_to_lab(rgb[0], rgb[1], rgb[2])
42
+ return min(self._lab, key=lambda entry: _delta_e_2000(target, entry[1]))[0]
43
+
44
+ def nearest_material(self, raw: str | None) -> str | None:
45
+ """
46
+ The palette material for a raw material name: an exact match after normalization, else
47
+ a match on the first word ("PLA Matte" -> "PLA"), else None (keep the current material).
48
+ """
49
+ if not isinstance(raw, str) or raw.strip() == "":
50
+ return None
51
+ exact = self._material_norm.get(_normalize_material(raw))
52
+ if exact:
53
+ return exact
54
+ return self._material_norm.get(_normalize_material(raw.strip().split()[0]))