constructelligence commited on
Commit
eb6c564
·
verified ·
1 Parent(s): 3d44c10

Upload test_paint_color.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. test_paint_color.py +194 -0
test_paint_color.py ADDED
@@ -0,0 +1,194 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """Tests for paint_color.py (numpy only, no model). Run: python3 test_paint_color.py"""
3
+ import numpy as np
4
+
5
+ from paint_color import (MATCH_DELTA_E, color_summary, delta_e76, delta_e2000, describe, hex_of,
6
+ lab_to_srgb, match_reference, nearest_named, paint_color_sample, parse_color,
7
+ quad_mask, srgb_to_lab, swatch_image)
8
+
9
+ MAGNOLIA = (241, 235, 214)
10
+ TERRACOTTA = (180, 90, 60)
11
+
12
+ # Sharma, Wu & Dalal (2005) CIEDE2000 test data: (Lab1, Lab2, published ΔE00).
13
+ CIEDE2000_DATA = [
14
+ ((50.0, 2.6772, -79.7751), (50.0, 0.0, -82.7485), 2.0425),
15
+ ((50.0, 3.1571, -77.2803), (50.0, 0.0, -82.7485), 2.8615),
16
+ ((50.0, -1.3802, -84.2814), (50.0, 0.0, -82.7485), 1.0000),
17
+ ((50.0, 2.4900, -0.0010), (50.0, -2.4900, 0.0009), 7.1792),
18
+ ((50.0, 2.4900, -0.0010), (50.0, -2.4900, 0.0011), 7.2195),
19
+ ((50.0, -0.0010, 2.4900), (50.0, 0.0009, -2.4900), 4.8045),
20
+ ((50.0, 2.5000, 0.0000), (73.0, 25.0, -18.0), 27.1492),
21
+ ((60.2574, -34.0099, 36.2677), (60.4626, -34.1751, 39.4387), 1.2644),
22
+ ((22.7233, 20.0904, -46.6940), (23.0331, 14.9730, -42.5619), 2.0373),
23
+ ((2.0776, 0.0795, -1.1350), (0.9033, -0.0636, -0.5514), 0.9082),
24
+ ]
25
+
26
+
27
+ def test_delta_e2000_matches_published_data():
28
+ for first, second, expected in CIEDE2000_DATA:
29
+ got = float(delta_e2000(np.array(first), np.array(second)))
30
+ assert abs(got - expected) < 0.01, f"{first} vs {second}: got {got}, expected {expected}"
31
+
32
+
33
+ def test_delta_e2000_is_symmetric_and_zero_for_identical():
34
+ lab = srgb_to_lab(np.array(MAGNOLIA))
35
+ assert float(delta_e2000(lab, lab)) < 1e-9
36
+ for first, second, _ in CIEDE2000_DATA:
37
+ a, b = np.array(first), np.array(second)
38
+ assert abs(float(delta_e2000(a, b)) - float(delta_e2000(b, a))) < 1e-9
39
+
40
+
41
+ def test_lab_roundtrip():
42
+ for rgb in (MAGNOLIA, TERRACOTTA, (0, 0, 0), (255, 255, 255), (12, 250, 133)):
43
+ back = lab_to_srgb(srgb_to_lab(np.array(rgb)))
44
+ assert np.abs(back.astype(int) - np.array(rgb)).max() <= 2, f"{rgb} -> {back}"
45
+
46
+
47
+ def test_named_colour_of_an_anchor_is_itself():
48
+ for name in ("magnolia", "terracotta", "teal", "charcoal"):
49
+ found, distance = nearest_named(srgb_to_lab(np.array(parse_color(name))))
50
+ assert found == name and distance < 0.5, f"{name} -> {found} ({distance})"
51
+
52
+
53
+ def test_hex_formatting():
54
+ assert hex_of(np.array(MAGNOLIA)) == "#F1EBD6" and hex_of(np.array([0, 0, 0])) == "#000000"
55
+
56
+
57
+ def test_sample_trims_shadow_and_glare():
58
+ wall = np.zeros((60, 80, 3), np.uint8)
59
+ ramp = np.linspace(-60, 60, 80) # shadow one side, glare the other
60
+ wall[:] = np.clip(np.array(MAGNOLIA) + ramp.reshape(1, -1, 1), 0, 255)
61
+ wall[5:15, 30:40] = 255 # a specular hotspot
62
+ sample = paint_color_sample(wall, np.ones((60, 80), bool))
63
+ error = float(delta_e2000(np.asarray(sample["lab"]), srgb_to_lab(np.array(MAGNOLIA))))
64
+ assert error < 4.0, f"trimmed sample drifted {error:.2f} from the true colour"
65
+ # The rollout is saturated at the top, so only the shadow side trims: some
66
+ # clipping happened, and the report still shows how wide the spread is.
67
+ assert 0.05 < sample["clipped_fraction"] < 0.35 and sample["spread_delta_e"] > 5.0
68
+ assert sample["pixels"] == 4800
69
+
70
+
71
+ def test_sample_needs_enough_pixels():
72
+ image = np.full((20, 20, 3), MAGNOLIA, np.uint8)
73
+ mask = np.zeros((20, 20), bool)
74
+ mask[0, 0] = True
75
+ assert paint_color_sample(image, mask) is None
76
+ assert paint_color_sample(image, mask, min_pixels=1)["hex"] == "#F1EBD6"
77
+ assert np.allclose(parse_color("#F1EBD6"), parse_color("magnolia"), atol=1)
78
+
79
+
80
+ def test_sample_returns_honest_spread_for_two_colours():
81
+ image = np.full((40, 100, 3), MAGNOLIA, np.uint8)
82
+ image[:, 50:] = TERRACOTTA
83
+ sample = paint_color_sample(image, np.ones((40, 100), bool))
84
+ # A split coat is a wide spread, not a single confident colour.
85
+ assert sample["spread_delta_e"] > 20.0
86
+
87
+
88
+ def test_tolerance_gates_the_match_verdict():
89
+ reference = "#F1EBD6"
90
+ same = match_reference(srgb_to_lab(np.array(MAGNOLIA)), reference)
91
+ assert same["band"] == "exact" and same["verdict"] == "match" and same["passes"]
92
+ # ΔE00 1.6: a touch-up match by default, failed by a tight tolerance.
93
+ touch_up = match_reference(srgb_to_lab(np.array([248, 240, 220])), reference)
94
+ assert touch_up["band"] == "match" and touch_up["verdict"] == "match"
95
+ tight = match_reference(srgb_to_lab(np.array([248, 240, 220])), reference, tolerance=0.2)
96
+ assert tight["verdict"] == "mismatch" and not tight["passes"] and tight["delta_e"] > 0.2
97
+ # ΔE00 2.5: the same at a glance, not a match.
98
+ near = match_reference(srgb_to_lab(np.array([244, 236, 220])), reference)
99
+ assert near["band"] == "close" and near["verdict"] == "close" and not near["passes"]
100
+ wrong = match_reference(srgb_to_lab(np.array(TERRACOTTA)), reference)
101
+ assert wrong["verdict"] == "mismatch" and wrong["band"] == "different"
102
+ assert wrong["delta_e"] > 30 and wrong["reference"]["name"] == "magnolia"
103
+
104
+
105
+ def test_parse_color_variants_and_errors():
106
+ assert np.allclose(parse_color("#F1EBD6"), MAGNOLIA)
107
+ assert np.allclose(parse_color("#f1ebd6"), MAGNOLIA)
108
+ assert np.allclose(parse_color("#F1EBD6FF"), MAGNOLIA)
109
+ assert np.allclose(parse_color("#fff"), (255, 255, 255))
110
+ assert np.allclose(parse_color("rgb(241, 235, 214)"), MAGNOLIA)
111
+ assert np.allclose(parse_color("241,235,214"), MAGNOLIA)
112
+ assert np.allclose(parse_color("Magnolia"), MAGNOLIA)
113
+ assert np.allclose(parse_color("dusty_pink"), (216, 177, 178))
114
+ for bad in ("", "#12345", "1,2", "not a colour"):
115
+ try:
116
+ parse_color(bad)
117
+ except ValueError:
118
+ continue
119
+ raise AssertionError(f"{bad!r} should not parse")
120
+
121
+
122
+ def test_quad_mask_fills_the_quad():
123
+ # Edges are inclusive, so a 20x10 quad covers 21 x 11 = 231 pixels.
124
+ mask = quad_mask([(10, 10), (30, 10), (30, 20), (10, 20)], (40, 40))
125
+ assert mask[15, 20] and not mask[5, 20] and not mask[35, 20]
126
+ assert int(mask.sum()) == 231
127
+
128
+
129
+ def test_quad_mask_handles_a_clockwise_quad():
130
+ clockwise = quad_mask([(10, 10), (10, 20), (30, 20), (30, 10)], (40, 40))
131
+ assert int(clockwise.sum()) == 231
132
+
133
+
134
+ def test_delta_e76_is_the_plain_distance():
135
+ lab = np.array([50.0, 2.0, 3.0])
136
+ assert abs(float(delta_e76(lab, lab + np.array([3.0, 4.0, 0.0]))) - 5.0) < 1e-9
137
+
138
+
139
+ def test_summary_reports_no_painted_wall():
140
+ image = np.full((30, 30, 3), MAGNOLIA, np.uint8)
141
+ summary = color_summary(image, np.zeros((30, 30), bool), reference="magnolia")
142
+ assert summary["status"] == "no_painted_wall" and summary["dominant"] is None
143
+ assert summary["reference"]["status"] == "no_painted_wall"
144
+
145
+
146
+ def test_summary_names_the_coat_and_the_odd_face():
147
+ image = np.full((60, 200, 3), MAGNOLIA, np.uint8)
148
+ image[:, 100:] = TERRACOTTA
149
+ painted = np.ones((60, 200), bool)
150
+ faces = [{"corners_px": [[0, 0], [99, 0], [99, 59], [0, 59]]},
151
+ {"corners_px": [[100, 0], [199, 0], [199, 59], [100, 59]]}]
152
+ summary = color_summary(image, painted, faces=faces, reference="magnolia")
153
+ assert summary["status"] == "estimated" and summary["dominant"]["name"] == "magnolia"
154
+ assert summary["multi_colour"] and summary["reference"]["verdict"] == "match"
155
+ assert [f["face"] for f in summary["faces"]] == [0, 1]
156
+ assert summary["faces"][0]["matches_dominant"] and not summary["faces"][1]["matches_dominant"]
157
+ assert any("disagree with the wall's main colour" in note for note in summary["notes"])
158
+
159
+
160
+ def test_summary_flags_a_wrong_tin():
161
+ image = np.full((50, 50, 3), TERRACOTTA, np.uint8)
162
+ summary = color_summary(image, np.ones((50, 50), bool), reference="#F1EBD6")
163
+ assert summary["reference"]["status"] == "mismatch" and summary["reference"]["passes"] is False
164
+ assert any("do not continue this coat" in note for note in summary["notes"])
165
+
166
+
167
+ def test_swatch_image_has_one_chip_per_colour():
168
+ image = np.full((40, 40, 3), MAGNOLIA, np.uint8)
169
+ summary = color_summary(image, np.ones((40, 40), bool), reference="magnolia")
170
+ swatch = swatch_image(summary, reference="magnolia")
171
+ # dominant + palette + reference; at least the reference chip must be drawn.
172
+ assert swatch.width >= 240 and swatch.height == 120
173
+
174
+
175
+ def test_describe_reports_rounded_values():
176
+ entry = describe(srgb_to_lab(np.array(MAGNOLIA)))
177
+ assert entry["name"] == "magnolia" and entry["hex"] == "#F1EBD6"
178
+ assert len(entry["lab"]) == 3 and all(isinstance(v, float) for v in entry["lab"])
179
+
180
+
181
+ def test_default_tolerance_is_the_trade_match_limit():
182
+ assert 1.0 <= MATCH_DELTA_E <= 2.5
183
+
184
+
185
+ def main():
186
+ tests = [value for name, value in sorted(globals().items()) if name.startswith("test_")]
187
+ for test in tests:
188
+ test()
189
+ print(f"ok {test.__name__}")
190
+ print(f"{len(tests)} tests passed")
191
+
192
+
193
+ if __name__ == "__main__":
194
+ main()