-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetect.go
47 lines (35 loc) · 1.08 KB
/
detect.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
package main
import "github.com/lucasb-eyer/go-colorful"
const bitonalWhiteThreshold = 0.99
const bitonalBlackThreshold = 0.01
const bitonalThreshold = 0.9999
func bitonalDetect(src imageFile) bool {
pixelCount := src.bounds.Max.Y * src.bounds.Max.X
bitoneCount := 0
for y := src.bounds.Min.Y; y < src.bounds.Max.Y; y++ {
for x := src.bounds.Min.X; x < src.bounds.Max.X; x++ {
orgColor := src.image.At(x, y)
colorfulColor, _ := colorful.MakeColor(orgColor)
_, _, l := colorfulColor.Hsl()
if l < bitonalBlackThreshold || bitonalWhiteThreshold < l {
bitoneCount++
}
}
}
return bitonalThreshold < (float64(bitoneCount) / float64(pixelCount))
}
func grayscaleDetect(src imageFile) bool {
pixelCount := src.bounds.Max.Y * src.bounds.Max.X
bitoneCount := 0
for y := src.bounds.Min.Y; y < src.bounds.Max.Y; y++ {
for x := src.bounds.Min.X; x < src.bounds.Max.X; x++ {
orgColor := src.image.At(x, y)
colorfulColor, _ := colorful.MakeColor(orgColor)
_, s, _ := colorfulColor.Hsl()
if s == 0.0 {
bitoneCount++
}
}
}
return bitoneCount == pixelCount
}