Skip to content

Commit 36986d5

Browse files
authored
Merge pull request #135 from whomwah/colour-extension
Improve to_png `color` and `fill` option handling
2 parents a8196ce + 199719a commit 36986d5

3 files changed

Lines changed: 122 additions & 7 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ The will produce a PNG using the [ChunkyPNG gem](https://github.com/wvanbergen/c
143143
```
144144
Options:
145145
146-
fill - Background ChunkyPNG::Color, defaults to 'white'
147-
color - Foreground ChunkyPNG::Color, defaults to 'black'
146+
fill - Background <ChunkyPNG::Color>, defaults to 'white'. Use [] for multi args
147+
color - Foreground <ChunkyPNG::Color>, defaults to 'black'. Use [] for multi args
148148
149149
When option :file is supplied you can use the following ChunkyPNG constraints:
150150

lib/rqrcode/export/png.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33
require "chunky_png"
44

55
# This class creates PNG files.
6-
# Code from: https://github.com/DCarper/rqrcode
76
module RQRCode
87
module Export
98
module PNG
109
# Render the PNG from the QR Code.
1110
#
1211
# Options:
13-
# fill - Background ChunkyPNG::Color, defaults to 'white'
14-
# color - Foreground ChunkyPNG::Color, defaults to 'black'
12+
# fill - Background ChunkyPNG::Color, defaults to 'white'.
13+
# color - Foreground ChunkyPNG::Color, defaults to 'black'.
1514
#
1615
# When option :file is supplied you can use the following ChunkyPNG constraints
1716
# color_mode - The color mode to use. Use one of the ChunkyPNG::COLOR_* constants.
@@ -62,8 +61,8 @@ def as_png(options = {})
6261

6362
googleis = options.length == 0 || !options[:size].nil?
6463
options = default_img_options.merge(options) # reverse_merge
65-
fill = ChunkyPNG::Color(options[:fill])
66-
color = ChunkyPNG::Color(options[:color])
64+
fill = ChunkyPNG::Color(*(options[:fill].is_a?(Array) ? options[:fill] : [options[:fill]]))
65+
color = ChunkyPNG::Color(*(options[:color].is_a?(Array) ? options[:color] : [options[:color]]))
6766
output_file = options[:file]
6867
module_px_size = nil
6968
border_px = nil

spec/rqrcode/export_png_spec.rb

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,122 @@
2525
RQRCode::QRCode.new("png").as_png
2626
end
2727

28+
context "with various color inputs" do
29+
before :each do
30+
allow(ChunkyPNG).to receive(:Color).and_call_original
31+
expect(mockImage).to receive(:save)
32+
.once
33+
.with("some/path", {bit_depth: 1, color_mode: 0})
34+
end
35+
36+
it "should handle the defaults" do
37+
expect(ChunkyPNG::Image).to receive(:new)
38+
.once
39+
.with(174, 174, 4294967295)
40+
.and_return(mockImage)
41+
42+
RQRCode::QRCode.new("png").as_png(
43+
file: "some/path"
44+
)
45+
46+
expect(ChunkyPNG).to have_received(:Color).with("black").once
47+
expect(ChunkyPNG).to have_received(:Color).with("white").once
48+
end
49+
50+
it "should handle a blue 'color'" do
51+
expect(ChunkyPNG::Image).to receive(:new)
52+
.once
53+
.with(174, 174, 4294967295)
54+
.and_return(mockImage)
55+
56+
RQRCode::QRCode.new("png").as_png(
57+
file: "some/path",
58+
color: "blue"
59+
)
60+
61+
expect(ChunkyPNG).to have_received(:Color).with("blue").once
62+
expect(ChunkyPNG).to have_received(:Color).with("white").once
63+
end
64+
65+
it "should handle a #FC0000 'color'" do
66+
expect(ChunkyPNG::Image).to receive(:new)
67+
.once
68+
.with(174, 174, 4294967295)
69+
.and_return(mockImage)
70+
71+
RQRCode::QRCode.new("png").as_png(
72+
file: "some/path",
73+
color: "#FC0000"
74+
)
75+
76+
expect(ChunkyPNG).to have_received(:Color).with("#FC0000").once
77+
expect(ChunkyPNG).to have_received(:Color).with("white").once
78+
end
79+
80+
it "should handle an rgb 'color'" do
81+
expect(ChunkyPNG::Image).to receive(:new)
82+
.once
83+
.with(174, 174, 4294967295)
84+
.and_return(mockImage)
85+
86+
RQRCode::QRCode.new("png").as_png(
87+
file: "some/path",
88+
color: [0, 0, 0]
89+
)
90+
91+
expect(ChunkyPNG).to have_received(:Color).with(0, 0, 0).once
92+
expect(ChunkyPNG).to have_received(:Color).with("white").once
93+
end
94+
95+
it "should handle a green 'fill'" do
96+
expect(ChunkyPNG::Image).to receive(:new)
97+
.once
98+
.with(174, 174, 8388863)
99+
.and_return(mockImage)
100+
101+
RQRCode::QRCode.new("png").as_png(
102+
file: "some/path",
103+
fill: "green"
104+
)
105+
106+
expect(ChunkyPNG).to have_received(:Color).with("black").once
107+
expect(ChunkyPNG).to have_received(:Color).with("green").once
108+
end
109+
110+
it "should handle an rgb 'fill'" do
111+
expect(ChunkyPNG::Image).to receive(:new)
112+
.once
113+
.with(174, 174, 4294967295)
114+
.and_return(mockImage)
115+
116+
RQRCode::QRCode.new("png").as_png(
117+
file: "some/path",
118+
fill: [255, 255, 255]
119+
)
120+
121+
expect(ChunkyPNG).to have_received(:Color).with("black").once
122+
expect(ChunkyPNG).to have_received(:Color).with(255, 255, 255).once
123+
end
124+
end
125+
126+
it "should not handle nonsense color " do
127+
expect {
128+
RQRCode::QRCode.new("png").as_png(
129+
file: "some/path",
130+
color: "madeupcolor"
131+
)
132+
}.to raise_error(ArgumentError, "Unknown color name madeupcolor!")
133+
end
134+
135+
it "should not handle nonsense fill " do
136+
expect {
137+
RQRCode::QRCode.new("png").as_png(
138+
file: "some/path",
139+
fill: "madeupcolor"
140+
)
141+
}.to raise_error(ArgumentError, "Unknown color name madeupcolor!")
142+
end
143+
28144
context "with file save and constaints" do
29145
it "should export using the correct defaults" do
30146
expect(ChunkyPNG::Image).to receive(:new)

0 commit comments

Comments
 (0)