Skip to content

Instantly share code, notes, and snippets.

@B-Interactive
Last active January 31, 2016 11:21
Show Gist options
  • Save B-Interactive/3887c2574cc71e2ce19c to your computer and use it in GitHub Desktop.
Save B-Interactive/3887c2574cc71e2ce19c to your computer and use it in GitHub Desktop.
A HaXe method for blending two 32bit hexadecimal ARGB colours together. It's important that the input values are ARGB, not just RGB values. For example: 0xFFFFFFFF (32bit ARGB), not just 0xFFFFFF (24bit RGB). Credit to Mark Ransom (https://stackoverflow.com/a/1944193/2178791) and Michael Baczynski (http://lab.polygonal.de/?p=81).
/**
* Merges two 32bit hexadecimal colours, and returns the result.
*
* @param First hexadecimal ARGB colour.
* @param Second hexadecimal ARGB colour.
* @return Returns the two colours blended as a hexadecimal ARGB value.
*/
public static function blendColours(colourA:Int, colourB:Int):Int
{
// Bitwise hex extraction...
var aA:Int = colourA >>> 24;
var aR:Int = colourA >>> 16 & 0xFF;
var aG:Int = colourA >>> 8 & 0xFF;
var aB:Int = colourA & 0xFF;
var bA:Int = colourB >>> 24;
var bR:Int = colourB >>> 16 & 0xFF;
var bG:Int = colourB >>> 8 & 0xFF;
var bB:Int = colourB & 0xFF;
// ... blending...
var a:Int = Std.int(aA + (bA * (255 - aA) / 255));
var r:Int = Std.int((aR * aA / 255) + (bR * bA * (255 - aA) / (255 * 255)));
var g:Int = Std.int((aG * aA / 255) + (bG * bA * (255) / (255 * 255)));
var b:Int = Std.int((aB * aA / 255) + (bB * bA * (255 - aA) / (255 * 255)));
// ... and bitwise re-assembly.
return a << 24 | r << 16 | g << 8 | b;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment