-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
191 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
defmodule Tint.Sigil do | ||
@moduledoc """ | ||
A module providing a sigil to build colors in a certain colorspace. | ||
""" | ||
|
||
alias Tint.HSV | ||
alias Tint.RGB | ||
|
||
@separator "," | ||
|
||
@doc """ | ||
A sigil to build a color. | ||
""" | ||
@spec sigil_K(String.t(), [char]) :: Tint.color() | ||
def sigil_K(str, []) do | ||
RGB.from_hex!(str) | ||
end | ||
|
||
def sigil_K(str, [?r]) do | ||
apply(RGB, :new, extract_args(str, 3)) | ||
end | ||
|
||
def sigil_K(str, [?h]) do | ||
apply(HSV, :new, extract_args(str, 3)) | ||
end | ||
|
||
defp extract_args(str, expected_count) do | ||
args = | ||
str | ||
|> String.split(@separator) | ||
|> Enum.map(&String.trim/1) | ||
|
||
args_count = length(args) | ||
|
||
if args_count != expected_count do | ||
raise ArgumentError, | ||
"Invalid number of args: #{args_count} " <> | ||
"(expected #{expected_count})" | ||
end | ||
|
||
args | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
defmodule Tint.SigilTest do | ||
use ExUnit.Case, async: true | ||
|
||
import Tint.Sigil | ||
|
||
alias Tint.HSV | ||
alias Tint.OutOfRangeError | ||
alias Tint.RGB | ||
|
||
describe "hex code" do | ||
test "success" do | ||
assert ~K[#FFCC00] == RGB.from_hex!("#FFCC00") | ||
end | ||
|
||
test "error" do | ||
assert_raise ArgumentError, "Invalid hex code: invalid", fn -> | ||
~K[invalid] | ||
end | ||
end | ||
end | ||
|
||
describe "RGB color" do | ||
test "success" do | ||
assert ~K(123,45,67)r == RGB.new(123, 45, 67) | ||
assert ~K(123, 45, 67)r == RGB.new(123, 45, 67) | ||
end | ||
|
||
test "error when value out of range" do | ||
assert_raise OutOfRangeError, "Value 256 is out of range [0,255]", fn -> | ||
~K(256,128,0)r | ||
end | ||
end | ||
|
||
test "raise when invalid number of args" do | ||
assert_raise ArgumentError, | ||
"Invalid number of args: 5 (expected 3)", | ||
fn -> | ||
~K(255, 0,, 255,)r | ||
end | ||
|
||
assert_raise ArgumentError, | ||
"Invalid number of args: 2 (expected 3)", | ||
fn -> | ||
~K(255,0)r | ||
end | ||
end | ||
end | ||
|
||
describe "HSV color" do | ||
test "success" do | ||
assert ~K(123,0.45,0.67)h == HSV.new(123, 0.45, 0.67) | ||
assert ~K(123, 0.45, 0.67)h == HSV.new(123, 0.45, 0.67) | ||
end | ||
|
||
test "error when value out of range" do | ||
assert_raise OutOfRangeError, "Value 361 is out of range [0,360)", fn -> | ||
~K(361,0,1)h | ||
end | ||
end | ||
|
||
test "raise when invalid number of args" do | ||
assert_raise ArgumentError, | ||
"Invalid number of args: 5 (expected 3)", | ||
fn -> | ||
~K(255, 0,, 1,)h | ||
end | ||
|
||
assert_raise ArgumentError, | ||
"Invalid number of args: 2 (expected 3)", | ||
fn -> | ||
~K(255,1)h | ||
end | ||
end | ||
end | ||
end |