~mendelmaleh/dummy

ref: 67121cb151871d5b36e6e29fefbfbd9c76e5b99d dummy/image.go -rw-r--r-- 914 bytes
67121cb1Mendel E Normalize in opt.FromForm directly 1 year, 6 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package main

import (
	"image"
	"image/color"
	"image/draw"
)

type img struct {
	*image.RGBA
}

func Image(opt Options) image.Image {
	im := img{image.NewRGBA(image.Rectangle{image.Point{}, image.Point{opt.Width, opt.Height}})}

	switch opt.Pattern {
	case "plain":
		draw.Draw(im, im.Bounds(), image.NewUniform(opt.Colors[0]), image.Point{}, draw.Src)
	case "diagonal":
		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
}

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}
}