draw2d/samples/gopher2/gopher2.go

123 lines
2.9 KiB
Go
Raw Normal View History

2015-07-11 16:44:36 +00:00
// Copyright 2010 The draw2d Authors. All rights reserved.
// created: 21/11/2010 by Laurent Le Goff
// Package gopher2 draws a gopher avatar based on a svg of:
// https://github.com/golang-samples/gopher-vector/
package gopher2
import (
"image"
"image/color"
"math"
2018-11-16 11:21:14 +00:00
"git.fromouter.space/crunchy-rocks/draw2d"
"git.fromouter.space/crunchy-rocks/draw2d/draw2dkit"
"git.fromouter.space/crunchy-rocks/draw2d/samples"
2015-07-11 16:44:36 +00:00
)
// Main draws a rotated face of the gopher. Afterwards it returns
// the filename. This should only be used during testing.
func Main(gc draw2d.GraphicContext, ext string) (string, error) {
gc.SetStrokeColor(image.Black)
gc.SetFillColor(image.White)
gc.Save()
// Draw a (partial) gopher
2015-07-12 22:33:03 +00:00
gc.Translate(-60, 65)
2015-07-11 16:44:36 +00:00
gc.Rotate(-30 * (math.Pi / 180.0))
Draw(gc, 48, 48, 240, 72)
gc.Restore()
// Return the output filename
return samples.Output("gopher2", ext), nil
}
// Draw a gopher head (not rotated)
func Draw(gc draw2d.GraphicContext, x, y, w, h float64) {
h23 := (h * 2) / 3
blf := color.RGBA{0, 0, 0, 0xff} // black
wf := color.RGBA{0xff, 0xff, 0xff, 0xff} // white
nf := color.RGBA{0x8B, 0x45, 0x13, 0xff} // brown opaque
brf := color.RGBA{0x8B, 0x45, 0x13, 0x99} // brown transparant
brb := color.RGBA{0x8B, 0x45, 0x13, 0xBB} // brown transparant
// round head top
2015-07-12 22:33:03 +00:00
gc.MoveTo(x, y+h*1.002)
gc.CubicCurveTo(x+w/4, y-h/3, x+3*w/4, y-h/3, x+w, y+h*1.002)
2015-07-11 16:44:36 +00:00
gc.Close()
gc.SetFillColor(brb)
gc.Fill()
2015-07-11 16:44:36 +00:00
// rectangle head bottom
draw2dkit.RoundedRectangle(gc, x, y+h, x+w, y+h+h, h/5, h/5)
2015-07-11 16:44:36 +00:00
gc.Fill()
2015-07-11 16:44:36 +00:00
// left ear outside
draw2dkit.Circle(gc, x, y+h, w/12)
2015-07-11 16:44:36 +00:00
gc.SetFillColor(brf)
gc.Fill()
2015-07-11 16:44:36 +00:00
// left ear inside
draw2dkit.Circle(gc, x, y+h, 0.5*w/12)
2015-07-11 16:44:36 +00:00
gc.SetFillColor(nf)
gc.Fill()
2015-07-11 16:44:36 +00:00
// right ear outside
draw2dkit.Circle(gc, x+w, y+h, w/12)
2015-07-11 16:44:36 +00:00
gc.SetFillColor(brf)
gc.Fill()
2015-07-11 16:44:36 +00:00
// right ear inside
draw2dkit.Circle(gc, x+w, y+h, 0.5*w/12)
2015-07-11 16:44:36 +00:00
gc.SetFillColor(nf)
gc.Fill()
2015-07-11 16:44:36 +00:00
// left eye outside white
draw2dkit.Circle(gc, x+w/3, y+h23, w/9)
2015-07-11 16:44:36 +00:00
gc.SetFillColor(wf)
gc.Fill()
2015-07-11 16:44:36 +00:00
// left eye black
draw2dkit.Circle(gc, x+w/3+w/24, y+h23, 0.5*w/9)
2015-07-11 16:44:36 +00:00
gc.SetFillColor(blf)
gc.Fill()
2015-07-11 16:44:36 +00:00
// left eye inside white
draw2dkit.Circle(gc, x+w/3+w/24+w/48, y+h23, 0.2*w/9)
2015-07-11 16:44:36 +00:00
gc.SetFillColor(wf)
gc.Fill()
2015-07-11 16:44:36 +00:00
// right eye outside white
draw2dkit.Circle(gc, x+w-w/3, y+h23, w/9)
2015-07-11 16:44:36 +00:00
gc.Fill()
2015-07-11 16:44:36 +00:00
// right eye black
draw2dkit.Circle(gc, x+w-w/3+w/24, y+h23, 0.5*w/9)
2015-07-11 16:44:36 +00:00
gc.SetFillColor(blf)
gc.Fill()
2015-07-11 16:44:36 +00:00
// right eye inside white
draw2dkit.Circle(gc, x+w-(w/3)+w/24+w/48, y+h23, 0.2*w/9)
2015-07-11 16:44:36 +00:00
gc.SetFillColor(wf)
gc.Fill()
2015-07-11 16:44:36 +00:00
// left tooth
gc.SetFillColor(wf)
draw2dkit.RoundedRectangle(gc, x+w/2-w/8, y+h+h/2.5, x+w/2-w/8+w/8, y+h+h/2.5+w/6, w/10, w/10)
2015-07-11 16:44:36 +00:00
gc.Fill()
2015-07-11 16:44:36 +00:00
// right tooth
draw2dkit.RoundedRectangle(gc, x+w/2, y+h+h/2.5, x+w/2+w/8, y+h+h/2.5+w/6, w/10, w/10)
2015-07-11 16:44:36 +00:00
gc.Fill()
2015-07-11 16:44:36 +00:00
// snout
draw2dkit.Ellipse(gc, x+(w/2), y+h+h/2.5, w/6, w/12)
2015-07-11 16:44:36 +00:00
gc.SetFillColor(nf)
gc.Fill()
2015-07-11 16:44:36 +00:00
// nose
draw2dkit.Ellipse(gc, x+(w/2), y+h+h/7, w/10, w/12)
2015-07-11 16:44:36 +00:00
gc.SetFillColor(blf)
gc.Fill()
}