// rshapes draws shapes with random colors, openvg.Strokes, and sizes. func rshapes(width, height, n int) { var sx, sy, cx, cy, px, py, ex, ey, pox, poy float64 np := 10 polyx := make([]float64, np) polyy := make([]float64, np) openvg.Start(width, height) for i := 0; i < n; i++ { openvg.FillRGB(randcolor(), randcolor(), randcolor(), rand.Float64()) openvg.Ellipse(randf(width), randf(height), randf(200), randf(100)) openvg.Circle(randf(width), randf(height), randf(100)) openvg.Rect(randf(width), randf(height), randf(200), randf(100)) openvg.Arc(randf(width), randf(height), randf(200), randf(200), randf(360), randf(360)) sx = randf(width) sy = randf(height) openvg.StrokeRGB(randcolor(), randcolor(), randcolor(), 1) openvg.StrokeWidth(randf(5)) openvg.Line(sx, sy, sx+randf(200), sy+randf(100)) openvg.StrokeWidth(0) sx = randf(width) sy = randf(height) ex = sx + randf(200) ey = sy cx = sx + ((ex - sx) / 2.0) cy = sy + randf(100) openvg.Qbezier(sx, sy, cx, cy, ex, ey) sx = randf(width) sy = randf(height) ex = sx + randf(200) ey = sy cx = sx + ((ex - sx) / 2.0) cy = sy + randf(100) px = cx py = sy - randf(100) openvg.Cbezier(sx, sy, cx, cy, px, py, ex, ey) pox = randf(width) poy = randf(height) for j := 0; j < np; j++ { polyx[j] = pox + randf(200) polyy[j] = poy + randf(100) } openvg.Polygon(polyx, polyy) // , np) pox = randf(width) poy = randf(height) for j := 0; j < np; j++ { polyx[j] = pox + randf(200) polyy[j] = poy + randf(100) } openvg.Polyline(polyx, polyy) // , np) } openvg.FillRGB(128, 0, 0, 1) openvg.Text(20, 20, "OpenVG on the Raspberry Pi", "sans", 32) openvg.End() }
func main() { var cx, cy, cw, ch, midy int message := "Now is the time for all good men to come to the aid of the party" w, h := openvg.Init() var speed openvg.VGfloat = 15.0 var x openvg.VGfloat = 0 midy = (h / 2) fontsize := w / 50 cx = 0 ch = fontsize * 2 cw = w cy = midy - (ch / 2) rx, ry, rw, rh := openvg.VGfloat(cx), openvg.VGfloat(cy), openvg.VGfloat(cw), openvg.VGfloat(ch) // scroll the text, only in the clipping rectangle for x = 0; x < rw+speed; x += speed { openvg.Start(w, h) openvg.Background(255, 255, 255) openvg.FillRGB(0, 0, 0, .2) openvg.Rect(rx, ry, rw, rh) openvg.ClipRect(cx, cy, cw, ch) openvg.Translate(x, ry+openvg.VGfloat(fontsize/2)) openvg.FillRGB(0, 0, 0, 1) openvg.Text(0, 0, message, "sans", fontsize) openvg.ClipEnd() openvg.End() } bufio.NewReader(os.Stdin).ReadBytes('\n') openvg.Finish() os.Exit(0) }
func paintBG(w, h openvg.VGfloat) { if !showTrails { openvg.Background(0, 0, 0) return } openvg.FillRGB(0, 0, 0, 0.3) openvg.Rect(0, 0, w, h) }
// testpattern shows a test pattern func testpattern(w, h int, s string) { var midx, midy1, midy2, midy3 float64 fontsize := 256 h2 := float64(h / 2) by := float64(h - 100) bx := float64(w - 100) tw1 := &FW{"mono", 0, fontsize} tw2 := &FW{"sans", 0, fontsize} tw3 := &FW{"serif", 0, fontsize} openvg.Start(w, h) // colored squares in the corners openvg.FillRGB(255, 0, 0, 1) openvg.Rect(0, 0, 100, 100) openvg.FillRGB(0, 255, 0, 1) openvg.Rect(0, by, 100, 100) openvg.FillRGB(0, 0, 255, 1) openvg.Rect(bx, 0, 100, 100) openvg.FillRGB(128, 128, 128, 1) openvg.Rect(bx, by, 100, 100) // for each font, (Sans, Serif, Mono), adjust the string to the w tw1.fitwidth(w, 20, s) tw2.fitwidth(w, 20, s) tw3.fitwidth(w, 20, s) midx = float64(w / 2) // Adjust the baselines to be medial midy1 = h2 + 20 + float64((tw1.fontsize)/2) midy2 = h2 - float64((tw2.fontsize)/2) midy3 = h2 - 20 - float64(tw2.fontsize) - float64((tw3.fontsize)/2) openvg.FillRGB(128, 128, 128, 1) openvg.TextMid(midx, midy1, s, tw1.font, tw1.fontsize) openvg.FillRGB(128, 0, 0, 1) openvg.TextMid(midx, midy2, s, tw2.font, tw2.fontsize) openvg.FillRGB(0, 0, 128, 1) openvg.TextMid(midx, midy3, s, tw3.font, tw3.fontsize) openvg.End() }
// countdown shows a countdown display to the top of minute func (d *display) countdown() { tick := time.NewTicker(1 * time.Second) ty := d.height / 2 th := d.height / 20 size := d.width / 70 for delay := 60 - time.Now().Second(); delay > 0; delay-- { select { case <-tick.C: tx := d.width * (openvg.VGfloat(60-delay) / 60) openvg.BackgroundColor(d.bgcolor) openvg.FillColor("black") openvg.Rect(0, ty, d.width, th) openvg.FillColor("white") openvg.TextEnd(tx, ty+(th/4), fmt.Sprintf("start in %d ", delay), "sans", int(size)) openvg.Rect(tx, ty, d.width-tx, th) openvg.End() } } openvg.BackgroundColor(d.bgcolor) }
// main plots data from specified files or standard input in a // grid where plotc specifies the number of columns. func main() { w, h := openvg.Init() openvg.Start(w, h) openvg.FillColor("white") openvg.Rect(0, 0, gwidth, gheight) filenames := flag.Args() if len(filenames) == 0 { doplot(beginx, beginy, "") } else { plotgrid(beginx, beginy, filenames) } openvg.SaveEnd("vgplot.raw") bufio.NewReader(os.Stdin).ReadByte() openvg.Finish() }
func gradient(width, height int) { w := float64(width) h := float64(height) stops := []openvg.Offcolor{ {0.0, openvg.RGB{255, 255, 255}, 1.0}, {0.5, openvg.RGB{128, 128, 128}, 1.0}, {1.0, openvg.RGB{0, 0, 0}, 1.0}, } x1 := w / 8 x2 := (w * 3) / 8 y1 := h / 3 y2 := (h * 2) / 3 cx := (w * 3) / 4 cy := (h / 2) r := (x2 - x1) fx := cx + (r / 4) fy := cy + (r / 4) openvg.Start(width, height) openvg.BackgroundRGB(128, 128, 128, 1) openvg.FillLinearGradient(x1, y1, x2, y2, stops) openvg.Rect(x1, y1, x2-x1, y2-y1) openvg.FillRadialGradient(cx, cy, fx, fy, r, stops) openvg.Circle(cx, cy, r) openvg.FillRGB(0, 0, 0, 0.3) openvg.Circle(x1, y1, 10) openvg.Circle(x2, y2, 10) openvg.Circle(cx, cy, 10) openvg.Circle(cx+r/2, cy, 10) openvg.Circle(fx, fy, 10) openvg.FillColor("black") SansTypeface := "sans" openvg.TextMid(x1, y1-20, "(x1, y1)", SansTypeface, 18) openvg.TextMid(x2, y2+10, "(x2, y2)", SansTypeface, 18) openvg.TextMid(cx, cy, "(cx, cy)", SansTypeface, 18) openvg.TextMid(fx, fy, "(fx, fy)", SansTypeface, 18) openvg.TextEnd(cx+(r/2)+20, cy, "r", SansTypeface, 18) openvg.TextMid(x1+((x2-x1)/2), h/6, "Linear Gradient", SansTypeface, 36) openvg.TextMid(cx, h/6, "Radial Gradient", SansTypeface, 36) openvg.End() }
func imagetable(w, h int) { imgw, imgh := 422, 238 itable := []it{ {"desert0.jpg", imgw, imgh}, {"desert1.jpg", imgw, imgh}, {"desert2.jpg", imgw, imgh}, {"desert3.jpg", imgw, imgh}, {"desert4.jpg", imgw, imgh}, {"desert5.jpg", imgw, imgh}, {"desert6.jpg", imgw, imgh}, {"desert7.jpg", imgw, imgh}, //{"http://farm4.static.flickr.com/3546/3338566612_9c56bfb53e_m.jpg", 240, 164}, //{"http://farm4.static.flickr.com/3642/3337734413_e36baba755_m.jpg", 240, 164}, } offset := openvg.VGfloat(50) left := offset bot := openvg.VGfloat(h-imgh) - offset gutter := offset x := left y := bot openvg.Start(w, h) openvg.BackgroundColor("black") for _, iname := range itable { openvg.Image(x, y, iname.width, iname.height, iname.name) openvg.FillRGB(255, 255, 255, 0.3) openvg.Rect(x, y, openvg.VGfloat(imgw), 32) openvg.FillRGB(0, 0, 0, 1) openvg.TextMid(x+openvg.VGfloat(imgw/2), y+10, iname.name, "sans", 16) x += openvg.VGfloat(iname.width) + gutter if x > openvg.VGfloat(w) { x = left y -= openvg.VGfloat(iname.height) + gutter } } y = openvg.VGfloat(h) * 0.1 openvg.FillRGB(128, 128, 128, 1) openvg.TextMid(openvg.VGfloat(w/2), 100, "Joshua Tree National Park", "sans", 48) openvg.End() }
func imagetable(w, h int) { imgw, imgh := 422, 238 itable := []string{ "desert0.jpg", "desert1.jpg", "desert2.jpg", "desert3.jpg", "desert4.jpg", "desert5.jpg", "desert6.jpg", "desert7.jpg", } left := 50.0 bot := float64(h-imgh) - 50.0 gutter := 50.0 x := left y := bot openvg.Start(w, h) openvg.BackgroundColor("black") for _, iname := range itable { openvg.Image(x, y, imgw, imgh, iname) openvg.FillRGB(255, 255, 255, 0.3) openvg.Rect(x, y, float64(imgw), 32) openvg.FillRGB(0, 0, 0, 1) openvg.TextMid(x+float64(imgw/2), y+10, iname, "sans", 16) x += float64(imgw) + gutter if x > float64(w) { x = left y -= float64(imgh) + gutter } } y = float64(h) * 0.1 openvg.FillRGB(128, 128, 128, 1) openvg.TextMid(float64(w/2), 100, "Joshua Tree National Park", "sans", 48) openvg.End() }
func main() { var ( filename = flag.String("f", "svgcolors.txt", "input file") fontname = flag.String("font", "sans", "fontname") neg = flag.Bool("neg", false, "negative") showrgb = flag.Bool("rgb", false, "show RGB") showcode = flag.Bool("showcode", true, "show colors and codes") circsw = flag.Bool("circle", true, "circle swatch") outline = flag.Bool("outline", false, "outline swatch") fontsize = flag.Int("fs", 12, "fontsize") rowsize = flag.Int("r", 32, "rowsize") colw = flag.Float64("c", 340, "column size") swatch = flag.Float64("s", 16, "swatch size") gutter = flag.Float64("g", 12, "gutter") err error tcolor, line string ) flag.Parse() f, oerr := os.Open(*filename) if oerr != nil { fmt.Fprintf(os.Stderr, "%v\n", oerr) return } width, height := openvg.Init() openvg.Start(width, height) fw := openvg.VGfloat(width) fh := openvg.VGfloat(height) if *neg { openvg.FillColor("black") openvg.Rect(0, 0, fw, fh) tcolor = "white" } else { openvg.FillColor("white") openvg.Rect(0, 0, fw, fh) tcolor = "black" } top := fh - 32.0 left := openvg.VGfloat(32.0) cw := openvg.VGfloat(*colw) sw := openvg.VGfloat(*swatch) g := openvg.VGfloat(*gutter) in := bufio.NewReader(f) for x, y, nr := left, top, 0; err == nil; nr++ { line, err = in.ReadString('\n') fields := strings.Split(strings.TrimSpace(line), "\t") if nr%*rowsize == 0 && nr > 0 { x += cw y = top } if len(fields) == 3 { var red, green, blue uint8 fmt.Sscanf(fields[2], "%d,%d,%d", &red, &green, &blue) openvg.FillRGB(red, green, blue, 1) if *outline { openvg.StrokeColor("black") openvg.StrokeWidth(1) } if *circsw { openvg.Circle(x+sw/2, y+sw/2, sw) } else { openvg.Rect(x, y, sw, sw) } openvg.StrokeWidth(0) openvg.FillColor(tcolor) openvg.Text(x+sw+openvg.VGfloat(*fontsize/2), y, fields[0], *fontname, *fontsize) var label string if *showcode { if *showrgb { label = fields[1] } else { label = fields[2] } openvg.FillColor("gray") openvg.TextEnd(x+cw-(sw+g), y, label, *fontname, *fontsize) } } y -= (sw + g) } openvg.End() bufio.NewReader(os.Stdin).ReadBytes('\n') openvg.Finish() }
// makepi draws the Raspberry Pi func makepi(x, y, w, h openvg.VGfloat) { // dimensions socw := h / 5 compw := h / 5 cjw := h / 10 cjh := h / 8 audw := h / 5 aujw := h / 10 aujh := cjh / 2 hdw := w / 6 hdh := w / 10 gpw := w / 3 gph := h / 8 pw := h / 10 usw := w / 5 ush := h / 5 etw := w / 5 eth := h / 5 sdw := w / 6 sdh := w / 4 offset := (w / 2) / 10 w34 := (w * 3) / 4 w2 := w / 2 h2 := h / 2 h40 := (h * 2) / 5 openvg.FillRGB(0, 128, 0, 1) openvg.Rect(x, y, w, h) // board openvg.FillRGB(255, 255, 0, 1) openvg.Rect(x+w2, (y+h)-compw, compw, compw) // composite openvg.FillRGB(192, 192, 192, 1) openvg.Rect(x+w2+(cjw/2), y+h, cjw, cjh) // composite jack openvg.FillRGB(0, 0, 0, 1) openvg.Rect(x+w34, y+h-audw, audw, audw) // audio openvg.Rect(x+w34+(aujw/2), y+h, aujw, aujh) // audio jack openvg.FillRGB(192, 192, 192, 1) openvg.Rect(x+w2, y, hdw, hdh) // HDMI openvg.Rect((x+w)-etw, y, etw, eth) // Ethernet openvg.Rect((x+w+offset)-usw, y+h40, usw, ush) // USB openvg.Rect(x, y, pw, pw) // Power openvg.FillRGB(0, 0, 0, 1) openvg.Rect(x+(w*2)/5, y+h40, socw, socw) // SoC openvg.Rect(x, (y+h)-gph, gpw, gph) // GPIO openvg.FillRGB(0, 0, 255, 1) openvg.Rect(x-sdw, (y+h2)-(sdh/2), sdw, sdh) // SD card }
// refcard shows a reference card of shapes func refcard(width, height int) { shapenames := []string{ "Circle", "Ellipse", "Rectangle", "Rounded Rectangle", "Line", "Polyline", "Polygon", "Arc", "Quadratic Bezier", "Cubic Bezier", "Image", } top := float64(height) * .95 sx := float64(width) * 0.10 sy := top sw := float64(width) * .05 sh := float64(height) * .045 dotsize := 7.0 spacing := 2.0 fontsize := int(float64(height) * .033) shapecolor := Color{202, 225, 255, 1.0} openvg.Start(width, height) openvg.FillRGB(128, 0, 0, 1) openvg.TextEnd(float64(width-20), float64(height/2), "OpenVG on the Raspberry Pi", "sans", fontsize+(fontsize/2)) openvg.FillRGB(0, 0, 0, 1) for _, s := range shapenames { openvg.Text(sx+sw+sw/2, sy, s, "sans", fontsize) sy -= sh * spacing } sy = top cx := sx + (sw / 2) ex := sx + sw openvg.FillRGB(shapecolor.red, shapecolor.green, shapecolor.blue, shapecolor.alpha) openvg.Circle(cx, sy, sw) coordpoint(cx, sy, dotsize, shapecolor) sy -= sh * spacing openvg.Ellipse(cx, sy, sw, sh) coordpoint(cx, sy, dotsize, shapecolor) sy -= sh * spacing openvg.Rect(sx, sy, sw, sh) coordpoint(sx, sy, dotsize, shapecolor) sy -= sh * spacing openvg.Roundrect(sx, sy, sw, sh, 20, 20) coordpoint(sx, sy, dotsize, shapecolor) sy -= sh * spacing openvg.StrokeWidth(1) openvg.StrokeRGB(204, 204, 204, 1) openvg.Line(sx, sy, ex, sy) coordpoint(sx, sy, dotsize, shapecolor) coordpoint(ex, sy, dotsize, shapecolor) sy -= sh px := []float64{sx, sx + (sw / 4), sx + (sw / 2), sx + ((sw * 3) / 4), sx + sw} py := []float64{sy, sy - sh, sy, sy - sh, sy} openvg.Polyline(px, py) // , 5) coordpoint(px[0], py[0], dotsize, shapecolor) coordpoint(px[1], py[1], dotsize, shapecolor) coordpoint(px[2], py[2], dotsize, shapecolor) coordpoint(px[3], py[3], dotsize, shapecolor) coordpoint(px[4], py[4], dotsize, shapecolor) sy -= sh * spacing py[0] = sy py[1] = sy - sh py[2] = sy - (sh / 2) py[3] = py[1] - (sh / 4) py[4] = sy openvg.Polygon(px, py) // , 5) sy -= (sh * spacing) + sh openvg.Arc(sx+(sw/2), sy, sw, sh, 0, 180) coordpoint(sx+(sw/2), sy, dotsize, shapecolor) sy -= sh * spacing var cy, ey float64 cy = sy + (sh / 2) ey = sy openvg.Qbezier(sx, sy, cx, cy, ex, ey) coordpoint(sx, sy, dotsize, shapecolor) coordpoint(cx, cy, dotsize, shapecolor) coordpoint(ex, ey, dotsize, shapecolor) sy -= sh * spacing ey = sy cy = sy + sh openvg.Cbezier(sx, sy, cx, cy, cx, sy, ex, ey) coordpoint(sx, sy, dotsize, shapecolor) coordpoint(cx, cy, dotsize, shapecolor) coordpoint(cx, sy, dotsize, shapecolor) coordpoint(ex, ey, dotsize, shapecolor) sy -= (sh * spacing * 1.5) openvg.Image(sx, sy, 110, 110, "starx.jpg") openvg.End() }
// plot places a plot at the specified location with the specified dimemsions // using the specified settings, using the specified data func plot(x, y, w, h float64, settings plotset, d []rawdata) { nd := len(d) if nd < 2 { fmt.Fprintf(os.Stderr, "%d is not enough points to plot\n", len(d)) return } // Compute the minima and maxima of the data maxx, minx := d[0].x, d[0].x maxy, miny := d[0].y, d[0].y for _, v := range d { if v.x > maxx { maxx = v.x } if v.y > maxy { maxy = v.y } if v.x < minx { minx = v.x } if v.y < miny { miny = v.y } } // Prepare for a area or line chart by allocating // polygon coordinates; for the horizon plot, you need two extra coordinates // for the extrema. needpoly := settings.opt["area"] || settings.opt["connect"] var xpoly, ypoly []float64 if needpoly { xpoly = make([]float64, nd+2) ypoly = make([]float64, nd+2) // preload the extrema of the polygon, // the bottom left and bottom right of the plot's rectangle xpoly[0] = x ypoly[0] = y xpoly[nd+1] = x + w ypoly[nd+1] = y } // Draw the plot's bounding rectangle if settings.opt["showbg"] && !settings.opt["sameplot"] { openvg.FillColor(settings.attr["bgcolor"]) openvg.Rect(x, y, w, h) } // Loop through the data, drawing items as specified spacer := 10.0 for i, v := range d { xp := fmap(v.x, minx, maxx, x, x+w) yp := fmap(v.y, miny, maxy, y, y+h) if needpoly { xpoly[i+1] = xp ypoly[i+1] = yp } if settings.opt["showbar"] { openvg.StrokeColor(settings.attr["barcolor"]) openvg.StrokeWidth(settings.size["barsize"]) openvg.Line(xp, yp, xp, y) } if settings.opt["showdot"] { openvg.FillColor(settings.attr["dotcolor"]) openvg.StrokeWidth(0) openvg.Circle(xp, yp, settings.size["dotsize"]) } if settings.opt["showx"] { if i%int(settings.size["xinterval"]) == 0 { openvg.FillColor("black") openvg.TextMid(xp, y-(spacer*2), fmt.Sprintf("%d", int(v.x)), settings.attr["font"], int(settings.size["fontsize"])) openvg.StrokeColor("silver") openvg.StrokeWidth(1) openvg.Line(xp, y, xp, y-spacer) } openvg.StrokeWidth(0) } } // Done constructing the points for the area or line plots, display them in one shot if settings.opt["area"] { openvg.FillColor(settings.attr["areacolor"]) openvg.Polygon(xpoly, ypoly) } if settings.opt["connect"] { openvg.StrokeColor(settings.attr["linecolor"]) openvg.StrokeWidth(settings.size["linesize"]) openvg.Polyline(xpoly[1:nd+1], ypoly[1:nd+1]) } // Put on the y axis labels, if specified if settings.opt["showy"] { bot := math.Floor(miny) top := math.Ceil(maxy) yrange := top - bot interval := yrange / float64(settings.size["yinterval"]) for yax := bot; yax <= top; yax += interval { yaxp := fmap(yax, bot, top, float64(y), float64(y+h)) openvg.FillColor("black") openvg.TextEnd(x-spacer, yaxp, fmt.Sprintf("%.1f", yax), settings.attr["font"], int(settings.size["fontsize"])) openvg.StrokeColor("silver") openvg.StrokeWidth(1) openvg.Line(x-spacer, yaxp, x, yaxp) } openvg.StrokeWidth(0) } // Finally, tack on the label, if specified if len(settings.attr["label"]) > 0 { openvg.FillColor(settings.attr["labelcolor"], 0.3) openvg.TextMid(x+(w/2), y+(h/2), settings.attr["label"], settings.attr["font"], int(w/8)) // int(settings.size["fontsize"])) } openvg.StrokeWidth(0) }
// regionFill colors a rectangular region, and sets the fill color for subsequent text func (d *dimen) regionFill(bgcolor, textcolor string) { openvg.FillColor(bgcolor) openvg.Rect(d.x, d.y, d.width, d.height) openvg.FillColor(textcolor) }