Skip to content

Commit

Permalink
fix: Conversion from RGB to HSV
Browse files Browse the repository at this point in the history
  • Loading branch information
tlux committed Sep 15, 2019
1 parent 1f65901 commit acc644c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 52 deletions.
15 changes: 8 additions & 7 deletions lib/tint/rgb.ex
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ defmodule Tint.RGB do
alias Tint.HSV

def to_hsv(rgb) do
red_ratio = Decimal.div(rgb.red, 255)
green_ratio = Decimal.div(rgb.green, 255)
blue_ratio = Decimal.div(rgb.blue, 255)
red_ratio = calc_ratio(rgb.red)
green_ratio = calc_ratio(rgb.green)
blue_ratio = calc_ratio(rgb.blue)
rgb_ratios = [red_ratio, green_ratio, blue_ratio]

min_ratio = Enum.reduce(rgb_ratios, &Decimal.min(&1, &2))
Expand All @@ -132,9 +132,12 @@ defmodule Tint.RGB do

hue = calc_hue(ratio_delta, max_ratio, rgb_ratios)
saturation = calc_saturation(ratio_delta, max_ratio)
value = calc_value(max_ratio)

HSV.new(hue, saturation, value)
HSV.new(hue, saturation, max_ratio)
end

defp calc_ratio(value) do
Decimal.div(value, 255)
end

defp calc_hue(ratio_delta, max_ratio, rgb_ratios) do
Expand Down Expand Up @@ -184,7 +187,5 @@ defmodule Tint.RGB do
Decimal.div(ratio_delta, max_ratio)
end
end

defp calc_value(max_ratio), do: Decimal.div(max_ratio, 1)
end
end
45 changes: 22 additions & 23 deletions test/tint/hsv_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -107,29 +107,28 @@ defmodule Tint.HSVTest do

describe "RGB.Convertible.to_rgb/1" do
test "convert to RGB" do
assert RGB.Convertible.to_rgb(HSV.new(0, 0.0, 0.0)) == RGB.new(0, 0, 0)

assert RGB.Convertible.to_rgb(HSV.new(0, 0.0, 1.0)) ==
RGB.new(255, 255, 255)

assert RGB.Convertible.to_rgb(HSV.new(48.0, 1.0, 1.0)) ==
RGB.new(255, 204, 0)

assert RGB.Convertible.to_rgb(
HSV.new(
332.7692307692308,
0.9420289855072463,
0.5411764705882353
)
) == RGB.new(138, 8, 67)

assert RGB.Convertible.to_rgb(
HSV.new(
220.67796610169492,
0.2458333333333333,
0.9411764705882353
)
) == RGB.new(181, 200, 240)
conversions = [
{RGB.new(0, 0, 0), HSV.new(0, 0, 0)},
{RGB.new(255, 255, 255), HSV.new(0, 0, 1)},
{RGB.new(255, 0, 0), HSV.new(0, 1, 1)},
{RGB.new(0, 255, 0), HSV.new(120, 1, 1)},
{RGB.new(0, 0, 255), HSV.new(240, 1, 1)},
{RGB.new(255, 255, 0), HSV.new(60, 1, 1)},
{RGB.new(0, 255, 255), HSV.new(180, 1, 1)},
{RGB.new(255, 0, 255), HSV.new(300, 1, 1)},
{RGB.new(191, 191, 191), HSV.new(0, 0, 0.75)},
{RGB.new(128, 128, 128), HSV.new(0, 0, 0.5)},
{RGB.new(128, 0, 0), HSV.new(0, 1, 0.5)},
{RGB.new(128, 128, 0), HSV.new(60, 1, 0.5)},
{RGB.new(0, 128, 0), HSV.new(120, 1, 0.5)},
{RGB.new(128, 0, 128), HSV.new(300, 1, 0.5)},
{RGB.new(0, 128, 128), HSV.new(180, 1, 0.5)},
{RGB.new(0, 0, 128), HSV.new(240, 1, 0.5)}
]

Enum.each(conversions, fn {rgb, hsv} ->
assert RGB.Convertible.to_rgb(hsv) == rgb
end)
end
end
end
44 changes: 22 additions & 22 deletions test/tint/rgb_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -210,28 +210,28 @@ defmodule Tint.RGBTest do

describe "HSV.Convertible.to_hsv/1" do
test "convert to HSV" do
assert HSV.Convertible.to_hsv(RGB.new(0, 0, 0)) ==
HSV.new(0, 0.0, 0.0)

assert HSV.Convertible.to_hsv(RGB.new(255, 255, 255)) ==
HSV.new(0, 0.0, 1.0)

assert HSV.Convertible.to_hsv(RGB.new(255, 204, 0)) ==
HSV.new(48.0, 1.0, 1.0)

assert HSV.Convertible.to_hsv(RGB.new(138, 8, 67)) ==
HSV.new(
Decimal.new("332.7"),
Decimal.new("0.942"),
Decimal.new("0.541")
)

assert HSV.Convertible.to_hsv(RGB.new(181, 200, 240)) ==
HSV.new(
Decimal.new("220.6"),
Decimal.new("0.245"),
Decimal.new("0.941")
)
conversions = [
{RGB.new(0, 0, 0), HSV.new(0, 0, 0)},
{RGB.new(255, 255, 255), HSV.new(0, 0, 1)},
{RGB.new(255, 0, 0), HSV.new(0, 1, 1)},
{RGB.new(0, 255, 0), HSV.new(120, 1, 1)},
{RGB.new(0, 0, 255), HSV.new(240, 1, 1)},
{RGB.new(255, 255, 0), HSV.new(60, 1, 1)},
{RGB.new(0, 255, 255), HSV.new(180, 1, 1)},
{RGB.new(255, 0, 255), HSV.new(300, 1, 1)},
{RGB.new(191, 191, 191), HSV.new(0, 0, 0.749)},
{RGB.new(128, 128, 128), HSV.new(0, 0, 0.501)},
{RGB.new(128, 0, 0), HSV.new(0, 1, 0.501)},
{RGB.new(128, 128, 0), HSV.new(60, 1, 0.501)},
{RGB.new(0, 128, 0), HSV.new(120, 1, 0.501)},
{RGB.new(128, 0, 128), HSV.new(300, 1, 0.501)},
{RGB.new(0, 128, 128), HSV.new(180, 1, 0.501)},
{RGB.new(0, 0, 128), HSV.new(240, 1, 0.501)}
]

Enum.each(conversions, fn {rgb, hsv} ->
assert HSV.Convertible.to_hsv(rgb) == hsv
end)
end
end
end

0 comments on commit acc644c

Please sign in to comment.