summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLex Neva <github.com@lexneva.name>2018-04-21 22:05:27 -0400
committerLex Neva <github.com@lexneva.name>2018-04-29 21:28:48 -0400
commit0700817a3b7de96dad7ca51989416cddfb7e65f9 (patch)
treedd25481309e64c29f37800106822761fe5f5c23f
parent0c60da4bb4887bfcd7a30c1a489391de5ded9cb8 (diff)
pick the palette with the most exact matches
-rw-r--r--inkstitch/threads/catalog.py20
1 files changed, 11 insertions, 9 deletions
diff --git a/inkstitch/threads/catalog.py b/inkstitch/threads/catalog.py
index 5bf75f67..80eb9dfe 100644
--- a/inkstitch/threads/catalog.py
+++ b/inkstitch/threads/catalog.py
@@ -30,6 +30,11 @@ class _ThreadCatalog(Sequence):
def __len__(self):
return len(self.palettes)
+ def _num_exact_color_matches(self, palette, threads):
+ """Number of colors in stitch plan with an exact match in this palette."""
+
+ return sum(1 for thread in threads if thread in palette)
+
def match_and_apply_palette(self, stitch_plan):
"""Figure out which color palette was used and set thread names.
@@ -41,16 +46,13 @@ class _ThreadCatalog(Sequence):
"""
threads = [color_block.color for color_block in stitch_plan]
+ palettes_and_matches = [(palette, self._num_exact_color_matches(palette, threads))
+ for palette in self]
+ palette, matches = max(palettes_and_matches, key=lambda item: item[1])
- for palette in self:
- num_matched = sum(1 for thread in threads if thread in palette)
- if num_matched > (0.8 * len(threads)):
- # Pick this one.
- break
- else:
- # This block will only get run if we ran off the end of the loop
- # without breaking. No palette had enough colors that exactly
- # matched, so just do nothing.
+ if matches < 0.8 * len(stitch_plan):
+ # if less than 80% of the colors are an exact match,
+ # don't use this palette
return
for thread in threads: