From bec112817b9c5eb2ef4fb51ae44d1f52917355f5 Mon Sep 17 00:00:00 2001 From: vcaesar Date: Sat, 2 Dec 2017 22:19:07 +0800 Subject: [PATCH] add FindColorCS, CHex type and update findcolor, code --- robot/robotgo.go | 66 +++++++++++++++++++------------------- robotgo.go | 82 +++++++++++++++++++++++++++--------------------- 2 files changed, 80 insertions(+), 68 deletions(-) diff --git a/robot/robotgo.go b/robot/robotgo.go index 4f45436..676518d 100644 --- a/robot/robotgo.go +++ b/robot/robotgo.go @@ -60,22 +60,11 @@ import ( ) const ( - version string = "v0.46.6.428, Pyrenees Mountains!" + version string = "v0.46.6.429, Pyrenees Mountains!" ) -// GetVersion get version -func GetVersion() string { - return version -} - -/* - _______. ______ .______ _______ _______ .__ __. - / | / || _ \ | ____|| ____|| \ | | - | (----`| ,----'| |_) | | |__ | |__ | \| | - \ \ | | | / | __| | __| | . ` | -.----) | | `----.| |\ \----.| |____ | |____ | |\ | -|_______/ \______|| _| `._____||_______||_______||__| \__| -*/ +// Map a map +type Map map[string]interface{} // Bitmap is Bitmap struct type Bitmap struct { @@ -87,6 +76,36 @@ type Bitmap struct { BytesPerPixel uint8 } +// MPoint is MPoint struct +type MPoint struct { + x int + y int +} + +// GetVersion get version +func GetVersion() string { + return version +} + +// Try handler(err) +func Try(fun func(), handler func(interface{})) { + defer func() { + if err := recover(); err != nil { + handler(err) + } + }() + fun() +} + +/* + _______. ______ .______ _______ _______ .__ __. + / | / || _ \ | ____|| ____|| \ | | + | (----`| ,----'| |_) | | |__ | |__ | \| | + \ \ | | | / | __| | __| | . ` | +.----) | | `----.| |\ \----.| |____ | |____ | |\ | +|_______/ \______|| _| `._____||_______||_______||__| \__| +*/ + // GetPixelColor get pixel color func GetPixelColor(x, y int) string { cx := C.size_t(x) @@ -240,12 +259,6 @@ func BCaptureScreen(args ...int) Bitmap { */ -// MPoint is MPoint struct -type MPoint struct { - x int - y int -} - // MoveMouse move the mouse func MoveMouse(x, y int) { //C.size_t int @@ -434,16 +447,6 @@ func ScrollMouse(x int, y string) { */ -// Try handler(err) -func Try(fun func(), handler func(interface{})) { - defer func() { - if err := recover(); err != nil { - handler(err) - } - }() - fun() -} - // KeyTap tap the keyboard; // // See keys: @@ -626,9 +629,6 @@ func SetKeyboardDelay(x int) { |_______| \__/ |_______||__| \__| |__| */ -// Map a map -type Map map[string]interface{} - // AddEvent add event listener func AddEvent(aeve string) int { keycode := Map{ diff --git a/robotgo.go b/robotgo.go index 056ab11..cc99831 100644 --- a/robotgo.go +++ b/robotgo.go @@ -51,6 +51,7 @@ import "C" import ( // "fmt" + "os" "reflect" "runtime" @@ -63,22 +64,15 @@ import ( ) const ( - version string = "v0.46.6.428, Pyrenees Mountains!" + version string = "v0.46.6.429, Pyrenees Mountains!" ) -// GetVersion get version -func GetVersion() string { - return version -} - -/* - _______. ______ .______ _______ _______ .__ __. - / | / || _ \ | ____|| ____|| \ | | - | (----`| ,----'| |_) | | |__ | |__ | \| | - \ \ | | | / | __| | __| | . ` | -.----) | | `----.| |\ \----.| |____ | |____ | |\ | -|_______/ \______|| _| `._____||_______||_______||__| \__| -*/ +type ( + // Map a map + Map map[string]interface{} + // CHex c rgb Hex type + CHex C.MMRGBHex +) // Bitmap is Bitmap struct type Bitmap struct { @@ -90,6 +84,36 @@ type Bitmap struct { BytesPerPixel uint8 } +// MPoint is MPoint struct +type MPoint struct { + x int + y int +} + +// GetVersion get version +func GetVersion() string { + return version +} + +// Try handler(err) +func Try(fun func(), handler func(interface{})) { + defer func() { + if err := recover(); err != nil { + handler(err) + } + }() + fun() +} + +/* + _______. ______ .______ _______ _______ .__ __. + / | / || _ \ | ____|| ____|| \ | | + | (----`| ,----'| |_) | | |__ | |__ | \| | + \ \ | | | / | __| | __| | . ` | +.----) | | `----.| |\ \----.| |____ | |____ | |\ | +|_______/ \______|| _| `._____||_______||_______||__| \__| +*/ + // GetPixelColor get pixel color func GetPixelColor(x, y int) string { cx := C.size_t(x) @@ -243,12 +267,6 @@ func BCaptureScreen(args ...int) Bitmap { */ -// MPoint is MPoint struct -type MPoint struct { - x int - y int -} - // MoveMouse move the mouse func MoveMouse(x, y int) { //C.size_t int @@ -437,16 +455,6 @@ func ScrollMouse(x int, y string) { */ -// Try handler(err) -func Try(fun func(), handler func(interface{})) { - defer func() { - if err := recover(); err != nil { - handler(err) - } - }() - fun() -} - // KeyTap tap the keyboard; // // See keys: @@ -832,7 +840,7 @@ func GetColor(bitmap C.MMBitmapRef, x, y int) C.MMRGBHex { } // FindColor find bitmap color -func FindColor(bitmap C.MMBitmapRef, color C.MMRGBHex, args ...float32) (int, int) { +func FindColor(bitmap C.MMBitmapRef, color CHex, args ...float32) (int, int) { var tolerance C.float if len(args) > 2 { @@ -841,13 +849,20 @@ func FindColor(bitmap C.MMBitmapRef, color C.MMRGBHex, args ...float32) (int, in tolerance = 0.5 } - pos := C.bitmap_find_color(bitmap, color, tolerance) + pos := C.bitmap_find_color(bitmap, C.MMRGBHex(color), tolerance) x := int(pos.x) y := int(pos.y) return x, y } +// FindColorCS findcolor by CaptureScreen +func FindColorCS(x, y, w, h int, color CHex) (int, int) { + bitmap := CaptureScreen(x, y, w, h) + rx, ry := FindColor(bitmap, color) + return rx, ry +} + /* ___________ ____ _______ .__ __. .___________. | ____\ \ / / | ____|| \ | | | | @@ -857,9 +872,6 @@ func FindColor(bitmap C.MMBitmapRef, color C.MMRGBHex, args ...float32) (int, in |_______| \__/ |_______||__| \__| |__| */ -// Map a map -type Map map[string]interface{} - // AddEvent add event listener func AddEvent(aeve string) int { keycode := Map{