Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse histograms with fractional colors #94

Merged
merged 1 commit into from
Jan 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/mogrify.ex
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@ defmodule Mogrify do
defp clean_histogram_entry({"hex", v}), do: {"hex", v}
defp clean_histogram_entry({"alpha", ""}), do: {"alpha", 255}
defp clean_histogram_entry({k, ""}), do: {k, 0}
defp clean_histogram_entry({k, v}), do: {k, v |> String.to_integer()}
defp clean_histogram_entry({k, v}), do: {k, v |> Float.parse() |> elem(0) |> Float.round(0) |> trunc}

defp extract_histogram_data(entry) do
~r/^\s+(?<count>\d+):\s+\((?<red>[\d\s]+),(?<green>[\d\s]+),(?<blue>[\d\s]+)(,(?<alpha>[\d\s]+))?\)\s+(?<hex>\#[abcdef\d]{6,8})\s+/i
def extract_histogram_data(entry) do
~r/^\s+(?<count>\d+):\s+\((?<red>[\d(?:\.\d+)?)\s]+),(?<green>[\d(?:\.\d+)?)\s]+),(?<blue>[\d(?:\.\d+)?)\s]+)(,(?<alpha>[\d(?:\.\d+)?)\s]+))?\)\s+(?<hex>\#[abcdef\d]{6,8})\s+/i
|> Regex.named_captures(entry)
|> Enum.map(fn {k, v} -> {k, v |> Compat.string_trim()} end)
|> cleanse_histogram
Expand Down
17 changes: 17 additions & 0 deletions test/mogrify_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,23 @@ defmodule MogrifyTest do
}
]

assert hist == expected
end

test ".histogram with fractional rgb values" do
hist = open(@fixture) |> custom("-alpha", "remove")
|> custom("-colors", 8)
|> histogram
expected = [
%{"alpha" => 255, "blue" => 34, "count" => 1976, "green" => 33, "hex" => "#202122", "red" => 32},
%{"alpha" => 255, "blue" => 64, "count" => 2424, "green" => 63, "hex" => "#3E3F40", "red" => 62},
%{"alpha" => 255, "blue" => 103, "count" => 4669, "green" => 101, "hex" => "#656567", "red" => 101},
%{"alpha" => 255, "blue" => 127, "count" => 1319, "green" => 128, "hex" => "#7D807F", "red" => 125},
%{"alpha" => 255, "blue" => 129, "count" => 4915, "green" => 129, "hex" => "#7F8181", "red" => 127},
%{"alpha" => 255, "blue" => 150, "count" => 5913, "green" => 148, "hex" => "#939496", "red" => 147},
%{"alpha" => 255, "blue" => 191, "count" => 8142, "green" => 192, "hex" => "#BDC0BF", "red" => 189},
%{"alpha" => 255, "blue" => 244, "count" => 58242, "green" => 245, "hex" => "#F4F5F4", "red" => 244}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: If you take this test and apply to master, you should get the error I encountered

test .histogram with fractional rgb values (MogrifyTest)
     test/mogrify_test.exs:504
     ** (Protocol.UndefinedError) protocol Enumerable not implemented for nil of type Atom. This protocol is implemented for the following type(s): HashSet, Range, Map, Function, List, Stream, Date.Range, HashDict, GenEvent.Stream, MapSet, File.Stream, IO.Stream
     code: |> histogram

]
assert hist == expected
end

Expand Down