whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1541)

Database (90)
Files (137)
Forms (107)
Graphic (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Math (76)
Misc (126)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (266)
VCL (242)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

33 Visitors Online


 
...mix two colors using transparency coefficient?
Autor: Yurii Zhukow
Homepage: http://www.infoteh.ru/yz
[ Print tip ]  

Tip Rating (19):  
     


// 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;


 

Rate this tip:

poor
very good


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners