Skip to content

Instantly share code, notes, and snippets.

@chenzx
Created January 7, 2013 09:41
Show Gist options
  • Save chenzx/4473730 to your computer and use it in GitHub Desktop.
Save chenzx/4473730 to your computer and use it in GitHub Desktop.
A script to do alpha-Blending of 2 RGBA values
def comp(_A, _R, _G, _B)
_A<<24 | _R<<16 | _G<<8 | _B;
end
def decomp( num )
_A = (num >> 24) & 0xFF;
_R = (num >> 16) & 0xFF;
_G = (num >> 8) & 0xFF;
_B = (num >> 0) & 0xFF;
[_A,_R,_G,_B]
end
def blend(srcARGB, backgroundARGB)
src = srcARGB.hex
dst = backgroundARGB.hex
#assume premultiplied;
srcA, srcR, srcG, srcB = decomp( src )
dstA, dstR, dstG, dstB = decomp( dst )
resultA = srcA + (255-srcA)*dstA/255;
resultR = srcR + (255-srcA)*dstR/255;
resultG = srcG + (255-srcA)*dstG/255;
resultB = srcB + (255-srcA)*dstB/255;
#
result = comp(resultA, resultR, resultG, resultB)
result.to_s(16)
end
result = blend(ARGV[0], ARGV[1])
puts "#{result}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment