diff --git a/TermTk/TTkCore/canvas.py b/TermTk/TTkCore/canvas.py index f4af7ace..170b10d0 100644 --- a/TermTk/TTkCore/canvas.py +++ b/TermTk/TTkCore/canvas.py @@ -42,6 +42,8 @@ class TTkCanvas(): '_data', '_colors', '_bufferedData', '_bufferedColors', '_visible', '_transparent', '_doubleBuffer') + _data:list[str] + _colors:list[TTkColor] def __init__(self, width:int=0, height:int=0) -> None: @@ -50,7 +52,7 @@ class TTkCanvas(): self._doubleBuffer = False self._width = 0 self._height = 0 - self._data = [[]] + self._data = [[]] self._colors = [[]] self._newWidth = width self._newHeight = height diff --git a/TermTk/TTkCore/color.py b/TermTk/TTkCore/color.py index 8d9e7e19..80cc786e 100644 --- a/TermTk/TTkCore/color.py +++ b/TermTk/TTkCore/color.py @@ -64,7 +64,8 @@ from TermTk.TTkCore.helper import TTkHelper class _TTkColor: __slots__ = ('_fg','_bg', '_colorMod', '_buffer', '_clean') - _fg: tuple[int]; _bg: tuple[int]; + _fg: tuple[int] + _bg: tuple[int] def __init__(self, fg:tuple[int]=None, bg:tuple[int]=None, @@ -80,13 +81,19 @@ class _TTkColor: if self._fg: return _TTkColor(fg=self._fg) else: - return None + return TTkColor.RST def background(self): if self._bg: return _TTkColor(bg=self._bg) else: - return None + return TTkColor.RST + + def hasForeground(self) -> bool: + return True if self._fg else False + + def hasBackground(self) -> bool: + return True if self._bg else False def bold(self) -> bool: return False diff --git a/TermTk/TTkWidgets/TTkPickers/colorpicker.py b/TermTk/TTkWidgets/TTkPickers/colorpicker.py index 6722c043..d77ae09a 100644 --- a/TermTk/TTkWidgets/TTkPickers/colorpicker.py +++ b/TermTk/TTkWidgets/TTkPickers/colorpicker.py @@ -207,10 +207,10 @@ class _TTkColorButton(TTkButton): style = self.style() for t in style: if 'color' in style[t]: - if fg := color.foreground(): - style[t]['color'] = fg.invertFgBg() - elif bg := color.background(): - style[t]['color'] = bg + if TTkK.ColorType.Foreground & (colorType := color.colorType()): + style[t]['color'] = color.foreground().invertFgBg() + elif TTkK.ColorType.Background & colorType: + style[t]['color'] = color.background() else: style[t]['color'] = TTkColor.BG_BLACK self.setStyle(style) @@ -223,12 +223,16 @@ class _TTkColorButton(TTkButton): self._returnType = returnType def color(self) -> TTkColor: - fg = self._color.foreground() - bg = self._color.background() if self._returnType==TTkK.ColorPickerReturnType.Foreground: - return fg if fg else bg.invertFgBg() if bg else TTkColor.RST + if self._color.colorType() & TTkK.ColorType.Foreground: + return self._color.foreground() + else: + return self._color.background().invertFgBg() if self._returnType==TTkK.ColorPickerReturnType.Background: - return bg if bg else fg.invertFgBg() if fg else TTkColor.RST + if self._color.colorType() & TTkK.ColorType.Background: + return self._color.background() + else: + return self._color.foreground().invertFgBg() return self._color def isCustom(self) -> bool: @@ -549,12 +553,16 @@ class TTkColorDialogPicker(TTkWindow): :return: the current color :rtype: :py:class:`TTkColor` ''' - fg = self._color.foreground() - bg = self._color.background() if self._returnType==TTkK.ColorPickerReturnType.Foreground: - return fg if fg else bg.invertFgBg() if bg else TTkColor.RST + if self._color.colorType() & TTkK.ColorType.Foreground: + return self._color.foreground() + else: + return self._color.background().invertFgBg() if self._returnType==TTkK.ColorPickerReturnType.Background: - return bg if bg else fg.invertFgBg() if fg else TTkColor.RST + if self._color.colorType() & TTkK.ColorType.Background: + return self._color.background() + else: + return self._color.foreground().invertFgBg() return self._color def setColor(self, color:TTkColor) -> None: @@ -663,10 +671,12 @@ class TTkColorButtonPicker(_TTkColorButton): @pyTTkSlot(TTkColor) def _processColorSelected(self, color:TTkColor): - if fg := self.color().foreground(): - bg = fg.invertFgBg() - elif bg := self.color().background(): - fg = bg.invertFgBg() + if TTkK.ColorType.Foreground & (colorType := color.colorType()): + fg = color.foreground() + bg = color.foreground().invertFgBg() + elif TTkK.ColorType.Background & colorType: + fg = color.background().invertFgBg() + bg = color.background() else: fg = TTkColor.BLACK bg = TTkColor.BG_BLACK diff --git a/TermTk/TTkWidgets/TTkPickers/textpicker.py b/TermTk/TTkWidgets/TTkPickers/textpicker.py index 6e46c81c..9e42d074 100644 --- a/TermTk/TTkWidgets/TTkPickers/textpicker.py +++ b/TermTk/TTkWidgets/TTkPickers/textpicker.py @@ -173,18 +173,18 @@ class TTkTextDialogPicker(TTkWindow): @pyTTkSlot(TTkColor) def _currentColorChangedCB(format:TTkColor): - if fg := format.foreground(): + if TTkK.ColorType.Foreground & (colorType := format.colorType()): cb_fg.setCheckState(TTkK.Checked) btn_fgColor.setEnabled() - btn_fgColor.setColor(fg) + btn_fgColor.setColor(format.foreground()) else: cb_fg.setCheckState(TTkK.Unchecked) btn_fgColor.setDisabled() - if bg := format.background(): + if TTkK.ColorType.Background & colorType: cb_bg.setCheckState(TTkK.Checked) btn_bgColor.setEnabled() - btn_bgColor.setColor(bg) + btn_bgColor.setColor(format.background()) else: cb_bg.setCheckState(TTkK.Unchecked) btn_bgColor.setDisabled() diff --git a/demo/showcase/textedit.py b/demo/showcase/textedit.py index 6f37b360..17466afe 100755 --- a/demo/showcase/textedit.py +++ b/demo/showcase/textedit.py @@ -157,19 +157,19 @@ def demoTextEdit(root=None, document=None): fontLayout.addWidget(superSimpleHorizontalLine(),0,12,2,1) @ttk.pyTTkSlot(ttk.TTkColor) - def _currentColorChangedCB(format): - if fg := format.foreground(): + def _currentColorChangedCB(format:ttk.TTkColor): + if ttk.TTkK.ColorType.Foreground & (colorType := format.colorType()): cb_fg.setCheckState(ttk.TTkK.Checked) btn_fgColor.setEnabled() - btn_fgColor.setColor(fg) + btn_fgColor.setColor(format.foreground()) else: cb_fg.setCheckState(ttk.TTkK.Unchecked) btn_fgColor.setDisabled() - if bg := format.background(): + if ttk.TTkK.ColorType.Background & colorType: cb_bg.setCheckState(ttk.TTkK.Checked) btn_bgColor.setEnabled() - btn_bgColor.setColor(bg) + btn_bgColor.setColor(format.background()) else: cb_bg.setCheckState(ttk.TTkK.Unchecked) btn_bgColor.setDisabled() diff --git a/tests/t.ui/test.ui.020.TextEdit.01.UndoRedo.py b/tests/t.ui/test.ui.020.TextEdit.01.UndoRedo.py index 115e09f5..19982c15 100755 --- a/tests/t.ui/test.ui.020.TextEdit.01.UndoRedo.py +++ b/tests/t.ui/test.ui.020.TextEdit.01.UndoRedo.py @@ -76,19 +76,19 @@ def demoTextEdit(root=None, document=None): fontLayout.addWidget(superSimpleHorizontalLine(),0,10,2,1) @ttk.pyTTkSlot(ttk.TTkColor) - def _currentColorChangedCB(format): - if fg := format.foreground(): + def _currentColorChangedCB(format:ttk.TTkColor): + if ttk.TTkK.ColorType.Foreground & (colorType := format.colorType()): cb_fg.setCheckState(ttk.TTkK.Checked) btn_fgColor.setEnabled() - btn_fgColor.setColor(fg.invertFgBg()) + btn_fgColor.setColor(format.foreground()) else: cb_fg.setCheckState(ttk.TTkK.Unchecked) btn_fgColor.setDisabled() - if bg := format.background(): + if ttk.TTkK.ColorType.Background & colorType: cb_bg.setCheckState(ttk.TTkK.Checked) btn_bgColor.setEnabled() - btn_bgColor.setColor(bg) + btn_bgColor.setColor(format.background()) else: cb_bg.setCheckState(ttk.TTkK.Unchecked) btn_bgColor.setDisabled() diff --git a/tools/dumb.image.tool.py b/tools/dumb.image.tool.py index 91cc1be9..faccf40a 100755 --- a/tools/dumb.image.tool.py +++ b/tools/dumb.image.tool.py @@ -84,19 +84,19 @@ class Ansieditor(ttk.TTkGridLayout): btn_redo.clicked.connect(self._te.redo) @ttk.pyTTkSlot(ttk.TTkColor) - def _currentColorChangedCB(format): - if fg := format.foreground(): + def _currentColorChangedCB(format:ttk.TTkColor): + if ttk.TTkK.ColorType.Foreground & (colorType := format.colorType()): cb_fg.setCheckState(ttk.TTkK.Checked) btn_fgColor.setEnabled() - btn_fgColor.setColor(fg.invertFgBg()) + btn_fgColor.setColor(format.foreground()) else: cb_fg.setCheckState(ttk.TTkK.Unchecked) btn_fgColor.setDisabled() - if bg := format.background(): + if ttk.TTkK.ColorType.Background & colorType: cb_bg.setCheckState(ttk.TTkK.Checked) btn_bgColor.setEnabled() - btn_bgColor.setColor(bg) + btn_bgColor.setColor(format.background()) else: cb_bg.setCheckState(ttk.TTkK.Unchecked) btn_bgColor.setDisabled() diff --git a/tools/dumbPaintTool/app/canvaslayer.py b/tools/dumbPaintTool/app/canvaslayer.py index be645f27..5a2cd2c0 100644 --- a/tools/dumbPaintTool/app/canvaslayer.py +++ b/tools/dumbPaintTool/app/canvaslayer.py @@ -278,8 +278,9 @@ class CanvasLayer(): palette = outData['palette'] = [] for row in colors: for c in row: - fg = f"{c.getHex(ttk.TTkK.Foreground)}" if c.foreground() else None - bg = f"{c.getHex(ttk.TTkK.Background)}" if c.background() else None + colorType = c.colorType() + fg = f"{c.getHex(ttk.TTkK.ColorType.Foreground)}" if (colorType&ttk.TTkK.ColorType.Foreground) else None + bg = f"{c.getHex(ttk.TTkK.ColorType.Background)}" if (colorType&ttk.TTkK.ColorType.Background) else None if (pc:=(fg,bg)) not in palette: palette.append(pc) @@ -296,8 +297,9 @@ class CanvasLayer(): for row in colors[hslice]: outData['colors'].append([]) for c in row[wslice]: - fg = f"{c.getHex(ttk.TTkK.Foreground)}" if c.foreground() else None - bg = f"{c.getHex(ttk.TTkK.Background)}" if c.background() else None + colorType = c.colorType() + fg = f"{c.getHex(ttk.TTkK.ColorType.Foreground)}" if (colorType&ttk.TTkK.ColorType.Foreground) else None + bg = f"{c.getHex(ttk.TTkK.ColorType.Background)}" if (colorType&ttk.TTkK.ColorType.Background) else None if palette: outData['colors'][-1].append(palette.index((fg,bg))) else: @@ -493,18 +495,15 @@ class CanvasLayer(): colors[oy+y][ox+x] = color else: glyph = data[ oy+y][ox+x] - ofg = colors[oy+y][ox+x].foreground() - obg = colors[oy+y][ox+x].background() - nfg = color.foreground() - nbg = color.background() + oColorType = (oc:=colors[oy+y][ox+x]).colorType() + nColorType = (nc:=color).colorType() if glyph==' ': - if obg: - colors[oy+y][ox+x] = nbg if nbg else ttk.TTkColor.RST + if oColorType & ttk.TTkK.ColorType.Background: + colors[oy+y][ox+x] = nc.background() else: - fg = nfg if nfg else ofg if ofg else ttk.TTkColor.RST - bg = nbg if nbg and obg else obg if obg else fg + fg = nc.foreground() if ttk.TTkK.ColorType.Foreground & nColorType else oc.foreground() + bg = nc.background() if ttk.TTkK.ColorType.Background & nColorType & oColorType else oc.background() if ttk.TTkK.ColorType.Background & oColorType else fg color = fg+bg - color = color if color else ttk.TTkColor.RST colors[oy+y][ox+x] = color return True return False diff --git a/tools/dumbPaintTool/app/importimage.py b/tools/dumbPaintTool/app/importimage.py index 2b0e6dac..bdfa9293 100644 --- a/tools/dumbPaintTool/app/importimage.py +++ b/tools/dumbPaintTool/app/importimage.py @@ -323,21 +323,19 @@ class TTkImageNew(ttk.TTkWidget): s = (0,0,w,h) for y,(rowd,rowc) in enumerate(zip(self._canvasImage._data[:h],self._canvasImage._colors[:h])): for x,(gl,c) in enumerate(zip(rowd[:w],rowc[:w])): - nfg = c.foreground() - nbg = c.background() - if gl == ' ' and not nbg: + nColorType = c.colorType() + if gl == ' ' and not (nColorType & ttk.TTkK.ColorType.Background): continue elif gl == ' ': canvas._data[ y][x] = ' ' - canvas._colors[y][x] = nbg - elif not nbg: + canvas._colors[y][x] = c.background() + elif not (nColorType & ttk.TTkK.ColorType.Background): nbg = canvas._colors[y][x].background() canvas._data[ y][x] = gl - canvas._colors[y][x] = nbg + nfg if nbg else nfg + canvas._colors[y][x] = c.foreground() + nbg else: canvas._data[ y][x] = gl canvas._colors[y][x] = c - # canvas.paintCanvas(self._canvasImage,s,s,s) class ImagePreview(TTkImageNew): __slots__ = ('_trColor1','_trColor2') diff --git a/tools/dumbPaintTool/app/paintarea.py b/tools/dumbPaintTool/app/paintarea.py index e5a8d039..975db792 100644 --- a/tools/dumbPaintTool/app/paintarea.py +++ b/tools/dumbPaintTool/app/paintarea.py @@ -200,25 +200,25 @@ class PaintArea(ttk.TTkAbstractScrollView): if self._tool & ToolType.PICKGLYPH: if mp: mpx,mpy = mp - color = None + color = ttk.TTkColor.RST glyph = None for lm in glbls.layers.layers(): lmx,lmy = lm.pos() if lm.isOpaque(mpx-lmx-dx,mpy-lmy-dy): _gl, _co = lm.glyphColorAt(mpx-lmx-dx,mpy-lmy-dy) - if not glyph and not color: + if not glyph and color == ttk.TTkColor.RST: glyph = _gl color = _co - elif not color.background(): - if _co.background(): - if _fg:=color.foreground(): + elif color.background() == ttk.TTkColor.RST: + if _co.background() != ttk.TTkColor.RST: + if (_fg:=color.foreground()) != ttk.TTkColor.RST: color = _fg + _co.background() else: color = _co.background() else: break glbls.brush.setColor(color) - glbls.brush.setGlyph(glyph) + glbls.brush.setGlyph(glyph if glyph else ' ') if mr: glbls.brush.setToolType(self._tool & ~ToolType.PICKGLYPH) self._mousePress = None diff --git a/tools/dumbPaintTool/app/painttoolkit.py b/tools/dumbPaintTool/app/painttoolkit.py index 6796a20c..60a75ee3 100644 --- a/tools/dumbPaintTool/app/painttoolkit.py +++ b/tools/dumbPaintTool/app/painttoolkit.py @@ -143,18 +143,18 @@ class PaintToolKit(ttk.TTkContainer): @ttk.pyTTkSlot(ttk.TTkColor) def setColor(self, color:ttk.TTkColor): - if fg := color.foreground(): + if ttk.TTkK.ColorType.Foreground & (colorType := color.colorType()): self._cbFg.setCheckState(ttk.TTkK.Checked) self._bpFg.setEnabled() - self._bpFg.setColor(fg) + self._bpFg.setColor(color.foreground()) else: self._cbFg.setCheckState(ttk.TTkK.Unchecked) self._bpFg.setDisabled() - if bg := color.background(): + if ttk.TTkK.ColorType.Background & colorType: self._cbBg.setCheckState(ttk.TTkK.Checked) self._bpBg.setEnabled() - self._bpBg.setColor(bg) + self._bpBg.setColor(color.background()) else: self._cbBg.setCheckState(ttk.TTkK.Unchecked) self._bpBg.setDisabled() diff --git a/tools/dumbPaintTool/app/palette.py b/tools/dumbPaintTool/app/palette.py index 87d5aba8..f77a211d 100644 --- a/tools/dumbPaintTool/app/palette.py +++ b/tools/dumbPaintTool/app/palette.py @@ -84,8 +84,8 @@ class Palette(ttk.TTkWidget): def setColor(self, color:ttk.TTkColor) -> None: fg = color.foreground() bg = color.background() - self.enableFg(fg!=None) - self.enableBg(bg!=None) + self.enableFg(fg!=ttk.TTkColor.RST) + self.enableBg(bg!=ttk.TTkColor.RST) def _getPos(col): for y,row in enumerate(self._palette): for x,c in enumerate(row): @@ -93,14 +93,14 @@ class Palette(ttk.TTkWidget): return x,y return None pw, ph = len(self._palette[0]),len(self._palette) - if fg: + if fg!=ttk.TTkColor.RST: if (pos:=_getPos(fg)) != None: self._fg = pos else: self._fg = (pw-2,ph-1) self._palette[ph-1][pw-2] = (fg,fg.invertFgBg()) - if bg: + if bg!=ttk.TTkColor.RST: if (pos:=_getPos(bg)) != None: self._bg = pos else: diff --git a/tools/dumbPaintTool/app/textarea.py b/tools/dumbPaintTool/app/textarea.py index 18772747..2631c515 100644 --- a/tools/dumbPaintTool/app/textarea.py +++ b/tools/dumbPaintTool/app/textarea.py @@ -133,19 +133,19 @@ class TextArea(ttk.TTkGridLayout): btn_redo.clicked.connect(self._te.redo) @ttk.pyTTkSlot(ttk.TTkColor) - def _currentColorChangedCB(format): - if fg := format.foreground(): + def _currentColorChangedCB(format:ttk.TTkColor): + if ttk.TTkK.ColorType.Foreground & (colorType := format.colorType()): cb_fg.setCheckState(ttk.TTkK.Checked) btn_fgColor.setEnabled() - btn_fgColor.setColor(fg) + btn_fgColor.setColor(format.foreground()) else: cb_fg.setCheckState(ttk.TTkK.Unchecked) btn_fgColor.setDisabled() - if bg := format.background(): + if ttk.TTkK.ColorType.Background & colorType: cb_bg.setCheckState(ttk.TTkK.Checked) btn_bgColor.setEnabled() - btn_bgColor.setColor(bg) + btn_bgColor.setColor(format.background()) else: cb_bg.setCheckState(ttk.TTkK.Unchecked) btn_bgColor.setDisabled() diff --git a/tools/dumbPaintTool/app/toolspanel.py b/tools/dumbPaintTool/app/toolspanel.py index 62695e2c..b250e4c8 100644 --- a/tools/dumbPaintTool/app/toolspanel.py +++ b/tools/dumbPaintTool/app/toolspanel.py @@ -212,8 +212,8 @@ class ToolsPanel(ttk.TTkVBoxLayout): ttk.TTkString("Glyph: '") + ttk.TTkString(glyph,color) + ttk.TTkString("'")) - self._cb_p_fg.setChecked(None != color.foreground()) - self._cb_p_bg.setChecked(None != color.background()) + self._cb_p_fg.setChecked(ttk.TTkColor.RST != color.foreground()) + self._cb_p_bg.setChecked(ttk.TTkColor.RST != color.background()) @ttk.pyTTkSlot(ttk.TTkColor) def setColor(self, color:ttk.TTkColor): diff --git a/tools/ttkDesigner/app/notepad.py b/tools/ttkDesigner/app/notepad.py index d784aaae..9a53f7b3 100644 --- a/tools/ttkDesigner/app/notepad.py +++ b/tools/ttkDesigner/app/notepad.py @@ -81,19 +81,19 @@ class NotePad(ttk.TTkGridLayout): fontLayout.addWidget(SuperSimpleHorizontalLine(),0,12,2,1) @ttk.pyTTkSlot(ttk.TTkColor) - def _currentColorChangedCB(format): - if fg := format.foreground(): + def _currentColorChangedCB(format:ttk.TTkColor): + if ttk.TTkK.ColorType.Foreground & (colorType := format.colorType()): cb_fg.setCheckState(ttk.TTkK.Checked) btn_fgColor.setEnabled() - btn_fgColor.setColor(fg) + btn_fgColor.setColor(format.foreground()) else: cb_fg.setCheckState(ttk.TTkK.Unchecked) btn_fgColor.setDisabled() - if bg := format.background(): + if ttk.TTkK.ColorType.Background & colorType: cb_bg.setCheckState(ttk.TTkK.Checked) btn_bgColor.setEnabled() - btn_bgColor.setColor(bg) + btn_bgColor.setColor(format.background()) else: cb_bg.setCheckState(ttk.TTkK.Unchecked) btn_bgColor.setDisabled()