You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
type TColorSwap
private
public
class function ComplementaryColorFromHtmlColor(AHtmlColor: TColor; bSwapRedBlue: Boolean = False): TColor;
class function ComplementaryColor(AColor: TColor): TColor;
class function HighContrastColor(AColor: TColor): TColor;
class function SwappedRedGreenColor(AColor: TColor): TColor;
class function SwappedRedBlueColor(AColor: TColor): TColor;
end;
.../...
implementation
.../...
class function TColorSwap.ComplementaryColorFromHtmlColor(AHtmlColor: TColor; bSwapRedBlue: Boolean = False): TColor;
{$REGION 'Xls: Comments section'}
{ @param bSwapRedBlue: this parameter depends from your material context, and the purpose of this call:
from the logic of use: let's say you found and copy a pretty #aabbcc color on a website, i.e. a hexdecimal representation in a string type, and you are looking for its complementary color to highlight TLabels on a TPanel for exemple, in Free Pascal. #aabbcc means that Red='#aa', Green='#bb', and Blue='#cc'. Even before calculating the complementary, it must be known that if you want to copy this color in Free Pascal, as it is, ie $aabbcc to a Canvas.Color property for example, it will not always work (depending on CPU type): indeed, the binary bits are read from right to left. So, the compiled equivalent will \ could have to be Red=$cc, Green=$bb Blue=$cc on your machine.
from your CPU: into a file, Motorola writes AColor's RGB in that order ie RGB; but Intel writes it inversed ie BGR.
Trick, to check first, which convention is used:
HTML writes the hex code in Motorola format, and the GIMP shows the color hex code in Motorola Format too ==> with such a format, if you take this color code from HTML or from the GIMP, then you'll have to swap red and blue.
easy other test: fill some component's Color property, with Color:= $FF ==> if it is red you are on an Intel machine (swap needed, if we use a HTML color's display).
Otherwise if it is blue you are on a Motorala machine (no swap needed, if we use a HTML color's display).
NB: if you use reference colors like clBlack, clLime, etc., from the LCL to find the complementary color of these, then leave bSwapRedBlue to false: the colors of the components are already natively - with respect to your machine's CPU - correctly managed. Or even better: use ComplementaryColor.
}
{$ENDREGION}
function SwapRedBlue(AColor: TColor): TColor;
{$REGION 'Xls: Comments section'}
{ See ComplementaryColorFromHtmlColor explanations. }
{$ENDREGION}
begin
Result:= RGBToColor(Graphics.Blue(AColor), Graphics.Green(AColor), Graphics.Red(AColor))
end;
begin
if bSwapRedBlue then
SwapRedBlue(AHtmlColor);
Result:= RGBToColor(255 - Graphics.Red(AHtmlColor), 255 - Graphics.Green(AHtmlColor), 255 - Graphics.Blue(AHtmlColor));
end;
class function TColorSwap.ComplementaryColor(AColor: TColor): TColor;
{$REGION 'Xls: Comments section'}
{ most efficient for complementary color. }
{$ENDREGION}
begin
Result := clWhite - ColorToRGB(AColor);
end;
class function TColorSwap.HighContrastColor(AColor: TColor): TColor;
{$REGION 'Xls: Comments section'}
{ The one that returns the color with the most contrast, in the middle of grey or pastel colors. }
{$ENDREGION}
begin
AColor := ColorToRGB(AColor);
if Red(AColor) + Green(AColor) + Blue(AColor) < 3*128 then
Result := clWhite
else
Result := clBlack;
end;
class function TColorSwap.SwappedRedGreenColor(AColor: TColor): TColor;
{$REGION 'Xls: Comments section'}
{ inspired from the "triadic color scheme": if color_base=RGB ==> sequencing triad_1=GBR, triad_2=BRG... }
{$ENDREGION}
begin
AColor := ColorToRGB(AColor);
Result := RGBToColor(Green(AColor), Red(AColor), Blue(AColor));
end;
class function TColorSwap.SwappedRedBlueColor(AColor: TColor): TColor;
{$REGION 'Xls: Comments section'}
{ inspired from the "triadic color scheme": if color_base=RGB ==> sequencing triad_1=GBR, triad_2=BRG... }
{$ENDREGION}
begin
AColor := ColorToRGB(AColor);
Result := RGBToColor(Blue(AColor), Green(AColor), Red(AColor));
end;
Best regards.
The text was updated successfully, but these errors were encountered:
type
.../...
type TColorSwap
private
public
class function ComplementaryColorFromHtmlColor(AHtmlColor: TColor; bSwapRedBlue: Boolean = False): TColor;
class function ComplementaryColor(AColor: TColor): TColor;
class function HighContrastColor(AColor: TColor): TColor;
class function SwappedRedGreenColor(AColor: TColor): TColor;
class function SwappedRedBlueColor(AColor: TColor): TColor;
end;
.../...
implementation
.../...
class function TColorSwap.ComplementaryColorFromHtmlColor(AHtmlColor: TColor; bSwapRedBlue: Boolean = False): TColor;
{$REGION 'Xls: Comments section'}
{
@param bSwapRedBlue: this parameter depends from your material context, and the purpose of this call:
Trick, to check first, which convention is used:
Otherwise if it is blue you are on a Motorala machine (no swap needed, if we use a HTML color's display).
NB: if you use reference colors like clBlack, clLime, etc., from the LCL to find the complementary color of these, then leave bSwapRedBlue to false: the colors of the components are already natively - with respect to your machine's CPU - correctly managed. Or even better: use ComplementaryColor.
}
{$ENDREGION}
begin
if bSwapRedBlue then
SwapRedBlue(AHtmlColor);
Result:= RGBToColor(255 - Graphics.Red(AHtmlColor), 255 - Graphics.Green(AHtmlColor), 255 - Graphics.Blue(AHtmlColor));
end;
class function TColorSwap.ComplementaryColor(AColor: TColor): TColor;
{$REGION 'Xls: Comments section'}
{ most efficient for complementary color. }
{$ENDREGION}
begin
Result := clWhite - ColorToRGB(AColor);
end;
class function TColorSwap.HighContrastColor(AColor: TColor): TColor;
{$REGION 'Xls: Comments section'}
{ The one that returns the color with the most contrast, in the middle of grey or pastel colors. }
{$ENDREGION}
begin
AColor := ColorToRGB(AColor);
if Red(AColor) + Green(AColor) + Blue(AColor) < 3*128 then
Result := clWhite
else
Result := clBlack;
end;
class function TColorSwap.SwappedRedGreenColor(AColor: TColor): TColor;
{$REGION 'Xls: Comments section'}
{ inspired from the "triadic color scheme": if color_base=RGB ==> sequencing triad_1=GBR, triad_2=BRG... }
{$ENDREGION}
begin
AColor := ColorToRGB(AColor);
Result := RGBToColor(Green(AColor), Red(AColor), Blue(AColor));
end;
class function TColorSwap.SwappedRedBlueColor(AColor: TColor): TColor;
{$REGION 'Xls: Comments section'}
{ inspired from the "triadic color scheme": if color_base=RGB ==> sequencing triad_1=GBR, triad_2=BRG... }
{$ENDREGION}
begin
AColor := ColorToRGB(AColor);
Result := RGBToColor(Blue(AColor), Green(AColor), Red(AColor));
end;
Best regards.
The text was updated successfully, but these errors were encountered: