Advertisement
WarPie90

RGBToHSL (Integer math)

Mar 14th, 2018
558
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.42 KB | None | 0 0
  1. // Hue        = 0..255 (circular)
  2. // Saturation = 0..255
  3. // Lightness  = 0..255
  4. type
  5.   iColorHSL = record H,S,L: Int32; end;
  6.  
  7. function ToHSL_I(R,G,B: Byte): iColorHSL;
  8. var
  9.   cLo, cHi, deltaC: Int32;
  10. begin
  11.   cLo := Min(R, Min(G,B));
  12.   cHi := Max(R, Max(G,B));
  13.   deltaC := cHi - cLo;
  14.  
  15.   Result.L := (256 * (cHi + cLo)) shr 9;
  16.   if deltaC = 0 then
  17.   begin
  18.     Result.H := 0;
  19.     Result.S := 0;
  20.   end else
  21.   begin
  22.     if 128 > Result.L then Result.S := (256 * deltaC) div (cHi + cLo)
  23.     else                   Result.S := (256 * deltaC) div (510-cHi-cLo);
  24.  
  25.     if      (R = cHi) then Result.H :=       (43 * (G - B)) div deltaC
  26.     else if (G = cHi) then Result.H := 85  + (43 * (B - R)) div deltaC
  27.     else{if (B = cHi) then}Result.H := 171 + (43 * (R - G)) div deltaC;
  28.     if(Result.H < 0) then  Result.H += 256;
  29.   end;
  30. end;
  31.  
  32.  
  33. // Hue        = 0..3600 (circular)
  34. // Saturation = 0..1000
  35. // Lightness  = 0..1000
  36. function ToHSL_I(R,G,B: Byte): iColorHSL;
  37. var
  38.   cLo, cHi, deltaC: Int32;
  39. begin
  40.   cLo := Min(R, Min(G,B));
  41.   cHi := Max(R, Max(G,B));
  42.   deltaC := cHi - cLo;
  43.  
  44.   Result.L := (1004 * (cHi + cLo)) shr 9;
  45.   if deltaC = 0 then
  46.   begin
  47.     Result.H := 0;
  48.     Result.S := 0;
  49.   end else
  50.   begin
  51.     {S := 256 * (cHi - cLo) div cHi; <--- HSV light}
  52.     if Result.L < 500 then Result.S := (1000 * deltaC) div (cHi + cLo)
  53.     else                   Result.S := (1000 * deltaC) div (510-cHi-cLo);
  54.     if      (R = cHi) then Result.H :=        (600 * (G - B)) div deltaC
  55.     else if (G = cHi) then Result.H := 1200 + (600 * (B - R)) div deltaC
  56.     else {if(B = cHi) then}Result.H := 2400 + (600 * (R - G)) div deltaC;
  57.     if(Result.H < 0) then  Result.H += 3600;
  58.   end;
  59. end;
  60.  
  61.  
  62.  
  63. // MAX LIMIT
  64. procedure ToHSL_I(R,G,B: Byte; out H,S,L: UInt32);
  65. var
  66.   cLo, cHi, deltaC: Int32;
  67. begin
  68.   cLo := Min(R, Min(G,B));
  69.   cHi := Max(R, Max(G,B));
  70.   deltaC := cHi - cLo;
  71.  
  72.   L := (10_039_216 * (cHi + cLo)) shr 9;
  73.   if deltaC = 0 then
  74.   begin
  75.     H := 0;
  76.     S := 0;
  77.   end else
  78.   begin
  79.     if L < 5_000_000 then S := (10_000_000 * deltaC) div (cHi + cLo)
  80.     else                  S := (10_000_000 * deltaC) div (510-cHi-cLo);
  81.  
  82.     if      (R = cHi) then H :=              (6_000_000 * (G - B)) div deltaC
  83.     else if (G = cHi) then H := 12_000_000 + (6_000_000 * (B - R)) div deltaC
  84.     else {if(B = cHi) then}H := 24_000_000 + (6_000_000 * (R - G)) div deltaC;
  85.     if(H < 0) then H += 36_000_000;
  86.   end;
  87. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement