Elixir v1.6.0-rc.0 released

This should work:

defmodule Example do
  defguard is_rgb_integer(integer) when integer in 0..255

  defguard is_rgb_strict(rgb)
           when is_rgb_integer(elem(rgb, 0)) and
                  is_rgb_integer(elem(rgb, 1)) and
                  is_rgb_integer(elem(rgb, 2))

  defguard is_css_alpha(alpha) when alpha >= 0 and alpha <= 1
    
  defguard is_rgba(rgba)
           when tuple_size(rgba) == 4 and is_rgb_strict(rgba) and is_css_alpha(elem(rgba, 3))

  defguard is_rgb(rgba)
           when (tuple_size(rgba) == 3 and is_rgb_strict(rgba)) or
                (tuple_size(rgba) == 4 and is_rgb_strict(rgba))
end
1 Like