Skip to content

Instantly share code, notes, and snippets.

@mebens
Created October 22, 2012 02:07
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mebens/3929259 to your computer and use it in GitHub Desktop.
Save mebens/3929259 to your computer and use it in GitHub Desktop.
Generate a linear radial gradient in Love2D
local function scale(x, min1, max1, min2, max2)
return min2 + ((x - min1) / (max1 - min1)) * (max2 - min2)
end
local function distance(x1, y1, x2, y2)
return math.sqrt((x2 - x1) ^ 2 + (y2 - y1) ^ 2)
end
function radialGradient(radius)
local data = love.image.newImageData(radius * 2, radius * 2)
data:mapPixel(function(x, y)
local dist = distance(radius, radius, x, y)
return 0, 0, 0, (dist <= radius and scale(dist, 0, radius, 255, 0) or 0)
end)
return love.graphics.newImage(data)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment