| 1 |
unit mbUtils; |
| 2 |
|
| 3 |
{$mode objfpc}{$H+} |
| 4 |
|
| 5 |
interface |
| 6 |
|
| 7 |
uses |
| 8 |
Classes, SysUtils, Graphics, LCLIntf; |
| 9 |
|
| 10 |
procedure Clamp(var AValue: Integer; AMin, AMax: Integer); overload; |
| 11 |
procedure Clamp(var AValue: Double; AMin, AMax: Double); overload; |
| 12 |
procedure DrawHorDottedLine(ACanvas: TCanvas; X1, X2, Y: Integer; AColor: TColor); |
| 13 |
function PointInCircle(p: TPoint; Size: integer): boolean; |
| 14 |
function PtInCircle(p, ctr: TPoint; Radius: Integer): Boolean; |
| 15 |
|
| 16 |
function HighContrastColor(AColor: TColor): TColor; |
| 17 |
|
| 18 |
function HeightOfRect(R: TRect): Integer; |
| 19 |
function WidthOfRect(R: TRect): Integer; |
| 20 |
function IsEmptyRect(R: TRect): Boolean; |
| 21 |
|
| 22 |
const |
| 23 |
EMPTY_RECT: TRect = (Left: -1; Top: -1; Right: -1; Bottom: -1); |
| 24 |
TWO_PI = 2.0 * pi; |
| 25 |
|
| 26 |
implementation |
| 27 |
|
| 28 |
procedure Clamp(var AValue: integer; AMin, AMax: integer); |
| 29 |
begin |
| 30 |
if AValue < AMin then AValue := AMin; |
| 31 |
if AValue > AMax then AValue := AMax; |
| 32 |
end; |
| 33 |
|
| 34 |
procedure Clamp(var AValue: Double; AMin, AMax: Double); |
| 35 |
begin |
| 36 |
if AValue < AMin then AValue := AMin; |
| 37 |
if AValue > AMax then AValue := AMax; |
| 38 |
end; |
| 39 |
|
| 40 |
procedure DrawHorDottedLine(ACanvas: TCanvas; X1, X2, Y: Integer; AColor: TColor); |
| 41 |
begin |
| 42 |
while X1 <= X2 do begin |
| 43 |
ACanvas.Pixels[X1, Y] := AColor; |
| 44 |
inc(X1, 2); |
| 45 |
end; |
| 46 |
end; |
| 47 |
|
| 48 |
function PointInCircle(p: TPoint; Size: integer): boolean; |
| 49 |
var |
| 50 |
r: integer; |
| 51 |
begin |
| 52 |
r := size div 2; |
| 53 |
Result := (sqr(p.x - r) + sqr(p.y - r) <= sqr(r)); |
| 54 |
end; |
| 55 |
|
| 56 |
function PtInCircle(p, ctr: TPoint; Radius: Integer): Boolean; |
| 57 |
begin |
| 58 |
Result := sqr(p.x - ctr.x) + sqr(p.y - ctr.y) <= sqr(Radius); |
| 59 |
end; |
| 60 |
|
| 61 |
function HeightOfRect(R: TRect): Integer; |
| 62 |
begin |
| 63 |
Result := R.Bottom - R.Top; |
| 64 |
end; |
| 65 |
|
| 66 |
function WidthOfRect(R: TRect): Integer; |
| 67 |
begin |
| 68 |
Result := R.Right - R.Left; |
| 69 |
end; |
| 70 |
|
| 71 |
function IsEmptyRect(R: TRect): Boolean; |
| 72 |
begin |
| 73 |
Result := (R.Left = -1) and (R.Top = -1) and (R.Right = -1) and (R.Bottom = -1); |
| 74 |
end; |
| 75 |
|
| 76 |
function HighContrastColor(AColor: TColor): TColor; |
| 77 |
begin |
| 78 |
if GetRValue(AColor) + GetGValue(AColor) + GetBValue(AColor) > 3*128 then |
| 79 |
Result := clBlack |
| 80 |
else |
| 81 |
Result := clWhite; |
| 82 |
end; |
| 83 |
|
| 84 |
end. |
| 85 |
|