Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Hue = 0..255 (circular)
- // Saturation = 0..255
- // Lightness = 0..255
- type
- iColorHSL = record H,S,L: Int32; end;
- function ToHSL_I(R,G,B: Byte): iColorHSL;
- var
- cLo, cHi, deltaC: Int32;
- begin
- cLo := Min(R, Min(G,B));
- cHi := Max(R, Max(G,B));
- deltaC := cHi - cLo;
- Result.L := (256 * (cHi + cLo)) shr 9;
- if deltaC = 0 then
- begin
- Result.H := 0;
- Result.S := 0;
- end else
- begin
- if 128 > Result.L then Result.S := (256 * deltaC) div (cHi + cLo)
- else Result.S := (256 * deltaC) div (510-cHi-cLo);
- if (R = cHi) then Result.H := (43 * (G - B)) div deltaC
- else if (G = cHi) then Result.H := 85 + (43 * (B - R)) div deltaC
- else{if (B = cHi) then}Result.H := 171 + (43 * (R - G)) div deltaC;
- if(Result.H < 0) then Result.H += 256;
- end;
- end;
- // Hue = 0..3600 (circular)
- // Saturation = 0..1000
- // Lightness = 0..1000
- function ToHSL_I(R,G,B: Byte): iColorHSL;
- var
- cLo, cHi, deltaC: Int32;
- begin
- cLo := Min(R, Min(G,B));
- cHi := Max(R, Max(G,B));
- deltaC := cHi - cLo;
- Result.L := (1004 * (cHi + cLo)) shr 9;
- if deltaC = 0 then
- begin
- Result.H := 0;
- Result.S := 0;
- end else
- begin
- {S := 256 * (cHi - cLo) div cHi; <--- HSV light}
- if Result.L < 500 then Result.S := (1000 * deltaC) div (cHi + cLo)
- else Result.S := (1000 * deltaC) div (510-cHi-cLo);
- if (R = cHi) then Result.H := (600 * (G - B)) div deltaC
- else if (G = cHi) then Result.H := 1200 + (600 * (B - R)) div deltaC
- else {if(B = cHi) then}Result.H := 2400 + (600 * (R - G)) div deltaC;
- if(Result.H < 0) then Result.H += 3600;
- end;
- end;
- // MAX LIMIT
- procedure ToHSL_I(R,G,B: Byte; out H,S,L: UInt32);
- var
- cLo, cHi, deltaC: Int32;
- begin
- cLo := Min(R, Min(G,B));
- cHi := Max(R, Max(G,B));
- deltaC := cHi - cLo;
- L := (10_039_216 * (cHi + cLo)) shr 9;
- if deltaC = 0 then
- begin
- H := 0;
- S := 0;
- end else
- begin
- if L < 5_000_000 then S := (10_000_000 * deltaC) div (cHi + cLo)
- else S := (10_000_000 * deltaC) div (510-cHi-cLo);
- if (R = cHi) then H := (6_000_000 * (G - B)) div deltaC
- else if (G = cHi) then H := 12_000_000 + (6_000_000 * (B - R)) div deltaC
- else {if(B = cHi) then}H := 24_000_000 + (6_000_000 * (R - G)) div deltaC;
- if(H < 0) then H += 36_000_000;
- end;
- end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement