-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathplctrl.c
More file actions
3153 lines (2741 loc) · 91.3 KB
/
Copy pathplctrl.c
File metadata and controls
3153 lines (2741 loc) · 91.3 KB
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
// Misc. control routines, like begin, end, exit, change graphics/text
// mode, change color. Includes some spillage from plcore.c. If you
// don't know where it should go, put it here.
//
// Copyright (C) 2004 Joao Cardoso
// Copyright (C) 2004 Rafael Laboissiere
// Copyright (C) 2008 Hazen Babcock
// Copyright (C) 2009-2014 Alan W. Irwin
// Copyright (C) 2011 Hezekiah M. Carty
//
// This file is part of PLplot.
//
// PLplot is free software; you can redistribute it and/or modify
// it under the terms of the GNU Library General Public License as published
// by the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// PLplot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public License
// along with PLplot; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
//
//! @file
//!
//! Part 1: Color map routines.
//! Part 2: "A grab-bag of various control routines".
//!
#define DEBUG
#define NEED_PLDEBUG
#include "plplotP.h"
#ifdef macintosh
#include "mac.h"
// for plMacLibOpen prototype; used in plLibOpen
#endif
#ifdef DJGPP // dos386/djgpp
#ifdef __unix
#undef __unix
#endif
#endif
#ifdef __unix
#include <sys/types.h>
#include <sys/stat.h>
#ifdef PL_HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <errno.h>
#endif
// Random number generator (Mersenne Twister)
#include "mt19937ar.h"
#define BUFFER_SIZE 256
#define COLLEN 30
#define PALLEN 160
#define MSGLEN 1024
// small epsilon for fuzzy range checks that is still large enough to
// work even in the single precision floating point case.
#define FUZZ_EPSILON 1.e-4
// Static functions
// Used by any external init code to suggest a path
char PLDLLIMPEXP * plplotLibDir = 0;
static void
color_set( PLINT i, U_CHAR r, U_CHAR g, U_CHAR b, PLFLT a, const char *name );
static void
strcat_delim( char *dirspec );
static int
( *exit_handler )( const char *errormsg );
static void
( *abort_handler )( const char *errormsg );
static void
plcmap0_def( int imin, int imax );
static void
plcmap1_def( void );
static PLFLT
value( double n1, double n2, double hue );
static char *
read_line( char *buffer, int length, FILE *fp );
static void
cmap0_palette_read( const char *filename,
int *number_colors, unsigned int **r, unsigned int **g,
unsigned int **b, double **a );
// An additional hardwired location for lib files.
// I have no plans to change these again, ever.
#if defined ( DJGPP )
#ifndef PLLIBDEV
#define PLLIBDEV "c:/plplot/lib"
#endif
#elif defined ( MSDOS )
#ifndef PLLIBDEV
#define PLLIBDEV "c:\\plplot\\lib"
#endif
#else
// Anything else is assumed to be Unix
#ifndef PLLIBDEV
#define PLLIBDEV "/usr/local/plplot/lib"
#endif
#endif
//--------------------------------------------------------------------------
// Routines that deal with colors & color maps.
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
// plcol0()
//
//! Set color, map 0. Argument is a integer between 0 and plsc->ncol0.
//!
//! @param icol0 The index of the color map 0 color to use as the current
//! color. (0 - plsc->ncol0).
void
c_plcol0( PLINT icol0 )
{
if ( plsc->level < 1 )
{
plabort( "plcol0: Please call plinit first" );
return;
}
if ( icol0 < 0 || icol0 >= plsc->ncol0 )
{
char buffer[BUFFER_SIZE];
snprintf( buffer, BUFFER_SIZE, "plcol0: Invalid color map entry: %d", (int) icol0 );
plabort( buffer );
return;
}
plsc->icol0 = icol0;
plsc->curcolor.r = plsc->cmap0[icol0].r;
plsc->curcolor.g = plsc->cmap0[icol0].g;
plsc->curcolor.b = plsc->cmap0[icol0].b;
plsc->curcolor.a = plsc->cmap0[icol0].a;
plsc->curcmap = 0;
plP_state( PLSTATE_COLOR0 );
}
//--------------------------------------------------------------------------
// plcol1()
//
//! Set color, map 1. Argument is a float between 0. and 1.
//!
//! @param col1 The index of the color map 1 color to use as the current
//! color. (0.0 - 1.0)
void
c_plcol1( PLFLT col1 )
{
PLINT icol1;
if ( plsc->level < 1 )
{
plabort( "plcol1: Please call plinit first" );
return;
}
if ( col1 < 0 || col1 > 1 || isnan( col1 ) )
{
char buffer[BUFFER_SIZE];
snprintf( buffer, BUFFER_SIZE, "plcol1: Invalid color map position: %f", (PLFLT) col1 );
plabort( buffer );
return;
}
icol1 = (PLINT) ( col1 * plsc->ncol1 );
icol1 = MIN( icol1, plsc->ncol1 - 1 );
plsc->icol1 = icol1;
plsc->curcolor.r = plsc->cmap1[plsc->icol1].r;
plsc->curcolor.g = plsc->cmap1[plsc->icol1].g;
plsc->curcolor.b = plsc->cmap1[plsc->icol1].b;
plsc->curcolor.a = plsc->cmap1[plsc->icol1].a;
plsc->curcmap = 1;
plP_state( PLSTATE_COLOR1 );
}
//--------------------------------------------------------------------------
// plscolbg()
//
//! Set the background color (cmap0[0]) by 8 bit RGB value
//!
//! @param r Red value of the background color (0 - 255).
//! @param g Green value of the background color (0 - 255).
//! @param b Blue value of the background color (0 - 255).
void
c_plscolbg( PLINT r, PLINT g, PLINT b )
{
plscol0( 0, r, g, b );
}
//--------------------------------------------------------------------------
// plscolbga()
//
//! Set the background color (cmap0[0]) by 8 bit RGB value and alpha value
//!
//! @param r Red value of the background color (0 - 255).
//! @param g Green value of the background color (0 - 255).
//! @param b Blue value of the background color (0 - 255).
//! @param alpha Alpha (transparency) value of the background color
//! (0.0 - 1.0).
//--------------------------------------------------------------------------
void
c_plscolbga( PLINT r, PLINT g, PLINT b, PLFLT alpha )
{
plscol0a( 0, r, g, b, alpha );
}
//--------------------------------------------------------------------------
// plgcolbg()
//
//! Returns the background color (cmap0[0]) by 8 bit RGB value
//!
//! @param r Current red value of the background color.
//! @param g Current green value of the background color.
//! @param b Current blue value of the background color.
void
c_plgcolbg( PLINT *r, PLINT *g, PLINT *b )
{
plgcol0( 0, r, g, b );
}
//--------------------------------------------------------------------------
// plgcolbga()
//
//! Returns the background color (cmap0[0]) by 8 bit RGB value and alpha value
//!
//! @param r Current red value of the background color.
//! @param g Current green value of the background color.
//! @param b Current blue value of the background color.
//! @param alpha Current alpha value of the background color.
void
c_plgcolbga( PLINT *r, PLINT *g, PLINT *b, PLFLT *alpha )
{
plgcol0a( 0, r, g, b, alpha );
}
//--------------------------------------------------------------------------
// plscol0()
//
//! Set a given color from color map 0 by 8 bit RGB value
//! Does not result in any additional cells to be allocated.
//!
//! @param icol0 index of the color to set (0 - plsc->ncol0)
//! @param r Red value of the color (0 - 255).
//! @param g Green value of the color (0 - 255).
//! @param b Blue value of the color (0 - 255).
void
c_plscol0( PLINT icol0, PLINT r, PLINT g, PLINT b )
{
if ( plsc->cmap0 == NULL )
plscmap0n( 0 );
if ( icol0 < 0 || icol0 >= plsc->ncol0 )
{
char buffer[BUFFER_SIZE];
snprintf( buffer, BUFFER_SIZE, "plscol0: Illegal color table value: %d", (int) icol0 );
plabort( buffer );
return;
}
if ( ( r < 0 || r > 255 ) || ( g < 0 || g > 255 ) || ( b < 0 || b > 255 ) )
{
char buffer[BUFFER_SIZE];
snprintf( buffer, BUFFER_SIZE, "plscol0: Invalid RGB color: %d, %d, %d",
(int) r, (int) g, (int) b );
plabort( buffer );
return;
}
plscol0a( icol0, r, g, b, 1.0 );
}
//--------------------------------------------------------------------------
// plscol0a()
//
//! Set a given color from color map 0 by 8 bit RGB value and alpha value.
//! Does not result in any additional cells to be allocated.
//!
//! @param icol0 index of the color to set (0 - plsc->ncol0)
//! @param r Red value of the color (0 - 255).
//! @param g Green value of the color (0 - 255).
//! @param b Blue value of the color (0 - 255).
//! @param alpha Alpha value of the color (0.0 - 1.0).
void
c_plscol0a( PLINT icol0, PLINT r, PLINT g, PLINT b, PLFLT alpha )
{
if ( plsc->cmap0 == NULL )
plscmap0n( 0 );
if ( icol0 < 0 || icol0 >= plsc->ncol0 )
{
char buffer[BUFFER_SIZE];
snprintf( buffer, BUFFER_SIZE, "plscol0a: Illegal color table value: %d", (int) icol0 );
plabort( buffer );
return;
}
if ( ( r < 0 || r > 255 ) || ( g < 0 || g > 255 ) || ( b < 0 || b > 255 ) || ( alpha < 0. || alpha > 1.0 ) )
{
char buffer[BUFFER_SIZE];
snprintf( buffer, BUFFER_SIZE, "plscol0a: Invalid RGB color: %d, %d, %d, %f",
(int) r, (int) g, (int) b, (double) alpha );
plabort( buffer );
return;
}
plsc->cmap0[icol0].r = (unsigned char) r;
plsc->cmap0[icol0].g = (unsigned char) g;
plsc->cmap0[icol0].b = (unsigned char) b;
plsc->cmap0[icol0].a = alpha;
if ( plsc->level > 0 )
plP_state( PLSTATE_CMAP0 );
}
//--------------------------------------------------------------------------
// plgcol0()
//
//! Returns 8 bit RGB values for given color from color map 0
//! Values are negative if an invalid color id is given
//!
//! @param icol0 Index of the color to be return (0 - plsc->ncol0).
//! @param r Current red value of the color.
//! @param g Current green value of the color.
//! @param b Current blue value of the color.
void
c_plgcol0( PLINT icol0, PLINT *r, PLINT *g, PLINT *b )
{
if ( plsc->cmap0 == NULL )
plscmap0n( 0 );
*r = -1;
*g = -1;
*b = -1;
if ( icol0 < 0 || icol0 > plsc->ncol0 )
{
char buffer[BUFFER_SIZE];
snprintf( buffer, BUFFER_SIZE, "plgcol0: Invalid color index: %d", (int) icol0 );
plabort( buffer );
return;
}
*r = plsc->cmap0[icol0].r;
*g = plsc->cmap0[icol0].g;
*b = plsc->cmap0[icol0].b;
return;
}
//--------------------------------------------------------------------------
// plgcol0a()
//
//! Returns 8 bit RGB values for given color from color map 0 and alpha value
//! Values are negative if an invalid color id is given
//!
//! @param icol0 Index of the color to be return (0 - plsc->ncol0).
//! @param r Current red value of the color.
//! @param g Current green value of the color.
//! @param b Current blue value of the color.
//! @param alpha Current alpha value of the color.
void
c_plgcol0a( PLINT icol0, PLINT *r, PLINT *g, PLINT *b, PLFLT *alpha )
{
if ( plsc->cmap0 == NULL )
plscmap0n( 0 );
*r = -1;
*g = -1;
*b = -1;
*alpha = -1.0;
if ( icol0 < 0 || icol0 > plsc->ncol0 )
{
char buffer[BUFFER_SIZE];
snprintf( buffer, BUFFER_SIZE, "plgcol0: Invalid color index: %d", (int) icol0 );
plabort( buffer );
return;
}
*r = plsc->cmap0[icol0].r;
*g = plsc->cmap0[icol0].g;
*b = plsc->cmap0[icol0].b;
*alpha = plsc->cmap0[icol0].a;
return;
}
//--------------------------------------------------------------------------
// plscmap0()
//
//! Set color map 0 colors by 8 bit RGB values. This sets the entire color
//! map -- only as many colors as specified will be allocated.
//!
//! @param r Array of red values.
//! @param g Array of green values.
//! @param b Array of blue values.
//! @param ncol0 Total number of RGB values.
void
c_plscmap0( const PLINT *r, const PLINT *g, const PLINT *b, PLINT ncol0 )
{
int i;
plscmap0n( ncol0 );
for ( i = 0; i < plsc->ncol0; i++ )
{
if ( ( r[i] < 0 || r[i] > 255 ) ||
( g[i] < 0 || g[i] > 255 ) ||
( b[i] < 0 || b[i] > 255 ) )
{
char buffer[BUFFER_SIZE];
snprintf( buffer, BUFFER_SIZE, "plscmap0: Invalid RGB color: %d, %d, %d",
(int) r[i], (int) g[i], (int) b[i] );
plabort( buffer );
return;
}
plsc->cmap0[i].r = (unsigned char) r[i];
plsc->cmap0[i].g = (unsigned char) g[i];
plsc->cmap0[i].b = (unsigned char) b[i];
plsc->cmap0[i].a = 1.0;
}
if ( plsc->level > 0 )
plP_state( PLSTATE_CMAP0 );
}
//--------------------------------------------------------------------------
// plscmap0a()
//
//! Set color map 0 colors by 8 bit RGB and alpha value. This sets the
//! entire color map -- only as many colors as specified will be allocated.
//!
//! @param r Array of red values.
//! @param g Array of green values.
//! @param b Array of blue values.
//! @param alpha Array of alpha values.
//! @param ncol0 Total number of RGBA values.
void
c_plscmap0a( const PLINT *r, const PLINT *g, const PLINT *b, const PLFLT *alpha, PLINT ncol0 )
{
int i;
plscmap0n( ncol0 );
for ( i = 0; i < plsc->ncol0; i++ )
{
if ( ( r[i] < 0 || r[i] > 255 ) ||
( g[i] < 0 || g[i] > 255 ) ||
( b[i] < 0 || b[i] > 255 ) ||
( alpha[i] < 0.0 || alpha[i] > 1.0 ) )
{
char buffer[BUFFER_SIZE];
snprintf( buffer, BUFFER_SIZE, "plscmap0a: Invalid RGB color: %d, %d, %d, %f",
(int) r[i], (int) g[i], (int) b[i], (double) alpha[i] );
plabort( buffer );
return;
}
plsc->cmap0[i].r = (unsigned char) r[i];
plsc->cmap0[i].g = (unsigned char) g[i];
plsc->cmap0[i].b = (unsigned char) b[i];
plsc->cmap0[i].a = alpha[i];
}
if ( plsc->level > 0 )
plP_state( PLSTATE_CMAP0 );
}
//--------------------------------------------------------------------------
// plscmap1()
//
//! Set color map 1 colors by 8 bit RGB values
//! This also sets the number of colors.
//!
//! @param r Array of red values.
//! @param g Array of green values.
//! @param b Array of blue values.
//! @param ncol1 Total number of RGB values.
void
c_plscmap1( const PLINT *r, const PLINT *g, const PLINT *b, PLINT ncol1 )
{
int i;
plscmap1n( ncol1 );
for ( i = 0; i < plsc->ncol1; i++ )
{
if ( ( r[i] < 0 || r[i] > 255 ) ||
( g[i] < 0 || g[i] > 255 ) ||
( b[i] < 0 || b[i] > 255 ) )
{
char buffer[BUFFER_SIZE];
snprintf( buffer, BUFFER_SIZE, "plscmap1: Invalid RGB color: %d, %d, %d",
(int) r[i], (int) g[i], (int) b[i] );
plabort( buffer );
return;
}
plsc->cmap1[i].r = (unsigned char) r[i];
plsc->cmap1[i].g = (unsigned char) g[i];
plsc->cmap1[i].b = (unsigned char) b[i];
plsc->cmap1[i].a = 1.0;
}
if ( plsc->level > 0 )
plP_state( PLSTATE_CMAP1 );
}
//--------------------------------------------------------------------------
// plscmap1a()
//
//! Set color map 1 colors by 8 bit RGB and alpha values
//! This also sets the number of colors.
//!
//! @param r Array of red values.
//! @param g Array of green values.
//! @param b Array of blue values.
//! @param alpha Array of alpha values.
//! @param ncol1 Total number of RGBA values.
void
c_plscmap1a( const PLINT *r, const PLINT *g, const PLINT *b, const PLFLT *alpha, PLINT ncol1 )
{
int i;
plscmap1n( ncol1 );
for ( i = 0; i < plsc->ncol1; i++ )
{
if ( ( r[i] < 0 || r[i] > 255 ) ||
( g[i] < 0 || g[i] > 255 ) ||
( b[i] < 0 || b[i] > 255 ) ||
( alpha[i] < 0.0 || alpha[i] > 1.0 ) )
{
char buffer[BUFFER_SIZE];
snprintf( buffer, BUFFER_SIZE, "plscmap1a: Invalid RGB color: %d, %d, %d, %f",
(int) r[i], (int) g[i], (int) b[i], (double) alpha[i] );
plabort( buffer );
return;
}
plsc->cmap1[i].r = (unsigned char) r[i];
plsc->cmap1[i].g = (unsigned char) g[i];
plsc->cmap1[i].b = (unsigned char) b[i];
plsc->cmap1[i].a = alpha[i];
}
if ( plsc->level > 0 )
plP_state( PLSTATE_CMAP1 );
}
//--------------------------------------------------------------------------
// plscmap1l()
//
//! Set color map 1 colors using a piece-wise linear relationship between
//! position in the color map (from 0 to 1) and position in HLS or RGB color
//! space. May be called at any time.
//!
//! The idea here is to specify a number of control points that specify the
//! mapping between HLS (or RGB or CMY) and palette 1 value. Between these
//! points, linear interpolation is used. By mapping position in the color
//! map to function value, this gives a smooth variation of color with
//! intensity. Any number of control points may be specified, located at
//! arbitrary positions (intensities), although typically 2 - 4 are enough.
//! Another way of stating this is that we are traversing a given number of
//! lines through HLS (or RGB) space as we move through cmap 1 entries. The
//! control points at the minimum and maximum intensity (0 and 1) must
//! always be specified. By adding more control points you can get more
//! variation. One good technique for plotting functions that vary about
//! some expected average is to use an additional 2 control points in the
//! center (intensity ~= 0.5) that are the same color as the background
//! (typically white for paper output, black for crt), and same hue as the
//! boundary control points. This allows the highs and lows to be very
//! easily distinguished.
//!
//! Each control point must specify the position in cmap 1 as well as three
//! coordinates in HLS or RGB space. The first point MUST correspond to
//! position = 0, and the last to position = 1.
//!
//! Every change in hue from one control point to the next can be linearly
//! interpolated in two ways. The usual (alt_hue_path[i] false) method for the ith interval
//! uses the dh = h[i+1] - h[i] interval for interpolation. The alternate (alt_hue_path true) method for the ith interval uses the dh = (h[i+1] - h[i]) - 360 if (h[i+1] - h[i]) is positive or dh = 360 - (h[i+1] - h[i]) if (h[i+1] - h[i]) is negative interval for the interpolation. Thus, alt_hue_path true interpolation intervals always include hue = 0.
//! Specifying
//! alt_hue_path=NULL is equivalent to setting alt_hue_path[]=false for every control point.
//!
//! Bounds on RGB coordinates:
//! R,G,B [0, 1] magnitude
//!
//! Bounds on HLS coordinates:
//! hue [0, 360] degrees
//! lightness [0, 1] magnitude
//! saturation [0, 1] magnitude
//!
//! The inputs are:
//! @param itype 0: HLS, 1: RGB
//! @param npts number of control points
//! @param intensity[] intensity index for each control point
//! @param coord1[] first coordinate for each control point
//! @param coord2[] second coordinate for each control point
//! @param coord3[] third coordinate for each control point
//! @param alt_hue_path[] if true, use alternative hue interpolation path
//! for the associated interval.
void
c_plscmap1l( PLINT itype, PLINT npts, const PLFLT *intensity,
const PLFLT *coord1, const PLFLT *coord2, const PLFLT *coord3, const PLINT *alt_hue_path )
{
int n;
PLFLT h, l, s, r, g, b;
if ( npts < 2 )
{
plabort( "plscmap1l: Must specify at least two control points" );
return;
}
if ( ( intensity[0] != 0 ) || ( intensity[npts - 1] != 1 ) )
{
plabort( "plscmap1l: First, last control points must lie on boundary" );
return;
}
if ( npts > PL_MAX_CMAP1CP )
{
plabort( "plscmap1l: exceeded maximum number of control points" );
return;
}
// Allocate if not done yet
if ( plsc->cmap1 == NULL )
plscmap1n( 0 );
// Save control points
plsc->ncp1 = npts;
for ( n = 0; n < npts; n++ )
{
if ( itype == 0 )
{
h = coord1[n];
l = coord2[n];
s = coord3[n];
}
else
{
r = coord1[n];
g = coord2[n];
b = coord3[n];
c_plrgbhls( r, g, b, &h, &l, &s );
}
plsc->cmap1cp[n].h = h;
plsc->cmap1cp[n].l = l;
plsc->cmap1cp[n].s = s;
plsc->cmap1cp[n].p = intensity[n];
plsc->cmap1cp[n].a = 1.0;
if ( alt_hue_path == NULL )
plsc->cmap1cp[n].alt_hue_path = 0;
else
if ( n != npts - 1 )
plsc->cmap1cp[n].alt_hue_path = alt_hue_path[n];
else
// Note final element is unused, so we set to zero for completeness.
plsc->cmap1cp[n].alt_hue_path = 0;
}
// Calculate and set color map
plcmap1_calc();
}
//--------------------------------------------------------------------------
// plscmap1la()
//
//! This is the same as plscmap1l, but also allows alpha value interpolation.
//!
//! @param itype 0: HLS, 1: RGB
//! @param npts number of control points
//! @param intensity[] intensity index for each control point
//! @param coord1[] first coordinate for each control point
//! @param coord2[] second coordinate for each control point
//! @param coord3[] third coordinate for each control point
//! @param alpha[] alpha value for each control point
//! @param alt_hue_path[] if true, use alternative hue interpolation path
//! for the associated interval.
void
c_plscmap1la( PLINT itype, PLINT npts, const PLFLT *intensity,
const PLFLT *coord1, const PLFLT *coord2, const PLFLT *coord3, const PLFLT *alpha, const PLINT *alt_hue_path )
{
int n;
PLFLT h, l, s, r, g, b;
if ( npts < 2 )
{
plabort( "plscmap1la: Must specify at least two control points" );
return;
}
if ( ( intensity[0] != 0 ) || ( intensity[npts - 1] != 1 ) )
{
plabort( "plscmap1la: First, last control points must lie on boundary" );
return;
}
if ( npts > PL_MAX_CMAP1CP )
{
plabort( "plscmap1la: exceeded maximum number of control points" );
return;
}
// Allocate if not done yet
if ( plsc->cmap1 == NULL )
plscmap1n( 0 );
// Save control points
plsc->ncp1 = npts;
for ( n = 0; n < npts; n++ )
{
if ( itype == 0 )
{
h = coord1[n];
l = coord2[n];
s = coord3[n];
}
else
{
r = coord1[n];
g = coord2[n];
b = coord3[n];
c_plrgbhls( r, g, b, &h, &l, &s );
}
plsc->cmap1cp[n].h = h;
plsc->cmap1cp[n].l = l;
plsc->cmap1cp[n].s = s;
plsc->cmap1cp[n].p = intensity[n];
plsc->cmap1cp[n].a = alpha[n];
if ( alt_hue_path == NULL )
plsc->cmap1cp[n].alt_hue_path = 0;
else
if ( n != npts - 1 )
plsc->cmap1cp[n].alt_hue_path = alt_hue_path[n];
else
// Note final element is unused, so we set to zero for completeness.
plsc->cmap1cp[n].alt_hue_path = 0;
}
// Calculate and set color map
plcmap1_calc();
}
//--------------------------------------------------------------------------
// plcmap1_calc()
//
//! Bin up cmap 1 space and assign colors to make inverse mapping easy.
//! Always do interpolation in HLS space.
void
plcmap1_calc( void )
{
int i, n;
PLFLT delta, dp, dh, dl, ds, da;
PLFLT h, l, s, p, r, g, b, a;
// Loop over all control point pairs
for ( n = 0; n < plsc->ncp1 - 1; n++ )
{
if ( plsc->cmap1cp[n].p == plsc->cmap1cp[n + 1].p )
continue;
// Differences in p, h, l, s between ctrl pts
dp = plsc->cmap1cp[n + 1].p - plsc->cmap1cp[n].p;
dh = plsc->cmap1cp[n + 1].h - plsc->cmap1cp[n].h;
dl = plsc->cmap1cp[n + 1].l - plsc->cmap1cp[n].l;
ds = plsc->cmap1cp[n + 1].s - plsc->cmap1cp[n].s;
da = plsc->cmap1cp[n + 1].a - plsc->cmap1cp[n].a;
// Adjust dh if we are to go around "the back side"
if ( plsc->cmap1cp[n].alt_hue_path )
dh = ( dh > 0 ) ? dh - 360 : dh + 360;
// Loop over all color cells. Only interested in cells located (in
// cmap1 space) between n_th and n+1_th control points
for ( i = 0; i < plsc->ncol1; i++ )
{
p = (double) i / ( plsc->ncol1 - 1.0 );
if ( ( p < plsc->cmap1cp[n].p ) ||
( p > plsc->cmap1cp[n + 1].p ) )
continue;
// Interpolate based on position of color cell in cmap1 space
delta = ( p - plsc->cmap1cp[n].p ) / dp;
// Linearly interpolate to get color cell h, l, s values
h = plsc->cmap1cp[n].h + dh * delta;
l = plsc->cmap1cp[n].l + dl * delta;
s = plsc->cmap1cp[n].s + ds * delta;
a = plsc->cmap1cp[n].a + da * delta;
while ( h >= 360. )
h -= 360.;
while ( h < 0. )
h += 360.;
c_plhlsrgb( h, l, s, &r, &g, &b );
plsc->cmap1[i].r = (unsigned char) MAX( 0, MIN( 255, (int) ( 256. * r ) ) );
plsc->cmap1[i].g = (unsigned char) MAX( 0, MIN( 255, (int) ( 256. * g ) ) );
plsc->cmap1[i].b = (unsigned char) MAX( 0, MIN( 255, (int) ( 256. * b ) ) );
plsc->cmap1[i].a = a;
}
}
if ( plsc->level > 0 )
plP_state( PLSTATE_CMAP1 );
}
//--------------------------------------------------------------------------
//! Set the color map 1 value range to use in continuous color plots.
//!
//! @param min_color Specifies the minimum color to use. A value of 0.0 or
//! less indicates that the range should start at the lowest color map 1
//! value available.
//! @param max_color Specifies the maximum color to use. A value of 1.0 or
//! greater indicates that the range should exten to the highest color map 1
//! value available.
//!
//! If min_color > max_color or min_color is greater than 1.0 or max_color is
//! less than 0.0 then no change is made.
//--------------------------------------------------------------------------
void
c_plscmap1_range( PLFLT min_color, PLFLT max_color )
{
if ( min_color > max_color || max_color < 0.0 || min_color > 1.0 )
{
plwarn( "plscmap1_range called with invalid color range" );
return;
}
if ( min_color < 0.0 )
{
plwarn( "plscmap1_range called with a negative minimum color value" );
min_color = 0.0;
}
if ( max_color > 1.0 )
{
plwarn( "plscmap1_range called with an out of range maximum color value" );
max_color = 1.0;
}
plsc->cmap1_min = min_color;
plsc->cmap1_max = max_color;
}
//--------------------------------------------------------------------------
//! Get the color map 1 value range used in continuous color plots.
//!
//! @param min_color Specifies the minimum color used.
//! @param max_color Specifies the maximum color used.
//--------------------------------------------------------------------------
void
c_plgcmap1_range( PLFLT *min_color, PLFLT *max_color )
{
*min_color = plsc->cmap1_min;
*max_color = plsc->cmap1_max;
}
//--------------------------------------------------------------------------
// plscmap0n()
//
//! Set number of colors in cmap 0, (re-)allocate cmap 0, and fill with
//! default values for those colors not previously allocated (and less
//! than index 15, after that you just get grey).
//!
//! The driver is not guaranteed to support all of these.
//!
//! @param ncol0 Total number of colors.
void
c_plscmap0n( PLINT ncol0 )
{
int ncol, size, imin, imax;
// No change
if ( ncol0 > 0 && plsc->ncol0 == ncol0 )
return;
// Handle all possible startup conditions
if ( plsc->ncol0 <= 0 && ncol0 <= 0 )
ncol = 16;
else if ( ncol0 <= 0 )
ncol = plsc->ncol0;
else
ncol = ncol0;
imax = ncol - 1;
size = ncol * (int) sizeof ( PLColor );
// Allocate the space
if ( plsc->cmap0 == NULL )
{
if ( ( plsc->cmap0 = (PLColor *) calloc( 1, (size_t) size ) ) == NULL )
{
plexit( "c_plscmap0n: Insufficient memory" );
}
imin = 0;
}
else
{
if ( ( plsc->cmap0 = (PLColor *) realloc( plsc->cmap0, (size_t) size ) ) == NULL )
{
plexit( "c_plscmap0n: Insufficient memory" );
}
imin = plsc->ncol0;
}
// Fill in default entries
plsc->ncol0 = ncol;
plcmap0_def( imin, imax );
if ( plsc->level > 0 )
plP_state( PLSTATE_CMAP0 );
}
//--------------------------------------------------------------------------
// color_set()
//
//! Initializes color table 0 entry by RGB values.
//!
//! @param i Index of the color.
//! @param r Red value of the color.
//! @param g Green value of the color.
//! @param b Blue value of the color.
//! @param a Alpha value of the color.
//! @param name The name of the color.
void
color_set( PLINT i, U_CHAR r, U_CHAR g, U_CHAR b, PLFLT a, const char *name )
{
plsc->cmap0[i].r = r;
plsc->cmap0[i].g = g;
plsc->cmap0[i].b = b;
plsc->cmap0[i].a = a;
plsc->cmap0[i].name = name;