From 1b765e23196931c07819b5b07fc27f10c5f5a7bd Mon Sep 17 00:00:00 2001 From: jaseg Date: Tue, 2 Apr 2013 02:15:02 +0200 Subject: First optimization: Do not double-output color codes. --- pixelterm.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pixelterm.py b/pixelterm.py index 5560f32..db6227e 100755 --- a/pixelterm.py +++ b/pixelterm.py @@ -8,12 +8,21 @@ def termify_pixels(img): sx, sy = img.size out = '' formatter = terminal256.Terminal256Formatter() + fg,bg = None,None def bgescape(color): + nonlocal bg + if bg == color: + return '' + bg=color r,g,b,a = color if color == (0,0,0,0): return terminal256.EscapeSequence(bg=formatter._closest_color(0,0,0)).reset_string() return terminal256.EscapeSequence(bg=formatter._closest_color(r,g,b)).color_string() def fgescape(color): + nonlocal fg + if fg == color: + return '' + fg=color r,g,b,_ = color return terminal256.EscapeSequence(fg=formatter._closest_color(r,g,b)).color_string() #NOTE: This ignores the last line if there is an odd number of lines. -- cgit