-
Notifications
You must be signed in to change notification settings - Fork 11
/
ColorPicker.p
3047 lines (2675 loc) · 89.8 KB
/
ColorPicker.p
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
{
File: ColorPicker.p
Contains: Color Picker package
This is the heart of the ColorPicker package, the part which presents the
color selection dialog, animates it, and lets the user ultimately select
a color. It changes the color table, if there is one, so that the user
sees the real color, not just the current "best match" returned by Quickdraw.
The conversion routines for the various color models, the assembly dispatcher
for the package, and the glue code that unlocks the package upon exit are
all elsewhere.
Copyright: © 1987-1991 by Apple Computer, Inc., all rights reserved.
This file is used in these builds: BigBang Sys606
Change History (most recent first):
<13> 5/21/91 gbm Stop using "empty" units like Memtypes...
<12> 1/18/91 gbm (csd) Change prompt string so it will be truncated instead of
wrapping around.
<11> 1/10/91 SMC Added code to ignore strings with non-numeric characters when
pasting into a number field. With KON.
<10> 12/14/90 dvb Eyedropper
<9> 12/14/90 gbm & JDR; Dispose the palette after closing the window, instead of
setting the palette to nil and disposing it first. This has the
side effect of causing more updates in some cases, but avoids
the unsightly color table change while the color wheel is still
visible. I am not completely sure that this is the right thing.
An alternative is to return to the old way, but to erase the
contents of the color picker dialog before changing the palette
(and indirectly, the color table).
<8> 10/12/90 dvb Remove color arrows
<7> 9/4/90 dvb Make whizzier.
<6> 8/28/90 dvb Numbering mixed up because project was corrupted and restored.
<8> 8/27/90 dvb Fix sample draw on non-clut devices
<7> 8/27/90 dvb NEEDED FOR SIXPACK: Remove calls to PixPatChanged and
CTabChanged... does this break 8¥24 GC?
<6> 8/15/90 dvb NEEDED FOR SIXPACK: Fix to work an b/w machines
<5> 6/12/90 DVB Make main-screen preferred in case of depth tie.
<4> 5/30/90 DVB Streamline: remove cWhite and cBlack. Use colors for input and
output in 2-bit mode.
<3> 5/23/90 DVB Make it use palettes instead of RestoreEntries, fix cursor bug
(7.0 only)
<2> 12/28/89 dba got rid of obsolete compile-time options (options are passed in
by the Makefile)
<1.4> 12/14/89 dvb NEEDED for 6.0.5: Change default roundrect to conform to Inside
Mac recommendations, make 'esc' key the same as cmd-. .
<1.3> 8/3/89 CSD NEEDED For 6.0.4: Set Cursor to an arrow when dialog opens (BRC
#30423). Also, changed the way that the scroll bar CDEF was
called to make it safer for 32-Bit Memory Manager.
<1.2> 8/3/89 CCH Changed references to "transIndex" to "ctFlags".
<¥1.1> 7/24/89 CSD Forced submission of the Color Picker from 32-Bit QuickDraw. Too
many changes, too long ago to merge without causing havoc.
older Change History (oldest first, Darin is too lazy to convert it):
1.0d7 amy Updated PickDlgClose to use the new color manager proc DelSearch.
Note that 1.0d6 won't run on >B3 ROMs, since it does a DisposPtr
on the proc queue element, which is now a handle.
1.0b1 amy Made the model dialog inaccessible by moving the button offscreen.
1.0b2 amy Corrected colorOk test. We now test that the machine has a Mac II
or later ROM, rather than checking that CPUFlag = 2 (68020).
Paint the final "tint" on the wheel (no saturation) with FillOval,
rather than filling a separate arc for each color.
Added SetPort to the beginning of PickDlgDrawItem, to correct a bug
that shows up iff you're on a non-clutType device (wheel and new
sample were being drawn in the wrong port initially).
Changed SampleDraw to use lines, not FrameRect, so there's less
flicker while updating iNewSample on a non-clutType device.
Rearranged order of colors in PColor, to alter the priority with
which colors are matched as they are swiped from the color table.
Old order was colors, white, black, gray, input & output samples.
New order is nearly the same, but colors come last. This insures
that black and white are always substituted correctly, that the
color being picked, if on screen, is more likely to animate, and
that the colors in the wheel are lowest priority for matching.
When resolving which CLUT entries to swipe, come down from the top
of the color table, not up from the bottom.
When resolving which CLUT entries to swipe, avoid the hilight color,
and any grays (except black and white), since they may be used in
gray scale fonts.
Removed MakeITable calls, which were making it impossible to avoid the
hilight color if the input color was close to any of the entries
Color Picker installs in the color table. This should affect no
one (and should make the package less of a memory hog, and safer),
because Color Picker uses a search proc to get any of its colors.
Changed CTabSearch to return FALSE if gdType <> clutType, since we'd
rather have the default search proc get the best match for fixed
devices.
Don't artifically spread the colors before GetSubTable by upping the
brightness, and don't invert inColor either.
Turned off debugging and (unused) remodel dialog to save space. Turned
off skip gray also, and removed remodel resources from the .r file.
1.1a1 amy Updated to use MPW 2.0 interface files.
Removed $LOAD file to help out SCM.
1.2d1 amy Iff we're in 8 bit mode or more, on a clut device, use more colors in
the wheel (36 for now). This does NOT slow use down by 36/6,
because of a clever idea of David's for using FrameOval for the
saturation levels, which eliminates ((wheelTints - 1) * maxColors)
PaintArc calls to (wheelTints - 1) FrameOvals.
1.2b1 amy Do a SetPort after GetNewDialog, in case there is no port at start.
Handle autoKey events just like keydown events to get validation.
Increased size of editText fields by 2 each to get rid of jumping.
Art added NewPalette and SetPalette to render ActivatePalette harmless,
so that the color environment won't change (esp. between the time
we chose which entries to animate and the dialog appears).
1.2b2 amy Preserve caller's port around calls to GetColor.
1.3b1 amy Fix HSL2RGB once and for all by replacing HiWrd, which is broken in tests.
1.4a2 CSD Added constants for resources STR# $E980, ppat $E980, wedg $E980-$E981, and
CURS $E980
Used STR# for drawing wedge names in 1 and 2 bit modes.
Make a where parameter of (-1, -1) place the dialog on the deepest screen.
1.4a3 CSD Accept arrow keys as well as digits and backspace.
Handle CMD-X, CMD-C, CMD-V as cut, copy, paste.
Handle CMD-. as cancel with a brief hilite of the cancel button.
Briefly hilite Ok button for return and enter.
Option-click in up and down arrows sets value to max and min.
1.4a4 CSD With a where parameter of (-1, -1), use the "best" screen, not neccessarily
the deepest screen. (i.e. color 4-bit is better than mono 8-bit)
Erase the hot-spot before redrawing the wheel to prevent trash from being
left outside the wheel.
Test for required memory and show an alert if we don't think we can
run safely.
If possible, animate the colors while the user is dragging the thumb
in the brightness scroll bar.
1.4a5 CSD Reduced the amount of memory that guarantees we can run. It used to have
about 9K of slop, now it has about 2K.
1.4a6 CSD Instead of testing for required memory by calling PurgeSpace, actually
allocate a handle and then dispos it. This will cause the heap to
grow if it can, giving us a more correct result.
When looking for the 'best' device, don't count inactive screens.
dvb 17 April 1989 - Fixed searchproc to work in 16-bit mode, made 8-color
wheel less interesting and much faster.
1.4b2 CSD Fixed test to see if we should allocate a palette for the window. It
used to test colorOk which wasn't setup yet.
Fixed bug where RGB/HSV values wouldn't update if the thumb was
dragged to one of the control extremes on an 8-bit display.
To Do:
Add error handling for pickDlg = NIL
Handle CLUTFixed, CLUTDirect better
Nicer looking cursor and hot spot
Use dithering to smooth color wheel appearance
Test for wheel hits via radius from center, not wheelRgn?
Build custom brightness control?
Beef up error handling throughout
Make model selection dialog work
Add YIQ conversion routines
Get Gerard's sqrt & get rid of SANE
What if calling app has protected lots of CLUT entries?
SetDAFont(applFont), and make prompt a user item in System font
How to check that we aren't swiping the CLUT entry for a desktop color?
Whenever editField is used, watch out for editField < 0?
Put scaling in model selection dialog?
Add graphical component legends?
Help button and text?
Keyboard equivalents?
Change setting of colorOk to use SysEnvirons
}
{$SETC HSV := TRUE} {Use HSV color model (FALSE=HSL)}
{$SETC GRUNGY := TRUE} {Grungy behavior that needs to be cleaner}
{$SETC UsePalette := TRUE}
UNIT ColorPicker;
INTERFACE
USES
Types, Memory, QuickDraw, Palettes, Resources, Fonts, Dialogs,
Packages, GestaltEqu, FixMath, ToolUtils, OSUtils, SysEqu,
ColorConvert;
FUNCTION GetColor(where: Point; prompt: Str255; inColor: RGBColor;
VAR outColor: RGBColor): BOOLEAN;
IMPLEMENTATION
CONST
arrowDeltaLow = 1; {Change in component value for inc/dec}
arrowDeltaMed = 10; {1st level of inc/dec acceleration}
arrowDeltaHi = 100; {2nd level of inc/dec acceleration}
arrowSlop = 2; {Slop around each arrow rect for tracking}
inputSlop = 4; {Slop around input sample for tracking}
lineBright = $100; {Change in bright control for scroll arrow}
maxColors = 36; {Wheel wedges if 8 bit mode w/ animation}
minColors = 6; {Wheel wedges otherwise}
pageBright = $10; {Change in bright control for scroll page}
pickClientID = 12; {Client ID used with picker's search proc}
slopRim = 10; {Rim outside wheel that still tracks}
twoPi = $0006487F; {Two times Pi, in Fixed format}
wheelTints = 5; {Number of saturation levels in wheel}
{Resource IDs}
rPickDlg = $E980; {Color picker dialog}
rArrowPic = $E980; {Inc/dec control picture}
rRemodelDlg = $E981; {Remodel dialog}
rColorNamesStr = $E980; {STR# of color slice names}
rGreenWedge = $E980; {Green wedge bits for wheel}
rRedWedge = $E981; {Red wedge bits for wheel}
rCursor = $E980; {A good looking cursor}
rMemShortageID = $E982; {Not enough memory alert}
{Memory Requirements}
kDirectMemReq = 55600; {RAM needed on 16/32 bit devices. Actually Å53608}
kClutMemReq = 24000; {RAM needed on clut devices. Actually Å22200}
{Items in the dialog}
{ok = 1;} {Defined for us by the Toolbox}
{cancel = 2;} {Defined for us by the Toolbox}
iRemodel = 3; {Button to bring up remodel dialog}
iPrompt = 4; {User-supplied prompt string}
iBrightCtl = 5; {Brightness control}
{Items iFirstUI to iLastUI are drawn by DrawUserItem}
iFirstUI = 6; {First of user items in dialog}
iOkOutline = 6; {Outline for OK button}
iWheel = 7; {Hue/saturation wheel}
iNewSample = 8; {Sample square of current color}
iOldSample = 9; {Sample square of input color}
iVersion = 10; {Color picker version number, writ small}
iFirstArrow = 11; {First of up/down controls}
iHSxArrows = 11; {First of three HSx up/down controls}
iRGBArrows = 14; {First of three RGB up/down controls}
iLastArrow = 16; {Last of up/down controls}
iLastUI = 16; {Last of user items in dialog}
{Color component labels are items 17-22, edit fields are below}
iFirstComponent = 23; {First color component text string}
iHue = 23;
iSaturation = 24;
iLightness = 25;
iRed = 26;
iGreen = 27;
iBlue = 28;
iLastComponent = 28; {Last color component text string}
TYPE
ColorItem = (rgbText, hsText, lText, hsWheel, lWheel, bright);
ColorItemSet = SET OF ColorItem;
IntPtr = ^INTEGER;
LongPtr = ^LONGINT;
RGBColorPtr = ^RGBColor;
PColor = (cInput, cSample, cGray, cSlice1);
PickColor = RECORD CASE INTEGER OF
0: (hsx : HSLColor;
rgb : RGBColor);
1: (components : ARRAY [0..5] OF INTEGER);
END;
ReqListPtr = ^ReqListRec;
ReqListHandle = ^ReqListPtr;
PickInfo = RECORD {Picker's "global" vars stick with dialog}
dlgRec: DialogRecord; {Dialog record must be first}
rgbInput: RGBColor; {Color the caller supplied as input}
theColor: PickColor; {Color being picked, in all req'd transforms}
showModel: BOOLEAN; {Show primary color model?}
showAltModel: BOOLEAN; {Show alternate color model?}
slopRgn: RgnHandle; {Region slightly larger than the wheel}
wheelRgn: RgnHandle; {Region of the wheel itself}
wheelCenter: Point; {Center of the color wheel}
wheelRadius: Fixed; {Radius of the color wheel}
wheelColors: INTEGER; {Number of wedges in the color wheel}
lastColor: PColor; {Color table index of last color in wheel}
whichPart: INTEGER; {Part of control we started tracking in}
ourCursor: BOOLEAN; {Is our cursor set (or the usual arrow)?}
cursorRgn: RgnHandle; {Composite of item rects in which we want ourCursor}
cursorHand: CursHandle; {Handle to our cursor}
hotSpot: Point; {Displayed hot spot (HS) on wheel, if any)}
pixSize: INTEGER; {pixelsize of screen we are on}
gdType: INTEGER; {type of gDevice we are on}
colorOK: BOOLEAN; {Not ok if: 128k ROM or one bit per pixel}
itsGDev: GDHandle; {deepest device the dlog intersects}
pickCTab: CTabHandle; {Table of colors picker borrows, or nil}
{$IFC UsePalette}
pickPal: PaletteHandle; {Handle to a palette full of animated entries}
{$ELSEC}
whichColor: PColor; {Which entry in pickCTab search proc should return}
saveCTab: CTabHandle; {Table of pre-picker CLUT entries, or nil}
saveIRes: INTEGER; {Caller's inverse table resolution value}
pickReqList: ReqListHandle; {List of stolen CLUT entries, or nil}
{$ENDC}
END;
PickInfoPtr = ^PickInfo; {Ptr to our info, and the dialog}
QDGlobals = RECORD
randSeed: LONGINT;
screenBits: BitMap;
arrow: Cursor;
dkGray: Pattern;
ltGray: Pattern;
gray: Pattern;
black: Pattern;
white: Pattern;
thePort: GrafPtr;
END;
QDGlobalsPtr = ^QDGlobals;
ControlThumbInfo = RECORD
limitRect: Rect;
slopRect: Rect;
axis: INTEGER;
startPoint: Point;
END;
ControlThumbInfoPtr = ^ControlThumbInfo;
{---------------------------------------------------------------------------------
Declare some EXTERNALS that are in ColorPicker.a.
}
FUNCTION NibbleUnpack(src,dst:LONGINT):LONGINT; EXTERNAL;
PROCEDURE SetGray25Pat; EXTERNAL;
PROCEDURE SetGray50Pat; EXTERNAL;
PROCEDURE SetGray75Pat; EXTERNAL;
PROCEDURE SetGray50BPat; EXTERNAL;
{---------------------------------------------------------------------------------
Declare everything FORWARD, so we can order the source alphabetically. Paired
routines are together. GetColor, the only one that will (eventually) be PACKaged,
is last.
}
FUNCTION ArrowTrack(theDialog: DialogPtr; where: Point; option : Boolean): BOOLEAN; FORWARD;
PROCEDURE BrightAdjust(pickDlg: DialogPtr; ctlHand: ControlHandle; forceIt : Boolean); FORWARD;
PROCEDURE BrightCtlGet(ctlHand: ControlHandle; VAR lightness: SmallFract); FORWARD;
PROCEDURE BrightCtlSet(ctlHand: ControlHandle; lightness: SmallFract); FORWARD;
FUNCTION BrightTrack(theDialog: DialogPtr; where: Point): BOOLEAN; FORWARD;
PROCEDURE BrightTrackScroll(theControl: ControlHandle; partCode: INTEGER); FORWARD;
FUNCTION CallControl(theControl : ControlHandle; message : Integer;
param : LongInt) : LongInt; FORWARD;
PROCEDURE CTabAdjust(pickDlg: DialogPtr; newBright: BOOLEAN); FORWARD;
PROCEDURE CTabBuild(VAR pickData: PickInfo; inColor: RGBColor); FORWARD;
PROCEDURE CTabInstall(pickDlg: DialogPtr; newBright: BOOLEAN); FORWARD;
PROCEDURE CTabRestore(pickPtr: PickInfoPtr); FORWARD;
PROCEDURE CTabSave(pickPtr: PickInfoPtr); FORWARD;
FUNCTION CTabSearch(myColor: RGBColor; VAR index: LONGINT): BOOLEAN; FORWARD;
FUNCTION DeviceCTab: CTabHandle; FORWARD;
FUNCTION DeviceITab: ITabHandle; FORWARD;
PROCEDURE RGBNormal; FORWARD;
FUNCTION GetIHand(theDialog: DialogPtr; item: INTEGER): Handle; FORWARD;
PROCEDURE GetIRect(theDialog: DialogPtr; item: INTEGER; VAR itemRect: Rect); FORWARD;
FUNCTION GetQDGlobals: QDGlobalsPtr; FORWARD;
PROCEDURE HSx2RGB(hsx: HSLColor; VAR rgb: RGBColor); FORWARD;
PROCEDURE MakeWedges32(wheelRect:Rect; brite:INTEGER); FORWARD;
PROCEDURE PickDlgClose(infoPtr: PickInfoPtr); FORWARD;
PROCEDURE PickDlgDrawItem(theWindow: WindowPtr; item: INTEGER); FORWARD;
FUNCTION PickDlgFilter(theDialog: DialogPtr; VAR theEvent: EventRecord; VAR itemHit: INTEGER): BOOLEAN; FORWARD;
FUNCTION PickDlgOpen(infoPtr: PickInfoPtr; where: Point; prompt: Str255): DialogPtr; FORWARD;
PROCEDURE RemodelDlg(pickRect: Rect; VAR showModel, showAltModel: BOOLEAN); FORWARD;
PROCEDURE RemodelDlgDrawItem(theWindow: WindowPtr; item: INTEGER); FORWARD;
PROCEDURE RGB2HSx(rgb: RGBColor; VAR hsx: HSLColor); FORWARD;
PROCEDURE SampleDraw(pickDlg: DialogPtr; item: INTEGER; sampleRect: Rect); FORWARD;
FUNCTION SampleTrack(theDialog: DialogPtr; where: Point): BOOLEAN; FORWARD;
PROCEDURE ShowAffected(pickDlg: DialogPtr; item: INTEGER); FORWARD;
PROCEDURE ShowHotSpot(where: Point); FORWARD;
PROCEDURE ShowNewColor(pickDlg: DialogPtr; affected: ColorItemSet); FORWARD;
PROCEDURE ShowNewValues(pickDlg: DialogPtr; newRGB: BOOLEAN; affected: ColorItemSet); FORWARD;
PROCEDURE SnareNewComponents(pickDlg: DialogPtr); FORWARD;
PROCEDURE StuffIt(pickDlg: DialogPtr; theItem: INTEGER; newVal: SmallFract); FORWARD;
PROCEDURE WheelDraw(pickDlg: DialogPtr; wheelRect: Rect); FORWARD;
PROCEDURE WheelPos2HS(pickDlg: DialogPtr; where: Point); FORWARD;
PROCEDURE HS2WheelPos(pickDlg: DialogPtr; VAR where: Point); FORWARD;
PROCEDURE WheelTintPattern(whichTint: INTEGER; VAR pat: Pattern); FORWARD;
FUNCTION WheelTrack(theDialog: DialogPtr; where: Point): BOOLEAN; FORWARD;
FUNCTION CallCDEFProc(varCode : Integer; theControl : ControlHandle; message : Integer;
param : LongInt; defProcPtr : ProcPtr) : LongInt;
INLINE $205F, {MOVE.L (A7)+, A0 ;Get procPtr}
$4E90; {JSR (A0) ;Call the defProc}
PROCEDURE CopyPix(src:PixMap;dst:BitMap; srcRect, dstRect: Rect;
mode: INTEGER; maskRgn: RgnHandle);
INLINE $A8EC;
{--------------------------------------------------------------------------------
ArrowTrack - track mouse-down in component plus/minus controls. All of the
control accelerate, but they purposely do NOT accelerate to really great
speed. The maximum speed they attain is one that takes the user through the
entire range of a given component (like hue or saturation) in a slightly
leisurely fashion, so they can see everything.
1.4a3 If the user option-clicks in an arrow, set the value to its upper or
lower limit and don't bother to keep tracking.
}
FUNCTION ArrowTrack(theDialog: DialogPtr; where: Point; option : Boolean): BOOLEAN;
VAR
c, i: INTEGER;
delta: Fixed;
doDelay: BOOLEAN;
finalTicks: LONGINT;
nextDelta: Fixed;
goingUp: BOOLEAN;
item: INTEGER;
lastKick: Fixed;
midLine: INTEGER;
slopRect: Rect;
temp: Fixed;
up: BOOLEAN;
BEGIN
ArrowTrack := FALSE;
item := -1; { signal if an arrow found }
WITH PickInfoPtr(theDialog)^, theColor DO
BEGIN
{The only areas in cursorRgn besides the wheel are the inc/dec
controls. Since WheelTrack has already had a shot at this
event, if it's in cursorRgn, it's ours. First, figure out
which control we're in.}
FOR i := iFirstArrow TO iLastArrow DO
BEGIN
GetIRect(theDialog, i, slopRect);
IF PtInRect(where, slopRect) THEN
BEGIN
ArrowTrack := TRUE;
c := i - iFirstArrow;
item := c + iFirstComponent;
InsetRect(slopRect, -arrowSlop, -arrowSlop);
WITH slopRect DO
midLine := (bottom + top) DIV 2;
LEAVE; {exit FOR}
END;
END;
IF item = -1 THEN EXIT(ArrowTrack);
{Now we know which component we're in. If the user held the option
key while clicking, slam the value to the upper or lower limit
as appropriate.}
IF option THEN
BEGIN
IF where.v < midLine THEN
components[c] := Fix2SMallFract(MaxSmallFract)
ELSE
components[c] := Fix2SMallFract(0);
ShowAffected(theDialog, item);
END
ELSE
BEGIN
{Now that we know which component the user is changing, track it.
Like WheelTrack, we use repeat here so that the block executes
at least once, and quick mouse clicks are seen.}
doDelay := TRUE;
REPEAT
GetMouse(where);
IF PtInRect(where, slopRect) THEN
BEGIN
up := (where.v < midLine);
temp := SmallFract2Fix(components[c]);
{Before inc/dec-ing the value, see if it's time to
accelerate, or to quit accelerating because of a
change in direction.}
IF up <> goingUp THEN
BEGIN
goingUp := up;
lastKick := temp;
delta := arrowDeltaLow;
nextDelta := arrowDeltaMed;
END
ELSE IF (Abs(temp-lastKick) >= nextDelta) &
(temp MOD nextDelta = 0) THEN
BEGIN
lastKick := temp;
delta := nextDelta;
IF delta = arrowDeltaMed
THEN nextDelta := arrowDeltaHi
ELSE nextDelta := MaxSmallFract;
END;
{If there's still room to move in the current direction,
do so.}
IF (up & (temp < MaxSmallFract)) |
(NOT up & (temp > 0)) THEN
BEGIN
IF up
THEN temp := Min(MaxSmallFract, temp + delta)
ELSE temp := Max(0, temp - delta);
components[c] := Fix2SmallFract(temp);
ShowAffected(theDialog, item);
END
{If the component we're tracking is hue, let the user
wrap around, so they can have fun circling the wheel.
lastKick is set so that the difference between it and
temp will remain < MaxSmallFract, and delta is set so
that the user keeps moving in the same sort of jumps
that they were before passing zero hue.}
ELSE IF (item = iHue) THEN
BEGIN
IF up THEN
BEGIN
lastKick := 1;
temp := delta;
END
ELSE
BEGIN
lastKick := MaxSmallFract - 1;
temp := MaxSmallFract - (MaxSmallFract MOD delta);
END;
temp := BAnd(temp, MaxSmallFract);
components[c] := Fix2SmallFract(temp);
ShowAffected(theDialog, item);
END;
END;
{Now that we've reflected the new value, check to see if
this is the first time 'round the loop. If so, we do
a small delay, to make life easier for slow clickers.}
IF doDelay THEN
BEGIN
Delay(3, finalTicks);
doDelay := FALSE;
END;
UNTIL NOT WaitMouseUp;
END;
{Regardless of where the user moused up, we've handled the mouse
down; let the filter proc know so ModalDialog will ignore it.}
END;
END;
{---------------------------------------------------------------------------------
BrightAdjust - Adjust to new value in brightness control. This routine
is called by both of the scroll bar tracking action procedures (BrightTrack
and BrightCtlTrack) once the new control value has been set.
}
PROCEDURE BrightAdjust(pickDlg: DialogPtr; ctlHand: ControlHandle; forceIt : Boolean);
VAR
newBright: SmallFract;
BEGIN
{See what lightness value the new control value translates to. If it's
the same as the current lightness, none of picker's values have
changed; don't bother updating them unless forceIt is set.}
BrightCtlGet(ctlHand, newBright);
WITH PickInfoPtr(pickDlg)^.theColor.hsx DO
IF (newBright <> lightness) OR forceIt THEN
BEGIN
lightness := newBright;
ShowNewValues(pickDlg, FALSE, [rgbText, lText, lWheel]);
END;
END;
{--------------------------------------------------------------------------------
BrightCtlGet - get the brightness control value into lightness range
BrightCtlSet - set the brightness control value from lightness
The following procedures convert between the brightness scroll bar control
value (which ranges from -32768 to 32767), and lightness/value (which ranges
from 0 to 1). In doing so, they also invert the value, because the scroll
bar minimum is at the top, where we want the lightness/value maximum to be.
}
PROCEDURE BrightCtlGet(ctlHand: ControlHandle; VAR lightness: SmallFract);
BEGIN
lightness := Fix2SmallFract(MaxSmallFract - MAXINT - 1 - GetCtlValue(ctlHand));
END;
PROCEDURE BrightCtlSet(ctlHand: ControlHandle; lightness: SmallFract);
BEGIN
SetCtlValue(ctlHand, MaxSmallFract - MAXINT - 1 - SmallFract2Fix(lightness));
END;
{--------------------------------------------------------------------------------
BrightThumbTrack - update the colors while dragging the brightness thumb
}
PROCEDURE BrightThumbTrack;
CONST
kArrowHeight = 15; {Height in pixels of a single scroll bar arrow}
kThumbHeight = 16; {Height in pixels of the scroll bar thumb}
VAR
dlgPtr: DialogPtr;
ctlHand: ControlHandle;
startValue: LongInt;
pThumbData: ControlThumbInfoPtr;
curMouse: Point;
deltaV: LongInt;
pixelRange: LongInt;
newValue: LongInt;
newBright: SmallFract;
oldPenState:PenState;
gdType: INTEGER;
BEGIN
dlgPtr := DialogPtr(FrontWindow);
gdType := PickInfoPtr(dlgPtr)^.gdType;
ctlHand := ControlHandle(GetIHand(dlgPtr, iBrightCtl));
startValue := GetCtlValue(ctlHand);
pThumbData := ControlThumbInfoPtr(GetCRefCon(ctlHand));
GetMouse(curMouse);
IF PtInRect(curMouse, pThumbData^.slopRect)
THEN
{Calculate what the new control value would be if the user released
the mouse button at this point. Too bad this isn't done for us.}
BEGIN
deltaV := curMouse.v - pThumbData^.startPoint.v;
WITH ctlHand^^.contrlRect
DO
pixelRange := bottom - top - 2 * kArrowHeight - kThumbHeight;
newValue := startValue + (deltaV * 65535) DIV pixelRange;
IF newValue < -32768
THEN
newValue := -32768
ELSE IF newValue > 32767
THEN
newValue := 32767;
END
ELSE
newValue := startValue;
newBright := Fix2SmallFract(MaxSmallFract - MAXINT - 1 - newValue);
WITH PickInfoPtr(dlgPtr)^.theColor.hsx DO
IF newBright <> lightness
THEN
BEGIN
lightness := newBright;
GetPenState(oldPenState); { control mgr uses weird xor mode }
PenNormal;
IF gdType = clutType { <8> }
THEN
ShowNewValues(dlgPtr, FALSE, [lText,rgbText,lWheel]) { <8> }
ELSE { <8> }
ShowNewValues(dlgPtr, FALSE, [lText,rgbText]); { <8> }
SetPenState(oldPenState);
END
END;
{--------------------------------------------------------------------------------
BrightTrack - track mouse-down inside brightness scroll bar
1.4a4 If we're on a clut device, do our color table animation
of the wheel and selection color while the user is
dragging the thumb in the scroll bar.
}
FUNCTION BrightTrack(theDialog: DialogPtr; where: Point): BOOLEAN;
VAR
ctlHand: ControlHandle;
itemHand: Handle;
partCode: INTEGER;
upCode: INTEGER;
thumbData: ControlThumbInfo;
dummyResult: LongInt;
BEGIN
BrightTrack := FALSE;
partCode := FindControl(where, WindowPtr(theDialog), ctlHand);
itemHand := GetIHand(theDialog, iBrightCtl);
IF (partCode <> 0) AND (ctlHand = ControlHandle(itemHand)) THEN
BEGIN
IF partCode = inThumb THEN
BEGIN
thumbData.startPoint := where;
thumbData.limitRect.topLeft := where;
{Ask the scroll bar defProc to calculate the slopRect we'll need in the
action proc.}
dummyResult := CallControl(ctlHand, thumbCntl, LongInt(@thumbData));
SetCRefCon(ctlHand, LongInt(@thumbData));
upCode := TrackControl(ctlHand, where, @BrightThumbTrack);
BrightAdjust(theDialog, ctlHand, TRUE);
END
ELSE
BEGIN
PickInfoPtr(theDialog)^.whichPart := partCode;
upCode := TrackControl(ctlHand, where, @BrightTrackScroll);
END;
{Regardless of where the user moused up, we've handled the mouse
down; let the filter proc know so ModalDialog will ignore it.}
BrightTrack := TRUE;
END;
END;
{---------------------------------------------------------------------------------
BrightTrackScroll - Adjust the value of the scroll bar for page/line up/down.
}
PROCEDURE BrightTrackScroll(theControl: ControlHandle; partCode: INTEGER);
VAR
ctlVal: INTEGER;
delta: INTEGER;
newVal: LONGINT;
pickDlg: DialogPtr;
up: BOOLEAN;
BEGIN
pickDlg := FrontWindow;
WITH PickInfoPtr(pickDlg)^ DO
IF (whichPart = partCode) THEN
BEGIN
{Figure out whether the user wants to scroll up or down.}
IF partCode IN [inUpButton, inPageUp]
THEN delta := -lineBright
ELSE delta := lineBright;
{Change movement to a page if appropriate.}
IF partCode IN [inPageUp, inPageDown] THEN
delta := delta * pageBright;
{Hooray! Now do the scroll.}
ctlVal := GetCtlValue(theControl);
newVal := ORD4(ctlVal) + delta;
IF newVal > MAXINT THEN
newVal := MAXINT
ELSE IF newVal < (-MAXINT - 1) THEN
newVal := (-MAXINT - 1);
SetCtlValue(theControl, newVal);
BrightAdjust(pickDlg, theControl, FALSE);
END
END;
{---------------------------------------------------------------------------------
CallControl - Safely get the control variant and definition procedure and call the
control with the given message and parameter.
}
FUNCTION CallControl(theControl : ControlHandle; message : Integer;
param : LongInt) : LongInt;
VAR
savedPort : GrafPtr;
ctlProcHandle : Handle;
savedState : SignedByte;
BEGIN
CallControl := 0; {This is the default, in case of error.}
GetPort(savedPort);
SetPort(theControl^^.contrlOwner);
ctlProcHandle := Handle(StripAddress(Ptr(theControl^^.contrlDefProc)));
IF ctlProcHandle^ = NIL THEN
LoadResource(ctlProcHandle);
IF ctlProcHandle^ <> NIL THEN BEGIN
savedState := HGetState(ctlProcHandle);
HLock(ctlProcHandle);
CallControl := CallCDEFProc(GetCVariant(theControl), theControl, message, param, ctlProcHandle^);
HSetState(ctlProcHandle, savedState);
END;
SetPort(savedPort);
END;
{---------------------------------------------------------------------------------
CTabAdjust - adjust one or more of the colors in picker's color table
}
PROCEDURE CTabAdjust(pickDlg: DialogPtr; newBright: BOOLEAN);
VAR
c: PColor;
hsx: HSLColor;
update: BOOLEAN;
BEGIN
WITH PickInfoPtr(pickDlg)^
DO
BEGIN
IF colorOK
THEN
BEGIN
WITH pickCTab^^, hsx
DO
BEGIN
IF newBright
THEN
FOR c := cInput TO lastColor
DO
BEGIN
update := TRUE;
CASE c OF
cSample:
hsx := theColor.hsx;
cInput:
update := FALSE;
cGray:
BEGIN
hue := 0;
saturation := 0;
END;
OTHERWISE {wheel colors}
BEGIN
IF wheelColors > minColors
THEN
BEGIN
hue := Fix2SmallFract(FixRatio(ORD(c) - ORD(cSlice1), wheelColors div 2)mod 65536);
IF(ORD(c) - ORD(cSlice1) >= wheelColors div 2)
THEN
saturation := 32767
ELSE
saturation := 65535;
END
ELSE
BEGIN
hue := Fix2SmallFract(FixRatio(ORD(c) - ORD(cSlice1), wheelColors));
saturation := 65535;
END
END
END; {case}
IF update
THEN
BEGIN
lightness := theColor.hsx.lightness;
HSx2RGB(hsx, ctTable[ORD(c)].RGB);
END;
END
{If the lightness is the same, only the color in the sample
swatch (the one the user is picking) needs adjustment,
and the new value is already computed.}
ELSE
ctTable[ORD(cSample)].RGB := theColor.rgb;
END; {with pickdlgptr}
{$IFC UsePalette}
AnimatePalette(pickDlg,pickCTab,0,0,pickCTab^^.ctSize+1);
{$ENDC}
END; {if colorOK}
END;
END;
{---------------------------------------------------------------------------------
CTabBuild - build picker's color table, and the list of CLUT entries to borrow
}
{$IFC UsePalette}
PROCEDURE CTabBuild(VAR pickData: PickInfo; inColor: RGBColor);
VAR
globRect: Rect;
BEGIN
WITH pickData
DO
BEGIN
{
First, make sure we have color. If not, we'll make do with black
and white, avoid calling any of color quickdraw, and building
the table is a null operation: we don't need it
Figure out whether or not we have color quickdraw, and enough
bits per pixel to really use color. For now, we check that the
machine has a Mac II or later ROM to see if color Quickdraw
is available. There should be a better way to do this
}
wheelColors := minColors;
pixSize := 1;
colorOK := (BAnd(IntPtr(ROM85)^, BNot($3FFF)) = 0);
IF colorOK
THEN
BEGIN
globRect := dlgRec.window.port.portRect; { figure global rect }
LocalToGlobal(globRect.topLeft);
LocalToGlobal(globRect.botRight);
itsGDev := GetMaxDevice(globRect); { find deepest device sect }
IF itsGDev = NIL { if no sect, fake it }
THEN
itsGDev := GetGDevice;
pixSize := PixMapHandle(itsGDev^^.gdPMap)^^.pixelSize;
gdType := itsGDev^^.gdType;
IF (gdType = clutType)
THEN
IF (pixSize >= 8)
THEN
wheelColors := maxColors
ELSE IF (pixSize = 2)
THEN
wheelColors := -1 { so all we get is input and sample }
END;
colorOK := colorOK AND (pixSize >= 2);
lastColor := PColor(ORD(cSlice1) + wheelColors - 1);
IF NOT colorOK
THEN
BEGIN
pickCTab := NIL;
pickPal := NIL;
lastColor := PColor(0);
EXIT (CTabBuild);
END;
{Allocate our color table.}
pickCTab := CTabHandle(NewHandle(
Sizeof(ColorTable) + (Sizeof(ColorSpec) * ORD(lastColor))));
{Set up our color table.}
WITH pickCTab^^
DO
BEGIN
ctSize := ORD(lastColor);
ctSeed := 0;
ctFlags := 0; { 1.2 }
END;
{Next, figure out which CLUT entries to borrow. For now, we get
the widest possible distribution (try to avoid conflicts) by
trying to match the hues at maximum brightness, the gray at
medium range, and the input and current sample as complements.
Surely there is better way to distribute the colors?}
pickCTab^^.ctTable[ORD(cInput)].rgb := inColor;
pickPal := NewPalette(pickCTab^^.ctSize+1,pickCTab,pmAnimated,0);
SetEntryUsage(pickPal,ORD(cInput),pmTolerant,0);
SetPalette(WindowPtr(@dlgRec),pickPal,TRUE);
CTabAdjust(DialogPtr(@pickData), TRUE);
END;
END;
{$ELSEC} {UsePalette}
PROCEDURE CTabBuild(VAR pickData: PickInfo; inColor: RGBColor);
CONST
HiliteRGB = $DA0;
VAR
c: PColor;
hiliteColor: INTEGER;
nextTry: INTEGER;
saveLight: SmallFract;
skipsAllowed: INTEGER;
tempColor: RGBColor;
saveGDev: GDHandle;
globRect: Rect; { rect to globalize port in, for maxDev }
FUNCTION BadChoice(index: INTEGER; c: PColor; doSkip: BOOLEAN): BOOLEAN;
BEGIN
IF index = hiliteColor THEN
BadChoice := TRUE
ELSE
BadChoice := FALSE;
END;
FUNCTION Conflict(index: INTEGER; c: PColor): BOOLEAN;
VAR
p: PColor;
BEGIN
{Return TRUE iff index is used in any earlier table entry.}
IF BadChoice(index, c, FALSE) THEN
Conflict := TRUE
ELSE
BEGIN
Conflict := FALSE;
WITH pickData.pickReqList^^ DO
FOR p := cInput TO PRED(c) DO
IF reqLData[ORD(p)] = index THEN
BEGIN
Conflict := TRUE;
EXIT (Conflict);
END;
END;
END;
FUNCTION Taken(index: INTEGER; c: PColor): BOOLEAN;
VAR
p: PColor;
BEGIN
{Return TRUE iff index, an entry we're considering stealing, is
already in the table, and taking it would cause a conflict.}
IF BadChoice(index, c, TRUE) THEN
Taken := TRUE
ELSE
BEGIN