alafr / SVG-to-PDFKit

Insert SVG into a PDF document created with PDFKit
MIT License
397 stars 111 forks source link

Add a support for parsing `hsl()` and `hsla()` color formats. #188

Open petrkotek opened 2 months ago

petrkotek commented 2 months ago

Fixes https://github.com/alafr/SVG-to-PDFKit/issues/187.

Note that I verified the functionality by temporarily moving the parseColor function out and running a jest test:

test("rgb()", () => {
  const result = parseColor("rgb(10,20,30)", null);

  expect(result).toEqual([[10, 20, 30], 1]);
});

test("rgba()", () => {
  const result = parseColor("rgba(10,20,30,0.3)", null);

  expect(result).toEqual([[10, 20, 30], 0.3]);
});

test("hsl()", () => {
  const result = parseColor("hsl(122, 57%, 35%)", null);

  expect(result).toEqual([[38, 140, 42], 1]);
});

test("hsla()", () => {
  const result = parseColor("hsla(122, 57%, 35%, 0.4)", null);

  expect(result).toEqual([[38, 140, 42], 0.4]);
});

I would have loved to add tests, but looks like there are none at this stage and the code does't seem to be easily testable without refactoring, so better left for a separate pull request.

Testing SVG -> PDF BEFORE (missing colors for some elements):

image

Testing SVG -> PDF AFTER (colors are correct):

image
petrkotek commented 1 month ago

gentle ping @alafr; would you please consider this contribution? Thanks!