mirror of
https://github.com/maziggy/bambuddy.git
synced 2026-10-09 15:35:39 +02:00
Rank near-colour matches by how they look, and share one filament type table (#2804)
Three follow-ups to #2804, all bearing on one decision: which spool a print uses when the exact colour is not loaded. Colour ranking is now perceptual. The ranking added in #2804 measured RGB distance, which rates a colour by how far apart the numbers are rather than how far apart they look, and it overweights blue badly enough to invert the answer: against a required #1E4821 green, a purple #38202F is the nearer of two eligible spools by RGB and four times the further once measured properly. Both sides now use CIEDE2000 -- perceptual_color_distance in backend/app/utils/color_utils.py and colorDistance in amsHelpers.ts, kept structurally identical so they can be read side by side. Verified against the Sharma/Wu/Dalal published reference set, all 31 pairs to 1e-4, and the two implementations agree to within 1e-9 across 800 sampled pairs. Eligibility is untouched, still the per-channel RGB box, so this only reorders spools that already qualified. Type matching now agrees between the interface and the scheduler. Bambu firmware treats PA-CF, PA12-CF and PAHT-CF as one material and the scheduler has always matched them accordingly, but the interface compared raw type strings and called that same pairing a mismatch. The badge contradicted what the printer was about to do, and the manual override picker, which groups by canonical type, offered the very spool the badge then rejected. The fifteen comparison sites in useFilamentMapping.ts, useMultiPrinterFilamentMapping.ts and PrinterSelector.tsx now call filamentTypesCompatible. The pipeline pre-flight reads the matcher's table instead of its own copy. That copy had drifted into disagreeing in both directions: it aliased PLA Basic to PLA where the matcher never has, so a run could clear the check and then fail to map its slots, and it lacked the nylon grouping, so it flagged runs the matcher handles without complaint. A check whose job is to predict dispatch is wrong whenever it disagrees with dispatch, whichever way it leans, so it and the scheduler now both read backend/app/utils/filament_types.py. That canonicaliser deliberately does not strip surrounding whitespace. It looks like a free improvement, but it would collapse a junk tray_type to "" just as a 3MF declaring no filament type yields "", and a typeless requirement would start matching a junk-typed tray instead of reporting the slot unmapped. Padded type strings are worth handling on their own terms, with that case addressed. One behaviour change outside the ranking: the pre-flight is stricter for a printer reporting a product name such as "PLA Basic" where the generic material belongs, which it now flags rather than passes. Rare in practice, since the printer reports material and product name in separate fields, and it is the answer the matcher would give. Nothing about which spool a print actually uses changed outside the colour ranking itself. Adds 203 backend and 6 frontend tests. The #2804 tie-break test now uses identical colours: two colours at equal RGB distance are not perceptually tied, which is rather the point.
This commit is contained in:
@@ -230,9 +230,11 @@ class TestCheckEligibility:
|
||||
)
|
||||
src = await library_file_factory()
|
||||
|
||||
# The printer reports the generic material in tray_type and the product
|
||||
# name in tray_sub_brands, so this is the shape a real AMS sends.
|
||||
live_status = {
|
||||
"connected": True,
|
||||
"raw_data": {"ams": [{"tray": [{"tray_type": "PLA Basic", "tray_color": "FFFFFFFF"}]}]},
|
||||
"raw_data": {"ams": [{"tray": [{"tray_type": "PLA", "tray_color": "FFFFFFFF"}]}]},
|
||||
}
|
||||
with patch(
|
||||
"backend.app.api.routes.pipeline_runs._load_printer_status",
|
||||
@@ -248,6 +250,59 @@ class TestCheckEligibility:
|
||||
assert body["issues"] == []
|
||||
assert body["target_printer_name"] == printer.name
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.integration
|
||||
async def test_a_product_name_in_tray_type_is_reported_as_a_mismatch(
|
||||
self,
|
||||
async_client: AsyncClient,
|
||||
db_session,
|
||||
printer_factory,
|
||||
pipeline_factory,
|
||||
library_file_factory,
|
||||
):
|
||||
"""Eligibility answers with the dispatch matcher's type rules, not its own.
|
||||
|
||||
It used to alias "PLA Basic" to "PLA" and pass this; the matcher never
|
||||
did, so the run cleared the pre-flight and then failed to map the slot.
|
||||
Flagging it here is the honest answer even though it is the stricter one.
|
||||
"""
|
||||
from backend.app.models.local_preset import LocalPreset
|
||||
|
||||
preset = LocalPreset(
|
||||
name="My PLA",
|
||||
preset_type="filament",
|
||||
source="manual",
|
||||
setting="{}",
|
||||
filament_type="PLA",
|
||||
default_filament_colour="#FFFFFF",
|
||||
)
|
||||
db_session.add(preset)
|
||||
await db_session.commit()
|
||||
await db_session.refresh(preset)
|
||||
|
||||
printer = await printer_factory()
|
||||
pipeline = await pipeline_factory(
|
||||
target_printer_id=printer.id,
|
||||
filament_presets=[{"source": "local", "id": str(preset.id)}],
|
||||
)
|
||||
src = await library_file_factory()
|
||||
|
||||
live_status = {
|
||||
"connected": True,
|
||||
"raw_data": {"ams": [{"tray": [{"tray_type": "PLA Basic", "tray_color": "FFFFFFFF"}]}]},
|
||||
}
|
||||
with patch(
|
||||
"backend.app.api.routes.pipeline_runs._load_printer_status",
|
||||
new=AsyncMock(return_value=live_status),
|
||||
):
|
||||
resp = await async_client.post(
|
||||
f"/api/v1/slicer-pipelines/{pipeline['id']}/check-eligibility",
|
||||
json={"source_library_file_id": src.id},
|
||||
)
|
||||
assert resp.status_code == 200
|
||||
body = resp.json()
|
||||
assert [i["kind"] for i in body["issues"]] == ["filament_type_mismatch"]
|
||||
|
||||
|
||||
class TestRunPipeline:
|
||||
"""POST /slicer-pipelines/{id}/run orchestrates slice + enqueue."""
|
||||
|
||||
@@ -0,0 +1,130 @@
|
||||
"""The dispatch matcher and the pipeline pre-flight must agree on filament types.
|
||||
|
||||
``pipeline_eligibility`` exists to tell the operator, before a run starts, what
|
||||
the matcher will do when it starts. It used to answer that from its own copy of
|
||||
the equivalence table, and the copies had drifted into disagreeing in both
|
||||
directions:
|
||||
|
||||
- it aliased ``PLA Basic`` to ``PLA`` where the matcher does not, so a job
|
||||
could clear the pre-flight and then fail on type at dispatch;
|
||||
- it lacked the ``PA12-CF``/``PAHT-CF`` grouping the matcher has, so a job the
|
||||
matcher handles fine was flagged as a mismatch.
|
||||
|
||||
Both now read ``backend.app.utils.filament_types``. These tests pin the shared
|
||||
answer and, more importantly, pin that the two modules give the *same* answer —
|
||||
that is the property that broke, and it cannot be caught by testing either side
|
||||
alone.
|
||||
"""
|
||||
|
||||
import pytest
|
||||
|
||||
from backend.app.services.pipeline_eligibility import _canonical as eligibility_canonical
|
||||
from backend.app.services.print_scheduler import canonical_filament_type as scheduler_canonical
|
||||
from backend.app.utils.filament_types import (
|
||||
FILAMENT_TYPE_GROUPS,
|
||||
canonical_filament_type,
|
||||
filament_types_compatible,
|
||||
)
|
||||
|
||||
# Every type either module has ever had an opinion about, plus the shapes the
|
||||
# printer actually emits.
|
||||
VOCABULARY = [
|
||||
"PLA",
|
||||
"PLA Basic",
|
||||
"PLA Matte",
|
||||
"PLA Silk",
|
||||
"PLA Pro",
|
||||
"PLA Tough",
|
||||
"PETG",
|
||||
"PETG HF",
|
||||
"PETG Basic",
|
||||
"PETG Translucent",
|
||||
"ABS",
|
||||
"ASA",
|
||||
"TPU",
|
||||
"TPU 95A",
|
||||
"PC",
|
||||
"PA",
|
||||
"PA-CF",
|
||||
"PA12-CF",
|
||||
"PAHT-CF",
|
||||
"PVA",
|
||||
"",
|
||||
" ", # whitespace-only: must behave identically on both sides
|
||||
"EXOTIC_WOOD",
|
||||
]
|
||||
|
||||
|
||||
class TestSchedulerAndEligibilityAgree:
|
||||
@pytest.mark.parametrize("ftype", VOCABULARY)
|
||||
def test_both_modules_canonicalise_identically(self, ftype):
|
||||
assert scheduler_canonical(ftype) == eligibility_canonical(ftype)
|
||||
|
||||
@pytest.mark.parametrize("a", VOCABULARY)
|
||||
@pytest.mark.parametrize("b", ["PLA", "PA-CF", "PA12-CF", "PETG"])
|
||||
def test_both_modules_agree_on_compatibility(self, a, b):
|
||||
"""The pre-flight must never claim a pairing the matcher would reject,
|
||||
nor flag one the matcher would accept."""
|
||||
assert (scheduler_canonical(a) == scheduler_canonical(b)) == (
|
||||
eligibility_canonical(a) == eligibility_canonical(b)
|
||||
)
|
||||
|
||||
def test_the_two_regressions_that_prompted_this(self):
|
||||
# Used to differ: eligibility aliased the product name, the matcher did not.
|
||||
assert eligibility_canonical("PLA Basic") == scheduler_canonical("PLA Basic")
|
||||
assert not filament_types_compatible("PLA Basic", "PLA")
|
||||
# Used to differ the other way: the matcher grouped the nylons, eligibility did not.
|
||||
assert eligibility_canonical("PA12-CF") == scheduler_canonical("PA12-CF")
|
||||
assert filament_types_compatible("PA12-CF", "PA-CF")
|
||||
|
||||
|
||||
class TestEquivalenceGroups:
|
||||
def test_nylon_variants_are_interchangeable(self):
|
||||
for variant in ("PA-CF", "PA12-CF", "PAHT-CF"):
|
||||
assert filament_types_compatible(variant, "PA-CF"), variant
|
||||
|
||||
def test_carbon_filled_nylon_is_not_plain_nylon(self):
|
||||
"""PA-CF is filled and PA is not; standing one in for the other changes
|
||||
the part."""
|
||||
assert not filament_types_compatible("PA-CF", "PA")
|
||||
|
||||
def test_product_variants_are_not_aliases(self):
|
||||
"""Silk, Matte and Basic print differently even though they dry alike,
|
||||
so the matcher must not substitute one for another."""
|
||||
for variant in ("PLA Silk", "PLA Matte", "PLA Basic", "PLA Tough"):
|
||||
assert not filament_types_compatible(variant, "PLA"), variant
|
||||
|
||||
def test_every_group_canonicalises_to_its_first_entry(self):
|
||||
for group in FILAMENT_TYPE_GROUPS:
|
||||
for member in group:
|
||||
assert canonical_filament_type(member) == group[0].upper()
|
||||
|
||||
|
||||
class TestNormalisation:
|
||||
def test_matching_is_case_insensitive(self):
|
||||
assert filament_types_compatible("pla", "PLA")
|
||||
assert filament_types_compatible("pa12-cf", "PA-CF")
|
||||
|
||||
def test_missing_type_canonicalises_to_empty(self):
|
||||
assert canonical_filament_type(None) == ""
|
||||
assert canonical_filament_type("") == ""
|
||||
|
||||
def test_surrounding_whitespace_is_preserved_on_purpose(self):
|
||||
"""Stripping would be a quiet behaviour change, not a tidy-up.
|
||||
|
||||
A whitespace-only tray_type would collapse to "", and so does the type
|
||||
of a 3MF filament element that declares none — so a typeless
|
||||
requirement would start matching a junk-typed tray instead of reporting
|
||||
the slot unmapped. Both sides of the app agree on the unstripped rule,
|
||||
which is what matters here; padded types are a separate fix.
|
||||
"""
|
||||
assert canonical_filament_type(" PETG ") == " PETG "
|
||||
assert not filament_types_compatible(" PETG ", "PETG")
|
||||
assert not filament_types_compatible(" ", "")
|
||||
|
||||
def test_an_unknown_type_passes_through_uppercased(self):
|
||||
"""Third-party materials still compare against themselves rather than
|
||||
collapsing into one bucket."""
|
||||
assert canonical_filament_type("exotic_wood") == "EXOTIC_WOOD"
|
||||
assert filament_types_compatible("exotic_wood", "EXOTIC_WOOD")
|
||||
assert not filament_types_compatible("EXOTIC_WOOD", "PLA")
|
||||
@@ -6,9 +6,13 @@ so the winner depended on which slot a spool sat in rather than on which colour
|
||||
was closest.
|
||||
|
||||
The worked example throughout is the maintainer's: a required ``#3A7BD5`` with a
|
||||
purple ``#6253AD`` in tray 1 (40/40/40 off — admitted by the box, distance ~69)
|
||||
and a near-identical ``#3B7AD2`` in tray 3 (distance ~3). Both qualify; the
|
||||
purple used to win on position alone.
|
||||
purple ``#6253AD`` in tray 1 (40/40/40 off — admitted by the box) and a
|
||||
near-identical ``#3B7AD2`` in tray 3. Both qualify; the purple used to win on
|
||||
position alone.
|
||||
|
||||
Ranking is by CIEDE2000 delta-E, so the two are ~18.5 and ~0.5 apart rather
|
||||
than the ~69 and ~3 an RGB metric reported. Eligibility is still the per-channel
|
||||
RGB box, unchanged — only the ordering within it is perceptual.
|
||||
"""
|
||||
|
||||
import pytest
|
||||
@@ -16,8 +20,8 @@ import pytest
|
||||
from backend.app.services.print_scheduler import PrintScheduler
|
||||
|
||||
REQUIRED_COLOR = "#3A7BD5"
|
||||
NEAR = "3B7AD2FF" # distance ~3
|
||||
FAR_BUT_ADMITTED = "6253ADFF" # 40/40/40 off — inside the box, distance ~69
|
||||
NEAR = "3B7AD2FF" # dE00 ~0.5
|
||||
FAR_BUT_ADMITTED = "6253ADFF" # 40/40/40 off — inside the box, dE00 ~18.5
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -48,9 +52,19 @@ class TestColorDistance:
|
||||
"""The alpha a slicer writes is not a colour the user chose."""
|
||||
assert scheduler._color_distance("#76D9F4", "76D9F400") == 0
|
||||
|
||||
def test_distance_is_euclidean_not_per_channel(self, scheduler):
|
||||
# 40 off on each channel is sqrt(3 * 40^2) ~= 69.28, not 40.
|
||||
assert scheduler._color_distance("#000000", "#282828") == pytest.approx(69.28, abs=0.01)
|
||||
def test_distance_is_perceptual_not_per_channel(self, scheduler):
|
||||
# A near-match is ranked by how different it looks, not by how far
|
||||
# apart the numbers are. Scale is CIEDE2000 delta-E, where ~1 is a
|
||||
# just-noticeable difference — see test_perceptual_color_distance.py
|
||||
# for the formula's verification against published reference data.
|
||||
assert scheduler._color_distance("#000000", "#282828") == pytest.approx(9.91, abs=0.01)
|
||||
|
||||
def test_a_perceptually_nearer_colour_beats_a_numerically_nearer_one(self, scheduler):
|
||||
# Against a green requirement, a purple is the closer of the two by RGB
|
||||
# distance (49.7 vs 56.9) and much the further once measured
|
||||
# perceptually. Both are inside the tolerance, so ranking alone decides.
|
||||
required = "#1E4821"
|
||||
assert scheduler._color_distance("#43683E", required) < scheduler._color_distance("#38202F", required)
|
||||
|
||||
def test_unusable_input_is_none_rather_than_a_number(self, scheduler):
|
||||
assert scheduler._color_distance(None, "#3A7BD5") is None
|
||||
@@ -82,8 +96,13 @@ class TestNearestSimilarWins:
|
||||
|
||||
def test_ties_keep_the_caller_order_so_prefer_lowest_still_decides(self, scheduler):
|
||||
"""Two spools equally close: the incoming order wins, which is the
|
||||
prefer-lowest sort when that preference is on."""
|
||||
loaded = [tray(2, "3A7BD0FF"), tray(7, "3A7BDAFF")] # both 5 away
|
||||
prefer-lowest sort when that preference is on.
|
||||
|
||||
Same colour in both trays, so the tie is exact. Two *different* colours
|
||||
at equal RGB distance are not perceptually tied — that is the whole
|
||||
point of the metric — so they cannot be used to test this any more.
|
||||
"""
|
||||
loaded = [tray(2, NEAR), tray(7, NEAR)]
|
||||
assert scheduler._match_filaments_to_slots([req()], loaded) == [2]
|
||||
assert scheduler._match_filaments_to_slots([req()], list(reversed(loaded))) == [7]
|
||||
|
||||
|
||||
@@ -0,0 +1,150 @@
|
||||
"""Verification for the CIEDE2000 colour metric used to rank spool matches.
|
||||
|
||||
The matcher ranks the spools its tolerance admits by how far their colour is
|
||||
from the one the file asks for. That ranking is only as trustworthy as the
|
||||
metric, so the formula is pinned against the published reference set rather
|
||||
than against numbers this codebase produced — an implementation that agrees
|
||||
with 31 independently published values is right; one that agrees with its own
|
||||
output is merely consistent.
|
||||
|
||||
``_ciede2000`` is private, and driven directly here on purpose: the reference
|
||||
data is expressed in L*a*b*, so going through the hex entry point would fold
|
||||
the sRGB conversion into what is meant to test the difference formula alone.
|
||||
"""
|
||||
|
||||
import math
|
||||
|
||||
import pytest
|
||||
|
||||
from backend.app.utils.color_utils import _ciede2000, _hex_to_lab, perceptual_color_distance
|
||||
|
||||
# Sharma, Wu & Dalal, "The CIEDE2000 Color-Difference Formula", Table 1.
|
||||
# Pairs 9-12 straddle the hue-angle discontinuity and are what catch a sign
|
||||
# error in the mean-hue branch; pairs 30-31 sit near black where the lightness
|
||||
# weighting dominates.
|
||||
SHARMA_PAIRS = [
|
||||
((50.0000, 2.6772, -79.7751), (50.0000, 0.0000, -82.7485), 2.0425),
|
||||
((50.0000, 3.1571, -77.2803), (50.0000, 0.0000, -82.7485), 2.8615),
|
||||
((50.0000, 2.8361, -74.0200), (50.0000, 0.0000, -82.7485), 3.4412),
|
||||
((50.0000, -1.3802, -84.2814), (50.0000, 0.0000, -82.7485), 1.0000),
|
||||
((50.0000, -1.1848, -84.8006), (50.0000, 0.0000, -82.7485), 1.0000),
|
||||
((50.0000, -0.9009, -85.5211), (50.0000, 0.0000, -82.7485), 1.0000),
|
||||
((50.0000, 0.0000, 0.0000), (50.0000, -1.0000, 2.0000), 2.3669),
|
||||
((50.0000, -1.0000, 2.0000), (50.0000, 0.0000, 0.0000), 2.3669),
|
||||
((50.0000, 2.4900, -0.0010), (50.0000, -2.4900, 0.0009), 7.1792),
|
||||
((50.0000, 2.4900, -0.0010), (50.0000, -2.4900, 0.0010), 7.1792),
|
||||
((50.0000, 2.4900, -0.0010), (50.0000, -2.4900, 0.0011), 7.2195),
|
||||
((50.0000, 2.4900, -0.0010), (50.0000, -2.4900, 0.0012), 7.2195),
|
||||
((50.0000, 2.5000, 0.0000), (50.0000, 0.0000, -2.5000), 4.3065),
|
||||
((50.0000, 2.5000, 0.0000), (73.0000, 25.0000, -18.0000), 27.1492),
|
||||
((50.0000, 2.5000, 0.0000), (61.0000, -5.0000, 29.0000), 22.8977),
|
||||
((50.0000, 2.5000, 0.0000), (56.0000, -27.0000, -3.0000), 31.9030),
|
||||
((50.0000, 2.5000, 0.0000), (58.0000, 24.0000, 15.0000), 19.4535),
|
||||
((50.0000, 2.5000, 0.0000), (50.0000, 3.1736, 0.5854), 1.0000),
|
||||
((50.0000, 2.5000, 0.0000), (50.0000, 3.2972, 0.0000), 1.0000),
|
||||
((50.0000, 2.5000, 0.0000), (50.0000, 1.8634, 0.5757), 1.0000),
|
||||
((50.0000, 2.5000, 0.0000), (50.0000, 3.2592, 0.3350), 1.0000),
|
||||
((60.2574, -34.0099, 36.2677), (60.4626, -34.1751, 39.4387), 1.2644),
|
||||
((63.0109, -31.0961, -5.8663), (62.8187, -29.7946, -4.0864), 1.2630),
|
||||
((61.2901, 3.7196, -5.3901), (61.4292, 2.2480, -4.9620), 1.8731),
|
||||
((35.0831, -44.1164, 3.7933), (35.0232, -40.0716, 1.5901), 1.8645),
|
||||
((22.7233, 20.0904, -46.6940), (23.0331, 14.9730, -42.5619), 2.0373),
|
||||
((36.4612, 47.8580, 18.3852), (36.2715, 50.5065, 21.2231), 1.4146),
|
||||
((90.8027, -2.0831, 1.4410), (91.1528, -1.6435, 0.0447), 1.4441),
|
||||
((90.9257, -0.5406, -0.9208), (88.6381, -0.8985, -0.7239), 1.5381),
|
||||
((6.7747, -0.2908, -2.4247), (5.8714, -0.0985, -2.2286), 0.6377),
|
||||
((2.0776, 0.0795, -1.1350), (0.9033, -0.0636, -0.5514), 0.9082),
|
||||
]
|
||||
|
||||
|
||||
class TestAgainstPublishedReference:
|
||||
@pytest.mark.parametrize(("lab1", "lab2", "expected"), SHARMA_PAIRS)
|
||||
def test_matches_sharma_reference_value(self, lab1, lab2, expected):
|
||||
assert _ciede2000(lab1, lab2) == pytest.approx(expected, abs=1e-4)
|
||||
|
||||
@pytest.mark.parametrize(("lab1", "lab2", "_expected"), SHARMA_PAIRS)
|
||||
def test_is_symmetric(self, lab1, lab2, _expected):
|
||||
"""Which spool is 'first' must not change how far apart two colours are."""
|
||||
assert _ciede2000(lab1, lab2) == pytest.approx(_ciede2000(lab2, lab1), abs=1e-12)
|
||||
|
||||
def test_a_colour_is_zero_from_itself(self):
|
||||
assert _ciede2000((50.0, 2.5, 0.0), (50.0, 2.5, 0.0)) == 0.0
|
||||
|
||||
|
||||
class TestHexEntryPoint:
|
||||
def test_identical_colours_are_zero_apart(self):
|
||||
assert perceptual_color_distance("#3A7BD5", "3A7BD5FF") == 0.0
|
||||
|
||||
def test_alpha_is_ignored_so_a_transparent_filament_matches_itself(self):
|
||||
assert perceptual_color_distance("#76D9F4", "76D9F400") == 0.0
|
||||
|
||||
@pytest.mark.parametrize("bad", [None, "", "#abc", "#zzzzzz", " "])
|
||||
def test_unusable_input_is_none_rather_than_a_number(self, bad):
|
||||
assert perceptual_color_distance(bad, "#3A7BD5") is None
|
||||
assert perceptual_color_distance("#3A7BD5", bad) is None
|
||||
|
||||
def test_black_and_white_are_the_full_lightness_range_apart(self):
|
||||
# L* runs 0..100, and with no chroma difference dE00 reduces to dL/SL.
|
||||
assert perceptual_color_distance("#000000", "#FFFFFF") == pytest.approx(100.0, abs=0.01)
|
||||
|
||||
def test_pure_hues_are_far_apart(self):
|
||||
assert perceptual_color_distance("#FF0000", "#0000FF") > 50
|
||||
|
||||
|
||||
class TestWhyItReplacedRgbDistance:
|
||||
"""RGB distance rates a colour by how far apart the numbers are, which is
|
||||
not how far apart they look. These are the cases that motivated the swap."""
|
||||
|
||||
def test_rgb_would_rank_a_purple_above_a_green_for_a_green_requirement(self):
|
||||
required = "#1E4821" # dark green
|
||||
purple = "#38202F"
|
||||
green = "#43683E"
|
||||
|
||||
# Both sit inside the per-channel tolerance, so both are eligible and
|
||||
# the ranking alone decides which one prints.
|
||||
assert all(
|
||||
abs(int(required[1:][i : i + 2], 16) - int(c[1:][i : i + 2], 16)) <= 40
|
||||
for c in (purple, green)
|
||||
for i in (0, 2, 4)
|
||||
)
|
||||
|
||||
def rgb_distance(a, b):
|
||||
return math.dist(
|
||||
[int(a[1:][i : i + 2], 16) for i in (0, 2, 4)],
|
||||
[int(b[1:][i : i + 2], 16) for i in (0, 2, 4)],
|
||||
)
|
||||
|
||||
# The old metric put the purple nearer...
|
||||
assert rgb_distance(purple, required) < rgb_distance(green, required)
|
||||
# ...and the perceptual one puts the green nearer, by a wide margin.
|
||||
assert perceptual_color_distance(green, required) < perceptual_color_distance(purple, required)
|
||||
|
||||
def test_equal_rgb_distances_are_not_equally_visible(self):
|
||||
"""Five steps of blue either side of the same colour are identical in
|
||||
RGB and measurably different perceptually — which is why the tie-break
|
||||
test uses genuinely identical colours."""
|
||||
required = "#3A7BD5"
|
||||
assert perceptual_color_distance("#3A7BD0", required) != perceptual_color_distance("#3A7BDA", required)
|
||||
|
||||
|
||||
class TestLabConversion:
|
||||
def test_reference_white_maps_to_l100_and_no_chroma(self):
|
||||
# Not exact: the sRGB->XYZ matrix and the D65 white point are each
|
||||
# rounded independently in the standards, so white lands a few parts in
|
||||
# 10^6 off L*=100. Immaterial next to a just-noticeable difference of 1.
|
||||
lab = _hex_to_lab("FFFFFF")
|
||||
assert lab is not None
|
||||
light, a, b = lab
|
||||
assert light == pytest.approx(100.0, abs=1e-4)
|
||||
assert a == pytest.approx(0.0, abs=1e-3)
|
||||
assert b == pytest.approx(0.0, abs=1e-3)
|
||||
|
||||
def test_black_maps_to_the_origin(self):
|
||||
assert _hex_to_lab("000000") == pytest.approx((0.0, 0.0, 0.0), abs=1e-9)
|
||||
|
||||
def test_greys_have_no_chroma(self):
|
||||
for grey in ("404040", "808080", "C0C0C0"):
|
||||
lab = _hex_to_lab(grey)
|
||||
assert lab is not None
|
||||
assert lab[1] == pytest.approx(0.0, abs=1e-3)
|
||||
assert lab[2] == pytest.approx(0.0, abs=1e-3)
|
||||
Reference in New Issue
Block a user