-
Notifications
You must be signed in to change notification settings - Fork 0
/
commondrawutils.pas
198 lines (178 loc) · 5.45 KB
/
commondrawutils.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
unit CommonDrawUtils;
{ some graphics and drawing methods
Copyright (C) 2019 Mohammadreza Bahrami [email protected]
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version with the following modification:
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent modules,and
to copy and distribute the resulting executable under terms of your choice,
provided that you also meet, for each linked independent module, the terms
and conditions of the license of that module. An independent module is a
module which is not derived from or based on this library. If you modify
this library, you may extend this exception to your version of the library,
but you are not obligated to do so. If you do not wish to do so, delete this
exception statement from your version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
for more details.
You should have received a copy of the GNU Library General Public License
along with this library; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1335, USA.
}
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Graphics, Controls, CommonNumeralUtils, Math;
procedure HalfEllipse(ACanvas: TCanvas; AColor: TColor;
AWidth, AHeight: Integer; AAnchorKind: TAnchorKind = akTop);
procedure HorzScaleTicks(ACanvas: TCanvas; AColor: TColor;
AWidth, AHeight: Integer; ATickWidth: Integer = 1; ASteps: Integer = 6;
AHorizontalFlipped: Boolean = False; AVerticalFlipped: Boolean = False);
function Luminance(AColor: TColor): Single; //returns a value from 0 to 255
function LuminanceLevel(AColor: TColor): Integer; //returns a percentage value
function LuminanceLevel(ABitmap: TBitmap): Integer; overload;
implementation
procedure HalfEllipse(ACanvas: TCanvas; AColor: TColor;
AWidth, AHeight: Integer; AAnchorKind: TAnchorKind = akTop);
var
RoundPart: TRect;
begin
if( AWidth < 4) or (AHeight < 4) then Exit;
RoundPart:=Default(TRect);
case AAnchorKind of
akTop:
begin
RoundPart.Left:=0;
RoundPart.Top:=-(AHeight div 2)-1;
end;
akLeft:
begin
RoundPart.Left:=-(AWidth div 2)-1;
RoundPart.Top:=0;
end;
akRight:
begin
RoundPart.Left:=(AWidth div 2)+1;
RoundPart.Top:=0;
end;
akBottom:
begin
RoundPart.Left:=0;
RoundPart.Top:=(AHeight div 2)+1;
end;
end;
RoundPart.Width:=AWidth;
RoundPart.Height:=AHeight;
ACanvas.Brush.Color:=AColor;
ACanvas.Pen.Color:=AColor;
ACanvas.Ellipse(RoundPart);
end;
procedure HorzScaleTicks(ACanvas: TCanvas; AColor: TColor; AWidth,
AHeight: Integer; ATickWidth: Integer; ASteps: Integer;
AHorizontalFlipped: Boolean; AVerticalFlipped: Boolean);
var
RectPart: TRect;
Scale: Double;
i: Integer;
begin
ACanvas.Brush.Color:=AColor;
ACanvas.Pen.Color:=AColor;
RectPart:=Default(TRect);
if ATickWidth<1 then
ATickWidth:=1;
if ASteps<=ATickWidth then
ASteps:=ATickWidth+1;
Scale:=AHeight/AWidth;
i:=0;
with ACanvas do
begin
if AHorizontalFlipped and not AVerticalFlipped then
begin
while i < AWidth-1 do
begin
RectPart.Left:=i;
RectPart.Top:=Round(Scale*i);
RectPart.Width:=ATickWidth;
RectPart.Height:=AHeight-RectPart.Top;
Rectangle(RectPart);
Inc(i, ASteps);
end;
end
else if not AHorizontalFlipped and AVerticalFlipped then
begin
while i < AWidth-1 do
begin
RectPart.Left:=i;
RectPart.Top:=0;
RectPart.Width:=ATickWidth;
RectPart.Height:=Round(Scale*i);
Rectangle(RectPart);
Inc(i, ASteps);
end;
end
else if AHorizontalFlipped and AVerticalFlipped then
begin
while i < AWidth-1 do
begin
RectPart.Left:=i;
RectPart.Top:=0;
RectPart.Width:=ATickWidth;
RectPart.Height:=AHeight-Round(Scale*i);
Rectangle(RectPart);
Inc(i, ASteps);
end;
end
else
begin
while i < AWidth-1 do
begin
RectPart.Left:=i;
RectPart.Top:=AHeight-Round(Scale*i);
RectPart.Width:=ATickWidth;
RectPart.Height:=AHeight-RectPart.Top;
Rectangle(RectPart);
Inc(i, ASteps);
end;
end;
end;
end;
function Luminance(AColor: TColor): Single;
var
r,g,b: Word;
begin
r:=Red(AColor);
g:=Green(AColor);
b:=Blue(AColor);
Result:=
Sqrt((r*r*0.241)+(g*g*0.691)+(b*b*0.068));
end;
function LuminanceLevel(AColor: TColor): Integer;
begin
Result:=
ConvertInRange(Round(Luminance(AColor)),0,255,0,100);
end;
function LuminanceLevel(ABitmap: TBitmap): Integer;
var
llevels: array of Single;
c: TColor;
i,j,k: Integer;
begin
k:=-1;
llevels:=nil;
SetLength(llevels,ABitmap.Width*ABitmap.Height);
for i:=0 to ABitmap.Height-1 do
begin
for j:=0 to ABitmap.Width-1 do
begin
c:=ABitmap.Canvas.Pixels[i,j];
Inc(k);
llevels[k]:=Luminance(c);
end;
end;
Result:=ConvertInRange(Round(Mean(llevels)),0,255,0,100);
end;
end.