From f39b2eb216ffca60d62e20c3369b755eb68e8567 Mon Sep 17 00:00:00 2001 From: Mendel E Date: Thu, 7 May 2020 17:14:30 -0400 Subject: [PATCH] Rework diagonal drawing Fix rectangles too. --- image.go | 27 ++++++++++++++------------- options.go | 3 --- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/image.go b/image.go index bb39957..b5f9b46 100644 --- a/image.go +++ b/image.go @@ -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} } diff --git a/options.go b/options.go index 117673d..49955c2 100644 --- a/options.go +++ b/options.go @@ -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 } -- 2.38.5