sepia, general filter function

This commit is contained in:
Ali Gator 2022-05-01 22:20:25 +02:00
parent c1e5e08a2b
commit 5f7508fe3d
2 changed files with 39 additions and 4 deletions

View File

@ -159,6 +159,42 @@ func grayscale(pixel color.Color) color.Color {
}
}
// sepia toning filter
func sepia(pixel color.Color) color.Color {
c := color.RGBAModel.Convert(pixel).(color.RGBA)
var npr uint8
var npg uint8
var npb uint8
tr := float64(c.R)*0.393 + float64(c.G)*0.769 + float64(c.B)*0.189
tg := float64(c.R)*0.349 + float64(c.G)*0.686 + float64(c.B)*0.168
tb := float64(c.R)*0.272 + float64(c.G)*0.534 + float64(c.B)*0.131
npr = uint8(math.Min(255, tr))
npg = uint8(math.Min(255, tg))
npb = uint8(math.Min(255, tb))
return color.RGBA{
R: npr,
G: npg,
B: npb,
A: c.A,
}
}
// if a filter is specified, apply it to the pixel, if not returns it unchanged
func filter(pixel color.Color) color.Color {
if ConfigRegister.Grayscale {
return grayscale(pixel)
}
if ConfigRegister.Sepia {
return sepia(pixel)
}
return pixel
}
func darken(fc uint8, bc uint8, fa uint8) float64 {
return math.Min(float64(fc), float64(bc))
}
@ -199,10 +235,7 @@ func blendColor(color1 color.Color, color2 color.Color) color.Color {
new := color.RGBA{
R: r, G: g, B: b, A: a,
}
if ConfigRegister.Grayscale == true {
return grayscale(new)
}
return new
return filter(new)
}
// creates a new rectangle with the min height and width from both images

View File

@ -18,6 +18,7 @@ type Config struct {
OutputWidth int `json:output_width`
OutputHeight int `json:output_height`
Grayscale bool
Sepia bool
Amount int
Quiet bool
}
@ -53,6 +54,7 @@ func initConfigRegister() {
flag.StringVar(&ConfigRegister.BaseImage, "base-img", "", "Path to the base image to work with. Random image if not set")
flag.StringVar(&ConfigRegister.Dimensions, "dimensions", "1280x1024", "Out image dimensions. <width>x<height>")
flag.BoolVar(&ConfigRegister.Grayscale, "grayscale", false, "Output image in shade of gray (around 50)")
flag.BoolVar(&ConfigRegister.Sepia, "sepia", false, "Output image with sepia tone")
flag.IntVar(&ConfigRegister.Amount, "amount", 2, "Number of images to blend together")
flag.BoolVar(&ConfigRegister.Quiet, "quiet", false, "No progress messages")
flag.Parse()