Created
February 25, 2010 16:26
-
-
Save bartman/314688 to your computer and use it in GitHub Desktop.
256.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import sys | |
BASIC_SIZE = 16 | |
CUBE_SIZE = 6 | |
GRAY_SIZE = 24 | |
NUMBERS = 0 | |
def rgb(corner, axis, x, y, z): | |
""" | |
corner -- #:RGB 0: 000, 1:005, 2:050, 3:055, 4:500, 5:505, 6:550, 7:555 | |
axis -- #:R,G or B 0:B, 1:G, 2:R | |
x -- (axis) (0):??x, (1):?x?, (2):x?? | |
y -- (axis) (0):?y?, (1):y??, (2):??y | |
z -- (axis) (0):z??, (1):??z, (2):?z? | |
""" | |
assert corner in range(8) | |
assert axis in range(3) | |
assert x in range(CUBE_SIZE) | |
assert y in range(CUBE_SIZE) | |
assert z in range(CUBE_SIZE) | |
r, g, b = (corner&4)/4, (corner&2)/2, (corner&1) | |
dr = (axis==0)*z + (axis==1)*y + (axis==2)*x | |
dg = (axis==0)*y + (axis==1)*x + (axis==2)*z | |
db = (axis==0)*x + (axis==1)*z + (axis==2)*y | |
r = r*(CUBE_SIZE-1) + dr * (1-2*r) | |
g = g*(CUBE_SIZE-1) + dg * (1-2*g) | |
b = b*(CUBE_SIZE-1) + db * (1-2*b) | |
return r, g, b | |
def joinendl(l): | |
return "".join(l)+"\x1b[0m" | |
def block(n): | |
if NUMBERS == 16: | |
return "\x1b[48;5;%dm%02x" % (n,n) | |
if NUMBERS == 10: | |
return "\x1b[48;5;%dm%3d" % (n,n) | |
return "\x1b[48;5;%dm "%n | |
def cblock(corner, axis, x, y, z): | |
r, g, b = rgb(corner, axis, x, y, z) | |
return block((r*CUBE_SIZE+g)*CUBE_SIZE+b+16) | |
def dblock(n): | |
assert n in range(BASIC_SIZE) | |
return block(n) | |
def gblock(n): | |
assert n in range(GRAY_SIZE) | |
return block(n+CUBE_SIZE**3+BASIC_SIZE) | |
def blank(num): | |
if NUMBERS == 10: | |
return "\x1b[0m"+" "*num | |
return "\x1b[0m"+" "*num | |
def print_88(): | |
global CUBE_SIZE, GRAY_SIZE | |
CUBE_SIZE = 4 | |
GRAY_SIZE = 8 | |
lg = [] | |
for y in range(1,3): | |
lg.append(joinendl( | |
[cblock(1, 0, y, x, 0) for x in range(1,3)] + | |
[blank(10)])) | |
for y in range(0,4): | |
lg.append(joinendl( | |
[cblock(0, 1, x, y, 0) for x in range(1,4)] + | |
[cblock(2, 2, y, x, 0) for x in range(1,4)] + | |
[cblock(3, 1, x, y, 0) for x in range(1,4)] + | |
[cblock(1, 2, y, x, 0) for x in range(1,4)])) | |
for y in range(1,3): | |
lg.append(joinendl( | |
[blank(6)] + | |
[cblock(7, 0, y, x, 0) for x in range(1,3)] + | |
[blank(4)])) | |
sm = [] | |
for y in range(1,3): | |
sm.append(joinendl( | |
[cblock(2, 2, y, x, 1) for x in range(1,3)] + | |
[cblock(1, 2, y, x, 1) for x in range(1,3)])) | |
sm.extend([blank(0)]) | |
sm.append(joinendl( | |
[gblock(7-x) for x in range(8)])) | |
sm.extend([blank(0),blank(0)]) | |
for y in range(2): | |
sm.append(joinendl( | |
[dblock(y*8+x) for x in range(8)])) | |
for l1,l2 in zip(lg, sm): | |
print l1+" "+l2 | |
def print_256(): | |
lg = [] | |
for y in range(1,5): | |
lg.append(joinendl( | |
[cblock(1, 0, y, x, 0) for x in range(1,5)] + | |
[blank(16)])) | |
for y in range(6): | |
lg.append(joinendl( | |
[cblock(0, 1, x, y, 0) for x in range(1,6)] + | |
[cblock(2, 2, y, x, 0) for x in range(1,6)] + | |
[cblock(3, 1, x, y, 0) for x in range(1,6)] + | |
[cblock(1, 2, y, x, 0) for x in range(1,6)])) | |
for y in range(1,5): | |
lg.append(joinendl( | |
#[blank(10)] + | |
[cblock(7, 0, y, x, 0) for x in range(1,5)] + | |
[blank(6)])) | |
sm = [] | |
for y in range(2,4): | |
sm.append(joinendl( | |
[cblock(1, 0, y, x, 1) for x in range(2,4)])) | |
for y in range(1,5): | |
sm.append(joinendl( | |
[cblock(0, 1, x, y, 1) for x in range(2,5)] + | |
[cblock(2, 2, y, x, 1) for x in range(2,5)] + | |
[cblock(3, 1, x, y, 1) for x in range(2,5)] + | |
[cblock(1, 2, y, x, 1) for x in range(2,5)])) | |
for y in range(2,4): | |
sm.append(joinendl( | |
[blank(6)] + | |
[cblock(7, 0, y, x, 1) for x in range(2,4)])) | |
for y in range(2,4): | |
sm.append(joinendl( | |
[cblock(2, 2, y, x, 2) for x in range(2,4)] + | |
[cblock(1, 2, y, x, 2) for x in range(2,4)])) | |
d16 = [blank(8),blank(8)] | |
for y in range(2): | |
d16.append(joinendl( | |
[dblock(y*8+x) for x in range(8)])) | |
g30 = [blank(0),blank(0)] | |
g30.append(joinendl( | |
[gblock(11-x) for x in range(12)])) | |
g30.append(joinendl( | |
[gblock(12+x) for x in range(12)])) | |
for l1,l2 in zip(lg,sm): | |
print l1+blank(2)+l2 | |
for l1, l2, l3 in zip(d16, lg[-4:], g30): | |
print l1+blank(2)+l2+blank(2)+l3 | |
ok_args = 0 | |
do_88 = False | |
if '-n' in sys.argv: | |
ok_args += 1 | |
NUMBERS = 16 | |
if '-d' in sys.argv: | |
ok_args += 1 | |
NUMBERS = 10 | |
if '-88' in sys.argv: | |
ok_args += 1 | |
do_88 = True | |
if len(sys.argv)-1 != ok_args: | |
print "usage: %s [-88] [-n]" % sys.argv[0] | |
elif do_88: | |
print_88() | |
else: | |
print_256() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment