TurboPowerIPro: Add support of 3-digit html color codes.

This commit is contained in:
wp_xyz 2024-06-01 13:55:34 +02:00
parent a3ac0ccd82
commit 2acde1ee85

View File

@ -211,6 +211,18 @@ begin
end; end;
function TryColorFromString(S: String; out AColor: TColor; out AErrMsg: String): Boolean; function TryColorFromString(S: String; out AColor: TColor; out AErrMsg: String): Boolean;
function ByteValue(HighNibble, LowNibble: Char): Byte;
var
S: String[3] = '$xx';
Err: Integer;
begin
S[2] := HighNibble;
S[3] := LowNibble;
val(S, Result, Err);
if Err <> 0 then Result := 255;
end;
var var
R, G, B, Err: Integer; R, G, B, Err: Integer;
begin begin
@ -226,21 +238,23 @@ begin
S := UpperCase(S); S := UpperCase(S);
if S[1] = '#' then if S[1] = '#' then
begin begin
if Length(S) <> 7 then if not (Length(S) in [4, 7]) then
begin begin
AErrMsg := SHtmlInvColor + S; AErrMsg := SHtmlInvColor + S;
Result := false; Result := false;
end end else
else begin begin
val('$'+Copy(S,2,2), R, Err); if Length(S) = 7 then
if Err <> 0 then begin
R := 255; R := ByteValue(S[2], S[3]);
val('$'+Copy(S,4,2), G, Err); G := ByteValue(S[4], S[5]);
if Err <> 0 then B := ByteValue(S[6], S[7]);
G := 255; end else
val('$'+Copy(S,6,2), B, Err); begin
if Err <> 0 then R := ByteValue(S[2], S[2]);
B := 255; G := ByteValue(S[3], S[3]);
B := ByteValue(S[4], S[4]);
end;
AColor := RGBToColor(R, G, B); AColor := RGBToColor(R, G, B);
Result := true; Result := true;
end; end;
@ -252,15 +266,9 @@ begin
end else end else
if Length(S) = 6 then if Length(S) = 6 then
try try
val('$'+Copy(S,1,2), R, Err); R := ByteValue(S[1], S[2]);
if Err <> 0 then G := ByteValue(S[3], S[4]);
R := 255; B := ByteValue(S[5], S[6]);
val('$'+Copy(S,3,2), G, Err);
if Err <> 0 then
G := 255;
val('$'+Copy(S,5,2), B, Err);
if Err <> 0 then
B := 255;
AColor := RGBToColor(R, G, B); AColor := RGBToColor(R, G, B);
Result := true; Result := true;
except except