From 7749db16cab5141209e38b9a578e11da37a4237b Mon Sep 17 00:00:00 2001 From: Ivan Azoyan Date: Wed, 7 Oct 2020 16:28:17 +0300 Subject: [PATCH] fix chevrons; update project structure --- geopattern/init.lua | 3 --- geopattern/lib.lua | 27 +++++++++++++++++---------- geopattern/svg.lua | 25 ++++++++++++++++++++++--- main.lua | 29 ++++++++++++++++++++++++++--- 4 files changed, 65 insertions(+), 19 deletions(-) delete mode 100644 geopattern/init.lua diff --git a/geopattern/init.lua b/geopattern/init.lua deleted file mode 100644 index db9da92..0000000 --- a/geopattern/init.lua +++ /dev/null @@ -1,3 +0,0 @@ -local GeoPattern = require "geopattern.lib" - -return GeoPattern \ No newline at end of file diff --git a/geopattern/lib.lua b/geopattern/lib.lua index 949ec2f..fe9a387 100644 --- a/geopattern/lib.lua +++ b/geopattern/lib.lua @@ -3,7 +3,7 @@ local Svg = require "geopattern.svg" local base64 = require "geopattern.base64" local sha1 = require "geopattern.sha1.init" -local PATTERNS = {'octogons', 'overlappingCircles', 'plusSigns', 'xes', 'sineWaves', 'hexagons', 'overlappingRings', +GeoPattern.patterns = {'octogons', 'overlappingCircles', 'plusSigns', 'xes', 'sineWaves', 'hexagons', 'overlappingRings', 'plaid', 'triangles', 'squares', 'concentricCircles', 'diamonds', 'tessellation', 'nestedSquares', 'mosaicSquares', 'chevrons'} @@ -177,9 +177,15 @@ function GeoPattern:new(str, options) } if options then - if options.generator then self.opts.generator = options.generator end - if options.color then self.opts.color = options.color end - if options.baseColor then self.opts.baseColor = options.baseColor end + if options.generator then + self.opts.generator = options.generator + end + if options.color then + self.opts.color = options.color + end + if options.baseColor then + self.opts.baseColor = options.baseColor + end end self.hash = sha1.sha1(str) @@ -215,7 +221,8 @@ function GeoPattern:generateBackground() self.color = rgb self.svg:rect(0, 0, '100%', '100%', { - fill = string.format('rgb(%d, %d, %d)', rgb[1] * 255, rgb[2] * 255, rgb[3] * 255) + fill = string.format('rgb(%d, %d, %d)', math.floor(rgb[1] * 255), math.floor(rgb[2] * 255), + math.floor(rgb[3] * 255)) }) end @@ -244,14 +251,14 @@ function GeoPattern:generatePattern() local generator = self.opts.generator if generator then - if table.indexOf(PATTERNS, generator) < 0 then + if table.indexOf(self.patterns, generator) < 0 then error("Generator doesn't exist", 1) end else - local index = hexVal(self.hash, 20) % #PATTERNS + 1 - generator = PATTERNS[index] + local index = hexVal(self.hash, 20) % #self.patterns + 1 + generator = self.patterns[index] end - + return self[generator](self); end @@ -698,7 +705,7 @@ end local function buildChevronShape(width, height) local e = height * 0.66 return string.format( - '', + '', 0, 0, width / 2, height - e, width / 2, height, 0, e, 0, 0, width / 2, height - e, width, 0, width, e, width / 2, height, width / 2, height - e) end diff --git a/geopattern/svg.lua b/geopattern/svg.lua index d132954..5a02f5e 100644 --- a/geopattern/svg.lua +++ b/geopattern/svg.lua @@ -41,15 +41,34 @@ function Svg:toString() return table.concat({self:header(), self.svg_string, self:closer()}, '') end +local function formattingSymbolAndNumber(number) + if type(number) == "number" then + return { + symbol = "%0.16f", + value = number + } + else + return { + symbol = "%s", + value = number + } + end +end + function Svg:rect(x, y, width, height, args) - local str = string.format('', x, y, width, height, - self:write_args(args)) + local width = formattingSymbolAndNumber(width) + local height = formattingSymbolAndNumber(height) + local template = '' + local str = template:format(x, y, width.value, height.value, self:write_args(args)) self.svg_string = self.svg_string .. str end function Svg:circle(x, y, radius, args) - local str = string.format('', x, y, radius, self:write_args(args)) + local radius = formattingSymbolAndNumber(radius) + local template = '' + local str = template:format(x, y, radius.value, self:write_args(args)) self.svg_string = self.svg_string .. str + end function Svg:path(str, args) diff --git a/main.lua b/main.lua index f03c9ad..f084f71 100644 --- a/main.lua +++ b/main.lua @@ -1,5 +1,28 @@ +local GeoPattern = require "geopattern.lib" -local GeoPattern = require "geopattern" +local pattern1 = GeoPattern:new("GitHub") -- without options +local svg1 = pattern1:toSvg() + +local pattern2 = GeoPattern:new("GitHub", { generator = "concentricCircles" }) +local svg2 = pattern2:toSvg() + +local pattern3 = GeoPattern:new("GitHub", { color = "#00ffff" }) +local base64 = pattern3:toBase64() + +local options = { + generator = "concentricCircles", + color = "#00ffff", + baseColor = "#af39b3" +} +local pattern4 = GeoPattern:new("GitHub", options) -- with all available options +local svg4 = pattern4:toSvg() + +for i = 1, #GeoPattern.patterns do + local pattern = GeoPattern:new("GitHub", { generator = GeoPattern.patterns[i] }) + + pattern:toBase64() + local file = io.open(GeoPattern.patterns[i] .. ".svg", "w") + file:write(pattern:toSvg()) + file:close() +end -local geo = GeoPattern:new("GitHubrahab") -print(geo:toSvg()) \ No newline at end of file