Add Hash special coloring mode
The Hash coloring mode computes a hash for a matched string segment, and always associates the same color with the same string, yet varies colors with even small changes.
This commit is contained in:
parent
f30838cc57
commit
185be07c80
1 changed files with 28 additions and 1 deletions
|
|
@ -15,6 +15,8 @@ import importlib
|
||||||
import logging
|
import logging
|
||||||
import signal
|
import signal
|
||||||
import string
|
import string
|
||||||
|
import hashlib
|
||||||
|
import functools
|
||||||
|
|
||||||
# set the SIGPIPE handler to kill the program instead of
|
# set the SIGPIPE handler to kill the program instead of
|
||||||
# ending in a write error when a broken pipe occurs
|
# ending in a write error when a broken pipe occurs
|
||||||
|
|
@ -364,6 +366,31 @@ def colorin(text, color="red", style="normal"):
|
||||||
color = cmap[i]
|
color = cmap[i]
|
||||||
color_code = str(color)
|
color_code = str(color)
|
||||||
|
|
||||||
|
# "hash" or "Hash"; useful to randomly but consistently color strings
|
||||||
|
elif color.lower() == "hash":
|
||||||
|
hasher = hashlib.md5()
|
||||||
|
hasher.update(text.encode('utf-8'))
|
||||||
|
hash = hasher.hexdigest()
|
||||||
|
|
||||||
|
f = float(functools.reduce(lambda x, y: x+ord(y), hash, 0) % 101)
|
||||||
|
|
||||||
|
if color[0].islower():
|
||||||
|
mode = 8
|
||||||
|
cmap = colormaps["rainbow"]
|
||||||
|
|
||||||
|
# normalize and scale over the nb of colors in cmap
|
||||||
|
i = int( math.ceil( (f - scale[0]) / (scale[1]-scale[0]) * (len(cmap)-1) ) )
|
||||||
|
|
||||||
|
color = cmap[i]
|
||||||
|
color_code = str(30 + colors[color])
|
||||||
|
|
||||||
|
else:
|
||||||
|
mode = 256
|
||||||
|
cmap = colormaps["Rainbow"]
|
||||||
|
i = int( math.ceil( (f - scale[0]) / (scale[1]-scale[0]) * (len(cmap)-1) ) )
|
||||||
|
color = cmap[i]
|
||||||
|
color_code = str(color)
|
||||||
|
|
||||||
# Really useful only when using colout as a library
|
# Really useful only when using colout as a library
|
||||||
# thus you can change the "colormap" variable to your favorite one before calling colorin
|
# thus you can change the "colormap" variable to your favorite one before calling colorin
|
||||||
elif color == "colormap":
|
elif color == "colormap":
|
||||||
|
|
@ -801,7 +828,7 @@ if __name__ == "__main__":
|
||||||
print("Available resources:")
|
print("Available resources:")
|
||||||
print("STYLES: %s" % ", ".join(styles) )
|
print("STYLES: %s" % ", ".join(styles) )
|
||||||
print("COLORS: %s" % ", ".join(colors) )
|
print("COLORS: %s" % ", ".join(colors) )
|
||||||
print("SPECIAL: %s" % ", ".join(["random", "Random", "scale", "Scale", "colormap"]) )
|
print("SPECIAL: %s" % ", ".join(["random", "Random", "scale", "Scale", "hash", "Hash", "colormap"]) )
|
||||||
|
|
||||||
if len(themes) > 0:
|
if len(themes) > 0:
|
||||||
print("THEMES: %s" % ", ".join(themes.keys()) )
|
print("THEMES: %s" % ", ".join(themes.keys()) )
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue