Events, colours and functions - how do they work?

sorry about silly title, i am at my wits’ end.
i want to change a colour, should be pretty straightforward, right?

local r, g, b, a = 11, 11, 11, 111 
app.fgColor = Color{ 
			red=r, 
			green=g, 
			blue=b, 
			alpha=a 
		}

ok, this works fine.

now i want to start a listener on fg and bg colour change, get the colour values, crunch the numbers and set those fg and bg colours to my new values.

i have a script and it spits outs correct numbers (i assume).

except… the colours don’t get changed. i see printed output changing, but not the colours themselves.
but wait, there’s more:
if i call another script, which contains only the code above, all works as expected.

so… does anyone have an idea what’s going on?

here’s what i have so far:

local listener0, listener1
local newClr 
-- list of values allowed in output: 
local vals = {0, 52, 87, 116, 144, 206, 255} 
-- TODO: add option to input values in dialog 
-- TODO: sort the list 


-- get the final value: 
local function calcVal(numba) 
	for i = 1, #vals do 
		if numba > vals[i] and numba < vals[i+1] then 
			local tempVal = vals[i+1] - vals[i] 
			if numba >= vals[i] + (tempVal/2) then 
				numba = vals[i+1] 
				-- return numba 
			elseif numba < vals[i] + (tempVal/2) then 
				numba = vals[i] 
				-- return numba 
			end 
		elseif numba < vals[1] then 
			numba = vals[1] 
			-- return numba 
		elseif numba > vals[#vals] then 
			numba = vals[#vals] 
			-- return numba 
		end 
	end 
	
	return numba 
end 


-- process all channels: 
local function main(clr, src) 
	local r = calcVal( clr.red ) 
	local g = calcVal( clr.green ) 
	local b = calcVal( clr.blue ) 
	local a = clr.alpha 
	
	if a <= 127 then a = 0 else a = 255 end 
	

	if src == "fg" then 
		app.fgColor = Color{ 
			red=r, 
			green=g, 
			blue=b, 
			alpha=a 
		}
		print(src .. " : FG") 
		print(src .. ": " .. r .. " " .. g .. " " .. b .. " " .. a) 
	else 		
		app.bgColor = Color{ 
			red=r, 
			green=g, 
			blue=b, 
			alpha=a 
		}
		print(src .. " : BG") 
		print(src .. ": " .. r .. " " .. g .. " " .. b .. " " .. a) 
	end 
 --[[
	newClr = Color{ 
			red=r, 
			green=g, 
			blue=b, 
			alpha=a 
		}
--]]	
	-- print(src .. ": " .. r .. " " .. g .. " " .. b .. " " .. a) 
	--return newClr 
end 


-- start the listeners: 
local function startAll() 
	listener0 = app.events:on('fgcolorchange', function() 
		main(app.fgColor, "fg") end 
	) 
	listener1 = app.events:on('bgcolorchange', function() 
		main(app.fgColor, "bg") end 
	) 
	-- print( "on" ) 
end 

-- stop the listeners: 
local function stopAll() 
	-- app.events:off(main) 
	app.events:off(listener0) 
	app.events:off(listener1) 
	-- print( "wtf" ) 
end 

-- WINDOW: 
local dlg = Dialog{ 
	title = "quantize colours v0.1   ",
	notitlebar = false, 
	onclose = function() stopAll() end 
}

dlg:check{ 
	id = "run",
	--label = "run",
	text = "s t a r t",
    selected = false, 
	onclick = function() 
		if dlg.data.run then startAll() else stopAll() end
	end 
}

dlg:show{ wait=false } 
-- ^^^ 

btw. i did try to return the colour from main and feed it into the app.fg/bgColour directly inside the listener function, but that didn’t work either.

Hi there @Olga_Galvanova! Actually this is probably a bug (or a limitation to avoid an infinite recursion between events and listeners). Because changing the app.fgColor when we listen the fgcolor event might generate another event. It’s something I have to check later.

Meanwhile, a possible workaround might be using a short Timer, in the event listener we start a timer so we can change the color after the event:

...
local function main(clr, src)
  ...
end

local timer_callback = function() end
local timer
local function ontick()
  timer_callback()
  timer:stop()
end
timer = Timer{ interval=0.001, ontick=ontick }
local function main1(clr, src)
  timer_callback = function() main(clr, src) end
  timer:start()
end

-- start the listeners:
local function startAll()
	listener0 = app.events:on('fgcolorchange', function()
		main1(app.fgColor, "fg") end
	)
	listener1 = app.events:on('bgcolorchange', function()
		main1(app.fgColor, "bg") end
	)
	-- print( "on" )
end

...
1 Like

oh, thank you very much!

1 Like