Does anyone have an idea how to do this ?
Especially Linux seems complicated to me
Code: Select all
; Samplecode to get the color of a Pixel on the Desktop
; by PureLust for PureBasic-Forum - 18.09.2009 (with thanks to netmaestro) ;)
; extended by infratec, optimized by idle
OpenWindow(0, 0, 0, 200, 20, "", #PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_ScreenCentered)
TextGadget(0, 0, 3, 200, 20, "", #PB_Text_Center)
HDC = GetDC_(0)
Repeat
Event = WaitWindowEvent(100)
x = DesktopMouseX()
y = DesktopMouseY()
PixelColour = GetPixel_(hDC,x,y)
Red = Red(PixelColour)
Green = Green(PixelColour)
Blue = Blue(PixelColour)
PixelColour$ = "#" + RSet(Hex(Red), 2, "0") + RSet(Hex(Green), 2, "0") + RSet(Hex(Blue), 2, "0")
If WindowHeight(0) > 10
SetWindowTitle(0, "GetPixelColour")
PixelColour$ + " (" + Str(Red) + ", " + Str(Green) + ", " + Str(Blue) + ")"
SetGadgetText(0, Str(x) + ","+ Str(y) +" : " + PixelColour$)
Else
SetWindowTitle(0, PixelColour$)
EndIf
Until Event = #PB_Event_CloseWindow
ReleaseDC_(0,HDC)
Code: Select all
import gtk2, gdk2, gdk2pixbuf
gtk2.nim_init()
proc getPixelColor(x, y: int32): auto =
var p = pixbufNew(COLORSPACE_RGB, false, 8, 1, 1)
discard p.getFromDrawable(getDefaultRootWindow().Drawable,
getDefaultScreen().getSystemColormap(), x, y, 0, 0, 1, 1)
result = cast[tuple[r, g, b: uint8]](p.getPixels[])
echo getPixelColor(0, 0)Code: Select all
def get_pixel_colour(i_x, i_y):
import gtk # python-gtk2
o_gdk_pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, 1, 1)
o_gdk_pixbuf.get_from_drawable(gtk.gdk.get_default_root_window(), gtk.gdk.colormap_get_system(), i_x, i_y, 0, 0, 1, 1)
return tuple(o_gdk_pixbuf.get_pixels_array().tolist()[0][0])
print get_pixel_colour(0, 0)Code: Select all
GdkPixbuf get_from_drawable(GdkWindow src, GdkColormap cmap, int src_x, int src_y, int dest_x, int dest_y, int width, int height);I see, but then isn't StartDrawing not returning the DC ? Then you just need the handle.wilbert wrote:Point() only works within StartDrawing() / StopDrawing() so you can't access any pixel on the desktop....
Code: Select all
; OSX 64 bit
ImportC ""
CGBitmapContextCreate(*bdata, width, height, bitsPerComponent, bytesPerRow, cs, bitmapInfo)
CGColorSpaceCreateDeviceRGB()
CGColorSpaceRelease(cs)
CGContextRelease(c)
CGImageRelease(image)
EndImport
Procedure CGContextDrawImage_addr()
ProcedureReturn ?cgcdi_start
cgcdi_start:
!extern _CGContextDrawImage
!sub rsp, 40
!movq [rsp ], xmm0
!movq [rsp + 8], xmm1
!movq [rsp + 16], xmm2
!movq [rsp + 24], xmm3
!call _CGContextDrawImage
!add rsp, 40
!ret
EndProcedure
Procedure CGWindowListCreateImage_addr()
ProcedureReturn ?cgwlci_start
cgwlci_start:
!extern _CGWindowListCreateImage
!sub rsp, 40
!movq [rsp ], xmm0
!movq [rsp + 8], xmm1
!movq [rsp + 16], xmm2
!movq [rsp + 24], xmm3
!call _CGWindowListCreateImage
!add rsp, 40
!ret
EndProcedure
Prototype CGContextDrawImage_proto(c, x.d, y.d, w.d, h.d, image)
Prototype CGWindowListCreateImage_proto(x.d, y.d, w.d, h.d, listOption, windowID, imageOption)
Global CGWindowListCreateImage.CGWindowListCreateImage_proto = CGWindowListCreateImage_addr()
Global CGContextDrawImage.CGContextDrawImage_proto = CGContextDrawImage_addr()
Procedure.l GetColorUnderMouse()
Protected.i image, csRGB, ctx, c.l
image = CGWindowListCreateImage(DesktopMouseX(), DesktopMouseY(), 1, 1, 1, 0, 0)
csRGB = CGColorSpaceCreateDeviceRGB()
ctx = CGBitmapContextCreate(@c, 1, 1, 8, 4, csRGB, 1)
CGContextDrawImage(ctx, 0, 0, 1, 1, image)
CGColorSpaceRelease(csRGB)
CGImageRelease(image)
CGContextRelease(ctx)
ProcedureReturn c
EndProcedureCode: Select all
; OSX 64 bit
ImportC ""
CGBitmapContextCreate(*bdata, width, height, bitsPerComponent, bytesPerRow, cs, bitmapInfo)
CGColorSpaceCreateDeviceRGB()
CGColorSpaceRelease(cs)
CGContextRelease(c)
CGImageRelease(image)
EndImport
Procedure.l GetColorUnderMouse()
Protected.i image, csRGB, ctx
Protected c.l, x.d, y.d, one.d = 1
x = DesktopMouseX()
y = DesktopMouseY()
csRGB = CGColorSpaceCreateDeviceRGB()
ctx = CGBitmapContextCreate(@c, 1, 1, 8, 4, csRGB, 1)
CGColorSpaceRelease(csRGB)
!extern _CGContextDrawImage
!extern _CGWindowListCreateImage
!movsd xmm0, [p.v_x]
!movsd xmm1, [p.v_y]
!movsd xmm2, [p.v_one]
!sub rsp, 32
!movsd [rsp ], xmm0
!movsd [rsp + 8], xmm1
!movsd [rsp + 16], xmm2
!movsd [rsp + 24], xmm2
!mov rdi, 1
!xor rsi, rsi
!xor rdx, rdx
!call _CGWindowListCreateImage
!add rsp, 32
!mov [p.v_image], rax
!mov rdi, [p.v_ctx]
!mov rsi, rax
!pxor xmm0, xmm0
!movsd xmm2, [p.v_one]
!sub rsp, 32
!movupd [rsp], xmm0
!movsd [rsp + 16], xmm2
!movsd [rsp + 24], xmm2
!call _CGContextDrawImage
!add rsp, 32
CGImageRelease(image)
CGContextRelease(ctx)
ProcedureReturn c
EndProcedureCode: Select all
ImportC ""
gdk_pixbuf_get_from_window(window, x, y, width, height)
gdk_pixbuf_get_pixels(pixbuf)
EndImport
Procedure.l GetColorUnderMouse()
Protected.i pixbuf, *buf.Ascii, c.l
pixbuf = gdk_pixbuf_get_from_window(gdk_get_default_root_window_(), DesktopMouseX(), DesktopMouseY(), 1, 1)
*buf = gdk_pixbuf_get_pixels(pixbuf)
c = *buf\a : *buf + 1
c | *buf\a << 8 : *buf + 1
c | *buf\a << 16
g_object_unref_(pixbuf)
ProcedureReturn c
EndProcedureI now have tested your Linux version on many distributions.... it only works for the main screen. If someone knows a solution that works across multiple monitors, please let me know.
Thanks for your feedback. I forgot to post about the cause.Oma wrote:Your picker routine works everywhere on both monitors and if you make the parent window (from picker) sticky, even on all desktops. However, the picker window must of course have the focus. I trigger the picker from the space-bar.