I want to draw various transparent radial gradients, but the method I use seems to sometimes leave a small transparent ring in the middle. I can’t seem to get rid of it. Does anybody know what is going on?

``````function setup()
size = 100
colour = color(255)
alpha = 150

g = gradient(size, colour, alpha)
end

function draw()
background(0, 0, 0, 255)

sprite(g, WIDTH/2, HEIGHT/2, size*2, size*2)
end

function gradient(size, col, alpha)
local img = image(size, size)
setContext(img)
fill(0,0)
strokeWidth(2)
smooth()
for j=1, size, 2 do
stroke( col.r, col.g, col.b, alpha-j*(alpha/size))
ellipse(img.width/2, img.height/2, j)
end
setContext()
return img
end
``````

not sure what is wrong. A codea bug maybe? You can use this instead

``````function setup()
size = 100
colour = color(255)
alpha = 150.0

g = gradient(size, colour, alpha)
end

function draw()
background(0, 0, 0, 255)

sprite(g, WIDTH/2, HEIGHT/2, size*2, size*2)
end

local sqrt,floor,max= math.sqrt,math.floor,math.max
function gradient(size, col, alpha)
local img = image(size, size)
local jc,ic = img.width/2.0, img.height/2.0
for j=0, size do for i=0,size do
local d = sqrt((i-ic)*(i-ic)+(j-jc)*(j-jc))/size*2
local a = max(floor(alpha*(1.0-d)),0)
img:set(j,i,col.r, col.g, col.b, a)
end end
return img
end
``````

Thank you jvm, but it is a lot slower.

I see you predefine the math functions, i assume for speed reasons, but why is this quicker?

the local variables are accessed ‘directly’ inside lua (registry, i think). The global vaiables, like ‘math’, are inside _G table: _G[‘math’]. The access to table elements is slower, especially when the table is big, which is the case for _G,