Skip to content

Instantly share code, notes, and snippets.

@megahomyak
Created August 19, 2022 15:27
Show Gist options
  • Save megahomyak/682219c6b6a32af5bfc9194d1743c9ee to your computer and use it in GitHub Desktop.
Save megahomyak/682219c6b6a32af5bfc9194d1743c9ee to your computer and use it in GitHub Desktop.
256-256-256 RGB HEX => it's-your-choice RGB => 256-256-256 RGB HEX. Used to convert some 256-256-256 colors to 8-8-4.
def tuple_to_hex(tup):
return "".join(map(
lambda n: hex(n)[2:].zfill(2),
tup
))
def hex_to_tuple(h):
return tuple(int(h[i:i+2], 16) for i in range(0, 6, 2))
def minimize(color, depth):
return (
round(color / round(256 / (depth - 1)))
* 255 // (depth - 1)
)
def conv(color_tup, depths):
return tuple(
minimize(color, depth)
for color, depth
in zip(color_tup, depths)
)
depths = tuple(map(
lambda num: int(num.strip()),
input("depths: ").split(",")
))
while True:
print(tuple_to_hex(conv(hex_to_tuple(input()), depths)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment