test_nearest_colour_match_2804.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. """Regression tests for slot-order-dependent colour matching (#2804).
  2. When no spool matches a required colour exactly, the matcher falls back to
  3. spools inside a per-channel tolerance. It took the first of those in tray order,
  4. so the winner depended on which slot a spool sat in rather than on which colour
  5. was closest.
  6. The worked example throughout is the maintainer's: a required ``#3A7BD5`` with a
  7. purple ``#6253AD`` in tray 1 (40/40/40 off — admitted by the box) and a
  8. near-identical ``#3B7AD2`` in tray 3. Both qualify; the purple used to win on
  9. position alone.
  10. Ranking is by CIEDE2000 delta-E, so the two are ~18.5 and ~0.5 apart rather
  11. than the ~69 and ~3 an RGB metric reported. Eligibility is still the per-channel
  12. RGB box, unchanged — only the ordering within it is perceptual.
  13. """
  14. import pytest
  15. from backend.app.services.print_scheduler import PrintScheduler
  16. REQUIRED_COLOR = "#3A7BD5"
  17. NEAR = "3B7AD2FF" # dE00 ~0.5
  18. FAR_BUT_ADMITTED = "6253ADFF" # 40/40/40 off — inside the box, dE00 ~18.5
  19. @pytest.fixture
  20. def scheduler():
  21. return PrintScheduler.__new__(PrintScheduler)
  22. def tray(global_tray_id, color, type_="PETG"):
  23. return {
  24. "global_tray_id": global_tray_id,
  25. "type": type_,
  26. "color": color,
  27. "tray_info_idx": "",
  28. "extruder_id": 0,
  29. "remain": 100,
  30. }
  31. def req(color=REQUIRED_COLOR, slot_id=1, type_="PETG", tray_info_idx=""):
  32. return {"slot_id": slot_id, "type": type_, "color": color, "tray_info_idx": tray_info_idx}
  33. class TestColorDistance:
  34. def test_identical_colours_are_zero_apart(self, scheduler):
  35. assert scheduler._color_distance("#3A7BD5", "3A7BD5FF") == 0
  36. def test_alpha_is_ignored_so_a_transparent_filament_matches_itself(self, scheduler):
  37. """The alpha a slicer writes is not a colour the user chose."""
  38. assert scheduler._color_distance("#76D9F4", "76D9F400") == 0
  39. def test_distance_is_perceptual_not_per_channel(self, scheduler):
  40. # A near-match is ranked by how different it looks, not by how far
  41. # apart the numbers are. Scale is CIEDE2000 delta-E, where ~1 is a
  42. # just-noticeable difference — see test_perceptual_color_distance.py
  43. # for the formula's verification against published reference data.
  44. assert scheduler._color_distance("#000000", "#282828") == pytest.approx(9.91, abs=0.01)
  45. def test_a_perceptually_nearer_colour_beats_a_numerically_nearer_one(self, scheduler):
  46. # Against a green requirement, a purple is the closer of the two by RGB
  47. # distance (49.7 vs 56.9) and much the further once measured
  48. # perceptually. Both are inside the tolerance, so ranking alone decides.
  49. required = "#1E4821"
  50. assert scheduler._color_distance("#43683E", required) < scheduler._color_distance("#38202F", required)
  51. def test_unusable_input_is_none_rather_than_a_number(self, scheduler):
  52. assert scheduler._color_distance(None, "#3A7BD5") is None
  53. assert scheduler._color_distance("", "#3A7BD5") is None
  54. assert scheduler._color_distance("#abc", "#3A7BD5") is None
  55. assert scheduler._color_distance("#zzzzzz", "#3A7BD5") is None
  56. class TestNearestSimilarWins:
  57. def test_closest_admitted_colour_wins_regardless_of_slot_order(self, scheduler):
  58. loaded = [tray(1, FAR_BUT_ADMITTED), tray(3, NEAR)]
  59. assert scheduler._match_filaments_to_slots([req()], loaded) == [3]
  60. def test_result_does_not_depend_on_tray_order(self, scheduler):
  61. """The bug in one line: reversing the AMS used to reverse the answer."""
  62. forward = scheduler._match_filaments_to_slots([req()], [tray(1, FAR_BUT_ADMITTED), tray(3, NEAR)])
  63. reversed_ = scheduler._match_filaments_to_slots([req()], [tray(3, NEAR), tray(1, FAR_BUT_ADMITTED)])
  64. assert forward == reversed_ == [3]
  65. def test_an_exact_match_still_outranks_a_near_one(self, scheduler):
  66. loaded = [tray(1, NEAR), tray(2, "3A7BD5FF")]
  67. assert scheduler._match_filaments_to_slots([req()], loaded) == [2]
  68. def test_eligibility_is_unchanged_so_a_far_colour_is_still_type_only(self, scheduler):
  69. """Ranking must not admit spools the tolerance excluded: 41 off on one
  70. channel fails the box and can only win as a type-only fallback."""
  71. loaded = [tray(5, "3A7BFEFF")] # blue channel 41 away
  72. assert scheduler._match_filaments_to_slots([req()], loaded) == [5]
  73. def test_ties_keep_the_caller_order_so_prefer_lowest_still_decides(self, scheduler):
  74. """Two spools equally close: the incoming order wins, which is the
  75. prefer-lowest sort when that preference is on.
  76. Same colour in both trays, so the tie is exact. Two *different* colours
  77. at equal RGB distance are not perceptually tied — that is the whole
  78. point of the metric — so they cannot be used to test this any more.
  79. """
  80. loaded = [tray(2, NEAR), tray(7, NEAR)]
  81. assert scheduler._match_filaments_to_slots([req()], loaded) == [2]
  82. assert scheduler._match_filaments_to_slots([req()], list(reversed(loaded))) == [7]
  83. def test_nearest_applies_within_a_shared_tray_info_idx_too(self, scheduler):
  84. """The tray_info_idx subset walks its own colour comparison."""
  85. loaded = [
  86. tray(1, FAR_BUT_ADMITTED) | {"tray_info_idx": "GFG99"},
  87. tray(3, NEAR) | {"tray_info_idx": "GFG99"},
  88. ]
  89. assert scheduler._match_filaments_to_slots([req(tray_info_idx="GFG99")], loaded) == [3]
  90. def test_type_is_still_a_hard_filter(self, scheduler):
  91. """A perfect colour in the wrong material never wins."""
  92. loaded = [tray(1, "3A7BD5FF", type_="ASA"), tray(3, NEAR)]
  93. assert scheduler._match_filaments_to_slots([req()], loaded) == [3]
  94. def test_each_slot_consumes_its_tray(self, scheduler):
  95. """Two slots wanting the same colour take different spools, nearest first."""
  96. loaded = [tray(1, FAR_BUT_ADMITTED), tray(3, NEAR)]
  97. mapping = scheduler._match_filaments_to_slots([req(slot_id=1), req(slot_id=2)], loaded)
  98. assert mapping == [3, 1]