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