forked from edivando-fpc/BGRABitmap
-
Notifications
You must be signed in to change notification settings - Fork 5
/
bgragradientscanner.pas
2004 lines (1783 loc) · 65.3 KB
/
bgragradientscanner.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{ ***************************************************************************
* *
* This file is part of BGRABitmap library which is distributed under the *
* modified LGPL. *
* *
* See the file COPYING.modifiedLGPL.txt, included in this distribution, *
* for details about the copyright. *
* *
* 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. *
* *
************************* BGRABitmap library ******************************
- Drawing routines with transparency and antialiasing with Lazarus.
Offers also various transforms.
- These routines allow to manipulate 32bit images in BGRA format or RGBA
format (depending on the platform).
- This code is under modified LGPL (see COPYING.modifiedLGPL.txt).
This means that you can link this library inside your programs for any purpose.
Only the included part of the code must remain LGPL.
- If you make some improvements to this library, please notify here:
http://www.lazarus.freepascal.org/index.php/topic,12037.0.html
********************* Contact : Circular at operamail.com *******************
******************************* CONTRIBUTOR(S) ******************************
- Edivando S. Santos Brasil | [email protected]
(Compatibility with FPC ($Mode objfpc/delphi) and delphi VCL 11/2018)
***************************** END CONTRIBUTOR(S) *****************************}
Unit BGRAGradientScanner;
{$i bgrabitmap.inc}{$H+}
interface
{ This unit contains scanners that generate gradients }
uses
Classes, SysUtils, BGRATypes,{$IFNDEF FPC}Types, GraphType,{$ENDIF} BGRAGraphics, BGRABitmapTypes, BGRATransform;
type
TBGRAColorInterpolation = (ciStdRGB, ciLinearRGB, ciLinearHSLPositive, ciLinearHSLNegative, ciGSBPositive, ciGSBNegative);
TBGRAGradientRepetition = (grPad, grRepeat, grReflect, grSine);
{ TBGRASimpleGradient }
TBGRASimpleGradient = class(TBGRACustomGradient)
protected
FColor1,FColor2: TBGRAPixel;
ec1,ec2: TExpandedPixel;
FRepetition: TBGRAGradientRepetition;
constructor Create(AColor1,AColor2: TBGRAPixel; ARepetition: TBGRAGradientRepetition); overload;
constructor Create(AColor1,AColor2: TExpandedPixel; ARepetition: TBGRAGradientRepetition); overload;
function InterpolateToBGRA(position: BGRAWord): TBGRAPixel; virtual; abstract;
function InterpolateToExpanded(position: BGRAWord): TExpandedPixel; virtual; abstract;
public
class function CreateAny(AInterpolation: TBGRAColorInterpolation; AColor1,AColor2: TBGRAPixel; ARepetition: TBGRAGradientRepetition): TBGRASimpleGradient; overload;
class function CreateAny(AInterpolation: TBGRAColorInterpolation; AColor1,AColor2: TExpandedPixel; ARepetition: TBGRAGradientRepetition): TBGRASimpleGradient; overload;
function GetColorAt(position: integer): TBGRAPixel; override;
function GetColorAtF(position: single): TBGRAPixel; override;
function GetExpandedColorAt(position: integer): TExpandedPixel; override;
function GetExpandedColorAtF(position: single): TExpandedPixel; override;
function GetAverageColor: TBGRAPixel; override;
function GetAverageExpandedColor: TExpandedPixel; override;
function GetMonochrome: boolean; override;
property Repetition: TBGRAGradientRepetition read FRepetition write FRepetition;
end;
{ TBGRASimpleGradientWithoutGammaCorrection }
TBGRASimpleGradientWithoutGammaCorrection = class(TBGRASimpleGradient)
protected
function InterpolateToBGRA(position: BGRAWord): TBGRAPixel; override;
function InterpolateToExpanded(position: BGRAWord): TExpandedPixel; override;
public
constructor Create(Color1,Color2: TBGRAPixel; ARepetition: TBGRAGradientRepetition = grPad); overload;
constructor Create(Color1,Color2: TExpandedPixel; ARepetition: TBGRAGradientRepetition = grPad); overload;
end;
{ TBGRASimpleGradientWithGammaCorrection }
TBGRASimpleGradientWithGammaCorrection = class(TBGRASimpleGradient)
protected
function InterpolateToBGRA(position: BGRAWord): TBGRAPixel; override;
function InterpolateToExpanded(position: BGRAWord): TExpandedPixel; override;
public
constructor Create(Color1,Color2: TBGRAPixel; ARepetition: TBGRAGradientRepetition = grPad); overload;
constructor Create(Color1,Color2: TExpandedPixel; ARepetition: TBGRAGradientRepetition = grPad); overload;
end;
THueGradientOption = (hgoRepeat, hgoReflect, //repetition
hgoPositiveDirection, hgoNegativeDirection, //hue orientation
hgoHueCorrection, hgoLightnessCorrection); //color interpolation
THueGradientOptions = set of THueGradientOption;
{ TBGRAHueGradient }
TBGRAHueGradient = class(TBGRASimpleGradient)
private
hsla1,hsla2: THSLAPixel;
hue1,hue2: BGRALongWord;
FOptions: THueGradientOptions;
procedure Init(c1,c2: THSLAPixel; AOptions: THueGradientOptions);
function InterpolateToHSLA(position: BGRAWord): THSLAPixel;
protected
function InterpolateToBGRA(position: BGRAWord): TBGRAPixel; override;
function InterpolateToExpanded(position: BGRAWord): TExpandedPixel; override;
public
constructor Create(Color1,Color2: TBGRAPixel; options: THueGradientOptions); overload;
constructor Create(Color1,Color2: TExpandedPixel; options: THueGradientOptions); overload;
constructor Create(Color1,Color2: THSLAPixel; options: THueGradientOptions); overload;
constructor Create(AHue1,AHue2: BGRAWord; Saturation,Lightness: BGRAWord; options: THueGradientOptions); overload;
end;
TGradientInterpolationFunction = function(t: single): single of object;
{ TBGRAMultiGradient }
TBGRAMultiGradient = class(TBGRACustomGradient)
private
FColors: array of TBGRAPixel;
FPositions: array of integer;
FPositionsF: array of single;
FEColors: array of TExpandedPixel;
FCycle: Boolean;
FInterpolationFunction: TGradientInterpolationFunction;
procedure Init(Colors: array of TBGRAPixel; Positions0To1: array of single; AGammaCorrection, ACycle: boolean);
public
GammaCorrection: boolean;
function CosineInterpolation(t: single): single;
function HalfCosineInterpolation(t: single): single;
constructor Create(Colors: array of TBGRAPixel; Positions0To1: array of single; AGammaCorrection: boolean; ACycle: boolean = false);
function GetColorAt(position: integer): TBGRAPixel; override;
function GetExpandedColorAt(position: integer): TExpandedPixel; override;
function GetAverageColor: TBGRAPixel; override;
function GetMonochrome: boolean; override;
property InterpolationFunction: TGradientInterpolationFunction read FInterpolationFunction write FInterpolationFunction;
end;
TBGRAGradientScannerInternalScanNextFunc = function():single of object;
TBGRAGradientScannerInternalScanAtFunc = function(const p: TPointF):single of object;
{ TBGRAGradientScanner }
TBGRAGradientScanner = class(TBGRACustomScanner)
protected
FGradientType: TGradientType;
FOrigin,FDir1,FDir2: TPointF;
FRelativeFocal: TPointF;
FRadius, FFocalRadius: single;
FTransform, FHiddenTransform: TAffineMatrix;
FSinus: Boolean;
FGradient: TBGRACustomGradient;
FGradientOwner: boolean;
FFlipGradient: boolean;
FMatrix: TAffineMatrix;
FRepeatHoriz, FIsAverage: boolean;
FAverageColor: TBGRAPixel;
FAverageExpandedColor: TExpandedPixel;
FScanNextFunc: TBGRAGradientScannerInternalScanNextFunc;
FScanAtFunc: TBGRAGradientScannerInternalScanAtFunc;
FFocalDistance: single;
FFocalDirection, FFocalNormal: TPointF;
FRadialDenominator, FRadialDeltaSign, maxW1, maxW2: single;
FPosition: TPointF;
FHorizColor: TBGRAPixel;
FHorizExpandedColor: TExpandedPixel;
procedure Init(AGradientType: TGradientType; AOrigin, d1: TPointF; ATransform: TAffineMatrix; Sinus: Boolean=False); overload;
procedure Init(AGradientType: TGradientType; AOrigin, d1, d2: TPointF; ATransform: TAffineMatrix; Sinus: Boolean=False); overload;
procedure Init(AOrigin: TPointF; ARadius: single; AFocal: TPointF; AFocalRadius: single; ATransform: TAffineMatrix; AHiddenTransform: TAffineMatrix); overload;
procedure InitGradientType;
procedure InitTransform;
procedure InitGradient;
function ComputeRadialFocal(const p: TPointF): single;
function ScanNextLinear: single;
function ScanNextReflected: single;
function ScanNextDiamond: single;
function ScanNextRadial: single;
function ScanNextRadial2: single;
function ScanNextRadialFocal: single;
function ScanAtLinear(const p: TPointF): single;
function ScanAtReflected(const p: TPointF): single;
function ScanAtDiamond(const p: TPointF): single;
function ScanAtRadial(const p: TPointF): single;
function ScanAtRadial2(const p: TPointF): single;
function ScanAtRadialFocal(const p: TPointF): single;
function ScanNextInline: TBGRAPixel; {$ifdef inline}inline;{$endif}
function ScanNextExpandedInline: TExpandedPixel; {$ifdef inline}inline;{$endif}
procedure SetTransform(AValue: TAffineMatrix);
procedure SetFlipGradient(AValue: boolean);
function GetGradientColor(a: single): TBGRAPixel;
function GetGradientExpandedColor(a: single): TExpandedPixel;
public
constructor Create(AGradientType: TGradientType; AOrigin, d1: TPointF); overload;
constructor Create(AGradientType: TGradientType; AOrigin, d1, d2: TPointF); overload;
constructor Create(AOrigin, d1, d2, AFocal: TPointF; ARadiusRatio: single = 1; AFocalRadiusRatio: single = 0); overload;
constructor Create(AOrigin: TPointF; ARadius: single; AFocal: TPointF; AFocalRadius: single); overload;
constructor Create(c1, c2: TBGRAPixel; AGradientType: TGradientType; AOrigin, d1: TPointF;
gammaColorCorrection: boolean = True; Sinus: Boolean=False); overload;
constructor Create(c1, c2: TBGRAPixel; AGradientType: TGradientType; AOrigin, d1, d2: TPointF;
gammaColorCorrection: boolean = True; Sinus: Boolean=False); overload;
constructor Create(gradient: TBGRACustomGradient; AGradientType: TGradientType; AOrigin, d1: TPointF;
Sinus: Boolean=False; AGradientOwner: Boolean=False); overload;
constructor Create(gradient: TBGRACustomGradient; AGradientType: TGradientType; AOrigin, d1, d2: TPointF;
Sinus: Boolean=False; AGradientOwner: Boolean=False); overload;
constructor Create(gradient: TBGRACustomGradient; AOrigin: TPointF; ARadius: single; AFocal: TPointF;
AFocalRadius: single; AGradientOwner: Boolean=False); overload;
procedure SetGradient(c1,c2: TBGRAPixel; AGammaCorrection: boolean = true); overload;
procedure SetGradient(AGradient: TBGRACustomGradient; AOwner: boolean); overload;
destructor Destroy; override;
procedure ScanMoveTo(X, Y: Integer); override;
function ScanNextPixel: TBGRAPixel; override;
function ScanNextExpandedPixel: TExpandedPixel; override;
function ScanAt(X, Y: Single): TBGRAPixel; override;
function ScanAtExpanded(X, Y: Single): TExpandedPixel; override;
procedure ScanPutPixels(pdest: PBGRAPixel; count: integer; mode: TDrawMode); override;
function IsScanPutPixelsDefined: boolean; override;
property Transform: TAffineMatrix read FTransform write SetTransform;
property Gradient: TBGRACustomGradient read FGradient;
property FlipGradient: boolean read FFlipGradient write SetFlipGradient;
property Sinus: boolean Read FSinus write FSinus;
end;
{ TBGRAConstantScanner }
TBGRAConstantScanner = class(TBGRAGradientScanner)
constructor Create(c: TBGRAPixel);
end;
{ TBGRARandomScanner }
TBGRARandomScanner = class(TBGRACustomScanner)
private
FOpacity: byte;
FGrayscale: boolean;
FRandomBuffer, FRandomBufferCount: integer;
public
constructor Create(AGrayscale: Boolean; AOpacity: byte);
function ScanAtInteger({%H-}X, {%H-}Y: integer): TBGRAPixel; override;
function ScanNextPixel: TBGRAPixel; override;
function ScanAt({%H-}X, {%H-}Y: Single): TBGRAPixel; override;
end;
{ TBGRAGradientTriangleScanner }
TBGRAGradientTriangleScanner= class(TBGRACustomScanner)
protected
FMatrix: TAffineMatrix;
FColor1,FDiff2,FDiff3,FStep: TColorF;
FCurColor: TColorF;
public
constructor Create(pt1,pt2,pt3: TPointF; c1,c2,c3: TBGRAPixel);
procedure ScanMoveTo(X,Y: Integer); override;
procedure ScanMoveToF(X,Y: Single);
function ScanAt(X,Y: Single): TBGRAPixel; override;
function ScanNextPixel: TBGRAPixel; override;
function ScanNextExpandedPixel: TExpandedPixel; override;
end;
{ TBGRASolidColorMaskScanner }
TBGRASolidColorMaskScanner = class(TBGRACustomScanner)
private
FOffset: TPoint;
FMask: TBGRACustomBitmap;
FSolidColor: TBGRAPixel;
FScanNext : TScanNextPixelFunction;
FScanAt : TScanAtFunction;
FMemMask: packed array of TBGRAPixel;
public
constructor Create(AMask: TBGRACustomBitmap; AOffset: TPoint; ASolidColor: TBGRAPixel);
destructor Destroy; override;
function IsScanPutPixelsDefined: boolean; override;
procedure ScanPutPixels(pdest: PBGRAPixel; count: integer; mode: TDrawMode); override;
procedure ScanMoveTo(X,Y: Integer); override;
function ScanNextPixel: TBGRAPixel; override;
function ScanAt(X,Y: Single): TBGRAPixel; override;
property Color: TBGRAPixel read FSolidColor write FSolidColor;
end;
{ TBGRATextureMaskScanner }
TBGRATextureMaskScanner = class(TBGRACustomScanner)
private
FOffset: TPoint;
FMask: TBGRACustomBitmap;
FTexture: IBGRAScanner;
FMaskScanNext,FTextureScanNext : TScanNextPixelFunction;
FMaskScanAt,FTextureScanAt : TScanAtFunction;
FGlobalOpacity: Byte;
FMemMask, FMemTex: packed array of TBGRAPixel;
public
constructor Create(AMask: TBGRACustomBitmap; AOffset: TPoint; ATexture: IBGRAScanner; AGlobalOpacity: Byte = 255);
destructor Destroy; override;
function IsScanPutPixelsDefined: boolean; override;
procedure ScanPutPixels(pdest: PBGRAPixel; count: integer; mode: TDrawMode); override;
procedure ScanMoveTo(X,Y: Integer); override;
function ScanNextPixel: TBGRAPixel; override;
function ScanAt(X,Y: Single): TBGRAPixel; override;
end;
{ TBGRAOpacityScanner }
TBGRAOpacityScanner = class(TBGRACustomScanner)
private
FTexture: IBGRAScanner;
FGlobalOpacity: Byte;
FScanNext : TScanNextPixelFunction;
FScanAt : TScanAtFunction;
FMemTex: packed array of TBGRAPixel;
public
constructor Create(ATexture: IBGRAScanner; AGlobalOpacity: Byte = 255);
destructor Destroy; override;
function IsScanPutPixelsDefined: boolean; override;
procedure ScanPutPixels(pdest: PBGRAPixel; count: integer; mode: TDrawMode); override;
procedure ScanMoveTo(X,Y: Integer); override;
function ScanNextPixel: TBGRAPixel; override;
function ScanAt(X,Y: Single): TBGRAPixel; override;
end;
implementation
uses BGRABlend, Math;
{ TBGRASimpleGradient }
constructor TBGRASimpleGradient.Create(AColor1, AColor2: TBGRAPixel; ARepetition: TBGRAGradientRepetition);
begin
FColor1 := AColor1;
FColor2 := AColor2;
ec1 := GammaExpansion(AColor1);
ec2 := GammaExpansion(AColor2);
FRepetition:= ARepetition;
end;
constructor TBGRASimpleGradient.Create(AColor1, AColor2: TExpandedPixel;
ARepetition: TBGRAGradientRepetition);
begin
FColor1 := GammaCompression(AColor1);
FColor2 := GammaCompression(AColor2);
ec1 := AColor1;
ec2 := AColor2;
FRepetition:= ARepetition;
end;
class function TBGRASimpleGradient.CreateAny(AInterpolation: TBGRAColorInterpolation;
AColor1, AColor2: TBGRAPixel; ARepetition: TBGRAGradientRepetition): TBGRASimpleGradient;
begin
case AInterpolation of
ciStdRGB: result := TBGRASimpleGradientWithoutGammaCorrection.Create(AColor1,AColor2);
ciLinearRGB: result := TBGRASimpleGradientWithGammaCorrection.Create(AColor1,AColor2);
ciLinearHSLPositive: result := TBGRAHueGradient.Create(AColor1,AColor2,[hgoPositiveDirection]);
ciLinearHSLNegative: result := TBGRAHueGradient.Create(AColor1,AColor2,[hgoNegativeDirection]);
ciGSBPositive: result := TBGRAHueGradient.Create(AColor1,AColor2,[hgoPositiveDirection, hgoHueCorrection, hgoLightnessCorrection]);
ciGSBNegative: result := TBGRAHueGradient.Create(AColor1,AColor2,[hgoNegativeDirection, hgoHueCorrection, hgoLightnessCorrection]);
end;
result.Repetition := ARepetition;
end;
class function TBGRASimpleGradient.CreateAny(AInterpolation: TBGRAColorInterpolation;
AColor1, AColor2: TExpandedPixel; ARepetition: TBGRAGradientRepetition): TBGRASimpleGradient;
begin
case AInterpolation of
ciStdRGB: result := TBGRASimpleGradientWithoutGammaCorrection.Create(AColor1,AColor2);
ciLinearRGB: result := TBGRASimpleGradientWithGammaCorrection.Create(AColor1,AColor2);
ciLinearHSLPositive: result := TBGRAHueGradient.Create(AColor1,AColor2,[hgoPositiveDirection]);
ciLinearHSLNegative: result := TBGRAHueGradient.Create(AColor1,AColor2,[hgoNegativeDirection]);
ciGSBPositive: result := TBGRAHueGradient.Create(AColor1,AColor2,[hgoPositiveDirection, hgoHueCorrection, hgoLightnessCorrection]);
ciGSBNegative: result := TBGRAHueGradient.Create(AColor1,AColor2,[hgoNegativeDirection, hgoHueCorrection, hgoLightnessCorrection]);
end;
result.Repetition := ARepetition;
end;
function TBGRASimpleGradient.GetAverageColor: TBGRAPixel;
begin
result := InterpolateToBGRA(32768);
end;
function TBGRASimpleGradient.GetAverageExpandedColor: TExpandedPixel;
begin
Result:= InterpolateToExpanded(32768);
end;
function TBGRASimpleGradient.GetColorAt(position: integer): TBGRAPixel;
begin
case FRepetition of
grSine: begin
position := Sin65536(position and $ffff);
if position = 65536 then
result := FColor2
else
result := InterpolateToBGRA(position);
end;
grRepeat: result := InterpolateToBGRA(position and $ffff);
grReflect:
begin
position := position and $1ffff;
if position >= $10000 then
begin
if position = $10000 then
result := FColor2
else
result := InterpolateToBGRA($20000 - position);
end
else
result := InterpolateToBGRA(position);
end;
else
begin
if position <= 0 then
result := FColor1 else
if position >= 65536 then
result := FColor2 else
result := InterpolateToBGRA(position);
end;
end;
end;
function TBGRASimpleGradient.GetColorAtF(position: single): TBGRAPixel;
begin
if FRepetition <> grPad then
result := GetColorAt(round(frac(position*0.5)*131072)) else //divided by 2 for reflected repetition
begin
if position <= 0 then
result := FColor1 else
if position >= 1 then
result := FColor2 else
result := GetColorAt(round(position*65536));
end;
end;
function TBGRASimpleGradient.GetExpandedColorAt(position: integer
): TExpandedPixel;
begin
case FRepetition of
grSine: begin
position := Sin65536(position and $ffff);
if position = 65536 then
result := ec2
else
result := InterpolateToExpanded(position);
end;
grRepeat: result := InterpolateToExpanded(position and $ffff);
grReflect:
begin
position := position and $1ffff;
if position >= $10000 then
begin
if position = $10000 then
result := ec2
else
result := InterpolateToExpanded($20000 - position);
end
else
result := InterpolateToExpanded(position);
end;
else
begin
if position <= 0 then
result := ec1 else
if position >= 65536 then
result := ec2 else
result := InterpolateToExpanded(position);
end;
end;
end;
function TBGRASimpleGradient.GetExpandedColorAtF(position: single
): TExpandedPixel;
begin
if FRepetition <> grPad then
result := GetExpandedColorAt(round(frac(position*0.5)*131072)) else //divided by 2 for reflected repetition
begin
if position <= 0 then
result := ec1 else
if position >= 1 then
result := ec2 else
result := GetExpandedColorAt(round(position*65536));
end;
end;
function TBGRASimpleGradient.GetMonochrome: boolean;
begin
Result:= BGRAPixelEqual(FColor1, {=} FColor2);
end;
{ TBGRAConstantScanner }
constructor TBGRAConstantScanner.Create(c: TBGRAPixel);
begin
inherited Create(c,c,gtLinear,PointF(0,0),PointF(0,0),false);
end;
{ TBGRARandomScanner }
constructor TBGRARandomScanner.Create(AGrayscale: Boolean; AOpacity: byte);
begin
FGrayscale:= AGrayscale;
FOpacity:= AOpacity;
FRandomBufferCount := 0;
end;
function TBGRARandomScanner.ScanAtInteger(X, Y: integer): TBGRAPixel;
begin
Result:=ScanNextPixel;
end;
function TBGRARandomScanner.ScanNextPixel: TBGRAPixel;
var rgb: integer;
begin
if FGrayscale then
begin
if FRandomBufferCount = 0 then
begin
FRandomBuffer := random(256*256*256);
FRandomBufferCount := 3;
end;
result.red := FRandomBuffer and 255;
FRandomBuffer:= FRandomBuffer shr 8;
FRandomBufferCount := FRandomBufferCount -1;
result.green := result.red;
result.blue := result.red;
result.alpha:= FOpacity;
end else
begin
rgb := random(256*256*256);
Result:= BGRA(rgb and 255,(rgb shr 8) and 255,(rgb shr 16) and 255,FOpacity);
end;
end;
function TBGRARandomScanner.ScanAt(X, Y: Single): TBGRAPixel;
begin
Result:=ScanNextPixel;
end;
{ TBGRAHueGradient }
procedure TBGRAHueGradient.Init(c1, c2: THSLAPixel; AOptions: THueGradientOptions);
begin
FOptions:= AOptions;
if (hgoLightnessCorrection in AOptions) then
begin
hsla1 := THSLAPixel(ExpandedToGSBA(ec1));
hsla2 := THSLAPixel(ExpandedToGSBA(ec2));
end else
begin
hsla1 := c1;
hsla2 := c2;
end;
if not (hgoHueCorrection in AOptions) then
begin
hue1 := c1.hue;
hue2 := c2.hue;
end else
begin
hue1 := HtoG(c1.hue);
hue2 := HtoG(c2.hue);
end;
if (hgoPositiveDirection in AOptions) and not (hgoNegativeDirection in AOptions) then
begin
if c2.hue <= c1.hue then hue2 := hue2 + 65536;
end else
if not (hgoPositiveDirection in AOptions) and (hgoNegativeDirection in AOptions) then
begin
if c2.hue >= c1.hue then hue1 := hue1 + 65536;
end;
end;
function TBGRAHueGradient.InterpolateToHSLA(position: BGRAWord): THSLAPixel;
var b,b2: BGRALongWord;
begin
b := position shr 2;
b2 := 16384-b;
result.hue := ((hue1 * b2 + hue2 * b + 8191) shr 14) and $ffff;
result.saturation := (hsla1.saturation * b2 + hsla2.saturation * b + 8191) shr 14;
result.lightness := (hsla1.lightness * b2 + hsla2.lightness * b + 8191) shr 14;
result.alpha := (hsla1.alpha * b2 + hsla2.alpha * b + 8191) shr 14;
if hgoLightnessCorrection in FOptions then
begin
if not (hgoHueCorrection in FOptions) then
result.hue := HtoG(result.hue);
end else
begin
if hgoHueCorrection in FOptions then
result.hue := GtoH(result.hue);
end;
end;
function TBGRAHueGradient.InterpolateToBGRA(position: BGRAWord): TBGRAPixel;
begin
if hgoLightnessCorrection in FOptions then
result := GSBAToBGRA(InterpolateToHSLA(position))
else
result := HSLAToBGRA(InterpolateToHSLA(position));
end;
function TBGRAHueGradient.InterpolateToExpanded(position: BGRAWord): TExpandedPixel;
begin
if hgoLightnessCorrection in FOptions then
result := GSBAToExpanded(InterpolateToHSLA(position))
else
result := HSLAToExpanded(InterpolateToHSLA(position));
end;
constructor TBGRAHueGradient.Create(Color1, Color2: TBGRAPixel;options: THueGradientOptions);
begin
if hgoReflect in options then
inherited Create(Color1,Color2,grReflect)
else if hgoRepeat in options then
inherited Create(Color1,Color2,grRepeat)
else
inherited Create(Color1,Color2,grPad);
Init(BGRAToHSLA(Color1),BGRAToHSLA(Color2),options);
end;
constructor TBGRAHueGradient.Create(Color1, Color2: TExpandedPixel;
options: THueGradientOptions);
begin
if hgoReflect in options then
inherited Create(Color1,Color2,grReflect)
else if hgoRepeat in options then
inherited Create(Color1,Color2,grRepeat)
else
inherited Create(Color1,Color2,grPad);
Init(ExpandedToHSLA(Color1),ExpandedToHSLA(Color2),options);
end;
constructor TBGRAHueGradient.Create(Color1, Color2: THSLAPixel; options: THueGradientOptions);
begin
if hgoReflect in options then
inherited Create(HSLAToExpanded(Color1),HSLAToExpanded(Color2),grReflect)
else if hgoRepeat in options then
inherited Create(HSLAToExpanded(Color1),HSLAToExpanded(Color2),grRepeat)
else
inherited Create(HSLAToExpanded(Color1),HSLAToExpanded(Color2),grPad);
Init(Color1,Color2, options);
end;
constructor TBGRAHueGradient.Create(AHue1, AHue2: BGRAWord; Saturation,
Lightness: BGRAWord; options: THueGradientOptions);
begin
Create(HSLA(AHue1,saturation,lightness), HSLA(AHue2,saturation,lightness), options);
end;
{ TBGRAMultiGradient }
procedure TBGRAMultiGradient.Init(Colors: array of TBGRAPixel;
Positions0To1: array of single; AGammaCorrection, ACycle: boolean);
var
i: Integer;
begin
if length(Positions0To1) <> length(colors) then
raise Exception.Create('Dimension mismatch');
if length(Positions0To1) = 0 then
raise Exception.Create('Empty gradient');
setlength(FColors,length(Colors));
setlength(FPositions,length(Positions0To1));
setlength(FPositionsF,length(Positions0To1));
setlength(FEColors,length(Colors));
for i := 0 to high(colors) do
begin
FColors[i]:= colors[i];
FPositions[i]:= round(Positions0To1[i]*65536);
FPositionsF[i]:= Positions0To1[i];
FEColors[i]:= GammaExpansion(colors[i]);
end;
GammaCorrection := AGammaCorrection;
FCycle := ACycle;
if FPositions[high(FPositions)] = FPositions[0] then FCycle := false;
end;
function TBGRAMultiGradient.CosineInterpolation(t: single): single;
begin
result := (1-cos(t*Pi))*0.5;
end;
function TBGRAMultiGradient.HalfCosineInterpolation(t: single): single;
begin
result := (1-cos(t*Pi))*0.25 + t*0.5;
end;
constructor TBGRAMultiGradient.Create(Colors: array of TBGRAPixel;
Positions0To1: array of single; AGammaCorrection: boolean; ACycle: boolean);
begin
Init(Colors,Positions0To1,AGammaCorrection, ACycle);
end;
function TBGRAMultiGradient.GetColorAt(position: integer): TBGRAPixel;
var i: BGRANativeInt;
ec: TExpandedPixel;
curPos,posDiff: BGRANativeInt;
begin
if FCycle then
position := (position-FPositions[0]) mod (FPositions[high(FPositions)] - FPositions[0]) + FPositions[0];
if position <= FPositions[0] then
result := FColors[0] else
if position >= FPositions[high(FPositions)] then
result := FColors[high(FColors)] else
begin
i := 0;
while (i < high(FPositions)-1) and (position >= FPositions[i+1]) do
inc(i);
if Position = FPositions[i] then
result := FColors[i]
else
begin
curPos := position-FPositions[i];
posDiff := FPositions[i+1]-FPositions[i];
if @FInterpolationFunction <> nil then
begin
curPos := round(FInterpolationFunction(curPos/posDiff)*65536);
posDiff := 65536;
end;
if GammaCorrection then
begin
if FEColors[i+1].red < FEColors[i].red then
ec.red := FEColors[i].red - BGRANativeUInt(curPos)*BGRANativeUInt(FEColors[i].red-FEColors[i+1].red) div BGRANativeUInt(posDiff) else
ec.red := FEColors[i].red + BGRANativeUInt(curPos)*BGRANativeUInt(FEColors[i+1].red-FEColors[i].red) div BGRANativeUInt(posDiff);
if FEColors[i+1].green < FEColors[i].green then
ec.green := FEColors[i].green - BGRANativeUInt(curPos)*BGRANativeUInt(FEColors[i].green-FEColors[i+1].green) div BGRANativeUInt(posDiff) else
ec.green := FEColors[i].green + BGRANativeUInt(curPos)*BGRANativeUInt(FEColors[i+1].green-FEColors[i].green) div BGRANativeUInt(posDiff);
if FEColors[i+1].blue < FEColors[i].blue then
ec.blue := FEColors[i].blue - BGRANativeUInt(curPos)*BGRANativeUInt(FEColors[i].blue-FEColors[i+1].blue) div BGRANativeUInt(posDiff) else
ec.blue := FEColors[i].blue + BGRANativeUInt(curPos)*BGRANativeUInt(FEColors[i+1].blue-FEColors[i].blue) div BGRANativeUInt(posDiff);
if FEColors[i+1].alpha < FEColors[i].alpha then
ec.alpha := FEColors[i].alpha - BGRANativeUInt(curPos)*BGRANativeUInt(FEColors[i].alpha-FEColors[i+1].alpha) div BGRANativeUInt(posDiff) else
ec.alpha := FEColors[i].alpha + BGRANativeUInt(curPos)*BGRANativeUInt(FEColors[i+1].alpha-FEColors[i].alpha) div BGRANativeUInt(posDiff);
result := GammaCompression(ec);
end else
begin
result.red := FColors[i].red + (curPos)*(FColors[i+1].red-FColors[i].red) div (posDiff);
result.green := FColors[i].green + (curPos)*(FColors[i+1].green-FColors[i].green) div (posDiff);
result.blue := FColors[i].blue + (curPos)*(FColors[i+1].blue-FColors[i].blue) div (posDiff);
result.alpha := FColors[i].alpha + (curPos)*(FColors[i+1].alpha-FColors[i].alpha) div (posDiff);
end;
end;
end;
end;
function TBGRAMultiGradient.GetExpandedColorAt(position: integer
): TExpandedPixel;
var i: BGRANativeInt;
curPos,posDiff: BGRANativeInt;
rw,gw,bw: BGRANativeUInt;
begin
if FCycle then
position := (position-FPositions[0]) mod (FPositions[high(FPositions)] - FPositions[0]) + FPositions[0];
if position <= FPositions[0] then
result := FEColors[0] else
if position >= FPositions[high(FPositions)] then
result := FEColors[high(FColors)] else
begin
i := 0;
while (i < high(FPositions)-1) and (position >= FPositions[i+1]) do
inc(i);
if Position = FPositions[i] then
result := FEColors[i]
else
begin
curPos := position-FPositions[i];
posDiff := FPositions[i+1]-FPositions[i];
if @FInterpolationFunction <> nil then
begin
curPos := round(FInterpolationFunction(curPos/posDiff)*65536);
posDiff := 65536;
end;
if GammaCorrection then
begin
if FEColors[i+1].red < FEColors[i].red then
result.red := FEColors[i].red - BGRANativeUInt(curPos)*BGRANativeUInt(FEColors[i].red-FEColors[i+1].red) div BGRANativeUInt(posDiff) else
result.red := FEColors[i].red + BGRANativeUInt(curPos)*BGRANativeUInt(FEColors[i+1].red-FEColors[i].red) div BGRANativeUInt(posDiff);
if FEColors[i+1].green < FEColors[i].green then
result.green := FEColors[i].green - BGRANativeUInt(curPos)*BGRANativeUInt(FEColors[i].green-FEColors[i+1].green) div BGRANativeUInt(posDiff) else
result.green := FEColors[i].green + BGRANativeUInt(curPos)*BGRANativeUInt(FEColors[i+1].green-FEColors[i].green) div BGRANativeUInt(posDiff);
if FEColors[i+1].blue < FEColors[i].blue then
result.blue := FEColors[i].blue - BGRANativeUInt(curPos)*BGRANativeUInt(FEColors[i].blue-FEColors[i+1].blue) div BGRANativeUInt(posDiff) else
result.blue := FEColors[i].blue + BGRANativeUInt(curPos)*BGRANativeUInt(FEColors[i+1].blue-FEColors[i].blue) div BGRANativeUInt(posDiff);
if FEColors[i+1].alpha < FEColors[i].alpha then
result.alpha := FEColors[i].alpha - BGRANativeUInt(curPos)*BGRANativeUInt(FEColors[i].alpha-FEColors[i+1].alpha) div BGRANativeUInt(posDiff) else
result.alpha := FEColors[i].alpha + BGRANativeUInt(curPos)*BGRANativeUInt(FEColors[i+1].alpha-FEColors[i].alpha) div BGRANativeUInt(posDiff);
end else
begin
rw := BGRANativeInt(FColors[i].red shl 8) + (((curPos) shl 8)*(FColors[i+1].red-FColors[i].red)) div (posDiff);
gw := BGRANativeInt(FColors[i].green shl 8) + (((curPos) shl 8)*(FColors[i+1].green-FColors[i].green)) div (posDiff);
bw := BGRANativeInt(FColors[i].blue shl 8) + (((curPos) shl 8)*(FColors[i+1].blue-FColors[i].blue)) div (posDiff);
if rw >= $ff00 then result.red := $ffff
else result.red := (GammaExpansionTab[rw shr 8]*BGRANativeUInt(255 - (rw and 255)) + GammaExpansionTab[(rw shr 8)+1]*BGRANativeUInt(rw and 255)) shr 8;
if gw >= $ff00 then result.green := $ffff
else result.green := (GammaExpansionTab[gw shr 8]*BGRANativeUInt(255 - (gw and 255)) + GammaExpansionTab[(gw shr 8)+1]*BGRANativeUInt(gw and 255)) shr 8;
if bw >= $ff00 then result.blue := $ffff
else result.blue := (GammaExpansionTab[bw shr 8]*BGRANativeUInt(255 - (bw and 255)) + GammaExpansionTab[(bw shr 8)+1]*BGRANativeUInt(bw and 255)) shr 8;
result.alpha := BGRANativeInt(FColors[i].alpha shl 8) + (((curPos) shl 8)*(FColors[i+1].alpha-FColors[i].alpha)) div (posDiff);
result.alpha := result.alpha + (result.alpha shr 8);
end;
end;
end;
end;
function TBGRAMultiGradient.GetAverageColor: TBGRAPixel;
var sumR,sumG,sumB,sumA: integer;
i: Integer;
begin
sumR := 0;
sumG := 0;
sumB := 0;
sumA := 0;
for i := 0 to high(FColors) do
begin
sumR := sumR +FColors[i].red;
sumG := sumG +FColors[i].green;
sumB := sumB +FColors[i].blue;
sumA := sumA +FColors[i].alpha;
end;
result := BGRA(sumR div length(FColors),sumG div length(FColors),
sumB div length(FColors),sumA div length(FColors));
end;
function TBGRAMultiGradient.GetMonochrome: boolean;
var i: integer;
begin
for i := 1 to high(FColors) do
if not BGRAPixelEqual(FColors[i], {<>} FColors[0]) then
begin
result := false;
exit;
end;
Result:= true;
end;
{ TBGRASimpleGradientWithGammaCorrection }
function TBGRASimpleGradientWithGammaCorrection.InterpolateToBGRA(position: BGRAWord
): TBGRAPixel;
var b,b2: BGRACardinal;
ec: TExpandedPixel;
begin
b := position;
b2 := 65536-b;
ec.red := (ec1.red * b2 + ec2.red * b + 32767) shr 16;
ec.green := (ec1.green * b2 + ec2.green * b + 32767) shr 16;
ec.blue := (ec1.blue * b2 + ec2.blue * b + 32767) shr 16;
ec.alpha := (ec1.alpha * b2 + ec2.alpha * b + 32767) shr 16;
result := GammaCompression(ec);
end;
function TBGRASimpleGradientWithGammaCorrection.InterpolateToExpanded(
position: BGRAWord): TExpandedPixel;
var b,b2: BGRACardinal;
begin
b := position;
b2 := 65536-b;
result.red := (ec1.red * b2 + ec2.red * b + 32767) shr 16;
result.green := (ec1.green * b2 + ec2.green * b + 32767) shr 16;
result.blue := (ec1.blue * b2 + ec2.blue * b + 32767) shr 16;
result.alpha := (ec1.alpha * b2 + ec2.alpha * b + 32767) shr 16;
end;
constructor TBGRASimpleGradientWithGammaCorrection.Create(Color1,
Color2: TBGRAPixel; ARepetition: TBGRAGradientRepetition);
begin
inherited Create(Color1,Color2,ARepetition);
end;
constructor TBGRASimpleGradientWithGammaCorrection.Create(Color1,
Color2: TExpandedPixel; ARepetition: TBGRAGradientRepetition);
begin
inherited Create(Color1,Color2,ARepetition);
end;
{ TBGRASimpleGradientWithoutGammaCorrection }
function TBGRASimpleGradientWithoutGammaCorrection.InterpolateToBGRA(
position: BGRAWord): TBGRAPixel;
var b,b2: BGRACardinal;
begin
b := position shr 6;
b2 := 1024-b;
result.red := (FColor1.red * b2 + FColor2.red * b + 511) shr 10;
result.green := (FColor1.green * b2 + FColor2.green * b + 511) shr 10;
result.blue := (FColor1.blue * b2 + FColor2.blue * b + 511) shr 10;
result.alpha := (FColor1.alpha * b2 + FColor2.alpha * b + 511) shr 10;
end;
function TBGRASimpleGradientWithoutGammaCorrection.InterpolateToExpanded(
position: BGRAWord): TExpandedPixel;
var b,b2: BGRACardinal;
rw,gw,bw: BGRAWord;
begin
b := position shr 6;
b2 := 1024-b;
rw := (FColor1.red * b2 + FColor2.red * b + 511) shr 2;
gw := (FColor1.green * b2 + FColor2.green * b + 511) shr 2;
bw := (FColor1.blue * b2 + FColor2.blue * b + 511) shr 2;
if rw >= $ff00 then
result.red := 65535
else
result.red := (GammaExpansionTab[rw shr 8]*BGRANativeUInt(255 - (rw and 255)) + GammaExpansionTab[(rw shr 8)+1]*BGRANativeUInt(rw and 255)) shr 8;
if gw >= $ff00 then
result.green := 65535
else
result.green := (GammaExpansionTab[gw shr 8]*BGRANativeUInt(255 - (gw and 255)) + GammaExpansionTab[(gw shr 8)+1]*BGRANativeUInt(gw and 255)) shr 8;
if bw >= $ff00 then
result.blue := 65535
else
result.blue := (GammaExpansionTab[bw shr 8]*BGRANativeUInt(255 - (bw and 255)) + GammaExpansionTab[(bw shr 8)+1]*BGRANativeUInt(bw and 255)) shr 8;
result.alpha := (FColor1.alpha * b2 + FColor2.alpha * b + 511) shr 2;
end;
constructor TBGRASimpleGradientWithoutGammaCorrection.Create(Color1,
Color2: TBGRAPixel; ARepetition: TBGRAGradientRepetition);
begin
inherited Create(Color1,Color2,ARepetition);
end;
constructor TBGRASimpleGradientWithoutGammaCorrection.Create(Color1,
Color2: TExpandedPixel; ARepetition: TBGRAGradientRepetition);
begin
inherited Create(Color1,Color2,ARepetition);
end;
{ TBGRAGradientTriangleScanner }
constructor TBGRAGradientTriangleScanner.Create(pt1, pt2, pt3: TPointF; c1, c2,
c3: TBGRAPixel);
var ec1,ec2,ec3: TExpandedPixel;
begin
FMatrix := AffineMatrix(pt2.X-pt1.X, pt3.X-pt1.X, 0,
pt2.Y-pt1.Y, pt3.Y-pt1.Y, 0);
if not IsAffineMatrixInversible(FMatrix) then
FMatrix := AffineMatrix(0,0,0,0,0,0)
else
FMatrix := AffMatrixMult(AffineMatrixInverse(FMatrix), AffineMatrixTranslation(-pt1.x,-pt1.y));
ec1 := GammaExpansion(c1);
ec2 := GammaExpansion(c2);
ec3 := GammaExpansion(c3);
FColor1[1] := ec1.red;
FColor1[2] := ec1.green;
FColor1[3] := ec1.blue;
FColor1[4] := ec1.alpha;
FDiff2[1] := ec2.red - ec1.red;
FDiff2[2] := ec2.green - ec1.green;
FDiff2[3] := ec2.blue - ec1.blue;
FDiff2[4] := ec2.alpha - ec1.alpha;
FDiff3[1] := ec3.red - ec1.red;
FDiff3[2] := ec3.green - ec1.green;
FDiff3[3] := ec3.blue - ec1.blue;
FDiff3[4] := ec3.alpha - ec1.alpha;
// FStep := FDiff2*FMatrix[1,1]+FDiff3*FMatrix[2,1];
end;
procedure TBGRAGradientTriangleScanner.ScanMoveTo(X, Y: Integer);
begin
ScanMoveToF(X, Y);
end;
procedure TBGRAGradientTriangleScanner.ScanMoveToF(X, Y: Single);
var
Cur: TPointF;
begin
Cur := AffMatrixMult(FMatrix,PointF(X,Y));
// FCurColor := FColor1+FDiff2*Cur.X+FDiff3*Cur.Y;
end;
function TBGRAGradientTriangleScanner.ScanAt(X, Y: Single): TBGRAPixel;
begin
ScanMoveToF(X,Y);
result := ScanNextPixel;
end;
function TBGRAGradientTriangleScanner.ScanNextPixel: TBGRAPixel;
var r,g,b,a: BGRAInt64;
begin