diff --git a/src/colors.py b/src/colors.py new file mode 100644 index 0000000..d62b115 --- /dev/null +++ b/src/colors.py @@ -0,0 +1,188 @@ +# find named color that is closest to rgb values +def closest_color(rgb,names): + name="" + mindist=float('inf') + for i in range(len(names)): + dist=(rgb[0]-names[i][1])**2+(rgb[1]-names[i][2])**2+(rgb[2]-names[i][3])**2 + if dist2: + self.message="error: could not open file: too many arguments -- usage: ':export '" + return + elif len(argv)==1: + self.message="error: could not open file: no argument found -- usage: ':export '" + return + + # check that the file can be edited + (ret,self.message)=filecheck.check_edit(argv[1]) + # error + if ret<0: + return + + # export + self.app.painter.export_tikz(argv[1]) diff --git a/src/painter.py b/src/painter.py index cf71c6b..1b518bf 100644 --- a/src/painter.py +++ b/src/painter.py @@ -10,6 +10,8 @@ from polyomino import Square_element from tools import remove_fromlist +import colors + # painter class class Painter(Widget): @@ -451,6 +453,36 @@ class Painter(Widget): ff.close() self.draw() + # export to tikz + def export_tikz(self,file): + # abort if no particles + if len(self.particles)==0: + return + ff=open(file,"w") + + # header + ff.write("\documentclass{standalone}\n") + ff.write("\n") + ff.write("\\usepackage[svgnames]{xcolor}\n") + ff.write("\\usepackage{tikz}\n") + ff.write("\\usepackage{jam}\n") + ff.write("\n") + ff.write("\\begin{document}\n") + ff.write("\\begin{tikzpicture}\n") + ff.write("\n") + + # write position of particles + for particle in self.particles: + if type(particle)==Cross: + ff.write("\cross{"+colors.closest_color(particle.color,colors.xcolor_names)+"}") + ff.write("{{({:05.2f},{:05.2f})}};\n".format(particle.squares[0].pos.x-self.particles[0].squares[0].pos.x,particle.squares[0].pos.y-self.particles[0].squares[0].pos.y)) + + ff.write("\\end{tikzpicture}\n") + ff.write("\\end{document}\n") + ff.write("\n") + + ff.close() + # set zoom level def set_zoom(self,level): Square_element.size=level*50