Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise error for colors with leading # #869

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a few characters that we probably don't want to match between '9' and 'A'. I'd prefer this regex to be something more specific. For example /\A[0-9a-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 (Unlike 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