Pacific Blue Software Logo

How to convert Web colour to Delphi on Windows

How to Convert Web colour to Delphi (or Windows)

I often have convert Web colour to Delphi (on Windows), It is simple, here is how.


Whats the difference between them?

Here are the color codes for Navy:

Web colour (CSS/HTML): #000080
Delphi colour (VCL): $800000

The difference is because:

Web uses RGB format (Red-Green-Blue)
Delphi uses BGR format (Blue-Green-Red)

So for Navy:

Web: RGB(0,0,128) = #000080
Delphi: BGR(128,0,0) = $800000

You can convert between them by swapping the first and last byte pairs.


Here is the Delphi code to do it


function ConvertWebColorToDelphi (const WebColor: string) : TColor;
var
  R, G, B: Byte;
begin
  // Remove # if present
  if WebColor[1] = '#' then
    WebColorHex := Copy(WebColor, 2)
  else
    WebColorHex := WebColor;

  // Convert hex to RGB
  R := StrToInt('$' + Copy(WebColorHex, 1, 2));
  G := StrToInt('$' + Copy(WebColorHex, 3, 2));
  B := StrToInt('$' + Copy(WebColorHex, 5, 2));

  // Convert RGB to BGR
  Result := RGB(B, G, R);
end;

// Usage
var
  NavyColor : TColor;
begin
  NavyColor := ConvertWebColorToDelphi ('#000080');  // Converts web navy to Delphi navy
end;
  

Ways to Convert Web colour to Delphi (or Windows)


Back to Articles for Developers
Back to Articles on Delphi

If you found this useful, then please consider making a donation.

paypal
QR Code for donation Please donate if helpful