www.pudn.com > XParticles.rar > SmoothColorUnit.pas


unit SmoothColorUnit; 
 
interface 
 
//------------------------------------------------------------------------------ 
type 
  TSmoothColor = record 
    R, G, B, A: Single; 
 
{    class operator Implicit(a: Real): TSmoothColor; 
    class operator Add(a, b: TSmoothColor): TSmoothColor; 
    class operator Multiply(a, b: TSmoothColor): TSmoothColor;} 
  end; 
 
//------------------------------------------------------------------------------ 
const 
  MIN_SM_COLOR = 0; 
  MAX_SM_COLOR = 255; 
 
  smClear: TSmoothColor = (R: 0; G: 0; B: 0; A: 0); 
  smBlack: TSmoothColor = (R: 0; G: 0; B: 0; A: MAX_SM_COLOR); 
  smWhite: TSmoothColor = (R: MAX_SM_COLOR; G: MAX_SM_COLOR; B: MAX_SM_COLOR; A: MAX_SM_COLOR); 
 
//------------------------------------------------------------------------------ 
function ToSmoothColor(const Color: Cardinal): TSmoothColor; 
function FromSmoothColor(Color: TSmoothColor): Cardinal; 
function SmoothRGBA(R, G, B, A: Real; Norm: boolean = true): TSmoothColor; 
function NormSmoothColor(Color: TSmoothColor): TSmoothColor; 
 
// for Delphi 2005 and lower versions 
function MultSmColor(const a: TSmoothColor; const b: Real): TSmoothColor; 
function AddSmColors(const a, b: TSmoothColor): TSmoothColor; 
function MultSmColors(const a, b: TSmoothColor): TSmoothColor; 
 
//------------------------------------------------------------------------------ 
 
implementation 
//------------------------------------------------------------------------------ 
function NormSmoothColor(Color: TSmoothColor): TSmoothColor; 
begin 
Result:= Color; 
 
if (Result.R < MIN_SM_COLOR) then Result.R:= MIN_SM_COLOR 
else 
  if (Result.R > MAX_SM_COLOR) then Result.R:= MAX_SM_COLOR; 
 
if (Result.G < MIN_SM_COLOR) then Result.G:= MIN_SM_COLOR 
else 
  if (Result.G > MAX_SM_COLOR) then Result.G:= MAX_SM_COLOR; 
 
if (Result.B < MIN_SM_COLOR) then Result.B:= MIN_SM_COLOR 
else 
  if (Result.B > MAX_SM_COLOR) then Result.B:= MAX_SM_COLOR; 
 
if (Result.A < MIN_SM_COLOR) then Result.A:= MIN_SM_COLOR 
else 
  if (Result.A > MAX_SM_COLOR) then Result.A:= MAX_SM_COLOR-1;// For perfomance 
end; 
//------------------------------------------------------------------------------ 
function ToSmoothColor(const Color: Cardinal): TSmoothColor; 
begin 
Result.R:= Color and $FF; 
Result.G:= (Color shr 8) and $FF; 
Result.B:= (Color shr 16) and $FF; 
Result.A:= (Color shr 24) and $FF; 
end; 
//------------------------------------------------------------------------------ 
function FromSmoothColor(Color: TSmoothColor): Cardinal; 
begin 
Result:= (Trunc(Color.R) and $FF)or 
         ((Trunc(Color.G)and $FF) shl 8)or 
         ((Trunc(Color.B)and $FF) shl 16)or 
         ((Trunc(Color.A)and $FF) shl 24); 
{} 
{Result:= Trunc(Color.R)or 
         (Trunc(Color.G) shl 8)or 
         (Trunc(Color.B) shl 16)or 
         (Trunc(Color.A) shl 24); 
{} 
end; 
//------------------------------------------------------------------------------ 
function SmoothRGBA(R, G, B, A: Real; Norm: boolean = true): TSmoothColor; 
begin 
Result.R:= R; 
Result.G:= G; 
Result.B:= B; 
Result.A:= A; 
if (Norm) then 
  Result:= NormSmoothColor(Result); 
end; 
//------------------------------------------------------------------------------ 
 
//------------------------------------------------------------------------------ 
// TSmoothColor. 
//------------------------------------------------------------------------------ 
function MultSmColor(const a: TSmoothColor; const b: Real): TSmoothColor; 
begin 
Result.R:= a.R * b; 
Result.G:= a.G * b; 
Result.B:= a.B * b; 
Result.A:= a.A * b; 
end; 
//------------------------------------------------------------------------------ 
function AddSmColors(const a, b: TSmoothColor): TSmoothColor; 
begin 
Result.R:= a.R + b.R; 
Result.G:= a.G + b.G; 
Result.B:= a.B + b.B; 
Result.A:= a.A + b.A; 
//Result:= NormSmoothColor(Result); 
end; 
//------------------------------------------------------------------------------ 
function MultSmColors(const a, b: TSmoothColor): TSmoothColor; 
begin 
Result.R:= a.R * b.R; 
Result.G:= a.G * b.G; 
Result.B:= a.B * b.B; 
Result.A:= a.A * b.A; 
//Result:= NormSmoothColor(Result); 
end; 
//------------------------------------------------------------------------------ 
{class operator TSmoothColor.Implicit(a: Real): TSmoothColor; 
begin 
Result.R:= a; 
Result.G:= a; 
Result.B:= a; 
Result.A:= a; 
end; 
//------------------------------------------------------------------------------ 
class operator TSmoothColor.Add(a, b: TSmoothColor): TSmoothColor; 
begin 
Result.R:= a.R + b.R; 
Result.G:= a.G + b.G; 
Result.B:= a.B + b.B; 
Result.A:= a.A + b.A; 
//Result:= NormSmoothColor(Result); 
end; 
//------------------------------------------------------------------------------ 
class operator TSmoothColor.Multiply(a, b: TSmoothColor): TSmoothColor; 
begin 
Result.R:= a.R * b.R; 
Result.G:= a.G * b.G; 
Result.B:= a.B * b.B; 
Result.A:= a.A * b.A; 
//Result:= NormSmoothColor(Result); 
end; } 
//------------------------------------------------------------------------------ 
end.