Last active
May 4, 2024 09:16
-
-
Save nanjizal/4ffee2273c82bb612e27587b902fb34b to your computer and use it in GitHub Desktop.
Alpha blend pixel colors
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
public inline | |
function channelBlend( ch0: PixelChannel | |
, ch1: PixelChannel | |
, ch2: PixelChannel | |
, ch3: PixelChannel ){ | |
var a1: Float = c0; // abstract conversion | |
var r1: Float = c1; | |
var g1: Float = c2; | |
var b1: Float = c3; | |
var a2: Float = ch0; | |
var r2: Float = ch1; | |
var g2: Float = ch2; | |
var b2: Float = ch3; | |
var a3 = a1 * ( 1 - a2 ); | |
var r = colBlendFunc( r1, r2, a3, a2 ); | |
var g = colBlendFunc( g1, g2, a3, a2 ); | |
var b = colBlendFunc( b1, b2, a3, a2 ); | |
var a = alphaBlendFunc( a3, a2 ); | |
return fromChannels( a, r, g, b ); | |
} | |
inline function colBlendFunc( x1: Float, x2: Float, a3: Float, a2: Float ): Int | |
return Std.int( 255 * ( x1 * a3 + x2 * a2 ) ); | |
inline function alphaBlendFunc( a3: Float, a2: Float ): Int | |
return Std.int( 255 * ( a3 + a2 ) ); | |
/*inline | |
public static function fromChannels( ch0: Int, ch1: Int, ch2: Int, ch3: Int ): Pixel32 | |
return ( cast ch0 << 24 | ch1 << 16 | ch2 << 8 | ch3 : Pixel32 );*/ | |
inline function fromChannels( ch0: Int, ch1: Int, ch2: Int, ch3: Int ): Int | |
return ch0 << 24 | ch1 << 16 | ch2 << 8 | ch3; | |
@:forward | |
@:transient | |
abstract PixelChannel( Int ) to Int from Int { | |
inline | |
public function new( v: Int ) | |
this = v; | |
@:from | |
public inline static function toHexInt( c: Float ): PixelChannel | |
return ( cast Math.round( c * 255 ) : PixelChannel ); | |
@:to | |
public inline function colIntToFloat(): Float | |
return ( this == 0 )? 0.: this/255; | |
@:to | |
public inline function stringHash(): String | |
return '#' + StringTools.hex( this, 2 ); | |
inline | |
public static function boundChannel( f: Float ): Int { | |
var i = Std.int( f ); | |
if( i > 0xFF ) i = 0xFF; | |
if( i < 0 ) i = 0; | |
return new PixelChannel( i ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment