Last active
April 11, 2020 00:54
-
-
Save quantum9Innovation/33f2f709b59c3fe71b976aa738fb1327 to your computer and use it in GitHub Desktop.
An additive color blending algorithm in 10 lines of code that blends colors while retaining the brightness of each color to create realistic blends.
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
/* | |
Pass in 2 colors {r1,g1,b1} and {r2,g2,b2} | |
Select bi the blend intensity of the first color (0-->1) | |
Output is in form {r,g,b} | |
*/ | |
var blend2A = function (r1,g1,b1,r2,g2,b2,bi) { | |
var endR = bi*r1+r2*(1-bi) | |
var endG = bi*g1+g2*(1-bi) | |
var endB = bi*b1+b2*(1-bi) | |
var scale = Math.max(Math.max(endR,endG),endB)/((2*bi*Math.max(Math.max(r1,g1),b1)+(2-2*bi)*Math.max(Math.max(r2,g2),b2))/2) | |
var endR = endR/scale | |
var endG = endG/scale | |
var endB = endB/scale | |
var blendArray = [endR,endG,endB] | |
return(blendArray) | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment