sorcery  image.lua at [3354e2aa29]

File lib/image.lua artifact 32e08434b2 part of check-in 3354e2aa29


local image
image = sorcery.lib.class {

	__concat   = function(self,with) return self:blit(with) end;
	__tostring = function(self)      return self:render()   end;

	construct = function(file) return {
		string = file;
		atop = {};
		fx = {};

		render = function(self)
			local str = ''
			for _,i in pairs(self.atop) do
				str = '(' .. i:render() .. ')^' .. str
			end
			local bracket = false
			if str ~= '' then str = str .. '(' bracket = true end
			str = str .. self.string
			for _,e in pairs(self.fx) do
				str = str .. '^[' .. e
				-- be sure to escape ones that take arguments
				-- correctly!
			end
			if bracket then str = str .. ')' end
			return str
		end;

		blit = function(self, img)
			if img then return image.change(self, {
				atop = sorcery.lib.tbl.append(self.atop, {img})
			}) else return self end
		end;

		multiply = function(self, color)
			return image.change(self, {
				fx = sorcery.lib.tbl.append(self.fx, {'multiply:' .. tostring(color)})
			})
		end;

		colorize = function(self, color, ratio)
			return image.change(self, {
				fx = sorcery.lib.tbl.append(self.fx, {'colorize:' .. tostring(color) .. ':' .. ratio})
			})
		end;

		fade = function(self, fac)
			return image.change(self, {
				fx = sorcery.lib.tbl.append(self.fx, {'opacity:' .. (255 - 255*fac)})
			})
		end;

		transform = function(self, kind)
			return image.change(self, {
				fx = sorcery.lib.tbl.append(self.fx, {'transform' .. tostring(kind)})
			})
		end;

		glow = function(self,color) return self:blit(self:multiply(color)) end;
	} end;
}
return image