@square-logic.html


001| '''Square Logic diagrams from Lewis Carroll's Symbolic Logic, Part 1.
001| 
001| Classes:
001|     FourSquare      Abstract base class with utility functions.
001|     FourSquare1     Basic Four-Square diagram maker.
001|     FourSquare2     Inner/Outer Four-Square diagram maker.
001| 
001| Mask Protocal:
001|     0000.0000:0000.0000:0000.0000:0000.1111   0000.000F inner/only red markers
001|     0000.0000:0000.0000:0000.0000:1111.0000   0000.00F0 inner/only grey markers
001|     0000.0000:0000.0000:0000.1111:0000.0000   0000.0F00 outer red markers
001|     0000.0000:0000.0000:1111.0000:0000.0000   0000.F000 outer grey markers
001|     0000.0000:0000.1111:0000.0000:0000.0000   000F.0000 inner/only red double markers
001|     0000.0000:1111.0000:0000.0000:0000.0000   00F0.0000 inner/only grey double markers
001|     0000.1111:0000.0000:0000.0000:0000.0000   0F00.0000 outer red double markers
001|     1111.0000:0000.0000:0000.0000:0000.0000   F000.0000 outer grey double markers
001| 
001| Commands:
001|     b1          do_b1_diagram           Make a basic four-square diagram
001|     b2          do_b2_diagram           Make an inner/outer four-square diagram
001| 
001|     ms          do_parse_marker_string  Parse marker string & make diagram
001|     bg1         do_b1_background        Make an diagram with background color
001|     bg2         do_b2_background        Make an diagram with background color
001| 
001|     b1.all      do_b1_all_markers       Test: All markers on (basic)
001|     b2.all      do_b2_all_marker        Test: All markers on (inner/outer)
001|     test        test                    Test
001| 
001| '''

002| ####################################################################################################
003| from sys import argv, exc_info
004| from traceback import extract_tb
005| from os import path
006| from PIL import Image, ImageDraw, ImageFont
007| from logger import logger,info,debug,trace, LogFilenameTimestamp
008| from goodies import KWARG
009| ####################################################################################################
010| BasePath = r'C:\CJS\prj\Python\blog\proj'
011| AnimPath = r'C:\CJS\prj\pov\ws\xyz\ani'
012| ImgsPath = path.join(BasePath, 'img')
013| Filename = 'logic-%s.png'
014| Log = logger('Logic')
015| 
016| CMND = ''
017| ARGS = {
018|     #'mask': str(0xf0f0),
019|     #'mode': 'goul, gour,goll,rolr, giul,riur,rill,gilr',
020|     #'task': str(2),
021|     #'ttl': 'Stoplight logic.\nAll states marked!',
022|     #'row': 'Green',
023|     #'col': 'Red',
024|     #'mid': 'Yellow',
025|     #'fname': Filename % 'x',
026|     'fpath': BasePath,
027| }
028| 
029| ##------------------------------------------------------------------------------------------------##
030| '''format key: <color> [<in-out>] <pos-1> <pos-2>
030| 
030|     <color>     r=red, g=grey
030|     <in-out>    i=inner, o=outer (optional, default is inner)
030|     <pos-1>     u=upper, l=lower, c=center
030|     <pos-2>     l=left, r=right, t=top, b=bottom
030| '''

031| MarkerKeys = {
032|     'rul' :0x00000001, 'rur' :0x00000002, 'rlr' :0x00000004, 'rll' :0x00000008,
033|     'riul':0x00000001, 'riur':0x00000002, 'rilr':0x00000004, 'rill':0x00000008,
034|     'roul':0x00000100, 'rour':0x00000200, 'rolr':0x00000400, 'roll':0x00000800,
035|     'rct' :0x00010000, 'rcr' :0x00020000, 'rcb' :0x00040000, 'rcl' :0x00080000,
036|     'rict':0x00010000, 'ricr':0x00020000, 'ricb':0x00040000, 'ricl':0x00080000,
037|     'roct':0x00100000, 'rocr':0x00200000, 'rocb':0x00400000, 'rocl':0x00800000,
038| 
039|     'gul' :0x00000010, 'gur' :0x00000020, 'glr' :0x00000040, 'gll' :0x00000080,
040|     'giul':0x00000010, 'giur':0x00000020, 'gilr':0x00000040, 'gill':0x00000080,
041|     'goul':0x00001000, 'gour':0x00002000, 'golr':0x00004000, 'goll':0x00008000,
042|     'gct' :0x01000000, 'gcr' :0x02000000, 'gcb' :0x04000000, 'gcl' :0x08000000,
043|     'gict':0x01000000, 'gicr':0x02000000, 'gicb':0x04000000, 'gicl':0x08000000,
044|     'goct':0x10000000, 'gocr':0x20000000, 'gocb':0x40000000, 'gocl':0x80000000,
045| }
046| 
047| ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
048| class FourSquare:
049|     '''Four-Square base class and utility functions.'''
050|     def __init__ (self, dims, limit,bias, title, rowname,colname, mask, bgcolor=(255,255,255)):
051|         self.title = title.replace('|','\n')
052|         self.rowname0 = rowname
053|         self.rowname1 = '~%s' % rowname
054|         self.colname0 = colname
055|         self.colname1 = '~%s' % colname
056|         self.mask = mask
057|         self.dims = dims
058|         self.lims  = ((-limit,+limit), (-limit,+limit))
059|         self.bias  = (+bias, -bias)
060|         self.frame = (self.lims[0][1]-self.lims[0][0], self.lims[1][1]-self.lims[1][0])
061|         self.scale = (self.dims[0]//self.frame[0], self.dims[1]//self.frame[1])
062|         self.im = Image.new('RGB', self.dims, color=bgcolor)
063|         self.gi = ImageDraw.Draw(self.im)
064|         self.fnt0 = None    # MUST be populated by subclass!
065|         self.fnt1 = None    # MUST be populated by subclass!
066|         self.box_width = 11
067|         self.box_color = (48, 48, 48)
068|         self.border_color = (0, 0, 64)
069|         self.marker_color1 = (255,  0,  0)
070|         self.marker_color0 = (127,127,127)
071| 
072|     def px (self, x):
073|         '''Convert virtual X to pixels (with bias).'''
074|         return (((x+self.bias[0])*self.scale[0]) + (self.dims[0]//2))
075| 
076|     def py (self, y):
077|         '''Convert virtual Y to pixels (with bias).'''
078|         return ((self.dims[1]//2) - ((y+self.bias[1])*self.scale[1]))
079| 
080|     def save (self, fname, fpath=None):
081|         '''Save the image file.'''
082|         filename = path.join(fpath,fname) if fpath else fname
083|         self.im.save(filename)
084|         print('wrote: %s' % filename)
085|         Log.info('wrote: %s' % filename)
086|         Log.info()
087| 
088|     def draw_main_box (self):
089|         '''Draw the four-square box.'''
090|         # Frame...
091|         ulc = (self.px(-1), self.py(+1))
092|         urc = (self.px(+1), self.py(+1))
093|         lrc = (self.px(+1), self.py(-1))
094|         llc = (self.px(-1), self.py(-1))
095|         corners = [ulc, urc, lrc, llc, ulc, urc]
096|         self.gi.line(corners, fill=self.box_color, width=self.box_width, joint='curve')
097|         # Horizontal bar...
098|         lc = (self.px(-1), self.py(0))
099|         rc = (self.px(+1), self.py(0))
100|         self.gi.line([lc, rc], fill=self.box_color, width=self.box_width)
101|         # Vertical bar...
102|         tc = (self.px(0), self.py(+1))
103|         bc = (self.px(0), self.py(-1))
104|         self.gi.line([tc, bc], fill=self.box_color, width=self.box_width)
105| 
106|     def draw_inner_box (self):
107|         '''Draw the inner four-square box.'''
108|         ulc = (self.px(-0.6), self.py(+0.6))
109|         urc = (self.px(+0.6), self.py(+0.6))
110|         lrc = (self.px(+0.6), self.py(-0.6))
111|         llc = (self.px(-0.6), self.py(-0.6))
112|         corners = [ulc, urc, lrc, llc, ulc, urc]
113|         self.gi.line(corners, fill=self.box_color, width=self.box_width, joint='curve')
114| 
115|     def draw_background_color (self, ulc, lrc, color):
116|         self.gi.rectangle([ulc,lrc], fill=color, outline=None, width=1)
117| 
118|     def draw_diagram_border (self):
119|         '''Draw a nice "3D" border around the diagram.'''
120|         border_width = 5
121|         edge  = (self.dims[0]-1, self.dims[1]-1)
122|         border_lines = [(0,0), (edge[0],0), (edge[0],edge[1]), (0,edge[1]), (0,0)]
123|         border_shade = self.border_color
124|         shift_templt = [(+1,+1), (-1,+1), (-1,-1), (+1,-1), (+1,+1)]
125| 
126|         # Draw border as series of one-line boxes shading color...
127|         for ix in range(border_width):
128|             self.gi.line(border_lines, fill=border_shade, width=1)
129|             border_lines = [(b[0]+s[0], b[1]+s[1]) for b,s in zip(border_lines,shift_templt)]
130|             border_shade = tuple([c+32 for c in border_shade])
131| 
132|     def draw_main_title (self):
133|         '''Draw the main title for diagram at top and centered.'''
134|         PX = lambda x: ((x*self.scale[0]) + (self.dims[0]//2)) # virtual X to pixels (no bias)
135|         PY = lambda y: ((self.dims[1]//2) - (y*self.scale[1])) # virtual Y to pixels (no bias)
136| 
137|         bb = self.gi.textbbox((0,0), self.title, font=self.fnt1)
138|         ix,iy = (PX(0), PY(self.lims[1][1]-0.05))
139|         self.gi.text([ix-((bb[2]-bb[0])//2), iy], self.title, font=self.fnt1, fill=(0,0,0), spacing=20, align='center')
140| 
141|     def draw_row_label (self, x,y, text, fill=(0,0,0)):
142|         '''Draw the row labels.'''
143|         bb = self.gi.textbbox((0,0), text, font=self.fnt0)
144|         ix,iy = (self.px(x), self.py(y))
145|         self.gi.text([ix-(bb[2]-bb[0]), iy-((bb[3]-bb[1])//2)], text, font=self.fnt0, fill=fill, stroke_fill=(0,0,0), stroke_width=1)
146| 
147|     def draw_col_label (self, x,y, text, fill=(0,0,0)):
148|         '''Draw the column labels.'''
149|         bb = self.gi.textbbox((0,0), text, font=self.fnt0)
150|         ix,iy = (self.px(x), self.py(y))
151|         self.gi.text([ix-((bb[2]-bb[0])//2), iy-(bb[3]-bb[1])], text, font=self.fnt0, fill=fill, stroke_fill=(0,0,0), stroke_width=1)
152| 
153|     def draw_mid_label (self, x,y, text, fill=(0,0,0)):
154|         bb = self.gi.textbbox((0,0), text, font=self.fnt0)
155|         ix,iy = (self.px(x), self.py(y))
156|         self.gi.text([ix-(bb[2]-bb[0]), iy-(bb[3]-bb[1])], text, font=self.fnt0, fill=fill, stroke_fill=(0,0,0), stroke_width=1)
157| 
158|     def draw_marker (self, point, radius, color, outline=(0,0,0), width=3):
159|         cul = lambda p,r:(p[0]-r, p[1]-r)   # circle offset ULC from center
160|         clr = lambda p,r:(p[0]+r, p[1]+r)   # circle offset LRC from center
161|         loc = (cul(point,radius), clr(point,radius))
162|         self.gi.ellipse(loc, fill=color, outline=outline, width=width)
163| 
164| ##================================================================================================##
165| class FourSquare1 (FourSquare):
166|     '''Basic Four-Square diagram maker.'''
167|     def __init__ (self, title, rowname, colname, mask, dims=(800,800), limit=1.40, bias=0.25):
168|         super().__init__(dims, limit,bias, title, rowname,colname, mask)
169|         self.fnt0 = ImageFont.truetype('corbelb.ttf', size=55)
170|         self.fnt1 = ImageFont.truetype('corbeli.ttf', size=72)
171|         self.row_color0 = (  0,127,127)
172|         self.row_color1 = ( 31,127,127)
173|         self.col_color0 = (127,127,  0)
174|         self.col_color1 = (127,127, 31)
175| 
176|     def make (self):
177|         '''Draw the diagram.'''
178|         test = lambda m: (self.mask & m)    # test for drawing a marker
179|         cul = lambda p,r:(p[0]-r, p[1]-r)   # circle offset ULC from center
180|         clr = lambda p,r:(p[0]+r, p[1]+r)   # circle offset LRC from center
181| 
182|         # Draw Title...
183|         self.draw_main_title()
184| 
185|         # Draw Labels...
186|         self.draw_row_label(-1.05, +0.50, self.rowname0, fill=self.row_color0)
187|         self.draw_row_label(-1.05, -0.50, self.rowname1, fill=self.row_color1)
188|         self.draw_col_label(-0.50, +1.06, self.colname0, fill=self.col_color0)
189|         self.draw_col_label(+0.50, +1.06, self.colname1, fill=self.col_color1)
190| 
191|         # Main box...
192|         self.draw_main_box()
193| 
194|         # Draw any requested markers...
195|         ul = (self.px(-0.5),self.py(+0.5))
196|         ur = (self.px(+0.5),self.py(+0.5))
197|         lr = (self.px(+0.5),self.py(-0.5))
198|         ll = (self.px(-0.5),self.py(-0.5))
199|         r = 0.25 * self.scale[0]
200| 
201|         test(0x01) and self.draw_marker(ul, r, self.marker_color1)
202|         test(0x02) and self.draw_marker(ur, r, self.marker_color1)
203|         test(0x04) and self.draw_marker(lr, r, self.marker_color1)
204|         test(0x08) and self.draw_marker(ll, r, self.marker_color1)
205|         test(0x10) and self.draw_marker(ul, r, self.marker_color0)
206|         test(0x20) and self.draw_marker(ur, r, self.marker_color0)
207|         test(0x40) and self.draw_marker(lr, r, self.marker_color0)
208|         test(0x80) and self.draw_marker(ll, r, self.marker_color0)
209| 
210|         # Draw any requested double markers...
211|         uc = (self.px(0.0), self.py(+0.5))
212|         rc = (self.px(+0.5), self.py(0.0))
213|         dc = (self.px(0.0), self.py(-0.5))
214|         lc = (self.px(-0.5), self.py(0.0))
215| 
216|         test(0x010000) and self.draw_marker(uc, r, self.marker_color1)
217|         test(0x020000) and self.draw_marker(rc, r, self.marker_color1)
218|         test(0x040000) and self.draw_marker(dc, r, self.marker_color1)
219|         test(0x080000) and self.draw_marker(lc, r, self.marker_color1)
220|         test(0x100000) and self.draw_marker(uc, r, self.marker_color0)
221|         test(0x200000) and self.draw_marker(rc, r, self.marker_color0)
222|         test(0x400000) and self.draw_marker(dc, r, self.marker_color0)
223|         test(0x800000) and self.draw_marker(lc, r, self.marker_color0)
224| 
225|         # Border...
226|         self.draw_diagram_border()
227| 
228| ##================================================================================================##
229| class FourSquare2 (FourSquare):
230|     '''Inner/Outer Four-Square diagram maker.'''
231|     def __init__ (self, title, rowname, colname, midname, mask, dims=(800,800), limit=1.50, bias=0.33):
232|         super().__init__(dims, limit,bias, title, rowname,colname, mask)
233|         self.midname = midname
234|         self.fnt0 = ImageFont.truetype('corbelb.ttf', size=60)
235|         self.fnt1 = ImageFont.truetype('corbeli.ttf', size=64)
236|         self.bias = (+0.35, -0.40)
237|         self.row_color0 = (  0,127,127)
238|         self.row_color1 = ( 31,127,127)
239|         self.col_color0 = (127,127,  0)
240|         self.col_color1 = (127,127, 31)
241|         self.mid_color0 = (127,  0,127)
242| 
243|     def make (self):
244|         '''Draw the diagram.'''
245|         test = lambda m: (self.mask & m)    # test for drawing a marker
246|         cul = lambda p,r:(p[0]-r, p[1]-r)   # circle offset ULC from center
247|         clr = lambda p,r:(p[0]+r, p[1]+r)   # circle offset LRC from center
248| 
249|         # Draw Title...
250|         self.draw_main_title()
251| 
252|         # Draw Labels...
253|         self.draw_row_label(-1.05, +0.50, self.rowname0, fill=self.row_color0)
254|         self.draw_row_label(-1.05, -0.50, self.rowname1, fill=self.row_color1)
255|         self.draw_col_label(-0.50, +1.06, self.colname0, fill=self.col_color0)
256|         self.draw_col_label(+0.50, +1.06, self.colname1, fill=self.col_color1)
257|         self.draw_mid_label(-1.10, +1.10, self.midname , fill=self.mid_color0)
258| 
259|         # Main box...
260|         self.draw_main_box()
261|         # Inner box...
262|         self.draw_inner_box()
263| 
264|         # Draw any requested inner markers...
265|         ul = (self.px(-0.30), self.py(+0.30))
266|         ur = (self.px(+0.30), self.py(+0.30))
267|         lr = (self.px(+0.30), self.py(-0.30))
268|         ll = (self.px(-0.30), self.py(-0.30))
269|         r = 0.18 * self.scale[0]
270| 
271|         test(0x01) and self.draw_marker(ul, r, self.marker_color1)
272|         test(0x02) and self.draw_marker(ur, r, self.marker_color1)
273|         test(0x04) and self.draw_marker(lr, r, self.marker_color1)
274|         test(0x08) and self.draw_marker(ll, r, self.marker_color1)
275|         test(0x10) and self.draw_marker(ul, r, self.marker_color0)
276|         test(0x20) and self.draw_marker(ur, r, self.marker_color0)
277|         test(0x40) and self.draw_marker(lr, r, self.marker_color0)
278|         test(0x80) and self.draw_marker(ll, r, self.marker_color0)
279| 
280|         # Draw any requested inner double markers...
281|         uc = (self.px( 0.00), self.py(+0.33))
282|         rc = (self.px(+0.33), self.py( 0.00))
283|         dc = (self.px( 0.00), self.py(-0.33))
284|         lc = (self.px(-0.33), self.py( 0.00))
285| 
286|         test(0x010000) and self.draw_marker(uc, r, self.marker_color1)
287|         test(0x020000) and self.draw_marker(rc, r, self.marker_color1)
288|         test(0x040000) and self.draw_marker(dc, r, self.marker_color1)
289|         test(0x080000) and self.draw_marker(lc, r, self.marker_color1)
290|         test(0x100000) and self.draw_marker(uc, r, self.marker_color0)
291|         test(0x200000) and self.draw_marker(rc, r, self.marker_color0)
292|         test(0x400000) and self.draw_marker(dc, r, self.marker_color0)
293|         test(0x800000) and self.draw_marker(lc, r, self.marker_color0)
294| 
295|         # Draw any requested outer markers...
296|         ul = (self.px(-0.75),self.py(+0.75))
297|         ur = (self.px(+0.75),self.py(+0.75))
298|         lr = (self.px(+0.75),self.py(-0.75))
299|         ll = (self.px(-0.75),self.py(-0.75))
300|         r = 0.17 * self.scale[0]
301| 
302|         test(0x0100) and self.draw_marker(ul, r, self.marker_color1)
303|         test(0x0200) and self.draw_marker(ur, r, self.marker_color1)
304|         test(0x0400) and self.draw_marker(lr, r, self.marker_color1)
305|         test(0x0800) and self.draw_marker(ll, r, self.marker_color1)
306|         test(0x1000) and self.draw_marker(ul, r, self.marker_color0)
307|         test(0x2000) and self.draw_marker(ur, r, self.marker_color0)
308|         test(0x4000) and self.draw_marker(lr, r, self.marker_color0)
309|         test(0x8000) and self.draw_marker(ll, r, self.marker_color0)
310| 
311|         # Draw any requested outer double markers...
312|         uc = (self.px( 0.00), self.py(+0.80))
313|         rc = (self.px(+0.80), self.py( 0.00))
314|         dc = (self.px( 0.00), self.py(-0.80))
315|         lc = (self.px(-0.80), self.py( 0.00))
316| 
317|         test(0x01000000) and self.draw_marker(uc, r, self.marker_color1)
318|         test(0x02000000) and self.draw_marker(rc, r, self.marker_color1)
319|         test(0x04000000) and self.draw_marker(dc, r, self.marker_color1)
320|         test(0x08000000) and self.draw_marker(lc, r, self.marker_color1)
321|         test(0x10000000) and self.draw_marker(uc, r, self.marker_color0)
322|         test(0x20000000) and self.draw_marker(rc, r, self.marker_color0)
323|         test(0x40000000) and self.draw_marker(dc, r, self.marker_color0)
324|         test(0x80000000) and self.draw_marker(lc, r, self.marker_color0)
325| 
326|         # Border...
327|         self.draw_diagram_border()
328| 
329| 
330| ##================================================================================================##
331| def parse_marker_string (markers:str):
332|     '''Parse string of comma-delimited marker keys. Returns mask value.'''
333|     # Split (and clean) the keys into a list...
334|     ts = [s.strip() for s in markers.split(',')]
335|     # Add each key to the output total...
336|     out = 0
337|     for tx,t in enumerate(ts):
338|         v = MarkerKeys[t]
339|         Log.debug('marker #%d: %s = %d' % (1+tx,t,v))
340|         out += v
341|     Log.debug()
342|     # Return the total...
343|     return out
344| 
345| ##================================================================================================##
346| def do_parse_marker_string (**kwargs):
347|     """CMND: Parse a marker string and print the mask value."""
348|     fpath = KWARG('fpath', ImgsPath, str, kwargs)
349|     fname = KWARG('fname', 'logic.png', str, kwargs)
350|     mode = KWARG('mode', '', str, kwargs)
351|     task = KWARG('task', 0, int, kwargs)
352|     title = KWARG('ttl', 'Parsed Marker String', str, kwargs)
353|     rownm = KWARG('row', 'This', str, kwargs)
354|     colnm = KWARG('col', 'That', str, kwargs)
355|     midnm = KWARG('mid', 'Other', str, kwargs)
356|     Log.info('parse-marker-string:: %s' % str(list(kwargs.keys())))
357|     Log.debug('fpath="%s"' % fpath)
358|     Log.debug('fname="%s"' % fname)
359|     Log.debug('mode="%s"' % mode)
360|     Log.debug('task=%d' % task)
361|     Log.debug('title="%s"' % title)
362|     Log.debug('rowname="%s"' % rownm)
363|     Log.debug('colname="%s"' % colnm)
364|     Log.debug('midname="%s"' % midnm)
365|     Log.info()
366| 
367|     # Parse the marker string...
368|     mask = parse_marker_string(mode)
369|     outs = 'Mask=%d (%04x::%s)' % (mask, mask, bin(mask)[2:].zfill(32))
370|     print(outs)
371|     print()
372|     Log.info(outs)
373|     Log.info()
374| 
375|     # Create an image using the parsed value for the mask...
376|     if task == 1:
377|         sqr = FourSquare1(title, rownm,colnm, mask)
378|         sqr.make()
379|         sqr.save(fname, fpath=fpath)
380| 
381|     if task == 2:
382|         ttl = title if '\n' in title else '%s\nmask=%d (%x)' % (title,mask,mask)
383|         sqr = FourSquare2(ttl, rownm,colnm,midnm, mask)
384|         sqr.make()
385|         sqr.save(fname, fpath=fpath)
386| 
387|     Log.info()
388|     return 'Done'
389| ##================================================================================================##
390| def do_b1_diagram (**kwargs):
391|     """CMND: Create the basic four-square box diagram."""
392|     fpath = KWARG('fpath', ImgsPath, str, kwargs)
393|     fname = KWARG('fname', 'logic.png', str, kwargs)
394|     mask = KWARG('mask', 0, int, kwargs)
395|     title = KWARG('ttl', 'This and/or That', str, kwargs)
396|     rownm = KWARG('row', 'This', str, kwargs)
397|     colnm = KWARG('col', 'That', str, kwargs)
398|     mode = KWARG('mode', '', str, kwargs)
399|     Log.info('b1-diagram:: %s' % str(list(kwargs.keys())))
400|     Log.debug('fpath="%s"' % fpath)
401|     Log.debug('fname="%s"' % fname)
402|     Log.debug('mask=%08X::%s' % (mask,bin(mask)[2:]))
403|     Log.debug('title="%s"' % title)
404|     Log.debug('rowname="%s"' % rownm)
405|     Log.debug('colname="%s"' % colnm)
406|     Log.debug('mode="%s"' % mode)
407|     Log.info()
408|     #
409|     sqr = FourSquare1(title, rownm,colnm, mask)
410|     if mode == 'stoplight':
411|         sqr.row_color0 = (255,  0,  0)
412|         sqr.row_color1 = (127,  0,  0)
413|         sqr.col_color0 = (  0,255,  0)
414|         sqr.col_color1 = (  0,127,  0)
415| 
416|     sqr.make()
417|     sqr.save(fname, fpath=fpath)
418|     #
419|     Log.info('dims=%s' % str(sqr.dims))
420|     Log.info('lims=%s' % str(sqr.lims))
421|     Log.info('bias=%s' % str(sqr.bias))
422|     Log.info('frame=%s' % str(sqr.frame))
423|     Log.info('scale=%s' % str(sqr.scale))
424|     Log.info()
425|     return 'Done'
426| ##================================================================================================##
427| def do_b2_diagram (**kwargs):
428|     """CMND: Create the combined double-four-square box diagram."""
429|     fpath = KWARG('fpath', ImgsPath, str, kwargs)
430|     fname = KWARG('fname', 'logic.png', str, kwargs)
431|     mask  = KWARG('mask', 0, int, kwargs)
432|     title = KWARG('ttl', 'This relates to That.\nOther relates to This.', str, kwargs)
433|     rownm = KWARG('row', 'This', str, kwargs)
434|     colnm = KWARG('col', 'That', str, kwargs)
435|     midnm = KWARG('mid', 'Other', str, kwargs)
436|     mode = KWARG('mode', '', str, kwargs)
437|     Log.info('b2-diagram:: %s' % str(list(kwargs.keys())))
438|     Log.debug('fpath="%s"' % fpath)
439|     Log.debug('fname="%s"' % fname)
440|     Log.debug('mask=%08X::%s' % (mask,bin(mask)[2:]))
441|     Log.debug('title="%s"' % title)
442|     Log.debug('rowname="%s"' % rownm)
443|     Log.debug('colname="%s"' % colnm)
444|     Log.debug('midname="%s"' % midnm)
445|     Log.debug('mode="%s"' % mode)
446|     Log.info()
447|     #
448|     sqr = FourSquare2(title, rownm,colnm,midnm, mask)
449|     if mode == 'stoplight':
450|         sqr.row_color0 = (255,  0,  0)
451|         sqr.row_color1 = (127,  0,  0)
452|         sqr.col_color0 = (  0,255,  0)
453|         sqr.col_color1 = (  0,127,  0)
454|         sqr.mid_color0 = (223,223,  0)
455| 
456|     sqr.make()
457|     sqr.save(fname, fpath=fpath)
458|     #
459|     Log.info('dims=%s' % str(sqr.dims))
460|     Log.info('lims=%s' % str(sqr.lims))
461|     Log.info('bias=%s' % str(sqr.bias))
462|     Log.info('frame=%s' % str(sqr.frame))
463|     Log.info('scale=%s' % str(sqr.scale))
464|     Log.info()
465|     return 'Done'
466| 
467| ##================================================================================================##
468| def do_b1_background (**kwargs):
469|     """CMND: Create a basic diagram with colored backgrounds."""
470|     fpath = KWARG('fpath', ImgsPath, str, kwargs)
471|     fname = KWARG('fname', 'logic.png', str, kwargs)
472|     mask = KWARG('mask', 0, int, kwargs)
473|     task = KWARG('task', 1, int, kwargs)
474|     title = KWARG('ttl', 'This and/or That', str, kwargs)
475|     rownm = KWARG('row', 'This', str, kwargs)
476|     colnm = KWARG('col', 'That', str, kwargs)
477|     Log.info('b1-background:: %s' % str(list(kwargs.keys())))
478|     Log.debug('fpath="%s"' % fpath)
479|     Log.debug('fname="%s"' % fname)
480|     Log.debug('mask=%08X::%s' % (mask,bin(mask)[2:]))
481|     Log.debug('task=%d' % task)
482|     Log.debug('title="%s"' % title)
483|     Log.debug('rowname="%s"' % rownm)
484|     Log.debug('colname="%s"' % colnm)
485|     Log.info()
486|     sqr = FourSquare1(title, rownm,colnm, mask)
487| 
488|     if task == 1:
489|         # Top half cyan...
490|         ulc = (sqr.px(-1.0), sqr.py(+1.0))
491|         lrc = (sqr.px(+1.0), sqr.py( 0.0))
492|         clr = (0x7f, 0xff, 0xff)
493|         sqr.draw_background_color(ulc, lrc, clr)
494|         # Bottom half gray...
495|         ulc = (sqr.px(-1.0), sqr.py( 0.0))
496|         lrc = (sqr.px(+1.0), sqr.py(-1.0))
497|         clr = (0xbf, 0xbf, 0xbf)
498|         sqr.draw_background_color(ulc, lrc, clr)
499|     if task == 2:
500|         # Left side yellow...
501|         ulc = (sqr.px(-1.0), sqr.py(+1.0))
502|         lrc = (sqr.px( 0.0), sqr.py(-1.0))
503|         clr = (0xff, 0xff, 0x7f)
504|         sqr.draw_background_color(ulc, lrc, clr)
505|         # Right side gray...
506|         ulc = (sqr.px( 0.0), sqr.py(+1.0))
507|         lrc = (sqr.px(+1.0), sqr.py(-1.0))
508|         clr = (0xbf, 0xbf, 0xbf)
509|         sqr.draw_background_color(ulc, lrc, clr)
510|     if task == 3:
511|         # Upper-left green...
512|         ulc = (sqr.px(-1.0), sqr.py(+1.0))
513|         lrc = (sqr.px( 0.0), sqr.py( 0.0))
514|         clr = (0x7f, 0xff, 0xcf)
515|         sqr.draw_background_color(ulc, lrc, clr)
516|         # Upper-right cyan...
517|         ulc = (sqr.px( 0.0), sqr.py(+1.0))
518|         lrc = (sqr.px(+1.0), sqr.py( 0.0))
519|         clr = (0x7f, 0xff, 0xff)
520|         sqr.draw_background_color(ulc, lrc, clr)
521|         # Lower-left yellow...
522|         ulc = (sqr.px(-1.0), sqr.py( 0.0))
523|         lrc = (sqr.px( 0.0), sqr.py(-1.0))
524|         clr = (0xff, 0xff, 0x7f)
525|         sqr.draw_background_color(ulc, lrc, clr)
526|         # Lower-right gray...
527|         ulc = (sqr.px( 0.0), sqr.py( 0.0))
528|         lrc = (sqr.px(+1.0), sqr.py(-1.0))
529|         clr = (0xbf, 0xbf, 0xbf)
530|         sqr.draw_background_color(ulc, lrc, clr)
531| 
532|     sqr.make()
533|     sqr.save(fname, fpath=fpath)
534|     Log.info()
535|     return 'Done'
536| ##================================================================================================##
537| def do_b2_background (**kwargs):
538|     """CMND: Create a basic diagram with colored backgrounds."""
539|     fpath = KWARG('fpath', ImgsPath, str, kwargs)
540|     fname = KWARG('fname', 'logic.png', str, kwargs)
541|     mask = KWARG('mask', 0, int, kwargs)
542|     task = KWARG('task', 1, int, kwargs)
543|     title = KWARG('ttl', 'This and/or That', str, kwargs)
544|     rownm = KWARG('row', 'This', str, kwargs)
545|     colnm = KWARG('col', 'That', str, kwargs)
546|     midnm = KWARG('mid', 'Other', str, kwargs)
547|     Log.info('b2-background:: %s' % str(list(kwargs.keys())))
548|     Log.debug('fpath="%s"' % fpath)
549|     Log.debug('fname="%s"' % fname)
550|     Log.debug('mask=%08X::%s' % (mask,bin(mask)[2:]))
551|     Log.debug('task=%d' % task)
552|     Log.debug('title="%s"' % title)
553|     Log.debug('rowname="%s"' % rownm)
554|     Log.debug('colname="%s"' % colnm)
555|     Log.debug('midname="%s"' % midnm)
556|     Log.info()
557|     sqr = FourSquare2(title, rownm,colnm,midnm, mask)
558| 
559|     if task == 1:
560|         # Top half cyan...
561|         ulc = (sqr.px(-1.0), sqr.py(+1.0))
562|         lrc = (sqr.px(+1.0), sqr.py( 0.0))
563|         clr = (0x7f, 0xff, 0xff)
564|         sqr.draw_background_color(ulc, lrc, clr)
565|         # Bottom half gray...
566|         ulc = (sqr.px(-1.0), sqr.py( 0.0))
567|         lrc = (sqr.px(+1.0), sqr.py(-1.0))
568|         clr = (0xbf, 0xbf, 0xbf)
569|         sqr.draw_background_color(ulc, lrc, clr)
570|     if task == 2:
571|         # Left side yellow...
572|         ulc = (sqr.px(-1.0), sqr.py(+1.0))
573|         lrc = (sqr.px( 0.0), sqr.py(-1.0))
574|         clr = (0xff, 0xff, 0x7f)
575|         sqr.draw_background_color(ulc, lrc, clr)
576|         # Right side gray...
577|         ulc = (sqr.px( 0.0), sqr.py(+1.0))
578|         lrc = (sqr.px(+1.0), sqr.py(-1.0))
579|         clr = (0xbf, 0xbf, 0xbf)
580|         sqr.draw_background_color(ulc, lrc, clr)
581|     if task == 3:
582|         # Upper-left green...
583|         ulc = (sqr.px(-1.0), sqr.py(+1.0))
584|         lrc = (sqr.px( 0.0), sqr.py( 0.0))
585|         clr = (0x7f, 0xff, 0xcf)
586|         sqr.draw_background_color(ulc, lrc, clr)
587|         # Upper-right cyan...
588|         ulc = (sqr.px( 0.0), sqr.py(+1.0))
589|         lrc = (sqr.px(+1.0), sqr.py( 0.0))
590|         clr = (0x7f, 0xff, 0xff)
591|         sqr.draw_background_color(ulc, lrc, clr)
592|         # Lower-left yellow...
593|         ulc = (sqr.px(-1.0), sqr.py( 0.0))
594|         lrc = (sqr.px( 0.0), sqr.py(-1.0))
595|         clr = (0xff, 0xff, 0x7f)
596|         sqr.draw_background_color(ulc, lrc, clr)
597|         # Lower-right gray...
598|         ulc = (sqr.px( 0.0), sqr.py( 0.0))
599|         lrc = (sqr.px(+1.0), sqr.py(-1.0))
600|         clr = (0xbf, 0xbf, 0xbf)
601|         sqr.draw_background_color(ulc, lrc, clr)
602|     if task == 4:
603|         # Outer magenta...
604|         ulc = (sqr.px(-1.0), sqr.py(+1.0))
605|         lrc = (sqr.px(+1.0), sqr.py(-1.0))
606|         clr = (0xdf, 0x7f, 0xff)
607|         sqr.draw_background_color(ulc, lrc, clr)
608|         # Inner gray...
609|         ulc = (sqr.px(-0.6), sqr.py(+0.6))
610|         lrc = (sqr.px(+0.6), sqr.py(-0.6))
611|         clr = (0xbf, 0xbf, 0xbf)
612|         sqr.draw_background_color(ulc, lrc, clr)
613|     if task == 5:
614|         # Outer magenta...
615|         ulc = (sqr.px(-1.0), sqr.py(+1.0))
616|         lrc = (sqr.px(+1.0), sqr.py(-1.0))
617|         clr = (0xdf, 0x7f, 0xff)
618|         sqr.draw_background_color(ulc, lrc, clr)
619|         # Inner gray...
620|         ulc = (sqr.px(-0.6), sqr.py(+0.6))
621|         lrc = (sqr.px(+0.6), sqr.py(-0.6))
622|         clr = (0xbf, 0xbf, 0xbf)
623|         sqr.draw_background_color(ulc, lrc, clr)
624|         # Markers...
625|         # Inner...
626|         ul = (sqr.px(-0.30), sqr.py(+0.30))
627|         ur = (sqr.px(+0.30), sqr.py(+0.30))
628|         lr = (sqr.px(+0.30), sqr.py(-0.30))
629|         ll = (sqr.px(-0.30), sqr.py(-0.30))
630|         r = 0.18 * sqr.scale[0]
631|         sqr.draw_marker(ul, r, (  0,255,  0))
632|         sqr.draw_marker(ur, r, (  0,255,255))
633|         sqr.draw_marker(ll, r, (255,255,  0))
634|         sqr.draw_marker(lr, r, ( 63, 63, 63))
635|         # Outer...
636|         ul = (sqr.px(-0.75), sqr.py(+0.75))
637|         ur = (sqr.px(+0.75), sqr.py(+0.75))
638|         lr = (sqr.px(+0.75), sqr.py(-0.75))
639|         ll = (sqr.px(-0.75), sqr.py(-0.75))
640|         r = 0.17 * sqr.scale[0]
641|         sqr.draw_marker(ul, r, (255,255,255))
642|         sqr.draw_marker(ur, r, (127,127,255))
643|         sqr.draw_marker(ll, r, (255,127,127))
644|         sqr.draw_marker(lr, r, (255,  0,255))
645| 
646|     sqr.make()
647|     sqr.save(fname, fpath=fpath)
648|     Log.info()
649|     return 'Done'
650| 
651| ##================================================================================================##
652| def do_b1_all_markers (**kwargs):
653|     """CMND: Create basic box with all markers on."""
654|     fpath = KWARG('fpath', ImgsPath, str, kwargs)
655|     fname = KWARG('fname', 'logic-all-1.png', str, kwargs)
656|     mode = KWARG('mode', 'r', str, kwargs)
657|     mode = mode.lower()
658|     Log.info('b1-all-markers-test:: %s' % str(list(kwargs.keys())))
659|     Log.debug('fpath="%s"' % fpath)
660|     Log.debug('fname="%s"' % fname)
661|     Log.debug('mode="%s"' % mode)
662|     Log.info()
663|     #
664|     s = 'red'[0:len(mode)]  # test string
665|     g = mode.startswith(s)  # red flag :)
666|     mask = 0x000f000f if g else 0x00f000f0
667|     name = ('Red' if g else 'Gray')
668| 
669|     sqr = FourSquare1('All Markers Test (%s)' % name, 'This','That', mask)
670|     sqr.make()
671|     sqr.save(fname, fpath=fpath)
672|     #
673|     Log.info('dims=%s' % str(sqr.dims))
674|     Log.info('lims=%s' % str(sqr.lims))
675|     Log.info('bias=%s' % str(sqr.bias))
676|     Log.info('frame=%s' % str(sqr.frame))
677|     Log.info('scale=%s' % str(sqr.scale))
678|     Log.info()
679|     return 'Done'
680| ##================================================================================================##
681| def do_b2_all_markers (**kwargs):
682|     """CMND: Create basic box with all markers on."""
683|     fpath = KWARG('fpath', ImgsPath, str, kwargs)
684|     fname = KWARG('fname', 'logic-all-2.png', str, kwargs)
685|     mode = KWARG('mode', 'r', str, kwargs)
686|     mode = mode.lower()
687|     Log.info('b2-all-markers-test:: %s' % str(list(kwargs.keys())))
688|     Log.debug('fpath="%s"' % fpath)
689|     Log.debug('fname="%s"' % fname)
690|     Log.debug('mode="%s"' % mode)
691|     Log.info()
692|     #
693|     s = 'red'[0:len(mode)]  # test string
694|     g = mode.startswith(s)  # red flag :)
695|     mask = 0x0f0f0f0f if g else 0xf0f0f0f0
696|     name = '%s markers' % ('Red' if g else 'Gray')
697| 
698|     sqr = FourSquare2('All Markers Test|(%s)' % name, 'This','That','Other', mask)
699|     sqr.make()
700|     sqr.save(fname, fpath=fpath)
701|     #
702|     Log.info('dims=%s' % str(sqr.dims))
703|     Log.info('lims=%s' % str(sqr.lims))
704|     Log.info('bias=%s' % str(sqr.bias))
705|     Log.info('frame=%s' % str(sqr.frame))
706|     Log.info('scale=%s' % str(sqr.scale))
707|     Log.info()
708|     return 'Done'
709| 
710| ##================================================================================================##
711| def do_test (**kwargs):
712|     """CMND: Do a test."""
713|     fpath = KWARG('fpath', BasePath, str, kwargs)
714|     fname = KWARG('fname', 'logic-test.png', str, kwargs)
715|     mask = KWARG('mask', 0, int, kwargs)
716|     task = KWARG('task', 0, int, kwargs)
717|     title = KWARG('ttl', 'This and/or That', str, kwargs)
718|     rownm = KWARG('row', 'This', str, kwargs)
719|     colnm = KWARG('col', 'That', str, kwargs)
720|     midnm = KWARG('mid', 'Other', str, kwargs)
721|     mode = KWARG('mode', '', str, kwargs)
722|     Log.info('test:: %s' % str(list(kwargs.keys())))
723|     Log.debug('fpath="%s"' % fpath)
724|     Log.debug('fname="%s"' % fname)
725|     Log.debug('mask=%08X::%s' % (mask,bin(mask)[2:]))
726|     Log.debug('task=%d' % task)
727|     Log.debug('title="%s"' % title)
728|     Log.debug('rowname="%s"' % rownm)
729|     Log.debug('colname="%s"' % colnm)
730|     Log.debug('midname="%s"' % midnm)
731|     Log.debug('mode="%s"' % mode)
732|     Log.info()
733|     #
734|     #
735|     Log.info()
736|     return 'Done'
737| ####################################################################################################
738| def dispatch (cmd, **kwargs):
739|     """Dispatch to a module function based on CMD."""
740|     Log.info('CMD: %s' % cmd)
741|     for kw in kwargs:
742|         Log.debug('argument[%s]: %s' % (kw,kwargs[kw]))
743|     Log.info()
744|     if cmd == 'ms': return do_parse_marker_string(**kwargs)
745|     if cmd == 'b1': return do_b1_diagram(**kwargs)
746|     if cmd == 'b2': return do_b2_diagram(**kwargs)
747|     if cmd == 'bg1': return do_b1_background(**kwargs)
748|     if cmd == 'bg2': return do_b2_background(**kwargs)
749|     if cmd == 'b1.all': return do_b1_all_markers(**kwargs)
750|     if cmd == 'b2.all': return do_b2_all_markers(**kwargs)
751|     if cmd == 'test': return do_test(**kwargs)
752|     return 'Nothing to do!'
753| ####################################################################################################
754| if __name__ == '__main__':
755|     print('autorun: %s' % argv[0])
756|     Log.start(path.join(BasePath,'logic.log'))
757|     Log.level(debug())
758|     Log.info(argv[0])
759|     Log.info()
760|     cmd = argv[1] if 1 < len(argv) else CMND
761|     kwargs = ARGS
762|     for a in argv[2:]:
763|         nv = a.split('=', 2)
764|         kwargs[nv[0]] = (nv[1] if 1<len(nv) else '')
765|         Log.debug('%s: "%s"' % (nv[0], kwargs[nv[0]]))
766|     try:
767|         print(cmd)
768|         print()
769|         obj = dispatch(cmd, **kwargs)
770|         print()
771|         print(obj)
772|         Log.info()
773|         Log.info(obj)
774|     except:
775|         etype, evalue, tb = exc_info()
776|         ts = extract_tb(tb)
777|         Log.error()
778|         Log.error('%s: %s' % (etype.__name__,str(evalue)))
779|         Log.error()
780|         for t in ts[-5:]:
781|             Log.error('[%d] %s (%s)' % (t[1], t[2], t[0]))
782|             Log.error(' %s' % t[3])
783|             Log.error()
784|         raise
785|     finally:
786|         Log.end()
787| ####################################################################################################
788| '''eof'''
789|