From 5f7508fe3d70c95dd7af5823ccd5c2185d62d8db Mon Sep 17 00:00:00 2001 From: Gator Date: Sun, 1 May 2022 22:20:25 +0200 Subject: [PATCH] sepia, general filter function --- blend.go | 41 +++++++++++++++++++++++++++++++++++++---- config.go | 2 ++ 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/blend.go b/blend.go index a3f8c34..723b80b 100644 --- a/blend.go +++ b/blend.go @@ -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 diff --git a/config.go b/config.go index 6ba8250..895870c 100644 --- a/config.go +++ b/config.go @@ -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. x") 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()