#!/usr/bin/env python
"""
The MIT License
Copyright (c) 2008 Conrado Porto Lopes Gouvea
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
from math import *
import Image, ImageDraw
import sys
f = open("data.txt")
#Read the number of entries and the entries itself
lst = [float(line) for line in f]
N = int(lst[0])
lst = lst[1:]
S = N
#Change this to enlarge each square in the table (side in pixels)
L = 1
S = L*N
im = Image.new("RGB", (S, S))
d = ImageDraw.Draw(im)
#Those formulas are used to represent a number between 0 and 1 as a color.
#They were taken from gnuplot.
def rgbf(i, x):
"Given a formula index i and a value x, return formula(x)"
d = {}
d[2] = lambda x: 1
d[3] = lambda x: x
d[5] = lambda x: x**3
d[6] = lambda x: x ** 4
d[7] = lambda x: sqrt(x)
d[11] = lambda x: abs(x-0.5)
d[15] = lambda x: sin(2 * pi * x)
d[21] = lambda x: 3*x
d[22] = lambda x: 3*x-1
d[23] = lambda x: 3*x-2
d[28] = lambda x: abs((3*x-1)/2)
d[30] = lambda x: x/0.32-0.78125
return d[i](x)
#Those are some colors schemes. Each index is the index of the formula
#used by the R, G, and B components
PM3D = (7,5,15)
GRV = (3,11,6)
OCEAN = (23,28,3)
HOT = (21,22,23)
CGRAY = (30,31,32)
def color(x, forms):
"Given a value X, returns a RGB triple using the colors scheme in forms"
rgb = rgbf(forms[0], x), rgbf(forms[1], x), rgbf(forms[2], x)
return tuple(int(c * 255) for c in rgb)
#Just to confirm the number of points...
print 'N',N, 'len(lst)',len(lst)
#Draw the addition/multiplication table
for i in xrange(N):
for j in xrange(N):
k = lst[i*N+j]
y = i * L
x = j * L
#Change "PM3D" to one of the above to change the color scheme
fill = color(k, PM3D)
d.rectangle((x,y,x+L,y+L), fill=fill)
#If you pass a file in the command line, it will write to it, else it will show
#on the screen
if len(sys.argv) > 1:
arq = sys.argv[1]
im.save(arq)
else:
im.show()