How to find the average between RGB colors c1 and c2 in Pascal

1 Answer

0 votes
program AverageColorHex;

uses
  SysUtils;

function RGBToHex(r, g, b: Byte): string;
begin
  RGBToHex := Format('#%.2x%.2x%.2x', [r, g, b]);
end;

var
  r1, g1, b1, r2, g2, b2: Byte;
  avgR, avgG, avgB: Byte;
  hexColor: string;
begin
  // Color 1: RGB(255, 100, 50)
  r1 := 255;
  g1 := 100;
  b1 := 50;

  // Color 2: RGB(50, 170, 200)
  r2 := 50;
  g2 := 170;
  b2 := 200;

  // Average components
  avgR := (r1 + r2) div 2;
  avgG := (g1 + g2) div 2;
  avgB := (b1 + b2) div 2;

  // Convert to hex format
  hexColor := RGBToHex(avgR, avgG, avgB);
  WriteLn('Average Color (hex): ', hexColor);
end.



(*
run:

Average Color (hex): #98877D

*)

 



answered Jun 18, 2025 by avibootz

Related questions

...