Created
August 31, 2014 06:10
-
-
Save anonymous/6b42853df4823cd326ba to your computer and use it in GitHub Desktop.
Gists Codea Upload
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--# Main | |
-- blendmode bench | |
displayMode(STANDARD) | |
function setup() | |
-- default colors | |
DST_background = color(0,0) | |
DST_foreground = color(255, 0, 0, 255) | |
SRC_background = color(0,0) | |
SRC_foreground = color(35, 0, 255, 255) | |
-- buttons | |
controlPanel() | |
createImages() | |
updateImages() | |
text0 = "DST: image already there" | |
text1 = "SRC: image to be blended onto DST" | |
text2 = "Blended result:" | |
text3 = "SRC sprited on top of DST" | |
end | |
function controlPanel() | |
parameter.clear() | |
ready = false | |
paramBlendMode() | |
paramImages() | |
ready = true | |
end | |
-- ############### Blendmode selection ################ | |
-- this is the interface to choose one of the various syntext of blendmode() | |
function paramBlendMode() | |
parameter.integer("blendmode_syntax",1,3,blendmode_syntax or 1,function() | |
switchBlendMode(blendmode_syntax) | |
if ready then controlPanel() end | |
end) | |
end | |
function switchBlendMode(i) | |
if i == 1 then selectBlendMode1() end | |
if i == 2 then selectBlendMode2() end | |
if i == 3 then selectBlendMode3() end | |
end | |
function selectBlendMode1() | |
title = "1 parameter blendMode:" | |
doc = "preset simple modes" | |
choices1 = {"NORMAL","ADDITIVE","MULTIPLY", | |
["NORMAL"]=NORMAL, ["ADDITIVE"]=ADDITIVE, ["MULTIPLY"]=MULTIPLY} | |
blendParams = {1} | |
parameter.integer("param1_1",1,3,param1_1 or 1,function() | |
textP1=choices1[param1_1] | |
p1 = choices1[textP1] | |
textBlend = "blendMode( " .. textP1 .. " )" | |
printOut() | |
end) | |
setBlendMode = function() blendMode(p1) end | |
end | |
function printOut() | |
output.clear() | |
print(textBlend) | |
end | |
choices21 = { | |
"ZERO","ONE", | |
"DST_COLOR","ONE_MINUS_DST_COLOR", | |
"SRC_ALPHA","ONE_MINUS_SRC_ALPHA", | |
"DST_ALPHA","ONE_MINUS_DST_ALPHA", | |
"SRC_ALPHA_SATURATE", | |
["ZERO"]=ZERO, ["ONE"]=ONE, | |
["DST_COLOR"]=DST_COLOR, ["ONE_MINUS_DST_COLOR"]=ONE_MINUS_DST_COLOR, | |
["SRC_ALPHA"]=SRC_ALPHA, ["ONE_MINUS_SRC_ALPHA"]=ONE_MINUS_SRC_ALPHA, | |
["DST_ALPHA"]=DST_ALPHA, ["ONE_MINUS_DST_ALPHA"]=ONE_MINUS_DST_ALPHA, | |
["SRC_ALPHA_SATURATE"]=SRC_ALPHA_SATURATE, | |
} | |
choices22 = { | |
"ZERO","ONE", | |
"SRC_COLOR","ONE_MINUS_SRC_COLOR", | |
"SRC_ALPHA","ONE_MINUS_SRC_ALPHA", | |
"DST_ALPHA","ONE_MINUS_DST_ALPHA", | |
["ZERO"]=ZERO, ["ONE"]=ONE, | |
["SRC_COLOR"]=SRC_COLOR, ["ONE_MINUS_SRC_COLOR"]=ONE_MINUS_SRC_COLOR, | |
["SRC_ALPHA"]=SRC_ALPHA, ["ONE_MINUS_SRC_ALPHA"]=ONE_MINUS_SRC_ALPHA, | |
["DST_ALPHA"]=DST_ALPHA, ["ONE_MINUS_DST_ALPHA"]=ONE_MINUS_DST_ALPHA, | |
} | |
doc2 = { | |
["ZERO"] = "(0,0,0,0)", | |
["ONE"] = "(1,1,1,1)", | |
["DST_COLOR"] = "( D.r, D.g, D.b, D.a )/255", | |
["ONE_MINUS_DST_COLOR"] = "(1,1,1,1) - ( D.r, D.g, D.b, D.a )/255", | |
["SRC_COLOR"] = "( S.r, S.g, S.b, S.a )/255", | |
["ONE_MINUS_SRC_COLOR"] = "(1,1,1,1) - ( S.r, S.g, S.b, S.a )/255", | |
["SRC_ALPHA"] = "( S.a, S.a, S.a, S.a )/255", | |
["ONE_MINUS_SRC_ALPHA"] = "(1,1,1,1) - ( S.a, S.a, S.a, S.a )/255", | |
["DST_ALPHA"] = "( D.a, D.a, D.a, D.a )/255", | |
["ONE_MINUS_DST_ALPHA"] = "(1,1,1,1) - ( D.a, D.a, D.a, D.a )/255", | |
["SRC_ALPHA_SATURATE"] = "(f,f,f,1) with f = min ( S.a, 255 - D.a )/255" | |
} | |
doc3 = { | |
["ZERO"] = "0", | |
["ONE"] = "1", | |
["DST_COLOR"] = "D.a /255", | |
["ONE_MINUS_DST_COLOR"] = "1 - D.a /255", | |
["SRC_COLOR"] = "S.a /255", | |
["ONE_MINUS_SRC_COLOR"] = "1 - S.a /255", | |
["SRC_ALPHA"] = "S.a /255", | |
["ONE_MINUS_SRC_ALPHA"] = "1 - S.a /255", | |
["DST_ALPHA"] = "D.a /255", | |
["ONE_MINUS_DST_ALPHA"] = "1 - D.a /255", | |
["SRC_ALPHA_SATURATE"] = "min ( S.a, 255 - D.a )/255" | |
} | |
function selectBlendMode2() | |
title = "2 parameter blendMode:" | |
parameter.integer("param2_1",1,#choices21, param2_1 or 1,function() | |
updateBlendMode2() | |
end) | |
parameter.integer("param2_2",1,#choices22, param2_2 or 1,function() | |
updateBlendMode2() | |
end) | |
setBlendMode = function() blendMode(p1,p2) end | |
end | |
function updateBlendMode2() | |
textP1=choices21[param2_1] | |
textP2=choices22[param2_2] | |
p1 = choices21[textP1] | |
p2 = choices22[textP2] | |
textBlend = "blendMode(\n " .. (textP1 or "") .. ",\n " .. (textP2 or "") .. "\n)" | |
printOut() | |
doc = "for r,g,b,a with S=SRC and D=DST:\n" | |
.. "new_color = S * s + D * d" .."\n" | |
.. "s = " .. (doc2[textP1] or "").."\n" | |
.. "d = " .. (doc2[textP2] or "") | |
end | |
function selectBlendMode3() | |
title = "4 parameter blendMode:" | |
parameter.integer("param2_1",1,#choices21, param2_1 or 1,function() | |
updateBlendMode3() | |
end) | |
parameter.integer("param2_2",1,#choices22, param2_2 or 1,function() | |
updateBlendMode3() | |
end) | |
parameter.integer("param2_3",1,#choices21, param2_3 or 1,function() | |
updateBlendMode3() | |
end) | |
parameter.integer("param2_4",1,#choices22, param2_4 or 1,function() | |
updateBlendMode3() | |
end) | |
setBlendMode = function() blendMode(p1,p2,p3,p4) end | |
end | |
function updateBlendMode3() | |
textP1=choices21[param2_1] | |
textP2=choices22[param2_2] | |
textP3=choices21[param2_3] | |
textP4=choices22[param2_4] | |
p1 = choices21[textP1] | |
p2 = choices22[textP2] | |
p3 = choices21[textP3] | |
p4 = choices22[textP4] | |
textBlend = "blendMode(\n " .. (textP1 or "") .. ",\n " .. (textP2 or "") | |
.. ",\n " .. (textP3 or "") .. ",\n " .. (textP4 or "") .. "\n)" | |
printOut() | |
doc = "for r,g,b with S=SRC and D=DST:\n" | |
.. "new_color = S * s + D * d" .."\n" | |
.. "s = " .. (doc2[textP1] or "").."\n" | |
.. "d = " .. (doc2[textP2] or "").."\n" | |
.. "new_a = S.a * s + D.a * d \n" | |
.. "s = " .. (doc3[textP3] or "").."\n" | |
.. "d = " .. (doc3[textP4] or "").."\n" | |
end | |
-- ############### test images selection ################ | |
function paramImages() | |
parameter.integer("DST_image", 1, 3, DST_image, updateImages) | |
parameter.integer("SRC_image", 1, 3, SRC_image, updateImages) | |
parameter.color("DST_background",DST_background,updateImages) | |
parameter.color("DST_foreground",DST_foreground,updateImages) | |
parameter.color("SRC_background",SRC_background,updateImages) | |
parameter.color("SRC_foreground",SRC_foreground,updateImages) | |
end | |
function createImages() | |
DST_image = 1 | |
SRC_image = 1 | |
-- real images | |
img0 = square() | |
img1 = square() | |
img2 = square() | |
end | |
function updateImages() | |
if not ready then return end | |
if DST_image == 1 then | |
simplePattern(img0, DST_background, DST_foreground, 0) | |
elseif DST_image == 2 then | |
colorPattern(img0,0) | |
elseif DST_image == 3 then | |
textPattern(img0,DST_background, DST_foreground, 90) | |
end | |
if SRC_image == 1 then | |
simplePattern(img1, SRC_background, SRC_foreground, 90) | |
elseif SRC_image == 2 then | |
colorPattern(img1,90) | |
elseif SRC_image == 3 then | |
textPattern(img1,SRC_background, SRC_foreground, 0) | |
end | |
end | |
-- empty image of final size | |
function square() | |
local m = math.min(WIDTH,HEIGHT) | |
local w = m/5 | |
local h = w | |
local img = image(w*2,h*2) | |
setContext(img) | |
background(0,0) | |
setContext() | |
return img | |
end | |
-- this is a diagonal grid to visualize transparency | |
function grid() | |
local w = 20 | |
local img = image(w,w) | |
setContext(img) | |
noSmooth() | |
background(0) | |
strokeWidth(0.5) | |
stroke(128) | |
for i=1,w/2 do | |
line(0,i*2,i*2,0) | |
line(i*2, w, w, i*2) | |
end | |
setContext() | |
return img | |
end | |
-- a background image | |
back = grid() | |
-- this is a text pattern | |
function textPattern(imgOut,c0,c1,angle) | |
local w,h = imgOut.width, imgOut.height | |
resetMatrix() | |
pushStyle() | |
textMode(CENTER) | |
font("Futura-CondensedExtraBold") | |
fontSize(40) | |
setContext(imgOut) | |
background(c0) | |
fill(c1) | |
translate(w/2, h/2) | |
rotate(angle or 0) | |
text("HELLO WORLD" ,0,0) | |
setContext() | |
resetMatrix() | |
popStyle() | |
return imgOut | |
end | |
-- this is a simple pattern | |
function simplePattern(imgOut,c0,c1,angle) | |
local w,h = imgOut.width, imgOut.height | |
resetMatrix() | |
ellipseMode(CENTER) | |
setContext(imgOut) | |
background(c0) | |
fill(c1) | |
translate(w/2, h/2) | |
rotate(angle or 0) | |
ellipse(0,0,w/2,h/4) | |
setContext() | |
resetMatrix() | |
return imgOut | |
end | |
-- this is a color pattern | |
function smallColorPattern() | |
-- first make a low resolution color pattern | |
local w = 10 | |
local img = image(w,25) | |
local as = { 0, 0.25, 0.5, 0.75, 1 } | |
local cs = { color(255), color(255,0,0), color(0,255,0), color(0,0,255)} | |
local y = 1 | |
setContext(img) | |
noSmooth() | |
background(0,0,0,0) | |
for _,a in pairs(as) do | |
strokeWidth(1) | |
local cl = color( 255,255,255,255*a) | |
stroke(cl) | |
line(0,y,w,y) | |
y = y + 1 | |
end | |
for _,c in pairs(cs) do for _,a in pairs(as) do | |
strokeWidth(1) | |
local cl = color( c.r*a, c.g*a, c.b*a, c.a ) | |
stroke(cl) | |
line(0,y,w,y) | |
y = y + 1 | |
end end | |
setContext() | |
return img | |
end | |
local colorGrid = smallColorPattern() | |
function colorPattern(imgOut,angle) | |
-- then sprite it into output image | |
local w,h = imgOut.width, imgOut.height | |
resetMatrix() | |
spriteMode(CENTER) | |
setContext(imgOut) | |
background(0,0) | |
translate(w/2, h/2) | |
rotate(angle or 0) | |
sprite(colorGrid, 0,0,w,h ) | |
setContext() | |
resetMatrix() | |
return imgOut | |
end | |
-- ############### blending ################ | |
function updateBlending() | |
local w,h = img2.width, img2.height | |
pushStyle() | |
spriteMode(CORNER) | |
setContext(img2) | |
blendMode(NORMAL) | |
background(0,0,0,0) | |
sprite(img0,0,0,w,h) | |
setBlendMode() | |
sprite(img1,0,0,w,h) | |
setContext() | |
blendMode(NORMAL) | |
popStyle() | |
end | |
function draw() | |
-- This sets a dark background color | |
background(40, 40, 50) | |
local m = math.min(WIDTH,HEIGHT) | |
local w = m/5 | |
local h = w | |
local s = (m-w*4)/3 | |
local x,y | |
fill(255) | |
fontSize(17) | |
spriteMode(CENTER) | |
-- DST: image already there | |
x,y = w+s,h*3+s*2 | |
text(text0, x, y+h+s/2) | |
sprite(back,x,y,img0.width,img0.height) | |
sprite(img0,x,y) | |
-- SRC: image to be blended onto DST | |
x,y = w+s,h+s | |
text(text1,x,y+h+s/2) | |
sprite(back,x,y,img0.width,img0.height) | |
sprite(img1,x,y) | |
-- Blended result: | |
updateBlending() | |
x,y = w*3+s*2,h+s | |
text(text2 ,x,y+h+s/2) | |
text(text3 ,x,y+h+s) | |
sprite(back,x,y,img0.width,img0.height) | |
sprite(img2,x,y) | |
-- details of the blending process | |
local t = title .."\n\n" .. textBlend | |
local tw,th =textSize(t) | |
x,y = w*3+s*2, HEIGHT - th/2 -20 | |
text( t ,x,y) | |
y = (y + h*2 +s)/2 | |
text(doc ,x,y) | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment