Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading