1 // SPDX-License-Identifier: LGPL-3.0-only (modified to allow linking)
2 {******************************* CONTRIBUTOR(S) ******************************
3 - Edivando S. Santos Brasil | mailedivando@gmail.com
4   (Compatibility with delphi VCL 11/2018)
5 
6 ***************************** END CONTRIBUTOR(S) *****************************}
7 
8 unit BCBrightAndContrast;
9 
10 { Unit contributed by esvignolo }
11 
12 {$I bgracontrols.inc}
13 
14 interface
15 
16 uses
17   Classes, SysUtils, Graphics,{$IFDEF FPC}LCLType{$ELSE}Types, BGRAGraphics, GraphType, FPImage{$ENDIF};
18 
Brightnull19 function Bright(aColor: TColor; BrightPercent: byte): TColor;
GetContrastColornull20 function GetContrastColor(ABGColor: TColor): TColor;
21 
22 implementation
23 
Brightnull24 function Bright(aColor: TColor; BrightPercent: byte): TColor;
25 var
26   r, g, b: byte;
27 begin
28   aColor := ColorToRGB(aColor);
29   r := Red(aColor);
30   g := Green(aColor);
31   b := Blue(aColor);
32   {muldiv (255-r, BrightPercent, 100); - color value in percentage,
33   By which it is necessary to increase initial color (integer)}
34   r := r + muldiv(255 - r, BrightPercent, 100);
35   g := g + muldiv(255 - g, BrightPercent, 100);
36   b := b + muldiv(255 - b, BrightPercent, 100);
37   Result := RGBToColor(r, g, b);
38 end;
39 
GetContrastColornull40 function GetContrastColor(ABGColor: TColor): TColor;
41 var
42   ADouble: double;
43   R, G, B: byte;
44 begin
45   if ABGColor <= 0 then
46   begin
47     Result := clWhite;
48     Exit; // *** EXIT RIGHT HERE ***
49   end;
50 
51   if ABGColor = clWhite then
52   begin
53     Result := clBlack;
54     Exit; // *** EXIT RIGHT HERE ***
55   end;
56 
57   // Get RGB from Color
58   R := Red(ABGColor);
59   G := Green(ABGColor);
60   B := Blue(ABGColor);
61 
62   // Counting the perceptive luminance - human eye favors green color...
63   ADouble := 1 - (0.299 * R + 0.587 * G + 0.114 * B) / 255;
64 
65   if (ADouble < 0.5) then
66     Result := clBlack  // bright colors - black font
67   else
68     Result := clWhite;  // dark colors - white font
69 end;
70 
71 end.
72 
73