Created
July 31, 2015 09:55
-
-
Save JordanDelcros/518396da1c13f75ee057 to your computer and use it in GitHub Desktop.
Combine two RGBA colors with JavaScript
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
// Fast and easy way to combine (additive mode) two RGBA colors with JavaScript. | |
// [red, green, blue, alpha] based on these maximul values [255, 255, 255, 1]. | |
var base = [69, 109, 160, 1]; | |
var added = [61, 47, 82, 0.8]; | |
var mix = []; | |
mix[3] = 1 - (1 - added[3]) * (1 - base[3]); // alpha | |
mix[0] = Math.round((added[0] * added[3] / mix[3]) + (base[0] * base[3] * (1 - added[3]) / mix[3])); // red | |
mix[1] = Math.round((added[1] * added[3] / mix[3]) + (base[1] * base[3] * (1 - added[3]) / mix[3])); // green | |
mix[2] = Math.round((added[2] * added[3] / mix[3]) + (base[2] * base[3] * (1 - added[3]) / mix[3])); // blue | |
// Will return [63, 59, 98, 1] |
Hi there. I made the Swift implementation. Maybe it will be useful for somebody.
https://gist.github.com/ilia3546/a90fcdc208b49f69be95ecd854544d73
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is more elaborate example https://jsfiddle.net/mw7optL5/32/