From 4f85eb5056456c31a44acd810ff888267885185f Mon Sep 17 00:00:00 2001 From: vcaesar Date: Mon, 25 Dec 2017 17:05:10 +0800 Subject: [PATCH] add more color processing and conversion --- robotgo.go | 41 +++++++++++++++++++++++++++++++++++------ screen/goScreen.h | 29 +++++++++++++++++++++++++++-- 2 files changed, 62 insertions(+), 8 deletions(-) diff --git a/robotgo.go b/robotgo.go index 7c83cde..f67385e 100644 --- a/robotgo.go +++ b/robotgo.go @@ -62,7 +62,7 @@ import ( ) const ( - version string = "v0.47.0.460, Mount Cook!" + version string = "v0.47.0.461, Mount Cook!" ) type ( @@ -114,6 +114,40 @@ func Try(fun func(), handler func(interface{})) { |_______/ \______|| _| `._____||_______||_______||__| \__| */ +// ToMMRGBHex trans CHex to C.MMRGBHex +func ToMMRGBHex(hex CHex) C.MMRGBHex { + return C.MMRGBHex(hex) +} + +// U32ToHex trans C.uint32_t to C.MMRGBHex +func U32ToHex(hex C.uint32_t) C.MMRGBHex { + return C.MMRGBHex(hex) +} + +// U8ToHex teans *C.uint8_t to C.MMRGBHex +func U8ToHex(hex *C.uint8_t) C.MMRGBHex { + return C.MMRGBHex(*hex) +} + +// PadHex trans C.MMRGBHex to string +func PadHex(hex C.MMRGBHex) string { + color := C.pad_hex(hex) + gcolor := C.GoString(color) + defer C.free(unsafe.Pointer(color)) + + return gcolor +} + +// HexToRgb trans hex to rgb +func HexToRgb(hex uint32) *C.uint8_t { + return C.color_hex_to_rgb(C.uint32_t(hex)) +} + +// RgbToHex trans rgb to hex +func RgbToHex(r, g, b uint8) C.uint32_t { + return C.color_rgb_to_hex(C.uint8_t(r), C.uint8_t(g), C.uint8_t(b)) +} + // GetPxColor get pixel color return C.MMRGBHex func GetPxColor(x, y int) C.MMRGBHex { cx := C.size_t(x) @@ -123,11 +157,6 @@ func GetPxColor(x, y int) C.MMRGBHex { return color } -// ToMMRGBHex trans CHex to C.MMRGBhex -func ToMMRGBHex(hex CHex) C.MMRGBHex { - return C.MMRGBHex(hex) -} - // GetPixelColor get pixel color return string func GetPixelColor(x, y int) string { cx := C.size_t(x) diff --git a/screen/goScreen.h b/screen/goScreen.h index 98a4f03..2078aa1 100644 --- a/screen/goScreen.h +++ b/screen/goScreen.h @@ -9,6 +9,7 @@ // except according to those terms. #include "../base/types.h" +#include "../base/rgb.h" #include "screengrab_c.h" #include "screen_c.h" // #include "../MMBitmap_c.h" @@ -19,6 +20,31 @@ void padHex(MMRGBHex color, char* hex){ snprintf(hex, 7, "%06x", color); } +char* pad_hex(MMRGBHex color){ + char hex[7]; + padHex(color, hex); + + // destroyMMBitmap(bitmap); + + char* str = (char*)calloc(100, sizeof(char*)); + if(str)strcpy(str, hex); + + return str; +} + +static uint8_t rgb[3]; + +uint8_t *color_hex_to_rgb(uint32_t h){ + rgb[0] = RED_FROM_HEX(h); + rgb[1] = GREEN_FROM_HEX(h); + rgb[2] = BLUE_FROM_HEX(h); + return rgb; +} + +uint32_t color_rgb_to_hex(uint8_t r, uint8_t g, uint8_t b){ + return RGB_TO_HEX(r, g, b); +} + MMRGBHex get_Pixel_Color(size_t x, size_t y){ MMBitmapRef bitmap; MMRGBHex color; @@ -97,8 +123,7 @@ MMBitmapRef capture_screen(size_t x, size_t y, size_t w, size_t h){ // if (){ // x = 0; // y = 0; - - // //Get screen size. + // // Get screen size. // MMSize displaySize = getMainDisplaySize(); // w = displaySize.width; // h = displaySize.height;