lgi-devs / lgi

Dynamic Lua binding to GObject libraries using GObject-Introspection
MIT License
440 stars 70 forks source link

Help me please to get pixel color from screen #272

Closed lua-rocks closed 3 years ago

lua-rocks commented 3 years ago

I'm trying to make a simple colorpicker. Currently it returns color in raw format (like this "���") and I have no idea how to fix it.

local lgi = require "lgi"
local Gtk = lgi.require "Gtk"
local Gdk = lgi.require "Gdk"

local function get_pixel_color(x, y)
  local w = Gdk.get_default_root_window()
  local pb = Gdk.pixbuf_get_from_window(w, x, y, 1, 1)
  return pb:get_pixels()
end

local color = get_pixel_color(10, 10)
print(color)
psychon commented 3 years ago

This should get you the pixel values as numbers from 0 to 255.

local lgi = require "lgi"
local Gtk = lgi.require "Gtk"
local Gdk = lgi.require "Gdk"

local function get_pixel_color(x, y)
  local w = Gdk.get_default_root_window()
  local pb = Gdk.pixbuf_get_from_window(w, x, y, 1, 1)
  local pixels = pb:get_pixels()
  return string.byte(pixels, 1, #pixels)
end

print(get_pixel_color(10, 10))
lua-rocks commented 3 years ago

@psychon Thank you so much!