Skip to content

Commit

Permalink
Add vibrance options
Browse files Browse the repository at this point in the history
  • Loading branch information
kipcole9 committed Jul 17, 2024
1 parent b824cea commit 619716d
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 16 deletions.
32 changes: 16 additions & 16 deletions lib/image.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8656,22 +8656,22 @@ defmodule Image do
def vibrance(%Vimage{} = image, vibrance, options \\ []) when is_multiplier(vibrance) do
use Image.Math

threshold = Keyword.get(options, :threshold, 100)

identity = Operation.identity!()
h2 = 255
h3 = 6 * identity / h2
h4 = 2 / (1 + exp!(-h3)) - 1
h5 = h3 / 6
h6 = h4 - h5
h7 = vibrance
h8 = h5 + h7 * h6

with_colorspace(image, :lch, fn image ->
g5 = h2 * image[1] / threshold
g7 = threshold * Operation.maplut!(g5, h8)
Image.join_bands([image[0], g7, image[2]])
end)
with {:ok, options} <- Options.Vibrance.validate_options(options) do
identity = Operation.identity!()
h2 = 255
h3 = 6 * identity / h2
h4 = 2 / (1 + exp!(-h3)) - 1
h5 = h3 / 6
h6 = h4 - h5
h7 = vibrance
h8 = h5 + h7 * h6

with_colorspace(image, :lch, fn image ->
g5 = h2 * image[1] / options.threshold
g7 = options.threshold * Operation.maplut!(g5, h8)
Image.join_bands([image[0], g7, image[2]])
end)
end
end

@doc """
Expand Down
66 changes: 66 additions & 0 deletions lib/image/options/vibrance.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
defmodule Image.Options.Vibrance do
@moduledoc """
Options and option validation for `Image.vibrance/2`.
"""

@typedoc """
Options applicable to Image.vibrance/3
"""
@type vibrance_option :: {:threshold, pos_integer()}

@typedoc """
Options list for Image.vibrance/3
"""
@type vibrance_options :: [vibrance_option()] | map()

@default_vibrance_threshold 70

@doc false
def default_vibrance_threshold do
@default_vibrance_threshold
end

@doc """
Validate the options for `Image.vibrance/3`.
See `t:Image.Options.Vibrance.vibrance_options/0`.
"""
def validate_options(options) when is_list(options) do
options = Keyword.merge(default_options(), options)

case Enum.reduce_while(options, options, &validate_option(&1, &2)) do
{:error, value} ->
{:error, value}

options ->
{:ok, Map.new(options)}
end
end

def validate_options(%{} = options) do
{:ok, options}
end

defp validate_option({:threshold, threshold}, options)
when is_integer(threshold) and threshold in 1..100//1 do
{:cont, options}
end

defp validate_option(option, _options) do
{:halt, {:error, invalid_option(option)}}
end

defp invalid_option(option) do
"Invalid option or option value: #{inspect(option)}"
end

defp default_options do
[
threshold: @default_vibrance_threshold
]
end
end

0 comments on commit 619716d

Please sign in to comment.