Skip to content

Commit 972d38d

Browse files
authored
Merge pull request #196 from akoutmos/main
Fixing RGB to hex conversion
2 parents e9f6826 + 2480133 commit 972d38d

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

lib/image/color.ex

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,11 @@ defmodule Image.Color do
173173
iex> Image.Color.rgb_to_hex(:green)
174174
{:ok, "#008000"}
175175
176-
iex> Image.Color.rgb_to_hex([10,20,30])
177-
{:ok, "#A141E"}
176+
iex> Image.Color.rgb_to_hex([10, 20, 30])
177+
{:ok, "#0A141E"}
178+
179+
iex> Image.Color.rgb_to_hex([12, 12, 13])
180+
{:ok, "#0C0C0D"}
178181
179182
"""
180183
def rgb_to_hex(color) do
@@ -219,12 +222,10 @@ defmodule Image.Color do
219222
"#" <> to_hex(r) <> to_hex(g) <> to_hex(b)
220223
end
221224

222-
defp to_hex(i) when i >= 10 do
223-
Integer.to_string(i, 16)
224-
end
225-
226225
defp to_hex(i) do
227-
"0" <> Integer.to_string(i, 16)
226+
i
227+
|> Integer.to_string(16)
228+
|> String.pad_leading(2, "0")
228229
end
229230

230231
@doc """

0 commit comments

Comments
 (0)