首页  编辑  

把前景色和背景色按指定透明颜色混合

Tags: /超级猛料/Picture.图形图像编程/颜色处理/   Date Created:

把前景色和背景色按指定透明颜色混合

http://www.swissdelphicenter.ch/torry/showcode.php?id=1832

Author: Yurii Zhukow

// This function mixes two bytes According to value of TRANS

// The value of TRANS is between 0 (result then will be equal to FG)

// and 255 (result then will be equal to BG)

function MixBytes(FG, BG, TRANS: byte): byte;

asm

 push bx  // push some regs

 push cx

 push dx

 mov DH,TRANS // remembering Transparency value (or Opacity - as you like)

 mov BL,FG    // filling registers with our values

 mov AL,DH    // BL = ForeGround (FG)

 mov CL,BG    // CL = BackGround (BG)

 xor AH,AH    // Clear High-order parts of regs

 xor BH,BH

 xor CH,CH

 mul BL       // AL=AL*BL

 mov BX,AX    // BX=AX

 xor AH,AH

 mov AL,DH

 xor AL,$FF   // AX=(255-TRANS)

 mul CL       // AL=AL*CL

 add AX,BX    // AX=AX+BX

 shr AX,8     // Fine! Here we have mixed value in AL

 pop dx       // Hm... No rubbish after us, ok?

 pop cx

 pop bx       // Bye, dear Assembler - we go home to Delphi!

end;

// Here we mix R,G and B channels of our colors separately.

// The value of T is between 0 and 255 as described above.

// As you know, TColor value is 4 bytes length integer value where

// low byte is red channel, 2nd byte is green and 3rd byte is blue

function MixColors(FG, BG: TColor; T: byte): TColor;

var r,g,b:byte;

begin

 R := MixBytes(FG and 255,BG and 255,T); // extracting and mixing Red

 G := MixBytes((FG shr 8) and 255,(BG shr 8) and 255,T); // the same with green

 B := MixBytes((FG shr 16) and 255,(BG shr 16) and 255,T); // and blue, of course

 Result := r+g*256+b*65536; // finishing with combining all channels together

end;