module primitives; // -*-C-*-ish import Image; import CGI; import Time; import Logger; public data DrawColour(Int r, Int g, Int b); public data Shape = Rectangle(DrawColour rcol, Int rx, Int ry) | Ellipse(DrawColour ecol, Int ex, Int ey) | Triangle(DrawColour tcol, Int base, Int height); public data Primitive(Shape shape, Int x, Int y); public data Drawing(Int x, Int y, DrawColour bg,[Primitive] ps); public [String] validCols = ["Red","Green","Blue","Yellow","White"]; public DrawColour mkColour(String s) { if (s=="Red") { return DrawColour(255,0,0); } else if (s=="Green") { return DrawColour(0,255,0); } else if (s=="Blue") { return DrawColour(0,0,255); } else if (s=="Yellow") { return DrawColour(255,255,0); } else if (s=="White") { return DrawColour(255,255,255); } else { srand(now); return DrawColour(rand()%255,rand()%255,rand()%255); } } public Void draw(Drawing d) { header("Content-type: image/png"); i = create(d.x,d.y); bg = makeColour(i,d.bg.r,d.bg.g,d.bg.b); // DrawColour col = DrawColour(0,0,0); for p in d.ps { case p.shape of { Rectangle(col,x,y) -> c = makeColour(i,col.r,col.g,col.b); rectangle(i, p.x, p.y, p.shape.rx+p.x, p.shape.ry+p.y, c, true); | Ellipse(col,x,y) -> c = makeColour(i,col.r,col.g,col.b); ellipse(i, p.x, p.y, p.shape.ex, p.shape.ey, c, true); | Triangle(col,base,height) -> c = makeColour(i,col.r,col.g,col.b); poly(i,[(p.x,p.y),(p.x+base/2,p.y-height),(p.x+base,p.y)],c,true); } } CGI::flushAll(); makePNG(i); }