Skip to content

Return an ok/error tuple from Image.average/1 and Image.chroma_color/1 - #219

Merged
kipcole9 merged 1 commit into
elixir-image:mainfrom
hlindset:feat/average-ok-tuple
Jul 28, 2026
Merged

Return an ok/error tuple from Image.average/1 and Image.chroma_color/1#219
kipcole9 merged 1 commit into
elixir-image:mainfrom
hlindset:feat/average-ok-tuple

Conversation

@hlindset

Copy link
Copy Markdown
Collaborator

Image.average/1 returned a bare list on success and {:error, reason} on failure, which made it awkward to use in with/case statements, having to guard on is_list/1 or rely on clause ordering instead of matching on an :ok/:error tuple.

  • The declared success type was wider than the actual return: Corrected from Pixel.t() to [number()] (and now wrapped in an :ok tuple)
  • Errors are now wrapped in an Image.Error struct
  • Image.chroma_color/1 now returns {:ok, [number()]} | {:error, Image.Error.t()} since it returns the result of Image.average/1
  • Added Image.chroma_color!/1 as a companion to Image.chroma_color/1
  • Added a test exercising errors in average/1 (using a truncated JPEG)
  • chroma_mask/2 and find_trim/2 now propagate a failed :auto color lookup instead of using the error tuple as a pixel

Breaking changes

Image.average/1 returns {:ok, [number()]} | {:error, Image.Error.t()}

# Before
color = Image.average(image)

# After
{:ok, color} = Image.average(image)
  • Code that guarded on shape (when is_list(color)) should now match {:ok, color} instead
  • Code that relied on clause ordering can now match {:ok, color} and {:error, reason} in any order
  • Errors are now Image.Error structs instead of binary()

Image.chroma_color/1 returns {:ok, [number()]} | {:error, Image.Error.t()}

# Before
color = Image.chroma_color(image)

# After
{:ok, color} = Image.chroma_color(image)

Previously the spec claimed Pixel.t() with no error case, which was inaccurate, as all of flatten/2, crop/5 and average/1 could return an error tuple.

@kipcole9
kipcole9 merged commit 94556ae into elixir-image:main Jul 28, 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.

2 participants