Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request to add 2 small functions in JclGraphUtils unit; #1

Open
devEric69 opened this issue Jul 18, 2019 · 0 comments
Open

Request to add 2 small functions in JclGraphUtils unit; #1

devEric69 opened this issue Jul 18, 2019 · 0 comments

Comments

@devEric69
Copy link

devEric69 commented Jul 18, 2019

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:

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant