Color lookup optimization and final color reset added
This commit is contained in:
parent
fa787ab9e3
commit
4493e514b3
1 changed files with 11 additions and 3 deletions
14
pixelterm.py
14
pixelterm.py
|
|
@ -7,24 +7,31 @@ from PIL import Image, PngImagePlugin
|
||||||
def termify_pixels(img):
|
def termify_pixels(img):
|
||||||
sx, sy = img.size
|
sx, sy = img.size
|
||||||
out = ''
|
out = ''
|
||||||
|
|
||||||
formatter = terminal256.Terminal256Formatter()
|
formatter = terminal256.Terminal256Formatter()
|
||||||
fg,bg = None,None
|
fg,bg = None,None
|
||||||
|
fgd,bgd = {},{}
|
||||||
def bgescape(color):
|
def bgescape(color):
|
||||||
nonlocal bg
|
nonlocal bg
|
||||||
if bg == color:
|
if bg == color:
|
||||||
return ''
|
return ''
|
||||||
bg=color
|
bg=color
|
||||||
r,g,b,a = color
|
r,g,b,a = color
|
||||||
|
if color in bgd:
|
||||||
|
return bgd[color]
|
||||||
|
bgd[color] = terminal256.EscapeSequence(bg=formatter._closest_color(r,g,b)).color_string()
|
||||||
if color == (0,0,0,0):
|
if color == (0,0,0,0):
|
||||||
return terminal256.EscapeSequence(bg=formatter._closest_color(0,0,0)).reset_string()
|
bgd[color] = terminal256.EscapeSequence(bg=formatter._closest_color(0,0,0)).reset_string()
|
||||||
return terminal256.EscapeSequence(bg=formatter._closest_color(r,g,b)).color_string()
|
return bgd[color]
|
||||||
|
|
||||||
def fgescape(color):
|
def fgescape(color):
|
||||||
nonlocal fg
|
nonlocal fg
|
||||||
if fg == color:
|
if fg == color:
|
||||||
return ''
|
return ''
|
||||||
fg=color
|
fg=color
|
||||||
r,g,b,_ = color
|
r,g,b,_ = color
|
||||||
return terminal256.EscapeSequence(fg=formatter._closest_color(r,g,b)).color_string()
|
fgd[color]= terminal256.EscapeSequence(fg=formatter._closest_color(r,g,b)).color_string()
|
||||||
|
return fgd[color]
|
||||||
#NOTE: This ignores the last line if there is an odd number of lines.
|
#NOTE: This ignores the last line if there is an odd number of lines.
|
||||||
for y in range(0, sy-1, 2):
|
for y in range(0, sy-1, 2):
|
||||||
for x in range(sx):
|
for x in range(sx):
|
||||||
|
|
@ -43,6 +50,7 @@ def termify_pixels(img):
|
||||||
c,te,be = cf,te,te
|
c,te,be = cf,te,te
|
||||||
out += te(coltop) + be(colbot) + c
|
out += te(coltop) + be(colbot) + c
|
||||||
out += '\n'
|
out += '\n'
|
||||||
|
out += terminal256.EscapeSequence(fg=formatter._closest_color(0,0,0), bg=formatter._closest_color(0,0,0)).reset_string()
|
||||||
return out
|
return out
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue