Added Checkerboard

This commit is contained in:
Mario Voigt 2020-08-02 15:22:01 +02:00
parent 3d7469b874
commit 38f9545e31
2 changed files with 141 additions and 0 deletions

View File

@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension">
<_name>Checkerboard</_name>
<id>fablabchemnitz.de.checkerboard</id>
<param name="tab" type="notebook">
<page name="params" _gui-text="Params">
<param name="size" type="string" _gui-text="Cell size">50</param>
<param name="rows" type="int" min="0" max="1000" _gui-text="Rows">10</param>
<param name="cols" type="int" min="0" max="1000" _gui-text="Columns">10</param>
<param name="layer" type="boolean" _gui-text="Create in current layer">true</param>
</page>
<page name="color1" _gui-text="1st color">
<param name="color1" type="color" _gui-text="Color 1">#000000ff</param>
</page>
<page name="color2" _gui-text="2nd color">
<param name="color2" type="color" _gui-text="Color 2">#ffffffff</param>
</page>
<page name="help" _gui-text="Help">
<param name="help_text" type="description">Create a checkerboard
1. On the Params tab, choose the cell size
(size of the constituent squares), number
of rows and columns, and whether to add
the checkerboard to the current layer
(if unchecked, the checkerboard will be
added to the root layer)
2. On the 1st color and 2nd color tabs,
select the colors for the two sets of
squares
3. Click Apply
More information and source code:
https://github.com/jeffkayser/inkscape-checkerboard</param>
</page>
</param>
<effect>
<object-type>all</object-type>
<effects-menu>
<submenu _name="FabLab Chemnitz">
<submenu _name="Shape/Pattern from Generator" />
</submenu>
</effects-menu>
</effect>
<script>
<command reldir="extensions" interpreter="python">fablabchemnitz_checkerboard.py</command>
</script>
</inkscape-extension>

View File

@ -0,0 +1,94 @@
#!/usr/bin/env python3
"""
Inkscape extension to create checkerboard patterns
Copyright (C) 2011 Jeff Kayser
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""
from gettext import gettext as _
import inkex
from lxml import etree
import re
from inkex import Color
def rgbToHex(pickerColor):
longcolor = int(pickerColor)
if longcolor < 0:
longcolor = longcolor & 0xFFFFFFFF
return '#' + format(longcolor >> 8, '06X')
def draw_square(x, y, w, h, color, parent, id_=None):
"""Draw a w*h square at (x, y) having color color
"""
if len(color) == 4:
opacity=color[3]
else:
opacity=1.0
style = {'stroke': 'none', 'stroke-width': '1', 'fill': rgbToHex(color), 'fill-opacity': opacity}
attribs = {'style': str(inkex.Style(style)), 'height': str(h), 'width': str(w), 'x': str(x), 'y': str(y)}
if id_ is not None:
attribs.update({'id': id_})
obj = etree.SubElement(parent, inkex.addNS('rect', 'svg'), attribs)
def draw_grid(x, y, rows, cols, size, color1, color2, parent):
"""Draw a rows*cols checkboard grid at (x, y) with square size of size*size,
with squares having alternating colors color1 and color2
"""
# Group like-colors
group1 = etree.SubElement(parent, 'g', {'id': 'diagonal1'})
group2 = etree.SubElement(parent, 'g', {'id': 'diagonal2'})
for row in range(int(rows)):
for col in range(int(cols)):
alternate = (col + row) % 2 == 0
color = color1 if alternate else color2
group = group1 if alternate else group2
id_ = 'cell{0}x{1}'.format(col, row)
draw_square(x + col * size, y + row * size, size, size, color, group, id_)
class Checkerboard(inkex.Effect):
def __init__(self):
inkex.Effect.__init__(self)
self.arg_parser.add_argument("--tab")
self.arg_parser.add_argument("--color1", type=Color)
self.arg_parser.add_argument("--color2", type=Color)
self.arg_parser.add_argument("--size")
self.arg_parser.add_argument("--rows", type=int)
self.arg_parser.add_argument("--cols", type=int)
self.arg_parser.add_argument("--layer", type=inkex.Boolean)
def effect(self):
if self.svg.get_current_layer() is not None:
group = etree.SubElement(self.svg.get_current_layer(), 'g', {'id': 'checkerboard'})
else:
parent = self.document.getroot()
group = etree.SubElement(parent, 'g', {'id': 'checkerboard'})
rows = self.options.rows
cols = self.options.cols
# Convert to pixels
size = self.svg.unittouu(self.options.size)
color1 = self.options.color1
color2 = self.options.color2
# Center checkerboard within visible viewport
x, y = self.svg.namedview.center[0] - cols * size / 2, self.svg.namedview.center[1] - rows * size / 2
draw_grid(x, y, rows, cols, size, color1, color2, group)
if __name__ == '__main__':
Checkerboard().run()