-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from abousquet/background_color
Fix #9 Background color
- Loading branch information
Showing
5 changed files
with
220 additions
and
42 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 |
---|---|---|
|
@@ -23,4 +23,7 @@ erl_crash.dump | |
eqrcode-*.tar | ||
|
||
**/.DS_Store | ||
.elixir_ls | ||
.elixir_ls | ||
|
||
/test/images/ | ||
/test/html/ |
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
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,71 @@ | ||
defmodule EQRCode.PNGTest do | ||
use ExUnit.Case | ||
|
||
defp content, do: "www.google.com" | ||
defp html_path, do: Path.expand("./test/html") | ||
defp image_path, do: Path.expand("./test/images") | ||
|
||
setup do | ||
html_path() |> File.mkdir_p!() | ||
image_path() |> File.mkdir_p!() | ||
qr = content() |> EQRCode.encode() | ||
[qr: qr] | ||
end | ||
|
||
test "Generate an html page with different types of PNG images", %{qr: qr} do | ||
color = <<0, 0, 255>> | ||
background_color = <<255, 255, 0>> | ||
|
||
pngs = | ||
[] | ||
## Test Default | ||
|> build_png(qr, "Default", []) | ||
## Test Foreground Color png | ||
|> build_png(qr, "Foreground", color: color) | ||
## Test Background Color png | ||
|> build_png(qr, "Background", background_color: background_color) | ||
## Test Foreground and Background Color | ||
|> build_png(qr, "Both Colors", background_color: background_color, color: color) | ||
## Test Background Transparency | ||
|> build_png(qr, "Transparent", background_color: :transparent, color: color) | ||
|
||
html = gen_html(pngs) | ||
|
||
html_path() | ||
|> Path.join("png_test.html") | ||
|> File.write(html) | ||
end | ||
|
||
defp write_png_to_file(png_binary, path), do: File.write!(path, png_binary, [:binary]) | ||
|
||
defp build_png(png_list, qr, label, opts) do | ||
png_path = image_path() |> Path.join(Macro.underscore(label) <> ".png") | ||
|
||
EQRCode.png(qr, opts) | ||
|> write_png_to_file(png_path) | ||
|
||
png_list ++ [{label, png_path}] | ||
end | ||
|
||
defp gen_html(kw_png) when is_list(kw_png) do | ||
png = | ||
Enum.map(kw_png, fn {key, value} -> | ||
~s{<p><strong>#{key}:</strong></p><image src="#{value}">} | ||
end) | ||
|
||
""" | ||
<!DOCTYPE html> | ||
<html xmlns="http://www.w3.org/1999/xhtml"> | ||
<body> | ||
<head> | ||
<title>EQRCode PNG Image Tests</title> | ||
<style> | ||
body { background: rgba(0,255,0); } | ||
</style> | ||
</head> | ||
#{png} | ||
</body> | ||
<html> | ||
""" | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
defmodule EQRCode.SVGTest do | ||
use ExUnit.Case | ||
|
||
defp content, do: "www.google.com" | ||
defp html_path, do: Path.expand("./test/html") | ||
|
||
setup do | ||
html_path() |> File.mkdir_p!() | ||
qr = content() |> EQRCode.encode() | ||
[qr: qr] | ||
end | ||
|
||
test "Generate an html page with different types of SVG images", %{qr: qr} do | ||
color = "blue" | ||
background_color = "yellow" | ||
|
||
svgs = | ||
[] | ||
## Test default option | ||
|> build_svgs(qr, "Default", []) | ||
## Test foreground color being set | ||
|> build_svgs(qr, "Foreground", color: color) | ||
## Test background being set | ||
|> build_svgs(qr, "Background", background_color: background_color) | ||
## Test both background and foreground color being set | ||
|> build_svgs(qr, "Both Colors", background_color: background_color, color: color) | ||
## Test transparency in HTML | ||
|> build_svgs(qr, "Transparent", background_color: :transparent, color: color) | ||
## Test ViewBox | ||
|> build_svgs(qr, "ViewBox", background_color: background_color, color: color, viewbox: true) | ||
## Test transparent viewbox in HTML | ||
|> build_svgs(qr, "Transparent ViewBox", | ||
background_color: :transparent, | ||
color: color, | ||
viewbox: true | ||
) | ||
|
||
html = gen_html(svgs) | ||
|
||
html_path() | ||
|> Path.join("svg_test.html") | ||
|> File.write(html) | ||
end | ||
|
||
defp build_svgs(svgs, qr, label, opts) do | ||
svg = EQRCode.svg(qr, opts) | ||
svg_c = EQRCode.svg(qr, [shape: "circle"] ++ opts) | ||
svgs ++ [{label, svg <> svg_c}] | ||
end | ||
|
||
defp gen_html(kw_svg) when is_list(kw_svg) do | ||
svg = Enum.map(kw_svg, fn {key, value} -> "<p><strong>#{key}:</strong></p>" <> value end) | ||
|
||
""" | ||
<!DOCTYPE html> | ||
<html xmlns="http://www.w3.org/1999/xhtml"> | ||
<body> | ||
<head> | ||
<title>EQRCode PNG Image Tests</title> | ||
<style> | ||
body { background: rgba(0,255,0); } | ||
</style> | ||
</head> | ||
#{svg} | ||
</body> | ||
<html> | ||
""" | ||
end | ||
end |