Skip to content

Commit 602704f

Browse files
author
Olivier Bonnaure
committed
feat: add cache for hexToRGB
1 parent bfed6a6 commit 602704f

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

.lua/pdfgenerator.lua

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ function PDFGenerator.new(options)
5656
resources = {},
5757
font_metrics = {},
5858
fonts = {},
59+
rgb_colors = {},
5960
last_font = { fontFamily = "Helvetica", fontWeight = "normal" },
6061
current_table = {
6162
current_row = {
@@ -782,6 +783,7 @@ function PDFGenerator:drawLine(x1, y1, x2, y2, width, options)
782783

783784
return self
784785
end
786+
785787
-- Draw circle on current page
786788
-- borderColor and fillColor should be hex color codes (e.g., "000000" for black)
787789
function PDFGenerator:drawCircle(radius, borderWidth, borderStyle, borderColor, fillColor)
@@ -879,6 +881,11 @@ end
879881

880882
-- Convert hex color to RGB values (0-1)
881883
function PDFGenerator:hexToRGB(hex)
884+
self.rgb_colors = self.rgb_colors or {}
885+
if self.rgb_colors[hex] then
886+
return self.rgb_colors[hex]
887+
end
888+
882889
-- Remove '#' if present
883890
hex = hex:gsub("#", "")
884891
-- If hex is 3 characters (shorthand), expand it to 6 characters
@@ -890,7 +897,8 @@ function PDFGenerator:hexToRGB(hex)
890897
local g = tonumber(hex:sub(3, 4), 16) / 255
891898
local b = tonumber(hex:sub(5, 6), 16) / 255
892899

893-
return { r, g, b }
900+
self.rgb_colors[hex] = { r, g, b }
901+
return self.rgb_colors[hex]
894902
end
895903

896904
-- Draw rectangle on current page

0 commit comments

Comments
 (0)