//showtext displays text func showtext(x, y openvg.VGfloat, t, align, font string, fs openvg.VGfloat) { //t := s // fromUTF8(s) fontsize := int(fs) switch align { case "center", "middle", "mid", "c": openvg.TextMid(x, y, t, font, fontsize) case "right", "end", "e": openvg.TextEnd(x, y, t, font, fontsize) default: openvg.Text(x, y, t, font, fontsize) } }
func gradient(width, height int) { w := openvg.VGfloat(width) h := openvg.VGfloat(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 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() }
// 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 openvg.VGfloat, 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 []openvg.VGfloat if needpoly { xpoly = make([]openvg.VGfloat, nd+2) ypoly = make([]openvg.VGfloat, 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 := openvg.VGfloat(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 := openvg.VGfloat(math.Floor(float64(miny))) top := openvg.VGfloat(math.Ceil(float64(maxy))) yrange := top - bot interval := yrange / openvg.VGfloat(settings.size["yinterval"]) for yax := bot; yax <= top; yax += interval { yaxp := fmap(yax, bot, top, openvg.VGfloat(y), openvg.VGfloat(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) }
// 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 := openvg.VGfloat(height) * .95 sx := openvg.VGfloat(width) * 0.10 sy := top sw := openvg.VGfloat(width) * .05 sh := openvg.VGfloat(height) * .045 dotsize := openvg.VGfloat(7.0) spacing := openvg.VGfloat(2.0) fontsize := int(openvg.VGfloat(height) * .033) shapecolor := Color{202, 225, 255, 1.0} openvg.Start(width, height) openvg.FillRGB(128, 0, 0, 1) openvg.TextEnd(openvg.VGfloat(width-20), openvg.VGfloat(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 := []openvg.VGfloat{sx, sx + (sw / 4), sx + (sw / 2), sx + ((sw * 3) / 4), sx + sw} py := []openvg.VGfloat{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 openvg.VGfloat 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() }