Skip to content

Commit

Permalink
Improve to_png color and fill option handling
Browse files Browse the repository at this point in the history
  • Loading branch information
whomwah committed May 12, 2023
1 parent a8196ce commit 5572ec7
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 5 deletions.
9 changes: 4 additions & 5 deletions lib/rqrcode/export/png.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
require "chunky_png"

# This class creates PNG files.
# Code from: https://github.com/DCarper/rqrcode
module RQRCode
module Export
module PNG
# Render the PNG from the QR Code.
#
# Options:
# fill - Background ChunkyPNG::Color, defaults to 'white'
# color - Foreground ChunkyPNG::Color, defaults to 'black'
# fill - Background ChunkyPNG::Color, defaults to 'white'. Use [] for multi options
# color - Foreground ChunkyPNG::Color, defaults to 'black'. Use [] for multi options
#
# When option :file is supplied you can use the following ChunkyPNG constraints
# color_mode - The color mode to use. Use one of the ChunkyPNG::COLOR_* constants.
Expand Down Expand Up @@ -62,8 +61,8 @@ def as_png(options = {})

googleis = options.length == 0 || !options[:size].nil?
options = default_img_options.merge(options) # reverse_merge
fill = ChunkyPNG::Color(options[:fill])
color = ChunkyPNG::Color(options[:color])
fill = ChunkyPNG::Color(*(options[:fill].is_a?(Array) ? options[:fill] : [options[:fill]]))
color = ChunkyPNG::Color(*(options[:color].is_a?(Array) ? options[:color] : [options[:color]]))
output_file = options[:file]
module_px_size = nil
border_px = nil
Expand Down
116 changes: 116 additions & 0 deletions spec/rqrcode/export_png_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,122 @@
RQRCode::QRCode.new("png").as_png
end

context "with various color inputs" do
before :each do
allow(ChunkyPNG).to receive(:Color).and_call_original
expect(mockImage).to receive(:save)
.once
.with("some/path", {bit_depth: 1, color_mode: 0})
end

it "should handle the defaults" do
expect(ChunkyPNG::Image).to receive(:new)
.once
.with(174, 174, 4294967295)
.and_return(mockImage)

RQRCode::QRCode.new("png").as_png(
file: "some/path"
)

expect(ChunkyPNG).to have_received(:Color).with("black").once
expect(ChunkyPNG).to have_received(:Color).with("white").once
end

it "should handle a blue 'color'" do
expect(ChunkyPNG::Image).to receive(:new)
.once
.with(174, 174, 4294967295)
.and_return(mockImage)

RQRCode::QRCode.new("png").as_png(
file: "some/path",
color: "blue"
)

expect(ChunkyPNG).to have_received(:Color).with("blue").once
expect(ChunkyPNG).to have_received(:Color).with("white").once
end

it "should handle a #FC0000 'color'" do
expect(ChunkyPNG::Image).to receive(:new)
.once
.with(174, 174, 4294967295)
.and_return(mockImage)

RQRCode::QRCode.new("png").as_png(
file: "some/path",
color: "#FC0000"
)

expect(ChunkyPNG).to have_received(:Color).with("#FC0000").once
expect(ChunkyPNG).to have_received(:Color).with("white").once
end

it "should handle an rgb 'color'" do
expect(ChunkyPNG::Image).to receive(:new)
.once
.with(174, 174, 4294967295)
.and_return(mockImage)

RQRCode::QRCode.new("png").as_png(
file: "some/path",
color: [0, 0, 0]
)

expect(ChunkyPNG).to have_received(:Color).with(0, 0, 0).once
expect(ChunkyPNG).to have_received(:Color).with("white").once
end

it "should handle a green 'fill'" do
expect(ChunkyPNG::Image).to receive(:new)
.once
.with(174, 174, 8388863)
.and_return(mockImage)

RQRCode::QRCode.new("png").as_png(
file: "some/path",
fill: "green"
)

expect(ChunkyPNG).to have_received(:Color).with("black").once
expect(ChunkyPNG).to have_received(:Color).with("green").once
end

it "should handle an rgb 'fill'" do
expect(ChunkyPNG::Image).to receive(:new)
.once
.with(174, 174, 4294967295)
.and_return(mockImage)

RQRCode::QRCode.new("png").as_png(
file: "some/path",
fill: [255, 255, 255]
)

expect(ChunkyPNG).to have_received(:Color).with("black").once
expect(ChunkyPNG).to have_received(:Color).with(255, 255, 255).once
end
end

it "should not handle nonsense color " do
expect {
RQRCode::QRCode.new("png").as_png(
file: "some/path",
color: "madeupcolor"
)
}.to raise_error(ArgumentError, "Unknown color name madeupcolor!")
end

it "should not handle nonsense fill " do
expect {
RQRCode::QRCode.new("png").as_png(
file: "some/path",
fill: "madeupcolor"
)
}.to raise_error(ArgumentError, "Unknown color name madeupcolor!")
end

context "with file save and constaints" do
it "should export using the correct defaults" do
expect(ChunkyPNG::Image).to receive(:new)
Expand Down

0 comments on commit 5572ec7

Please sign in to comment.