~mendelmaleh/dummy

f39b2eb216ffca60d62e20c3369b755eb68e8567 — Mendel E 1 year, 6 months ago b55e2dd
Rework diagonal drawing

Fix rectangles too.
2 files changed, 14 insertions(+), 16 deletions(-)

M image.go
M options.go
M image.go => image.go +14 -13
@@ 17,23 17,24 @@ func Image(opt Options) image.Image {
	case "plain":
		draw.Draw(im, im.Bounds(), image.NewUniform(opt.Colors[0]), image.Point{}, draw.Src)
	case "diagonal":
		var c [2]color.RGBA
		copy(c[:], opt.Colors)
		im.drawDiagonal(opt.Width, opt.Height, c)
		h, w := float64(im.Bounds().Max.Y-im.Bounds().Min.Y), float64(im.Bounds().Max.X-im.Bounds().Min.X)

		draw.Draw(im, im.Bounds(), image.NewUniform(opt.Colors[0]), image.Point{}, draw.Src)
		draw.DrawMask(im, im.Bounds(), image.NewUniform(opt.Colors[1]), image.Point{}, &diagonal{im, h, w}, image.Point{}, draw.Over)
	}

	return im
}

func (im *img) drawDiagonal(w, h int, c [2]color.RGBA) {
	for x := 0; x < w; x++ {
		for y := 0; y < h; y++ {
			// if y < h/w)*x) {
			if y <= h-x {
				im.Set(x, y, c[0])
			} else {
				im.Set(x, y, c[1])
			}
		}
type diagonal struct {
	image.Image
	h, w float64
}

func (im *diagonal) At(x, y int) color.Color {
	if float64(y)+0.5 < im.h-(im.h/im.w)*float64(x) {
		return color.Alpha{}
	}

	return color.Alpha{0xff}
}

M options.go => options.go +0 -3
@@ 94,7 94,4 @@ func (opt *Options) Normalize() {
	if opt.Width > 600 {
		opt.Width = 600
	}

	// TODO: fix rectangles, for now only squares
	opt.Height = opt.Width
}