Created
October 6, 2011 20:02
-
-
Save Phize/1268489 to your computer and use it in GitHub Desktop.
An emulation of Adobe Photoshop's layer blend mode on RSL(Renderman Shading Language).
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
# Renderman Shading Language | |
# http://twitter.com/Phize/status/122033158265901056 | |
float pblend(float c1; float c2; string mode) { | |
float result = 0.0; | |
float d = 0.0; | |
if (mode == "normal") { | |
result = c2; | |
} | |
if (mode == "multiply") { | |
result = c1 * c2; | |
} | |
if (mode == "screen") { | |
result = c1 + c2 - (c1 * c2); | |
} | |
if (mode == "overlay") { | |
if (c1 <= 0.5) { | |
result = c2 * (2.0 * c1); | |
} else { | |
result = c2 + (2.0 * c1 - 1.0) - (c2 * (2.0 * c1 - 1.0)); | |
} | |
} | |
if (mode == "darken") { | |
result = min(c1, c2); | |
} | |
if (mode == "lighten") { | |
result = max(c1, c2); | |
} | |
if (mode == "colordodge") { | |
if (c2 < 1.0) { | |
result = min(1.0, c1 / (1.0 - c2)); | |
} else { | |
result = 1.0; | |
} | |
} | |
if (mode == "colorburn") { | |
if (c2 > 0.0) { | |
result = 1 - min(1.0, (1.0 - c1) / c2); | |
} else { | |
result = 0.0; | |
} | |
} | |
if (mode == "hardlight") { | |
if (c2 <= 0.5) { | |
result = c1 * (2.0 * c2); | |
} else { | |
result = c1 + (2.0 * c2 - 1.0) - (c1 * (2.0 * c2 - 1.0)); | |
} | |
} | |
if (mode == "softlight") { | |
if (c2 <= 0.5) { | |
result = c1 - (1.0 - 2.0 * c2) * c1 * (1.0 - c1); | |
} else { | |
if (c1 <= 0.25) { | |
d = ((16.0 * c1 - 12.0) * c1 + 4.0) * c1; | |
} else { | |
d = sqrt(c1); | |
} | |
result = c1 + (2.0 * c2 - 1.0) * (d - c1); | |
} | |
} | |
if (mode == "difference") { | |
result = abs(c1 - c2); | |
} | |
if (mode == "exclusion") { | |
result = c1 + c2 - 2.0 * c1 * c2; | |
} | |
return result; | |
} | |
float palphablend(float c1; float a1; float c2; float a2; string mode) { | |
float result = 0.0; | |
float ca = 0.0; | |
ca = a2 * a1 + a2 * (1 - a1) + (1 - a2) * a1; | |
result = (a2 * a1 * pblend(c1, c2, mode) + a2 * (1 - a1) * c2 + (1 - a2) * a1 * c1) / ca; | |
return result; | |
} | |
color cblend(color c1; color c2; string mode) { | |
color result = color(0.0, 0.0, 0.0); | |
float c1R = comp(c1, 0), c1G = comp(c1, 1), c1B = comp(c1, 2); | |
float c2R = comp(c2, 0), c2G = comp(c2, 1), c2B = comp(c2, 2); | |
result = color( | |
pblend(c1R, c2R, mode), | |
pblend(c1G, c2G, mode), | |
pblend(c1B, c2B, mode) | |
); | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment