Skip to content

Commit

Permalink
Validate colors passed in as strings are valid hex
Browse files Browse the repository at this point in the history
  • Loading branch information
tomprats committed Feb 24, 2016
1 parent 53adad1 commit cbf476b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@

## PrawnPDF master branch

### Validate colors passed in as strings must be valid hexadecimal

Colors that were passed with a # would previously be misrepresented. Now
any colors passed in as a string must be valid hexadecimal or they will
raise an error.

(Tom Prats, [#807](https://github.com/prawnpdf/prawn/issues/807), [#869](https://github.com/prawnpdf/prawn/issues/869))

### Added support for PNG images with indexed transparency

Prawn now properly hadles transparency in PNG images with indexed color.
Expand Down
6 changes: 5 additions & 1 deletion lib/prawn/graphics/color.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ def process_color(*color)
def color_type(color)
case color
when String
:RGB
if color =~ /\A[0-F]{6}\z/i
:RGB
else
fail ArgumentError, "Unknown type of color: #{color.inspect}"
end
when Array
case color.length
when 3
Expand Down
2 changes: 1 addition & 1 deletion manual/graphics/color.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
Prawn::ManualBuilder::Example.generate(filename) do
stroke_axis

# Fill with Yellow using RGB
# Fill with Yellow using RGB (Unlink css, there is no leading #)
fill_color "FFFFCC"
fill_polygon [50, 150], [150, 200], [250, 150],
[250, 50], [150, 0], [50, 50]
Expand Down
8 changes: 8 additions & 0 deletions spec/graphics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,14 @@
expect(colors.fill_color).to eq([0.8, 1.0, 0])
end

it "should raise an error for a color with a leading #" do
expect { @pdf.fill_color "#ccff00" }.to raise_error(ArgumentError)
end

it "should raise an error for a color string that isn't a hex" do
expect { @pdf.fill_color "zcff00" }.to raise_error(ArgumentError)
end

it "should reset the colors on each new page if they have been defined" do
@pdf.fill_color "ccff00"

Expand Down

0 comments on commit cbf476b

Please sign in to comment.