mirror of
https://github.com/go-vgo/robotgo.git
synced 2025-06-02 23:23:55 +00:00
optimize findColor and uniform API with findBitmap [ci skip]
This commit is contained in:
parent
91b6f74131
commit
c6d60bd3f2
45
robotgo.go
45
robotgo.go
@ -1062,14 +1062,27 @@ func GetColor(bitmap C.MMBitmapRef, x, y int) C.MMRGBHex {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// FindColor find bitmap color
|
// FindColor find bitmap color
|
||||||
func FindColor(bitmap C.MMBitmapRef, color CHex, args ...float32) (int, int) {
|
func FindColor(color CHex, args ...interface{}) (int, int) {
|
||||||
var tolerance C.float = 0.01
|
var (
|
||||||
|
tolerance C.float = 0.01
|
||||||
|
bitmap C.MMBitmapRef
|
||||||
|
)
|
||||||
|
|
||||||
if len(args) > 0 {
|
if len(args) > 0 {
|
||||||
tolerance = C.float(args[0])
|
bitmap = args[0].(C.MMBitmapRef)
|
||||||
|
} else {
|
||||||
|
bitmap = CaptureScreen()
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(args) > 1 {
|
||||||
|
tolerance = C.float(args[1].(float32))
|
||||||
}
|
}
|
||||||
|
|
||||||
pos := C.bitmap_find_color(bitmap, C.MMRGBHex(color), tolerance)
|
pos := C.bitmap_find_color(bitmap, C.MMRGBHex(color), tolerance)
|
||||||
|
if len(args) <= 0 {
|
||||||
|
FreeBitmap(bitmap)
|
||||||
|
}
|
||||||
|
|
||||||
x := int(pos.x)
|
x := int(pos.x)
|
||||||
y := int(pos.y)
|
y := int(pos.y)
|
||||||
|
|
||||||
@ -1077,7 +1090,7 @@ func FindColor(bitmap C.MMBitmapRef, color CHex, args ...float32) (int, int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// FindColorCS findcolor by CaptureScreen
|
// FindColorCS findcolor by CaptureScreen
|
||||||
func FindColorCS(x, y, w, h int, color CHex, args ...float32) (int, int) {
|
func FindColorCS(color CHex, x, y, w, h int, args ...float32) (int, int) {
|
||||||
var tolerance float32 = 0.01
|
var tolerance float32 = 0.01
|
||||||
|
|
||||||
if len(args) > 0 {
|
if len(args) > 0 {
|
||||||
@ -1085,27 +1098,39 @@ func FindColorCS(x, y, w, h int, color CHex, args ...float32) (int, int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bitmap := CaptureScreen(x, y, w, h)
|
bitmap := CaptureScreen(x, y, w, h)
|
||||||
rx, ry := FindColor(bitmap, color, tolerance)
|
rx, ry := FindColor(color, bitmap, tolerance)
|
||||||
FreeBitmap(bitmap)
|
FreeBitmap(bitmap)
|
||||||
|
|
||||||
return rx, ry
|
return rx, ry
|
||||||
}
|
}
|
||||||
|
|
||||||
// CountColor count bitmap color
|
// CountColor count bitmap color
|
||||||
func CountColor(bitmap C.MMBitmapRef, color CHex, args ...float32) int {
|
func CountColor(color CHex, args ...interface{}) int {
|
||||||
var tolerance C.float = 0.01
|
var (
|
||||||
|
tolerance C.float = 0.01
|
||||||
|
bitmap C.MMBitmapRef
|
||||||
|
)
|
||||||
|
|
||||||
if len(args) > 0 {
|
if len(args) > 0 {
|
||||||
tolerance = C.float(args[0])
|
bitmap = args[0].(C.MMBitmapRef)
|
||||||
|
} else {
|
||||||
|
bitmap = CaptureScreen()
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(args) > 1 {
|
||||||
|
tolerance = C.float(args[1].(float32))
|
||||||
}
|
}
|
||||||
|
|
||||||
count := C.bitmap_count_of_color(bitmap, C.MMRGBHex(color), tolerance)
|
count := C.bitmap_count_of_color(bitmap, C.MMRGBHex(color), tolerance)
|
||||||
|
if len(args) <= 0 {
|
||||||
|
FreeBitmap(bitmap)
|
||||||
|
}
|
||||||
|
|
||||||
return int(count)
|
return int(count)
|
||||||
}
|
}
|
||||||
|
|
||||||
// CountColorCS count bitmap color by CaptureScreen
|
// CountColorCS count bitmap color by CaptureScreen
|
||||||
func CountColorCS(x, y, w, h int, color CHex, args ...float32) int {
|
func CountColorCS(color CHex, x, y, w, h int, args ...float32) int {
|
||||||
var tolerance float32 = 0.01
|
var tolerance float32 = 0.01
|
||||||
|
|
||||||
if len(args) > 0 {
|
if len(args) > 0 {
|
||||||
@ -1113,7 +1138,7 @@ func CountColorCS(x, y, w, h int, color CHex, args ...float32) int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bitmap := CaptureScreen(x, y, w, h)
|
bitmap := CaptureScreen(x, y, w, h)
|
||||||
rx := CountColor(bitmap, color, tolerance)
|
rx := CountColor(color, bitmap, tolerance)
|
||||||
FreeBitmap(bitmap)
|
FreeBitmap(bitmap)
|
||||||
|
|
||||||
return rx
|
return rx
|
||||||
|
Loading…
Reference in New Issue
Block a user