Search code examples
delphicolorsrgbhsl

delphi: RGB to HSL incorrect


i'm want to do RGB to HSL with delphi, my code:

uses 
  System.UIConsts;
procedure TForm1.Button3Click(Sender: TObject);
var
  H, S, L: Single;
begin
  var R := 157;
  var G := 157;
  var B := 152;

  var alc := MakeColor(R, G, B, 255);

  RGBtoHSL(alc, H, S, L);

  //FROM: https://www.rapidtables.com/convert/color/rgb-to-hsl.html
  //HSL: 60,2.5,60.6 <--correct Value
  ShowMessage(Format('HSL: %.1f,%.1f,%.1f', [H, S * 100, L * 100])); //<---wrong Hue's value
end;

how can i get the correct Hue value? thanks.


Solution

  • It appears like the UIConsts.RGBtoHSL function represents a colour's hue as a real number between 0 and 1. If you need a value between 0 and 360, you need to scale by a factor of 360:

    Format('HSL: %.1f,%.1f,%.1f', [360 * H, S * 100, L * 100])
    

    I consider it a documentation bug that the Embarcadero documentation doesn't explicitly state the convention used by the function.