192
.gitignore
vendored
Normal file
192
.gitignore
vendored
Normal file
@@ -0,0 +1,192 @@
|
|||||||
|
### Python ###
|
||||||
|
# Byte-compiled / optimized / DLL files
|
||||||
|
__pycache__/
|
||||||
|
*.py[cod]
|
||||||
|
*$py.class
|
||||||
|
|
||||||
|
# C extensions
|
||||||
|
*.so
|
||||||
|
|
||||||
|
# Distribution / packaging
|
||||||
|
.Python
|
||||||
|
build/
|
||||||
|
develop-eggs/
|
||||||
|
dist/
|
||||||
|
downloads/
|
||||||
|
eggs/
|
||||||
|
.eggs/
|
||||||
|
lib/
|
||||||
|
lib64/
|
||||||
|
parts/
|
||||||
|
sdist/
|
||||||
|
var/
|
||||||
|
wheels/
|
||||||
|
share/python-wheels/
|
||||||
|
*.egg-info/
|
||||||
|
.installed.cfg
|
||||||
|
*.egg
|
||||||
|
MANIFEST
|
||||||
|
|
||||||
|
# PyInstaller
|
||||||
|
# Usually these files are written by a python script from a template
|
||||||
|
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
||||||
|
*.manifest
|
||||||
|
*.spec
|
||||||
|
|
||||||
|
# Installer logs
|
||||||
|
pip-log.txt
|
||||||
|
pip-delete-this-directory.txt
|
||||||
|
|
||||||
|
# Unit test / coverage reports
|
||||||
|
htmlcov/
|
||||||
|
.tox/
|
||||||
|
.nox/
|
||||||
|
.coverage
|
||||||
|
.coverage.*
|
||||||
|
.cache
|
||||||
|
nosetests.xml
|
||||||
|
coverage.xml
|
||||||
|
*.cover
|
||||||
|
*.py,cover
|
||||||
|
.hypothesis/
|
||||||
|
.pytest_cache/
|
||||||
|
cover/
|
||||||
|
|
||||||
|
# Translations
|
||||||
|
*.mo
|
||||||
|
*.pot
|
||||||
|
|
||||||
|
# Django stuff:
|
||||||
|
*.log
|
||||||
|
local_settings.py
|
||||||
|
db.sqlite3
|
||||||
|
db.sqlite3-journal
|
||||||
|
|
||||||
|
# Flask stuff:
|
||||||
|
instance/
|
||||||
|
.webassets-cache
|
||||||
|
|
||||||
|
# Scrapy stuff:
|
||||||
|
.scrapy
|
||||||
|
|
||||||
|
# Sphinx documentation
|
||||||
|
docs/_build/
|
||||||
|
|
||||||
|
# PyBuilder
|
||||||
|
.pybuilder/
|
||||||
|
target/
|
||||||
|
|
||||||
|
# Jupyter Notebook
|
||||||
|
.ipynb_checkpoints
|
||||||
|
|
||||||
|
# IPython
|
||||||
|
profile_default/
|
||||||
|
ipython_config.py
|
||||||
|
|
||||||
|
# pyenv
|
||||||
|
# For a library or package, you might want to ignore these files since the code is
|
||||||
|
# intended to run in multiple environments; otherwise, check them in:
|
||||||
|
# .python-version
|
||||||
|
|
||||||
|
# pipenv
|
||||||
|
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
||||||
|
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
||||||
|
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
||||||
|
# install all needed dependencies.
|
||||||
|
#Pipfile.lock
|
||||||
|
|
||||||
|
# poetry
|
||||||
|
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
||||||
|
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
||||||
|
# commonly ignored for libraries.
|
||||||
|
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
||||||
|
#poetry.lock
|
||||||
|
|
||||||
|
# pdm
|
||||||
|
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
||||||
|
#pdm.lock
|
||||||
|
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
||||||
|
# in version control.
|
||||||
|
# https://pdm.fming.dev/#use-with-ide
|
||||||
|
.pdm.toml
|
||||||
|
|
||||||
|
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
||||||
|
__pypackages__/
|
||||||
|
|
||||||
|
# Celery stuff
|
||||||
|
celerybeat-schedule
|
||||||
|
celerybeat.pid
|
||||||
|
|
||||||
|
# SageMath parsed files
|
||||||
|
*.sage.py
|
||||||
|
|
||||||
|
# Environments
|
||||||
|
.env
|
||||||
|
.venv
|
||||||
|
env/
|
||||||
|
venv/
|
||||||
|
ENV/
|
||||||
|
env.bak/
|
||||||
|
venv.bak/
|
||||||
|
|
||||||
|
# Spyder project settings
|
||||||
|
.spyderproject
|
||||||
|
.spyproject
|
||||||
|
|
||||||
|
# Rope project settings
|
||||||
|
.ropeproject
|
||||||
|
|
||||||
|
# mkdocs documentation
|
||||||
|
/site
|
||||||
|
|
||||||
|
# mypy
|
||||||
|
.mypy_cache/
|
||||||
|
.dmypy.json
|
||||||
|
dmypy.json
|
||||||
|
|
||||||
|
# Pyre type checker
|
||||||
|
.pyre/
|
||||||
|
|
||||||
|
# pytype static type analyzer
|
||||||
|
.pytype/
|
||||||
|
|
||||||
|
# Cython debug symbols
|
||||||
|
cython_debug/
|
||||||
|
|
||||||
|
# PyCharm
|
||||||
|
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
||||||
|
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
||||||
|
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
||||||
|
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
||||||
|
#.idea/
|
||||||
|
|
||||||
|
### Python Patch ###
|
||||||
|
# Poetry local configuration file - https://python-poetry.org/docs/configuration/#local-configuration
|
||||||
|
poetry.toml
|
||||||
|
|
||||||
|
# ruff
|
||||||
|
.ruff_cache/
|
||||||
|
|
||||||
|
# LSP config files
|
||||||
|
pyrightconfig.json
|
||||||
|
|
||||||
|
### Vim ###
|
||||||
|
# Swap
|
||||||
|
[._]*.s[a-v][a-z]
|
||||||
|
!*.svg # comment out if you don't need vector files
|
||||||
|
[._]*.sw[a-p]
|
||||||
|
[._]s[a-rt-v][a-z]
|
||||||
|
[._]ss[a-gi-z]
|
||||||
|
[._]sw[a-p]
|
||||||
|
|
||||||
|
# Session
|
||||||
|
Session.vim
|
||||||
|
Sessionx.vim
|
||||||
|
|
||||||
|
# Temporary
|
||||||
|
.netrwhist
|
||||||
|
*~
|
||||||
|
# Auto-generated tag files
|
||||||
|
tags
|
||||||
|
# Persistent undo
|
||||||
|
[._]*.un~
|
||||||
114
colorhash/__main__.py
Normal file
114
colorhash/__main__.py
Normal file
@@ -0,0 +1,114 @@
|
|||||||
|
import hashlib
|
||||||
|
import sys
|
||||||
|
|
||||||
|
|
||||||
|
# TODO - WASM compile for embedding directly in HTML
|
||||||
|
# TODO - command line parsing for hash type, infile, forcing a palette, etc
|
||||||
|
# TODO - file streaming for infile so we aren't loading e.g. 4GB into memory unnecessarily
|
||||||
|
|
||||||
|
|
||||||
|
def hash2matrix(data: bytes, w: int, h: int) -> list[list[int]]:
|
||||||
|
"""
|
||||||
|
Convert a hash to a list of rows of nibbles.
|
||||||
|
"""
|
||||||
|
# Split the data into nibbles first, in case the width is an odd number
|
||||||
|
nibbles = []
|
||||||
|
for b in data:
|
||||||
|
top = (b & 0xF0) >> 4
|
||||||
|
bottom = b & 0x0F
|
||||||
|
nibbles += [top, bottom]
|
||||||
|
|
||||||
|
if len(nibbles) != w * h:
|
||||||
|
raise ValueError(
|
||||||
|
f"input data length ({len(nibbles)}) must match matrix dimensions ({w}x{h} = {w * h})"
|
||||||
|
)
|
||||||
|
|
||||||
|
cols = []
|
||||||
|
row = []
|
||||||
|
for b in nibbles:
|
||||||
|
row += [b]
|
||||||
|
if len(row) == w:
|
||||||
|
cols += [row]
|
||||||
|
row = []
|
||||||
|
|
||||||
|
return cols
|
||||||
|
|
||||||
|
|
||||||
|
def gensvg(matrix: list[list[int]], square_size: int = 32):
|
||||||
|
h = len(matrix)
|
||||||
|
w = len(matrix[0])
|
||||||
|
|
||||||
|
# Start SVG string
|
||||||
|
svg = f'<svg width="{w * square_size}" height="{h * square_size}" xmlns="http://www.w3.org/2000/svg">\n'
|
||||||
|
|
||||||
|
# Generate grid
|
||||||
|
for r in range(h):
|
||||||
|
for c in range(w):
|
||||||
|
x = c * square_size
|
||||||
|
y = r * square_size
|
||||||
|
color = matrix[r][c]
|
||||||
|
svg += f' <rect x="{x}" y="{y}" width="{square_size}" height="{square_size}" fill="{color}" />\n'
|
||||||
|
|
||||||
|
# Close SVG string
|
||||||
|
svg += "</svg>"
|
||||||
|
return svg
|
||||||
|
|
||||||
|
|
||||||
|
color_table = [
|
||||||
|
# red
|
||||||
|
[f"#{0x110000 * i:06x}" for i in range(0x10)],
|
||||||
|
# green
|
||||||
|
[f"#{0x001100 * i:06x}" for i in range(0x10)],
|
||||||
|
# blue
|
||||||
|
[f"#{0x000011 * i:06x}" for i in range(0x10)],
|
||||||
|
# black
|
||||||
|
[f"#{0x111111 * i:06x}" for i in range(0x10)],
|
||||||
|
# cyan
|
||||||
|
[f"#{0x001111 * i:06x}" for i in range(0x10)],
|
||||||
|
# yellow
|
||||||
|
[f"#{0x111100 * i:06x}" for i in range(0x10)],
|
||||||
|
# magenta
|
||||||
|
[f"#{0x110011 * i:06x}" for i in range(0x10)],
|
||||||
|
# white
|
||||||
|
[f"#{0x111111 * (0xF - i):06x}" for i in range(0x10)],
|
||||||
|
]
|
||||||
|
|
||||||
|
dimensions_table = {
|
||||||
|
"md5": (8, 4),
|
||||||
|
"sha1": (8, 5),
|
||||||
|
"sha224": (8, 7),
|
||||||
|
"sha256": (8, 8),
|
||||||
|
"sha384": (12, 8),
|
||||||
|
"sha512": (16, 8),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
hash_algo = "sha512"
|
||||||
|
|
||||||
|
infile = sys.stdin.buffer
|
||||||
|
|
||||||
|
|
||||||
|
if len(sys.argv) > 1:
|
||||||
|
inpath = sys.argv[1]
|
||||||
|
if inpath != '-':
|
||||||
|
infile = open(inpath, 'rb')
|
||||||
|
|
||||||
|
|
||||||
|
hashdata = hashlib.file_digest(infile, hash_algo).digest()
|
||||||
|
w, h = dimensions_table[hash_algo]
|
||||||
|
matrix = hash2matrix(hashdata, w, h)
|
||||||
|
|
||||||
|
# Print matrix
|
||||||
|
# pprint.pprint([[hex(c) for c in row] for row in matrix])
|
||||||
|
|
||||||
|
palette_no = sum(hashdata) % 8
|
||||||
|
# print('using palette:', palette_no)
|
||||||
|
palette = color_table[palette_no]
|
||||||
|
|
||||||
|
colors = [[palette[v] for v in row] for row in matrix]
|
||||||
|
|
||||||
|
# Print colors
|
||||||
|
# pprint.pprint([[hex(c) for c in row] for row in colors])
|
||||||
|
|
||||||
|
# Print SVG
|
||||||
|
print(gensvg(colors))
|
||||||
Reference in New Issue
Block a user