/
greyscale.go
71 lines (61 loc) · 2.03 KB
/
greyscale.go
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package cmd
import (
"github.com/Nyarum/hadfield"
"github.com/Nyarum/img/greyscale"
"github.com/Nyarum/img/utils"
)
var (
greyscaleAverage, greyscaleLightness, greyscaleLuminosity bool
greyscaleRed, greyscaleGreen, greyscaleBlue bool
greyscaleMaximal, greyscaleMinimal, greyscalePhotoshop bool
)
func Greyscale() *hadfield.Command {
cmd := &hadfield.Command{
Usage: "greyscale [options]",
Short: "convert image to greyscale",
Long: `
Greyscale takes an image from STDIN, and prints to STDOUT a greyscale version
--average # Use average method
--lightness # Use lightness method
--luminosity # Use standard luminosity method
--maximal # Use maximal decomposition
--minimal # Use minimal decomposition
--red # Use the values of the red channel
--green # Use the values of the green channel
--blue # Use the values of the blue channel
`,
}
cmd.Run = runGreyscale
cmd.Flag.BoolVar(&greyscaleAverage, "average", false, "")
cmd.Flag.BoolVar(&greyscaleLightness, "lightness", false, "")
cmd.Flag.BoolVar(&greyscaleLuminosity, "luminosity", false, "")
cmd.Flag.BoolVar(&greyscaleMaximal, "maximal", false, "")
cmd.Flag.BoolVar(&greyscaleMinimal, "minimal", false, "")
cmd.Flag.BoolVar(&greyscaleRed, "red", false, "")
cmd.Flag.BoolVar(&greyscaleGreen, "green", false, "")
cmd.Flag.BoolVar(&greyscaleBlue, "blue", false, "")
return cmd
}
func runGreyscale(cmd *hadfield.Command, args []string) {
i, data := utils.ReadStdin()
if greyscaleAverage {
i = greyscale.Average(i)
} else if greyscaleLightness {
i = greyscale.Lightness(i)
} else if greyscaleLuminosity {
i = greyscale.Luminosity(i)
} else if greyscaleMaximal {
i = greyscale.Maximal(i)
} else if greyscaleMinimal {
i = greyscale.Minimal(i)
} else if greyscaleRed {
i = greyscale.Red(i)
} else if greyscaleGreen {
i = greyscale.Green(i)
} else if greyscaleBlue {
i = greyscale.Blue(i)
} else {
i = greyscale.Greyscale(i)
}
utils.WriteStdout(i, data)
}