Code: Select all
function brightness(img, amount) --must be between 100 and -100
output = Image.createEmpty(img:height(), img:width())
for x = 1, img:width()-1 do
for y = 1, img:height()-1 do
p = img:pixel(y, x)
r = p:colors().r+amount
if r < 0 then r = 0 end
if r > 255 then r = 255 end
g = p:colors().g+amount
if g < 0 then g = 0 end
if g > 255 then g = 255 end
b = p:colors().b+amount
if b < 0 then b = 0 end
if b > 255 then b = 255 end
c = Color.new(r,g,b)
output:pixel(y, x, c)
end
end
return output
end
function invert(img)
output = Image.createEmpty(img:height(), img:width())
for x = 1, img:width()-1 do
for y = 1, img:height()-1 do
p = img:pixel(y, x)
c = Color.new(255-p:colors().r,255-p:colors().g,255-p:colors().b)
output:pixel(y, x, c)
end
end
return output
end
function contrast(img, amount) -- must be between 100 and -100
if amount < -100 then amount = -100 end
if amount > 100 then amount = 100 end
amount = amount + 100
amount = amount / 100
amount = amount * amount
output = Image.createEmpty(img:height(), img:width())
for x = 1, img:width()-1 do
for y = 1, img:height()-1 do
p = img:pixel(y, x)
r = p:colors().r
r = r / 255
r = r - 0.5
r = r * amount
r = r + 0.5
r = r * 255
if r < 0 then r = 0 end
if r > 255 then r = 255 end
g = p:colors().g
g = g / 255
g = g - 0.5
g = g * amount
g = g + 0.5
g = g * 255
if g < 0 then g = 0 end
if g > 255 then g = 255 end
b = p:colors().b
b = b / 255
b = b - 0.5
b = b * amount
b = b + 0.5
b = b * 255
if b < 0 then b = 0 end
if b > 255 then b = 255 end
c = Color.new(r,g,b)
output:pixel(y, x, c)
end
end
return output
end
function stripcolor(img, red, green, blue) -- 1 mean strip that color, 0 means leave it
output = Image.createEmpty(img:height(), img:width())
for x = 1, img:width()-1 do
for y = 1, img:height()-1 do
p = img:pixel(y, x)
r = p:colors().r
g = p:colors().g
b = p:colors().b
if red == 0 then r = r end
if red == 1 then r = -255 end
if green == 0 then g = g end
if green == 1 then g = -255 end
if blue == 0 then b = b end
if blue == 1 then b = -255 end
c = Color.new(r,g,b)
output:pixel(y, x, c)
end
end
return output
end
test = Image.load("64.png")
screen:blit(30, 26, test) -- the original image
screen:blit(106, 36, invert(test))
screen:blit(40, 102, stripcolor(test, 1, 1, 0)) -- make it blue
screen:blit(106, 102, stripcolor(test, 0, 1, 1)) -- make it red
screen:blit(172, 102, stripcolor(test, 1, 0, 1)) -- make it green
screen:blit(40, 168, contrast(test, 50)) -- increase by 50
screen:blit(106, 168, contrast(test, -50)) -- decrease by -50
screen:blit(172, 168, brightness(test, 50)) -- increase by 50
screen:blit(238, 168, brightness(test, -50)) -- decrease by -50
screen:flip()
screen.waitVblankStart(600)
--screen:save("filters.png")
and the various things you can do