-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrevpals.py
executable file
·47 lines (37 loc) · 1.04 KB
/
revpals.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Usage: python3 revpals.py image.png...
List Pokemon and trainer sprites with reversed palettes (darker color first).
Copyright (c) 2024, Rangi42
SPDX-License-Identifier: MIT
"""
import sys
import png
def luminance(c):
r, g, b = c
return 0.299 * r + 0.587 * g + 0.114 * b
def format_color(c):
r, g, b = c
return f'#{r:02X}{g:02X}{b:02X}'
def reversed_colors(filename):
with open(filename, 'rb') as file:
reader = png.Reader(file)
reader.preamble()
try:
palette = [c[:3] for c in reader.palette()]
except:
return
if len(palette) != 4 or palette[0] != (255, 255, 255) or palette[3] != (0, 0, 0):
return
if luminance(palette[1]) < luminance(palette[2]):
return (palette[1], palette[2])
def main():
if len(sys.argv) < 2:
print(f'Usage: {sys.argv[0]} image.png...', file=sys.stderr)
sys.exit(1)
for filename in sys.argv[1:]:
if (r := reversed_colors(filename)):
print(f'{filename} indexes {format_color(r[0])} before {format_color(r[1])}')
if __name__ == '__main__':
main()