Skip to content

Make conflicting chroma key strategies an error + remove inert options - #224

Merged
hlindset merged 3 commits into
elixir-image:mainfrom
hlindset:fix/chroma-key-options
Aug 4, 2026
Merged

Make conflicting chroma key strategies an error + remove inert options#224
hlindset merged 3 commits into
elixir-image:mainfrom
hlindset:fix/chroma-key-options

Conversation

@hlindset

@hlindset hlindset commented Aug 3, 2026

Copy link
Copy Markdown
Collaborator

Image.Options.ChromaKey has two mutually exclusive strategies, but nothing enforced it.

  • select_strategy/1 matched on the merged options map, and the color range clause is defined before the threshold clause, so the color range options always won when both were present.
  • because default_options/0 merged [color: :auto, threshold: 20], supplying only one of :greater_than and :less_than fell through to the threshold clause instead, leaving the final clause unreachable.
Image.Options.ChromaKey.validate_options(img,
  color: [0, 255, 0], threshold: 30, greater_than: [0, 200, 0], less_than: [50, 255, 50])
# {:ok, %{greater_than: [0, 200, 0], less_than: [50, 255, 50]}} ← :color and :threshold dropped

Image.Options.ChromaKey.validate_options(img, greater_than: [0, 200, 0])
# {:ok, %{threshold: 20, color: :auto}} ← range discarded, falls back to defaults

Image.chroma_key(img, sigma: 2.0, min_amplitude: 0.05)
# {:ok, image} ← validated, but never used for anything

Changes

  • Strategy is selected from the keys the caller supplied, before defaults are merged
  • The validated map carries an explicit :strategy key, which do_chroma_mask/2 dispatches on (instead of map key presence)
  • Removed :sigma and :min_amplitude. They were never read by anything, and were leftovers from copying Image.Options.Blur (in 1326801)
  • Both new errors are %Image.Error{reason: :invalid_option}. The unreachable clause originally returned a bare binary
  • Fixed the :greater_than and :less_than docs (which were inverted). The mask covers the range between the bounds, so :greater_than is the lower one

Breaking changes

Affects Image.chroma_mask/2, Image.chroma_key/2, Image.replace_color/2 and their bang variants.

  • Supplying options from both strategies returns an error. Previously the range won and :color and :threshold were silently dropped
  • Supplying only one of :greater_than / :less_than returns an error. Previously the supplied option was silently discarded and auto chroma detection ran instead
  • :sigma and :min_amplitude are rejected. They had no effect, but were accepted

@hlindset
hlindset merged commit 8609288 into elixir-image:main Aug 4, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant