Skip to content

Commit

Permalink
fix chevrons; update project structure
Browse files Browse the repository at this point in the history
  • Loading branch information
azoyan committed Oct 7, 2020
1 parent 9e37733 commit 7749db1
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 19 deletions.
3 changes: 0 additions & 3 deletions geopattern/init.lua

This file was deleted.

27 changes: 17 additions & 10 deletions geopattern/lib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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'}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -698,7 +705,7 @@ end
local function buildChevronShape(width, height)
local e = height * 0.66
return string.format(
'<polyline point="%f, %f, %f, %f, %f, %f, %f, %f, %f, %f"></polyline><polyline point="%f, %f, %f, %f, %f, %f, %f, %f, %f, %f"></polyline>',
'<polyline points="%f, %f, %f, %f, %f, %f, %f, %f, %f, %f"></polyline><polyline points="%f, %f, %f, %f, %f, %f, %f, %f, %f, %f"></polyline>',
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
Expand Down
25 changes: 22 additions & 3 deletions geopattern/svg.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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('<rect x="%d" y="%d" width="%s" height="%s" %s/>', x, y, width, height,
self:write_args(args))
local width = formattingSymbolAndNumber(width)
local height = formattingSymbolAndNumber(height)
local template = '<rect x="%f" y="%f" width="' .. width.symbol .. '" height="' .. height.symbol .. '" %s/>'
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('<circle cx="%d" cy="%d" r="%d" %s/>', x, y, radius, self:write_args(args))
local radius = formattingSymbolAndNumber(radius)
local template = '<circle cx="%f" cy="%f" r="' .. radius.symbol .. '" %s/>'
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)
Expand Down
29 changes: 26 additions & 3 deletions main.lua
Original file line number Diff line number Diff line change
@@ -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())

0 comments on commit 7749db1

Please sign in to comment.