Putting Colors in Order Listings



{____________________Putting Colors in Order_________________________}

{ LISTING ONE }

{--------------------------------------}
procedure Hvc2Rgb( H, V, C : Double; var R, G, B : Double);
  { Converts H-hue, V-value, C-chroma to R-red, G-green, B-blue }
const
  Pi    = 3.14159265358979324;
  Pi2o3 = 2.0 * Pi / 3.0;
  DpR   = 180.0 / Pi;          { Degrees per Radian }
var
  HRad : Double;
begin
  HRad:= H / DpR;
  R:= V - C * Cos( HRad + Pi2o3);
  G:= V - C * Cos( HRad);
  B:= V - C * Cos( HRad - Pi2o3);
end; { Hvc2Rgb }

{                                                     END LISTING ONE }

{ LISTING TWO }

{--------------------------------------}
function ATan2( Y, X : Double) : Double;  { Arc Tangent of Y over X }
  { -Pi < ATan2 <= Pi }
const
  Pi   = 3.14159265358979324;
  Pio2 = Pi / 2.0;
var
  Temp : Double;
begin
  if X = 0 then begin
    if Y > 0 then  ATan2:= Pio2
    else if Y < 0 then  ATan2:= -Pio2
    else  ATan2:= 0;
  end
  else begin
    Temp:= ArcTan( Y / X);
    if X > 0 then  ATan2:= Temp
    else if Y >= 0 then  ATan2:= Temp + Pi
    else  ATan2:= Temp - Pi;
  end;
end; { ATan2 }

{--------------------------------------}
procedure Rgb2Hvc( R, G, B : Double; var H, V, C : Double);
  { Converts R-red, G-green B-blue, to H-hue, V-value, C-chroma }
const
  Pi    = 3.14159265358979324;
  Pi2   = 2.0 * Pi;
  DpR   = 180.0 / Pi;          { Degrees per Radian }
  SqRt3 = 1.73205080756887719; { Square root of 3.0 }
var
  CosHue : Double;
  HRad   : Double;
begin
  V:= (R + G + B) / 3.0;
  HRad:= ATan2( R - B, SqRt3 * (V - G));
  CosHue:= Cos( HRad);
  if abs( CosHue) > 0.2 then
    C:= (V - G) / CosHue
  else
    C:= (R - B) / (SqRt3 * Sin( HRad));
  H:= HRad * DpR;
  if H < 0.0 then  H:= H + 360.0;
end; { Rgb2Hvc }

{                                                     END LISTING TWO }

{ LISTING THREE }

{--------------------------------------}
procedure Ega2Rgb( EgaCode : Integer; var R, G, B : Double);
  { Converts EGA color number to R-red, G-green B-blue }
begin
  R:= 0.0;  G:= 0.0;  B:= 0.0;
  if (EgaCode and  1) <> 0 then  B:= B + 0.75;
  if (EgaCode and  2) <> 0 then  G:= G + 0.75;
  if (EgaCode and  4) <> 0 then  R:= R + 0.75;
  if (EgaCode and  8) <> 0 then  B:= B + 0.25;
  if (EgaCode and 16) <> 0 then  G:= G + 0.25;
  if (EgaCode and 32) <> 0 then  R:= R + 0.25;
end; { Ega2Rgb }

{                                                        END LISTINGS }

Return to Putting Colors in Order
Return to Harry's Home Page


This page accessed times since October 20, 2004.
Page created by: hjsmithh@sbcglobal.net
Changes last made on Saturday, 14-May-05 12:44:05 PDT

1