-
Notifications
You must be signed in to change notification settings - Fork 1
/
Colors.pas
1070 lines (960 loc) · 26.4 KB
/
Colors.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
{*******************************************************************************
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in
compliance with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS"
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
License for the specific language governing rights and limitations
under the License.
*******************************************************************************}
unit Colors;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Dialogs, Math, Controls, Forms;
const
HashNum = 510511; // buckets for hashing colors
MAXCOL = 1000; // base colors for palettes
type
PRGBArray = ^TRGBArray;
TRGBArray = array [0..maxint div 4] of TRGBTriple;
TColDistribution = ( cdDirect, cdLogarithmic, cdSquareRoot, cdSquare, cdSine, cdCosine, cdCube, cdCubeRoot, cdCircular, cdArcSin, cdExponential );
TColApplication = ( caLinear, caRepeated, ca2Times, ca3Times, ca4Times, ca8Times, ca16Times, ca32Times, ca64Times );
TColMethod = ( cmHSL, cmHSV, cmRGB );
TColorize = class
private
FHashIter: array[ 0 .. HashNum ] of extended;
FHashCol: array[ 0 .. HashNum ] of TRGBTriple;
FOrigColors: array[ 0 .. MAXCOL ] of TColor;
FDestColors: array[ 0 .. MAXCOL ] of TColor;
FColorsNumber: integer; // colors assigned
FMaxIter: integer;
FApplication: TColApplication;
FDistribution: TColDistribution;
FMethod: TColMethod;
FLastColor: Tcolor;
FPrevIter : extended;
FprevResult : TRGBTriple;
FOffset: Extended;
FRecMaxCol: Extended;
FFileName: string;
procedure SetApplication(const Value: string);
procedure SetDistribution(const Value: string);
procedure SetMaxIter( const Value: integer );
function GetOrigColors(NumCol: integer): TColor;
procedure SetOrigColors(NumCol: integer; const Value: TColor);
function GetDestColors(NumCol: integer): TColor;
procedure SetDestColors(NumCol: integer; const Value: TColor);
function GetDistribution: String;
function GetApplication: string;
function GetMethod: string;
procedure SetMethod(const Value: string);
procedure SetLastColor(const Value: Tcolor);
procedure SetOffset(const Value: Extended);
procedure ClearHashes;
public
DefaultPath: string;
constructor Create( AMaxIter: integer );
function FromIterToColor( Iter: Extended ): TRGBTriple;
procedure AddColor(Orig, Dest: TColor);
procedure InsertColor( Orig, Dest: TColor; NumCol: integer = -1 ); // -1 to Append to last
procedure RemoveColor( NumCol: integer );
function LoadFromFile(const AFileName: string; verbose: boolean ): boolean;
function SaveToFile(const AFileName: string; Verbose: boolean ): boolean;
function ImportMap(const AFileName: string; verbose: boolean ): boolean;
property OrigColors[ NumCol: integer ] :TColor read GetOrigColors write SetOrigColors;
property DestColors[ NumCol: integer ] :TColor read GetDestColors write SetDestColors;
property ColorsNumber: integer read FColorsNumber; // colors assigned
property LastColor: Tcolor read FLastColor write SetLastColor;
property Offset: Extended read FOffset write SetOffset;
property FileName: string read FFileName;
property MaxIter: integer read FMaxIter write SetMaxIter;
property ApplyTo: string read GetApplication write SetApplication;
property Distribution: String read GetDistribution write SetDistribution;
property Method: string read GetMethod write SetMethod;
property OrdMethod: TColMethod read FMethod;
end;
function ColToRGBTriple(ACol: TColor): TRGBTriple;
function RGBTripleToCol( ACol: TRGBTriple ): TColor;
function FindGradientColorRGB(StartCol, EndCol: TRGBTriple; Pos: extended): TRGBTriple;
function FindGradientColorHSL(StartCol, EndCol: TRGBTriple; Pos: extended): TRGBTriple;
function FindGradientColorHSV(StartCol, EndCol: TRGBTriple; Pos: extended): TRGBTriple;
procedure RGBtoHSL(rgb : TRGBTriple; var H, S, L : extended);
procedure HSLtoRGB(H, S, L : extended; var rgb : TRGBTriple);
procedure HSVtoRGB(H, S, V : extended; var rgb : TRGBTriple);
procedure RGBtoHSV(rgb : TRGBTriple; var H, S, V : extended);
procedure SwitchRB(var rgb : TRGBTriple);
function Lighter(Color: TColor; Amount: Double = 0.5): TColor;
function Darker(Color: TColor; Amount: Double = 0.5): TColor;
implementation
{$IFNDEF VER220}
uses
UITypes;
{$ENDIF VER220}
function RGBTripleToCol( ACol: TRGBTriple ): TColor;
begin
result := ACol.rgbtRed * 65536;
result := Result + ACol.rgbtGreen * 256;
result := Result + ACol.rgbtBlue;
end;
function ColToRGBTriple(ACol: TColor): TRGBTriple;
begin
ACol := ColorToRGB(Acol);
result.rgbtRed := (ACol shr 16) and $000000FF;
result.rgbtGreen := (ACol shr 8) and $000000FF;
result.rgbtBlue := ACol and $000000FF;
end;
{
asm
mov EAX, Acol
mov [ EDX ], EAX
end;
}
function FindGradientColorRGB(StartCol, EndCol: TRGBTriple; Pos: extended): TRGBTriple;
begin
Result.rgbtRed := round( ((StartCol.rgbtRed * ( 1.0 - pos ) ) + ( EndCol.rgbtRed * pos ) ) );
Result.rgbtGreen := round( ( (StartCol.rgbtGreen * ( 1.0 - pos ) ) + (EndCol.rgbtGreen * pos) ) );
Result.rgbtBlue := round( ( (StartCol.rgbtBlue * ( 1.0 - pos ) ) + (EndCol.rgbtBlue * pos) ) );
end;
function FindGradientColorHSL(StartCol, EndCol: TRGBTriple; Pos: extended): TRGBTriple;
var
posH, posS, posL, startH, startS, startL, endH, endS, endL : extended;
begin
RGBtoHSL(StartCol, startH, startS, startL);
RGBtoHSL(endCol, endH, endS, endL);
if (endH - startH) > 0.5 then
startH := startH + 1;
if (endH - startH) < -0.5 then
endH := endH + 1;
posH := (StartH * (1.0 - pos)) + (EndH * pos);
posS := (StartS * (1.0 - pos)) + (EndS * pos);
posL := (StartL * (1.0 - pos)) + (EndL * pos);
if PosH > 1 then posH := posH - 1;
HSLtoRGB(posH, posS, posL, result);
end;
function FindGradientColorHSV(StartCol, EndCol: TRGBTriple; Pos: extended): TRGBTriple;
var
posH, posS, posV, startH, startS, startV, endH, endS, endV : extended;
begin
RGBtoHSV(StartCol, startH, startS, startV);
RGBtoHSV(endCol, endH, endS, endV);
if (endH - startH) > 0.5 then
startH := startH + 1;
if (endH - startH) < -0.5 then
endH := endH + 1;
posH := (StartH * (1.0 - pos)) + (EndH * pos);
posS := (StartS * (1.0 - pos)) + (EndS * pos);
posV := (StartV * (1.0 - pos)) + (EndV * pos);
if PosH > 1 then posH := posH - 1;
HSVtoRGB(posH, posS, posV, result);
end;
procedure RGBtoHSL(rgb : TRGBTriple; var H, S, L : extended);
var
delta, r, g, b, cmax, cmin: extended;
begin
r := rgb.rgbtRed / 255;
g := rgb.rgbtGreen / 255;
b := rgb.rgbtBlue / 255;
if (r > b) and (r > g) then
cmax := r
else if g > b then
cmax := g
else
cmax := b;
if (r < b) and (r < g) then
cmin := r
else if g < b then
cmin := g
else
cmin := b;
L := (cmax+cmin) / 2.0;
if cmax=cmin then
begin
S := 0;
H := 0; //sarebbe indefinita
end
else
begin
delta := cmax - cmin;
if L <= 0.5 then
s := delta / (cmax + cmin)
else
s := delta / (2.0 - cmax - cmin);
if r = cmax then
H := (g - b) / delta
else if g = cmax then
H := 2.0 + (b - r) / delta
else
H := 4.0 + (r - g) / delta;
H := H / 6.0;
if H < 0 then
H := H + 1;
end;
{
showMessage('R: '+inttostr(rgb.rgbtRed)+ #13 +
'G: '+inttostr(rgb.rgbtGreen)+ #13 +
'B: '+inttostr(rgb.rgbtBlue)+ #13 +
'H: '+floattostr(H)+ #13 +
'S: '+floattostr(S)+ #13 +
'L: '+floattostr(L));
}
end;
procedure HSLtoRGB(H, S, L : extended; var rgb : TRGBTriple);
var
r, g, b, m1, m2 : double;
function HuetoRGB(m1,m2, h: double): double;
begin
if h < 0 then
h := h + 1.0
else if h > 1 then
h := h - 1.0;
if 6.0*h < 1 then
result := (m1+(m2-m1)*h*6.0)
else if 2.0*h < 1 then
result := m2
else if 3.0*h < 2.0 then
result := (m1+(m2-m1)*((2.0/3.0)-h)*6.0)
else
result := m1;
end;
begin
if S = 0 then
begin
r := L;
g := L;
b := L;
end
else
begin
if L <= 0.5 then
m2 := L*(1.0+S)
else
m2 := L+S-L*S;
m1 := 2.0*L-m2;
r := HuetoRGB(m1,m2,H+1.0/3.0);
g := HuetoRGB(m1,m2,H);
b := HuetoRGB(m1,m2,H-1.0/3.0);
end;
rgb.rgbtBlue := round(b * 255);
rgb.rgbtGreen := round(g * 255);
rgb.rgbtRed := round(r * 255);
{ showMessage('H: '+floattostr(H)+ #13 +
'S: '+floattostr(S)+ #13 +
'L: '+floattostr(L)+ #13 +
'R: '+inttostr(rgb.rgbtRed)+ #13 +
'G: '+inttostr(rgb.rgbtGreen)+ #13 +
'B: '+inttostr(rgb.rgbtBlue));
}
end;
procedure RGBtoHSV(rgb : TRGBTriple; var H, S, V : extended);
var
delta, r, g, b, cmax, cmin: double;
begin
r := rgb.rgbtRed / 255;
g := rgb.rgbtGreen / 255;
b := rgb.rgbtBlue / 255;
if (r > b) and (r > g) then
cmax := r
else if g > b then
cmax := g
else
cmax := b;
if (r < b) and (r < g) then
cmin := r
else if g < b then
cmin := g
else
cmin := b;
if cmax = 0 then
begin
H := 0;
S := 0;
V := 0;
exit;
end;
delta := cmax - cmin;
S := delta / cmax;
V := cmax;
if delta = 0 then
begin
H := 0; //sarebbe indefinito
exit;
end;
if r = cmax then
H := (g - b) / delta
else if g = cmax then
H := 2 + (b - r) / delta
else
H := 4 + (r - g) / delta;
H := H / 6;
if H < 0 then
H := H + 1;
if H >=1 then
showMessage('H>=1');
end;
procedure HSVtoRGB(H, S, V : extended; var rgb : TRGBTriple);
var
r, g, b, f, p, q : double;
i : integer;
begin
if S = 0 then
begin
r := V;
g := V;
b := V;
end
else
begin
if h=1 then
h :=0
else
h := h * 6;
i := trunc(h);
if odd(i) then
f := h - i
else
f := (i + 1) - h ;
p := v *(1 - s);
q := v *(1 - s * f);
case i of
0:begin
r := v;
g := q;
b := p;
end;
1:begin
r := q;
g := v;
b := p;
end;
2:begin
r := p;
g := v;
b := q;
end;
3:begin
r := p;
g := q;
b := v;
end;
4:begin
r := q;
g := p;
b := v;
end;
else //5
begin
r := v;
g := p;
b := q;
end;
end;
end;
rgb.rgbtBlue := round(b * 255);
rgb.rgbtGreen := round(g * 255);
rgb.rgbtRed := round(r * 255);
end;
procedure SwitchRB(var rgb : TRGBTriple);
var
bt : byte;
begin
bt := rgb.rgbtBlue;
rgb.rgbtBlue := rgb.rgbtRed;
rgb.rgbtRed := bt;
end;
{ TColorize }
procedure TColorize.AddColor(Orig, Dest: TColor);
begin
if ColorsNumber - 1 < high( FOrigColors ) * 2 then
inc( FColorsNumber );
FOrigColors[ ColorsNumber - 1 ] := Orig;
FDestColors[ ColorsNumber - 1 ] := Dest;
ClearHashes;
end;
procedure TColorize.ClearHashes;
var
k: integer;
begin
FprevIter := -1;
for k := 0 to HashNum do
FHashIter[ k ] := -1;
end;
constructor TColorize.Create( AMaxIter: integer );
begin
FColorsNumber := 0;
Fapplication := caLinear;
FDistribution := cdDirect;
MaxIter := AMaxIter;
FMethod := cmHSL;
FLastColor := clBlack;
FOffset := 0;
AddColor( clWhite, clBlack ); //default
end;
function TColorize.FromIterToColor(Iter: Extended): TRGBTriple;
var
SCol, ECol : TRGBTriple;
hhitern, NumCol: integer;
iternorm: Extended;
HashVal: Extended;
begin
if Iter = FprevIter then
begin
Result := FprevResult;
exit;
end;
if ( FColorsNumber < 1 ) or ( FMaxIter < 1 ) then
exit;
if Iter >= MaxIter then
begin
Iter := MaxIter;
iternorm := 1; //no offset for the last color
hhitern := HashNum;
end
else // FRecMaxCol = 1/MaxIter
begin
iternorm := frac( iter * FRecMaxCol + foffset );
hhitern := trunc( iternorm * HashNum );
//slower! hhitern := trunc(iternorm * 4000000000) mod HashNum;
end;
HashVal := FHashIter[ hhitern ];
if ( HashVal > 0 ) and ( abs(HashVal - iternorm) < 2E-16) then
begin //check if hash bucket has current value
Result := FHashCol[ hhitern ];
exit;
end
else
begin
//only for debug
{ if HashVal > 0 then
OutputDebugString(pchar('fail ' + inttostr(hhitern)
+ ' ' + floattostr( abs(HashVal - iternorm))));
}
FHashIter[ hhitern ] := iternorm;
end;
if Iter >= MaxIter then
begin
Result := ColToRGBTriple( LastColor );
end
else
begin
case FDistribution of
cdLogarithmic:
iternorm := log10( iternorm * 9 + 1 );
cdExponential:
iternorm := Power( 10, iternorm ) / 10;
cdSquareRoot:
iternorm := sqrt( iternorm );
cdSquare:
iternorm := ( iternorm * iternorm );
cdCube:
iternorm := ( iternorm * iternorm * iternorm );
cdCubeRoot:
iternorm := exp( ln( iternorm ) / 3 );
cdSine:
iternorm := sin( iternorm * ( pi / 2 ) );
cdCosine:
iternorm := 1 - cos( iternorm * ( pi / 2 ) );
cdCircular:
iternorm := 1 - ( cos( iternorm * 2 * pi ) / 2 + 0.5 );
cdArcSin:
iternorm := ( arcsin( iternorm * 2 - 1 ) ) / pi + 0.5;
else //direct
// nothing to do
end;
case FApplication of
caRepeated:
begin
NumCol := trunc( iter ) mod FColorsNumber;
end;
ca2Times:
begin
iternorm := frac( iternorm * 2 );
iternorm := iternorm * FColorsNumber;
NumCol := trunc( iternorm );
iternorm := iternorm - NumCol;
end;
ca3Times:
begin
iternorm := frac( iternorm * 3 );
iternorm := iternorm * FColorsNumber;
NumCol := trunc( iternorm );
iternorm := iternorm - NumCol;
end;
ca4Times:
begin
iternorm := frac( iternorm * 4 );
iternorm := iternorm * FColorsNumber;
NumCol := trunc( iternorm );
iternorm := iternorm - NumCol;
end;
ca8Times:
begin
iternorm := frac( iternorm * 8 );
iternorm := iternorm * FColorsNumber;
NumCol := trunc( iternorm );
iternorm := iternorm - NumCol;
end;
ca16Times:
begin
iternorm := frac( iternorm * 16 );
iternorm := iternorm * FColorsNumber;
NumCol := trunc( iternorm );
iternorm := iternorm - NumCol;
end;
ca32Times:
begin
iternorm := frac( iternorm * 32 );
iternorm := iternorm * FColorsNumber;
NumCol := trunc( iternorm );
iternorm := iternorm - NumCol;
end;
ca64Times:
begin
iternorm := frac( iternorm * 64 );
iternorm := iternorm * FColorsNumber;
NumCol := trunc( iternorm );
iternorm := iternorm - NumCol;
end
else
begin
iternorm := iternorm * FColorsNumber;
NumCol := trunc( iternorm );
iternorm := iternorm - NumCol;
end;
end;
SCol := ColToRGBTriple( OrigColors[ NumCol ] );
ECol := ColToRGBTriple( DestColors[ NumCol ] );
case FMethod of
cmRGB:
result := FindGradientColorRGB( SCol, ECol, iternorm );
cmHSV:
result := FindGradientColorHSV( SCol, ECol, iternorm );
else
result := FindGradientColorHSL( SCol, ECol, iternorm );
end;
end;
Fpreviter := iter;
Fprevresult := Result;
FHashCol[ hhitern ] := Result;
end;
function TColorize.GetApplication: string;
begin
case FApplication of
caRepeated:
Result := 'Repeated';
ca2Times:
Result := '2Times';
ca3Times:
Result := '3Times';
ca4Times:
Result := '4Times';
ca8Times:
Result := '8Times';
ca16Times:
Result := '16Times';
ca32Times:
Result := '32Times';
ca64Times:
Result := '64Times';
else
Result := 'Linear';
end;
end;
function TColorize.GetDestColors(NumCol: integer): TColor;
begin
Result := FDestColors[ NumCol ];
end;
function TColorize.GetDistribution: String;
begin
case FDistribution of
cdLogarithmic:
Result := 'Logarithmic';
cdSquareRoot:
Result := 'SquareRoot';
cdSquare:
Result := 'Square';
cdSine:
Result := 'Sine';
cdCosine:
Result := 'Cosine';
cdCube:
Result := 'Cube';
cdCubeRoot:
Result := 'CubeRoot';
cdCircular:
Result := 'Circular';
cdArcSin:
Result := 'ArcSin';
cdExponential:
Result := 'Exponential';
else
Result := 'Direct';
end;
end;
function TColorize.GetMethod: string;
begin
case FMethod of
cmHSV:
Result := 'HSV';
cmRGB:
Result := 'RGB';
else
Result := 'HSL';
end;
end;
function TColorize.GetOrigColors(NumCol: integer): TColor;
begin
Result := FOrigColors[ NumCol ];
end;
function TColorize.ImportMap(const AFileName: string; verbose: boolean): boolean;
var
sl: TStringList;
k: integer;
ro, go, bo: Byte;
s: string;
function GetValues( astring: string; var r, g, b: Byte ): boolean;
var
sr, sg, sb, eb, i: integer;
s: string;
inum : boolean;
begin
Result := false;
sr := -1;
sg := -1;
sb := -1;
eb := -1;
i := 0;
inum := false;
while true do
begin
inc( i );
if i > Length( astring ) then
Break;
if astring[ i ] = ' ' then
begin
inum := False;
Continue;
end
else if inum then
Continue;
inum := true;
if sr = -1 then
sr := i
else if sg = -1 then
sg := i
else if sb = -1 then
sb := i
else if eb = -1 then
eb := i
else
break;
end;
if sb > -1 then // found right values
begin
if eb = -1 then
eb := i;
s := Trim( Copy( astring, sr, sg - sr ) );
b := StrToIntDef( s, 0 ) mod 256;
s := Trim( Copy( astring, sg, sb - sg ) );
g := StrToIntDef( s, 0 ) mod 256;
s := Trim( Copy( astring, sb, eb - sb ) );
r := StrToIntDef( s, 0 ) mod 256;
Result := True;
end;
end;
begin
Result := false;
if not FileExists( AFileName ) then
begin
if verbose then
ShowMessage( 'Error: ' + AFileName + ' not found !' );
exit;
end;
sl := TStringList.Create;
try
sl.LoadFromFile( AFileName );
k := sl.count;
if k < 3 then
exit;
FColorsNumber := 0;
s := sl[ 0 ];
if not GetValues( s, ro, go, bo ) then
begin
ro := 0;
go := 0;
bo := 0;
end;
LastColor := ro * 65536 + go *256 + bo;
for k := 1 to sl.count - 1 do
begin
// 'RRR GGG BBB'
s := sl[ k ];
if GetValues( s, ro, go, bo ) then
begin
FOrigColors[ FColorsNumber ] := ro * 65536 + go *256 + bo;
if FColorsNumber > 0 then
FDestColors[ FColorsNumber - 1 ] := FOrigColors[ FColorsNumber ];
inc( FColorsNumber );
if FColorsNumber > MAXCOL then
break;
end;
end;
if FColorsNumber > 0 then
begin
dec( FColorsNumber );
end;
ApplyTo := 'Linear';
Distribution := 'Direct';
Method := 'RGB';
Result := True;
finally
sl.Free;
end;
end;
procedure TColorize.InsertColor(Orig, Dest: TColor; NumCol: integer);
begin
end;
function TColorize.LoadFromFile(const AFileName: string; verbose: boolean ): boolean;
var
sl: TStringList;
k: integer;
ro, go, bo, rd, gd, bd: Byte;
s: string;
path: string;
begin
Result := false;
FFileName := ChangeFileExt( AFileName, '.mnc' );
path := ExtractFilePath( FFileName );
if path = '' then
path := DefaultPath
else
path := ''; // FileName with path
if not FileExists( path + FFileName ) then
begin
if verbose then
ShowMessage( 'Error: ' + FFileName + ' not found !' );
exit;
end;
FFileName := path + FFileName;
sl := TStringList.Create;
try
sl.LoadFromFile( FFileName );
if sl.count < 5 then
exit;
k := FColorsNumber;
FColorsNumber := strtointdef( sl[ 4 ], 1 );
if sl.count < ( FColorsNumber + 18 ) then
begin
FColorsNumber := k;
exit;
end;
for k := 0 to FColorsNumber - 1 do
begin
// '| R | G | B | | R | G | B |'
s := sl[ 9 + k ];
bo := strtointdef( copy( s, 2, 3 ), 0 );
go := strtointdef( copy( s, 6, 3 ), 0 );
ro := strtointdef( copy( s, 10, 3 ), 0 );
bd := strtointdef( copy( s, 17, 3 ), 0 );
gd := strtointdef( copy( s, 21, 3 ), 0 );
rd := strtointdef( copy( s, 25, 3 ), 0 );
FOrigColors[ k ] := ro * 65536 + go *256 + bo;
FDestColors[ k ] := rd * 65536 + gd *256 + bd;
end;
s := sl[ 10 + FColorsNumber];
bo := strtointdef( copy( s, 2, 3 ), 0 );
go := strtointdef( copy( s, 6, 3 ), 0 );
ro := strtointdef( copy( s, 10, 3 ), 0 );
LastColor := ro * 65536 + go *256 + bo;
ApplyTo := sl[ 13 + FColorsNumber ];
Distribution := sl[ 15 + FColorsNumber ];
Method := sl[ 17 + FColorsNumber ];
Result := true;
finally
sl.Free;
end;
end;
procedure TColorize.RemoveColor(NumCol: integer);
begin
if ColorsNumber > 1 then
begin
DestColors[ ColorsNumber - 1 ] := 0;
OrigColors[ ColorsNumber - 1 ] := 0;
dec( FColorsNumber );
ClearHashes;
end;
end;
function TColorize.SaveToFile(const AFileName: string; Verbose: boolean ): boolean;
var
sl: TStringList;
k: integer;
ro, go, bo, rd, gd, bd: integer;
path: string;
begin
Result := false;
FFileName := ChangeFileExt( AFileName, '.mnc' );
path := ExtractFilePath( FFileName );
if path = '' then
path := DefaultPath;
if not DirectoryExists( path ) then
if Verbose and ( MessageDlg('Create directory ' + path + ' ?', mtWarning, [mbYes, mbNo], 0) <> mrYes ) then
exit
else
ForceDirectories( path );
FFileName := path + ExtractFileName( FFileName );
if FileExists( FFileName ) then
begin
if Verbose and ( MessageDlg('Do you wish to replace ' + FFileName + ' ?', mtWarning, [mbYes, mbNo], 0) <> mrYes ) then
exit
else
DeleteFile( FFileName );
end;
sl := TStringList.Create;
try
sl.Add( Application.Title );
sl.Add( 'By Uberto Barbini [email protected]' );
sl.Add( '' );
sl.Add( 'Number of colors:' );
sl.Add( IntToStr( FColorsNumber ) );
sl.Add( 'Colors:' );
sl.Add( '+---+---+---+ +---+---+---+' );
sl.Add( '| R | G | B | | R | G | B |' );
sl.Add( '+---+---+---+ +---+---+---+' );
for k := 0 to FColorsNumber - 1 do
begin
bo := OrigColors[ k ] div 65536;
go := OrigColors[ k ] mod 65536 div 256 ;
ro := OrigColors[ k ] mod 256;
bd := DestColors[ k ] div 65536;
gd := DestColors[ k ] mod 65536 div 256 ;
rd := DestColors[ k ] mod 256;
sl.Add( Format( '|%3d|%3d|%3d| |%3d|%3d|%3d|', [ ro, go, bo, rd, gd, bd ] ) );
end;
sl.Add( '+---+---+---+ +---+---+---+' );
bd := LastColor div 65536;
gd := LastColor mod 65536 div 256 ;
rd := LastColor mod 256;
sl.Add( Format( '|%3d|%3d|%3d|', [ rd, gd, bd ] ) );
sl.Add( '+---+---+---+' );
sl.Add( 'Application:' );
sl.Add( ApplyTo );
sl.Add( 'Distribution:' );
sl.Add( Distribution );
sl.Add( 'Method:' );
sl.Add( Method );
sl.SaveToFile( FFileName );
Result := true;
finally
sl.Free;
end;
end;
procedure TColorize.SetApplication(const Value: string);
begin
ClearHashes;
if CompareText( Value, 'Repeated' ) = 0 then
FApplication := caRepeated
else if CompareText( Value, '2Times' ) = 0 then
FApplication := ca2Times
else if CompareText( Value, '3Times' ) = 0 then
FApplication := ca3Times
else if CompareText( Value, '4Times' ) = 0 then
FApplication := ca4Times
else if CompareText( Value, '8Times' ) = 0 then
FApplication := ca8Times
else if CompareText( Value, '16Times' ) = 0 then
FApplication := ca16Times
else if CompareText( Value, '32Times' ) = 0 then
FApplication := ca32Times
else if CompareText( Value, '64Times' ) = 0 then
FApplication := ca64Times
else
FApplication := caLinear;
end;
procedure TColorize.SetDestColors(NumCol: integer; const Value: TColor);
begin
ClearHashes;
if ( NumCol >= Low( FDestColors ) ) and ( NumCol <= High( FDestColors ) ) then
FDestColors[ NumCol ] := ColorToRGB( Value );
end;
procedure TColorize.SetDistribution(const Value: string );
begin // use CompareText 'cause it's case-insensitive
ClearHashes;
if CompareText( Value, 'Logarithmic' ) = 0 then
FDistribution := cdLogarithmic
else if CompareText( Value, 'SquareRoot' ) = 0 then
FDistribution := cdSquareRoot
else if CompareText( Value, 'Square' ) = 0 then
FDistribution := cdSquare
else if CompareText( Value, 'Sine' ) = 0 then
FDistribution := cdSine
else if CompareText( Value, 'Cosine' ) = 0 then
FDistribution := cdCosine
else if CompareText( Value, 'Cube' ) = 0 then
FDistribution := cdCube
else if CompareText( Value, 'CubeRoot' ) = 0 then
FDistribution := cdCubeRoot
else if CompareText( Value, 'Circular' ) = 0 then
FDistribution := cdCircular
else if CompareText( Value, 'ArcSin' ) = 0 then
FDistribution := cdArcSin
else if CompareText( Value, 'Exponential' ) = 0 then
FDistribution := cdExponential
else
FDistribution := cdDirect;
end;