luawxwidgets

How to get point color (RGB) in panel Lua (wxLua)?


I am using ZeroBrane Studio (2.01; MobDebug 0.805) on Windows 10.

How to get the color of a panel point?

Select a point with the left mouse button.

My MWE:

package.cpath = package.cpath..";./?.dll;./?.so;../lib/?.so;../lib/vc_dll/?.dll;../lib/bcc_dll/?.dll;../lib/mingw_dll/?.dll;"
require("wx")

frame = wx.wxFrame(wx.NULL, wx.wxID_ANY,  "How tom use GetPixel?", wx.wxDefaultPosition, wx.wxSize(450, 450), wx.wxDEFAULT_FRAME_STYLE)

panel = wx.wxPanel(frame, wx.wxID_ANY)

function OnLeftUp(event)
local point = { x = event:GetX(), y = event:GetY() }
local pdc = wx.wxClientDC(panel)
-- local colour = pdc:GetPixel(point.x, point.y) -- This line results in an error
  pdc:DrawText("Left mouse button UP at x = " .. tostring(point.x) .. " y = " .. tostring(point.y), point.x, point.y)
--  pdc:DrawText("Left mouse button UP at x = " .. tostring(point.x) .. " y = " .. tostring(point.y) .. " Red = " .. tostring(colour:GetRed()), point.x, point.y) -- This line results in an error
  pdc:delete()
end

panel:Connect(wx.wxEVT_LEFT_UP, OnLeftUp)

local sizer = wx.wxBoxSizer(wx.wxVERTICAL)
sizer:Add(panel, 1, wx.wxEXPAND)
frame:SetSizer(sizer)

frame:SetAutoLayout(true)
wx.wxGetApp():MainLoop()

enter image description here

I tried to use GetPixel but it gives an error:

enter image description here


Solution

  • Regarding the official wxWidgets's doc, GetPixel doesn't return the colour : only a boolean that indicates if all was correct. You have to pass a wxColour var as 3rd parameter:

    local colour = wx.wxColour(0, 0, 0)
    
    pdc:GetPixel(point.x, point.y, colour)