summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/generic/pgfplots/util/pgfplotscolormap.code.tex
blob: fe79bad26654e9da9d8ee337635b7ed89eea4e5d (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
%--------------------------------------------
% $Header: /cvsroot/pgfplots/pgfplots/generic/pgfplots/util/pgfplotscolormap.code.tex,v 1.15 2009/07/21 18:18:48 ludewich Exp $
%
% Package pgfplots
%
% Provides a user-friendly interface to create function plots (normal
% plots, semi-logplots and double-logplots).
% 
% It is based on Till Tantau's PGF package.
%
% Copyright 2007-2013 by Christian Feuersänger.
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
% 
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
% 
% You should have received a copy of the GNU General Public License
% along with this program.  If not, see <http://www.gnu.org/licenses/>.
%
%--------------------------------------------

\input pgfplotscolor.code.tex

\pgfkeyssetvalue{/pgfplots/colormap default colorspace}{auto}

% This package relies on pgfplots temporary registers and its array
% data structure.

% Internal structure:
% the colormap data structure consists of
% - an array 'pgfpl@cm@#1' of RGB color triples,
% - numbers 'pgfpl@cm@#1@h' and 'pgfpl@cm@#1@invh' for the mesh width
%   and inverse mesh width.

% Creates a new colormap.
%
% #1 : the name of the color map as string (not as macro).
% #2 : a <color specification> as is expected for a shading spec of
% pgf. However, the format allows a little bit more freedom:
% 	- it supports 'rgb255' in addition to pgf,
% 	- the length is not required (defaults to 1cm for the first and to
% 	the last length+h for all others).
% 	- the semicolons can be omitted.
%
% Example: 
% \pgfplotscreatecolormap{my map}{rgb(0cm)=(1,0,0); rgb(1cm)=(0,1,0); rgb255(1.5cm)=(128,5,255); rgb(2cm)=(0,0,1); gray(3cm)=(3);  color(4cm)=(green); }
% \pgfplotscreatecolormap{my map}{color=(green); color=(red); color=(blue); color=(yellow)}
\def\pgfplotscreatecolormap#{\pgfplots@createcolormap}%
\def\pgfplots@createcolormap#1#2{%
	\edef\pgfplots@createcolormap@name{pgfpl@cm@#1}%
	\expandafter\pgfplotsarraynewempty\expandafter{\pgfplots@createcolormap@name}%
	\def\pgfplots@createcolormap@MIN{0}% NORMALIZATION: assume that lower-end is 0pt.
	\def\pgfplots@createcolormap@MAX{0}% To be computed.
	\pgfplots@createcolormap@initcolorspace
	\let\pgfplots@createcolormap@LAST=\pgfplots@createcolormap@MIN
	% PARSE IT:
	\edef\pgfplots@loc@TMPa{#2}%
	\let\pgfplots@createcolormap@context=\pgfutil@empty
	% this does also init @H:
	\expandafter\pgfplots@createcolormap@startloop\pgfplots@loc@TMPa\pgfplots@EOI
	\expandafter\pgfplotsarraycheckempty\expandafter{\pgfplots@createcolormap@name}%
	% sanity checking:
	\expandafter\pgfplotsarraysizetomacro\expandafter{\pgfplots@createcolormap@name}\to\pgfplots@loc@TMPa
	\ifcase\pgfplots@loc@TMPa\relax
		\pgfplots@error{Sorry, you need to provide at least two points of a colormap.}%
	\or
		\pgfplots@error{Sorry, you need to provide at least two points of a colormap.}%
	\fi
	% Map the input range [0pt,MAX] linearly to [0,1000]
	\pgfmathdivide@{\pgfplotscolormaprange}{\pgfplots@createcolormap@MAX}%
	\let\pgfplots@loc@TMPb=\pgfmathresult
	\pgfmathmultiply@{\pgfplots@loc@TMPb}{\pgfplots@createcolormap@H}%
	\let\pgfplots@createcolormap@H=\pgfmathresult
	\expandafter\let\csname\pgfplots@createcolormap@name @h\endcsname=\pgfplots@createcolormap@H
	\pgfmathreciprocal@{\pgfplots@createcolormap@H}%
	\expandafter\let\csname\pgfplots@createcolormap@name @invh\endcsname=\pgfmathresult
	%
	\endpgfplotscolornormalizesequence%
	\expandafter\let\csname\pgfplots@createcolormap@name @colspace\endcsname=\pgfplotsretval
	\expandafter\let\csname\pgfplots@createcolormap@name @col@comps\endcsname=\pgfplotsretvalb
	\c@pgf@counta=\expandafter\pgfplotsarraysizeof\expandafter{\pgfplots@createcolormap@name}\relax
	\advance\c@pgf@counta by-1 %
	\expandafter\edef\csname\pgfplots@createcolormap@name @lastindex\endcsname{\the\c@pgf@counta}%
%\pgfplots@colormap@showdebuginfofor{#1}%
	\pgfplots@colormap@LUA@new{#1}%
}


\def\pgfplots@createcolormap@initcolorspace{%
	\pgfplotscolornormalizesequence[
		colorspace=\pgfkeysvalueof{/pgfplots/colormap default colorspace},
		context message=\pgfplots@createcolormap@context,
	]%
}%
\def\pgfplots@createcolormap@seth[#1]{%
	\def\pgfplots@createcolormap@context{[#1]}%
	\pgfmathparse{#1}%
	\let\pgfplots@createcolormap@H=\pgfmathresult
	\pgfplots@createcolormap@
}%

\def\pgfplots@createcolormap@startloop{%
	\pgfutil@ifnextchar[%
		{\pgfplots@createcolormap@seth}%
		{%
			\let\pgfplots@createcolormap@H=\pgfutil@empty
		 	\pgfplots@createcolormap@
		}%
}%
\def\pgfplots@createcolormap@{%
	\pgfutil@ifnextchar\pgfplots@EOI{\pgfutil@gobble}%done!
	{%
		\pgfutil@ifnextchar;{\pgfplots@createcolormap@grabsemicolon}%
		{%
			\expandafter\pgfutil@ifnextchar\pgfplots@activesemicolon{\pgfplots@createcolormap@grabsemicolon@active}%
			{%
				\pgfplots@createcolormap@grabnext
			}%
		}%
	}%
}
\def\pgfplots@createcolormap@error#1#2\pgfplots@EOI{%
	{%
		\t@pgfplots@toka={#1#2}%
		\t@pgfplots@tokb={#1}%
		\pgfplots@error{Illformed colormap specification: I could not read the substring `\the\t@pgfplots@toka' starting at `\the\t@pgfplots@tokb'}%
	}%
}%

\def\pgfplots@createcolormap@grabnext#1({%
	\pgfplots@createcolormap@grabnext@{#1}%
}
\def\pgfplots@createcolormap@grabnext@#1#2){%
	\pgfutil@in@={#1}%
	\ifpgfutil@in@
		% Ah. we do not have a position, i.e. we have
		%   color=(green)
		% or something like this.
		%
		% this here defines \pgfplots@loc@TMPa to contain the
		% colorspace:
		\pgfplots@createcolormap@grabnext@remove@equal@sign#1\pgfplots@EOI
		%
		\pgfplots@createcolormap@grabnext@computenextposition
		\let\pgfplots@loc@TMPb=\pgfmathresult% posisiton
		\def\pgfplots@loc@TMPc{#2}% color data
		\let\pgfplots@loc@TMPd=\pgfplots@createcolormap@grabnext@complete
	\else
		% Ah. We have something like 'color(1cm)' and we did not see
		% the equal sign so far.
		\def\pgfplots@loc@TMPa{#1}% colorspace
		\def\pgfplots@loc@TMPb{#2}% position
		%
		% and collect the color data:
		\def\pgfplots@loc@TMPd{%
			\pgfutil@ifnextchar={%
				\pgfplots@createcolormap@grabnext@@
			}{%
				\pgfplots@createcolormap@error#1(#2)%
			}%
		}%
	\fi
	\pgfplots@loc@TMPd
}%

\def\pgfplots@createcolormap@grabnext@remove@equal@sign#1=#2\pgfplots@EOI{%
	\def\pgfplots@loc@TMPa{#1}%
	%
	% FIXME : what if #2 is not empty!?
}%

\def\pgfplots@createcolormap@grabnext@@={%
	\pgfutil@ifnextchar({%
		\pgfplots@createcolormap@grabnext@@@
	}{%
		\pgfplots@createcolormap@error=%
	}%
}%
\def\pgfplots@createcolormap@grabnext@@@(#1){%
	\def\pgfplots@loc@TMPc{#1}%
	\pgfplots@createcolormap@grabnext@complete
}%

%%%%%%%%%%

% defines \pgfplotsretval to contain the argument and \pgfplotsretvalb
% to contain the colormap name.
\def\pgfplots@createcolormap@special@split#1{%
	\pgfutil@in@{of }{#1}%
	\ifpgfutil@in@
		\pgfplots@createcolormap@special@split@#1\pgfplots@EOI
	\else
		\pgfkeys@spdef\pgfplotsretval{#1}%
		\pgfkeysgetvalue{/pgfplots/colormap name}\pgfplotsretvalb
	\fi
	\pgfplots@createcolormap@special@ensure@source@cm{\pgfplotsretvalb}%
}%
\def\pgfplots@createcolormap@special@split@#1of #2\pgfplots@EOI{%
	\pgfkeys@spdef\pgfplotsretval{#1}%
	\pgfkeys@spdef\pgfplotsretvalb{#2}%
}
\def\pgfplots@createcolormap@special@ensure@source@cm#1{%
	\pgfplotscolormapifdefined{#1}{%
	}{%
		\pgfplots@error{The argument '#1' is no valid colormap.}%
	}%
}%

% defines \pgfplotsretval to contain an expanded color definition
% series
% #1: position
% #2: argument(s)
\expandafter\def\csname pgfplots@createcolormap@grabnextspecial@colors of colormap\endcsname#1#2{%
	\pgfplots@createcolormap@special@split{#2}%
	\pgfplots@createcolormap@special@process{#1}{color of colormap}%
%\message{colors of colormap(#1)=(#2): resulted in \meaning\pgfplotsretval^^J}%
}
\expandafter\def\csname pgfplots@createcolormap@grabnextspecial@samples of colormap\endcsname#1#2{%
	\pgfplots@createcolormap@special@split{#2}%
	\begingroup
	\let\pgfplots@source=\pgfplotsretval
	\let\pgfplots@sourcecm=\pgfplotsretvalb
	\pgfplots@domain@to@foreach 0:1000\relax{\pgfplots@source}%
	\t@pgfplots@toka=\expandafter{\pgfplotsretval}%
	\edef\pgfplotsretval{colors of colormap(#1)=({\the\t@pgfplots@toka} of \pgfplots@sourcecm)}%
	\pgfmath@smuggleone\pgfplotsretval
	\endgroup
%\message{samples of colormap(#1)=(#2): resulted in \meaning\pgfplotsretval^^J}%
}
\expandafter\def\csname pgfplots@createcolormap@grabnextspecial@indices of colormap\endcsname#1#2{%
	\pgfplots@createcolormap@special@split{#2}%
	\pgfplots@createcolormap@special@process{#1}{index of colormap}%
%\message{indices of colormap(#1)=(#2): resulted in \meaning\pgfplotsretval^^J}%
}
\expandafter\def\csname pgfplots@createcolormap@grabnextspecial@color of colormap\endcsname#1#2{%
	\begingroup
	% colormap access=map
	\def\pgfplots@color@of@colormap@access{m}%
	\pgfplots@color@of@colormap@define{#2}%
	\edef\pgfplotsretval{%
		\csname pgfpl@cm@\pgfplots@color@of@colormap@name @colspace\endcsname(#1)=(\pgfmathresult)%
	}%
	\pgfmath@smuggleone\pgfplotsretval
	\endgroup
}
\expandafter\def\csname pgfplots@createcolormap@grabnextspecial@index of colormap\endcsname#1#2{%
	\begingroup
	% colormap access=direct
	\def\pgfplots@color@of@colormap@access{d}%
	\pgfplots@color@of@colormap@define{#2}%
	\edef\pgfplotsretval{%
		\csname pgfpl@cm@\pgfplots@color@of@colormap@name @colspace\endcsname(#1)=(\pgfmathresult)%
	}%
	\pgfmath@smuggleone\pgfplotsretval
	\endgroup
}
\def\pgfplots@createcolormap@special@process#1#2{%
	\begingroup
	\let\pgfplots@source=\pgfplotsretval
	\let\pgfplots@sourcecm=\pgfplotsretvalb
	\def\pgfplots@temp@pos{(#1)}%
	%
	\pgfplotsapplistXnewempty\pgfplots@createcolormap@special@process@result
	\def\pgfplots@loc@TMPa{\pgfplotsforeachungrouped \x in }%
	\expandafter\pgfplots@loc@TMPa\expandafter{\pgfplots@source}{%
		\t@pgfplots@tokb=\expandafter{\pgfplots@sourcecm}%
		%
		\edef\currentValue{#2\pgfplots@temp@pos=(\x\space of \the\t@pgfplots@tokb) }%
		%
		\expandafter\pgfplotsapplistXpushback\currentValue\to\pgfplots@createcolormap@special@process@result
		%
		% all following ones receive auto-positions:
		\let\pgfplots@temp@pos=\pgfutil@empty
	}%
	\pgfplotsapplistXlet\pgfplotsretval=\pgfplots@createcolormap@special@process@result
	\global\let\pgfplots@glob@TMPa=\pgfplotsretval
	\endgroup
	\let\pgfplotsretval=\pgfplots@glob@TMPa
}%
%%%%%%%%%%

\def\pgfplots@createcolormap@grabnext@complete{%
	\pgfutil@IfUndefined{pgfplots@createcolormap@grabnextspecial@\pgfplots@loc@TMPa}{%
		% ok, this is a 'normal' color specification with position,
		% colorspace, and color data.
		%
		\edef\pgfplots@loc@TMPa{%
			{\pgfplots@loc@TMPb}% position
			{%
			\pgfplots@loc@TMPa% colorspace
			=\pgfplots@loc@TMPc% color data
			}%
		}%
		\expandafter\pgfplots@createcolormap@nextcolor\pgfplots@loc@TMPa
		%
		% continue loop:
		\def\pgfplots@loc@TMPd{\pgfplots@createcolormap@}%
	}{%
		% ah - a special one based on another colormap!
		% convert it into the standard format:
		%
		\t@pgfplots@tokb=\expandafter{\pgfplots@loc@TMPc}%
		\edef\pgfplots@loc@TMPd{%
			\expandafter\noexpand\csname pgfplots@createcolormap@grabnextspecial@\pgfplots@loc@TMPa\endcsname
				% position:
				{\pgfplots@loc@TMPb}
				% argument:
				{\the\t@pgfplots@tokb}%
		}%
		\pgfplots@loc@TMPd
		%
		\def\pgfplots@loc@TMPd{%
			\expandafter\pgfplots@createcolormap@\pgfplotsretval
		}%
	}%
	%
	%
	% continue loop:
	\pgfplots@loc@TMPd
}%

\def\pgfplots@createcolormap@grabnext@computenextposition{%
	% determine next step size automatically:
	\expandafter\pgfplotsarraycheckempty\expandafter{\pgfplots@createcolormap@name}%
	\ifpgfplotsarrayempty
		% first: must be at 0.
		\def\pgfmathresult{0sp}%
	\else
		% not first:
		\ifx\pgfplots@createcolormap@H\pgfutil@empty
			% ah; we really have to deduce something. We are at the
			% second node:
			\def\pgfmathresult{1cm}%
		\else
			\pgfmathadd@\pgfplots@createcolormap@LAST\pgfplots@createcolormap@H
		\fi
	\fi
}%

\def\pgfplots@createcolormap@grabsemicolon;{\pgfplots@createcolormap@}%

{
	\catcode`\;=13
	\gdef\pgfplots@createcolormap@grabsemicolon@active;{\pgfplots@createcolormap@}%
}

\def\pgfplots@createcolormap@nextcolor@tostring#1=#2\pgfplots@EOI#3{%
	#1(#3)=(#2)%
}%

% #1: h
% #2: a compound element of the form
% '<colorspace>=<comma-separated-color-components>'
%  The format is chosen such that it can be forwarded directly to
%  \pgfplotscolornormalizesequencenext
%
% see  \pgfplotscolornormalizesequencenext for details
\def\pgfplots@createcolormap@nextcolor#1#2{%
	\def\pgfplots@createcolormap@context{\pgfplots@createcolormap@nextcolor@tostring#2\pgfplots@EOI{#1}}%
	%
	\pgfplotscolornormalizesequencenext{#2}%
	\let\pgfplots@createcolormap@col@comps=\pgfplotsretvalb
	%
	\edef\pgfplots@loc@TMPa{{#1}{\pgfplotsretval}}%
	\expandafter\pgfplots@createcolormap@nextcolor@\pgfplots@loc@TMPa%
}%
\def\pgfplots@createcolormap@nextcolor@#1#2{%
%\message{processing color #1=(#2)^^J}%
	% compute 'h':
	\pgfmathparse{#1}%
	\let\pgfplots@createcolormap@MAX=\pgfmathresult
	\expandafter\pgfmathsubtract@\expandafter{\pgfmathresult}{\pgfplots@createcolormap@LAST}%
	\let\pgfplots@createcolormap@H@cur=\pgfmathresult
%\message{found current diff  = \pgfplots@createcolormap@H@cur\ ( from \pgfplots@createcolormap@MAX pt - \pgfplots@createcolormap@LAST pt)^^J}%
	\let\pgfplots@createcolormap@LAST=\pgfplots@createcolormap@MAX
	\ifx\pgfplots@createcolormap@H\pgfutil@empty
		\expandafter\pgfplotsarraycheckempty\expandafter{\pgfplots@createcolormap@name}%
		\ifpgfplotsarrayempty
			\ifdim\pgfplots@createcolormap@MAX pt=0pt
			\else
				\pgfplots@error{Sorry, the left end of a colormap (at 0pt) must be provided explicitly. You cannot start with \pgfplots@createcolormap@MAX pt. The error occured near `\pgfplots@createcolormap@context'}%
				\def\pgfplots@createcolormap@MAX{0}%
			\fi
		\else
			\let\pgfplots@createcolormap@H=\pgfplots@createcolormap@H@cur
%\message{H := \pgfplots@createcolormap@H( from #1).}%
		\fi
	\else
		\pgfmathapproxequalto@{\pgfplots@createcolormap@H}{\pgfplots@createcolormap@H@cur}%
		\ifpgfmathcomparison
		\else
			\pgfmathdivide@{\pgfplots@createcolormap@H@cur}{\pgfplots@createcolormap@H}%
			% after this group, \pgfmathresult is
			% - empty if no reinterpolation is possible,
			% - non-empty if reinterpolation IS possible. In this
			%   case, it contains the integer multiple of H.
			\begingroup
				\afterassignment\pgfplots@createcolormap@nextRGB@consider@reinterpolation
				\c@pgf@counta=\pgfmathresult\relax
				\pgfmath@smuggleone\pgfmathresult
			\endgroup
			\ifx\pgfmathresult\pgfutil@empty
				% I can't do that yet.
			\else
				\let\pgfplots@createcolormap@loop@end=\pgfmathresult
				% interpolate missing values using the already fixed H.
				% This interpolation procedure is stupid because it works
				% only in forward direction - but it works at least.
				% For the backwards direction, you can provide the
				% meshwidth explicitly at
				% \pgfplotscreatecolormap{}{[1pt]}
				\pgfplotsforeachungrouped \c@pgfplots@createcolormap in {1,2,...,\pgfplots@createcolormap@loop@end} {%
					\ifdim\c@pgfplots@createcolormap pt=\pgfplots@createcolormap@loop@end pt %
						% omit the last.
					\else
						\pgfmathparse{\c@pgfplots@createcolormap/\pgfplots@createcolormap@loop@end}%
						\let\pgfplots@createcolormap@scale@current=\pgfmathresult
						\pgfmathparse{1-\pgfplots@createcolormap@scale@current}%
						\let\pgfplots@createcolormap@scale@last=\pgfmathresult
						%
						\pgfplotscolornormalizesequencezero
						\pgfplotscolornormalizesequenceaddweighted
							{\pgfplotsretval}
							{\pgfplots@createcolormap@scale@current}
							{#2}%
						\pgfplotscolornormalizesequenceaddweighted
							{\pgfplotsretval}
							{\pgfplots@createcolormap@scale@last}
							{\pgfplots@createcolormap@last}%
						%
%\message{interpolation step \c@pgfplots@createcolormap = \pgfplotsretval^^J}%
						\edef\pgfplots@loc@TMPa{%
							\noexpand\pgfplotsarraypushback{\pgfplotsretval}%
							\noexpand\to}%
						\expandafter\pgfplots@loc@TMPa\expandafter{\pgfplots@createcolormap@name}%
					\fi
				}%
			\fi
		\fi
	\fi
	\edef\pgfplots@loc@TMPa{\noexpand\pgfplotsarraypushback{#2}\noexpand\to}%
	\expandafter\pgfplots@loc@TMPa\expandafter{\pgfplots@createcolormap@name}%
	\edef\pgfplots@createcolormap@last{#2}%
}%
\def\pgfplots@createcolormap@nextRGB@consider@reinterpolation#1\relax{%
	\pgf@xa=#1pt
	\ifdim\pgf@xa>0.5pt % we have something like 99.995 or so.
		% round up and compute 1 - #1:
		\advance\c@pgf@counta by1
		\pgf@xa=1pt \advance\pgf@xa by-#1pt
	\fi
	%
	% compute relative error:
	\pgf@xb=\the\c@pgf@counta pt
	\divide\pgf@xb by10000
%\message{Checking  H/h = \pgfplots@createcolormap@H@cur pt/\pgfplots@createcolormap@H pt = \the\c@pgf@counta+-\pgf@sys@tonumber\pgf@xa\space:  \the\pgf@xa > \the\pgf@xb\space (relative to \the\c@pgf@counta)?}%
	%
	\ifdim\pgf@xa>\pgf@xb
		\pgfplots@error{Sorry, non-uniform colormaps are only partially implemented, yet: the provided points must be multiples of the mesh width h=\pgfplots@createcolormap@H pt (but I found one with H/h = \pgfplots@createcolormap@H@cur pt/\pgfplots@createcolormap@H pt = \the\c@pgf@counta+-\pgf@sys@tonumber\pgf@xa\space which is no integer). Perhaps it helps to provide the mesh widths as argument as in {<name>}{[1cm] <color arguments>}? The error occured near `\pgfplots@createcolormap@context'}%
		\let\pgfmathresult=\pgfutil@empty
	\else
		\ifnum\c@pgf@counta=0
			\let\pgfmathresult=\pgfutil@empty
		\else
			\edef\pgfmathresult{\the\c@pgf@counta}%
		\fi
	\fi
}%

\def\pgfplots@colormap@LUA@new#1{%
	\ifpgfplots@LUA@supported
		\pgfplotscolormapserializecomponentstomacro{#1}\pgfplots@loc@TMPa
		\pgfplotsutil@directlua{%
			local colorspace =^^J%
				pgfplots.\csname pgfpl@cm@#1@colspace\endcsname;^^J%
			if colorspace then^^J%
				pgfplots.ColorMaps["#1"] = pgfplots.ColorMap.new(^^J%
					\csname pgfpl@cm@#1@h\endcsname,^^J%
					colorspace,^^J%
					{ \pgfplots@loc@TMPa });^^J%
			else^^J%
				io.write("There is no LUA colorspace for '\csname pgfpl@cm@#1@colspace\endcsname' - skipping LUA color map definition of #1\string\n");^^J%
			end^^J%
		}%
	\fi
}%
% Shows debug info about colormap #1 into the console.
\def\pgfplots@colormap@showdebuginfofor#1{%
	\message{Debug info for color map '#1':^^J}%
	\message{(Transformed) Range: [0:\pgfplotscolormaprange];  ^^J}%
	\message{H: \csname pgfpl@cm@#1@h\endcsname;    ^^J}%
	\message{H:^{-1}: \csname pgfpl@cm@#1@invh\endcsname;    ^^J}%
	\pgfplotsarraysizetomacro{pgfpl@cm@#1}\to\pgfplots@loc@TMPa
	\message{Size: \pgfplots@loc@TMPa;    ^^J}%
	\message{Colorspace: \csname pgfpl@cm@#1@colspace\endcsname^^JValues (\csname pgfpl@cm@#1@col@comps\endcsname\space components each): ^^J}%
	\begingroup
	\c@pgf@counta=0
	\pgfplotsarrayforeachungrouped{pgfpl@cm@#1}\as\elem{%
		\pgfmath@basic@multiply@{\the\c@pgf@counta}{\csname pgfpl@cm@#1@h\endcsname}%
		\message{\#\the\c@pgf@counta=(\pgfmathresult)=(\elem);^^J}%
		\advance\c@pgf@counta by1 %
	}%
	\message{^^J}%
	\endgroup
}

% #1: comma-separated list of values for the current entry
% #2: color map name
% #3: the current index into the colormap
% #4: the "x coordinate", i.e. the offset within the colormap
%
% Defines \pgfplotsretval
%
% Example:
% \pgfplotscolormaptodatafile[
%		output each nth=1,
%		output format=csv,
%	]{viridis}
%	{viridis.dat}
\def\pgfplotscolormaptodatafile@elem#1#2#3#4{
	\let\pgfplotsretval=\pgfutil@empty
}%

\begingroup
\catcode`\[=1
\catcode`\]=2
\catcode`\{=12
\catcode`\}=12
\xdef\pgfplots@lbrace[{]%
\xdef\pgfplots@rbrace[}]%
\endgroup


\pgfkeys{
	/pgfplots/colormap/output each nth/.initial=1,
	/pgfplots/colormap/output format/.is choice,
	/pgfplots/colormap/output format/csv/.code={%
		\def\pgfplotscolormaptodatafile@head##1{%
			\let\pgfplotsretval=\pgfutil@empty
		}%
		\def\pgfplotscolormaptodatafile@elem##1##2##3##4{%
			\edef\pgfplotsretval{##4,##1}%
		}%
		\def\pgfplotscolormaptodatafile@foot##1{%
			\let\pgfplotsretval=\pgfutil@empty
		}%
	},
	/pgfplots/colormap/output format/native/.code={%
		\def\pgfplotscolormaptodatafile@head##1{%
			\edef\pgfplotsretval{\string\pgfplotsset\pgfplots@lbrace colormap={##1}\pgfplots@lbrace}%
		}%
		\def\pgfplotscolormaptodatafile@elem##1##2##3##4{%
			\edef\pgfplotsretval{\csname pgfpl@cm@##2@colspace\endcsname=(##1)}%
		}%
		\def\pgfplotscolormaptodatafile@foot##1{%
			\edef\pgfplotsretval{\pgfplots@rbrace\pgfplots@rbrace}%
		}%
	},
	/pgfplots/colormap/output format=csv,
}


% An export of a color map.
% #1: options. Can use 'output each nth=2' and 'output format=csv|native'
% #2: a color map
% #3: a data file
\def\pgfplotscolormaptodatafile{\pgfutil@ifnextchar[{\pgfplotscolormaptodatafile@}{\pgfplotscolormaptodatafile@[]}}%
\def\pgfplotscolormaptodatafile@[#1]#2#3{%
	\begingroup
	\pgfqkeys{/pgfplots/colormap}{#1}%
	\pgfkeysgetvalue{/pgfplots/colormap/output each nth}{\pgfplots@eachnth}%
	\immediate\openout\w@pgf@writea=#3\relax
	\pgfplotscolormaptodatafile@head{#2}%
	\ifx\pgfplotsretval\pgfutil@empty
	\else
		\immediate\write\w@pgf@writea{%
			\pgfplotsretval
		}%
	\fi
	\gdef\pgfplots@eachnth@cur{0}%
	\pgfplotsarrayforeach{pgfpl@cm@#2}\as\elem{%
		\ifnum\pgfplots@eachnth@cur=0\relax
			\gdef\pgfplots@eachnth@cur{0}%
			\pgfmath@basic@multiply@{\pgfplotsarrayforeachindex}{\csname pgfpl@cm@#2@h\endcsname}%
			\edef\pgfplots@loc@TMPa{{\elem}{#2}{\pgfplotsarrayforeachindex}{\pgfmathresult}}%
			\expandafter\pgfplotscolormaptodatafile@elem\pgfplots@loc@TMPa
			\ifx\pgfplotsretval\pgfutil@empty
			\else
				\immediate\write\w@pgf@writea{%
					\pgfplotsretval
				}%
			\fi
		\fi
		\pgfplotsutil@advancestringcounter@global\pgfplots@eachnth@cur%
		\ifnum\pgfplots@eachnth@cur=\pgfplots@eachnth\relax
			\gdef\pgfplots@eachnth@cur{0}%
		\fi
	}%
	\pgfplotscolormaptodatafile@foot{#2}%
	\ifx\pgfplotsretval\pgfutil@empty
	\else
		\immediate\write\w@pgf@writea{%
			\pgfplotsretval
		}%
	\fi
	\immediate\closeout\w@pgf@writea
	\endgroup
}

% Defines \pgfplotsretval to contain the mesh width of colormap #1
\def\pgfplotscolormapgetmeshwidth#1{%
	\expandafter\let\expandafter\pgfplotsretval\csname pgfpl@cm@#1@h\endcsname%
}%

% defines macro #2 to contain a serialized variant of the color
% components (only the color components!)
\def\pgfplotscolormapserializecomponentstomacro#1#2{%
	\pgfplotsapplistXnewempty\pgfplots@serialize@list@
	\pgfplotsarrayforeachungrouped{pgfpl@cm@#1}\as\elem{%
		\expandafter\pgfplotsapplistXpushback\expandafter{\elem},\to\pgfplots@serialize@list@
	}%
	\pgfplotsapplistXlet#2=\pgfplots@serialize@list@
}%

% Copies the contents of the colormap named '#1' into a macro '#2'.
% Invocation of the macro will then re-create the colormap.
%
% #1: color map name
% #2: a macro name
\def\pgfplotscolormapserializetomacro#1#2{%
	\begingroup
		\pgfplotscolormapserializecomponentstomacro{#1}\pgfplots@serialize@list
		\toks0={\expandafter\def\csname pgfpl@cm@#1@h\endcsname}%
		\toks1={\expandafter\def\csname pgfpl@cm@#1@invh\endcsname}%
		\toks2={%
			\pgfplotsarraynewempty{pgfpl@cm@#1}%
			\expandafter\pgfplotsutilforeachcommasep\pgfplots@loc@TMPa\as\pgfplots@loc@TMPb{%
				\ifx\pgfplots@loc@TMPb\pgfutil@empty
				\else
					\expandafter\pgfplotsarraypushback\pgfplots@loc@TMPb\to{pgfpl@cm@#1}%
				\fi
			}%
		}%
		\toks3=\expandafter{\pgfplots@serialize@list}%
		\toks4=\expandafter{\expandafter\def\csname pgfpl@cm@#1@colspace\endcsname}%
		\toks5=\expandafter{\expandafter\def\csname pgfpl@cm@#1@col@comps\endcsname}%
		\toks6=\expandafter{\expandafter\def\csname pgfpl@cm@#1@lastindex\endcsname}%
		\xdef\pgfplots@glob@TMPa{%
			\the\toks0 {\csname pgfpl@cm@#1@h\endcsname}%
			\the\toks1 {\csname pgfpl@cm@#1@invh\endcsname}%
			\noexpand\def\noexpand\pgfplots@loc@TMPa{\the\toks3 }%
			\the\toks2
			\the\toks4 {\csname pgfpl@cm@#1@colspace\endcsname}%
			\the\toks5 {\csname pgfpl@cm@#1@col@comps\endcsname}%
			\the\toks6 {\csname pgfpl@cm@#1@lastindex\endcsname}%
		}%
	\endgroup
	\let#2=\pgfplots@glob@TMPa
}%

% this is a CONSTANT! Do NOT change it!
% Just read it -- just in case \pgfplotscolormaptopdffunction will use
% a different upper bound in the future.
\def\pgfplotscolormappdfmax{1}% FIXME : why? 1000 would be more precise...?

% Expands to the size of colormap #1 .
\def\pgfplotscolormapsizeof#1{%
	\pgfplotscolormapifdefined{#1}{%
		\pgfplotsarraysizeof{pgfpl@cm@#1}%
	}{%
		0%
	}%
}%

% Expands to the last index of colormap #1 (or -1)
\def\pgfplotscolormaplastindexof#1{%
	\pgfplotscolormapifdefined{#1}{%
		\csname pgfpl@cm@#1@lastindex\endcsname%
	}{%
		-1%
	}%
}%

% Writes a PDF function of /FunctionType 3 to \pgfplotsretval
%
% The /Domain argument will be set to [ 0 \pgfplotscolormappdfmax ] and bounds will be
% computed accordingly.
%
% Note that the value depends on 'colormap access'
%
% #1: the colormap
\def\pgfplotscolormaptopdffunction#1{%
	\begingroup
	\pgfplotscolormapassertexists{#1}%
	\pgfplotsapplistXnewempty\pgfplots@resultaccum
	\pgfplotsapplistXpushback{
		<< 
			/FunctionType 3 
			/Domain [0 \pgfplotscolormappdfmax] 
			/Functions [ 
	}\to\pgfplots@resultaccum%
	\c@pgf@counta=0
	\c@pgf@countb=\pgfplotsarraysizeof{pgfpl@cm@#1} %
	\advance\c@pgf@countb by-1
	\def\pgfplots@loc@TMPa{}%
	\pgfplotsapplistXnewempty\pgfplots@bounds@accum%
	\pgfplotsapplistXnewempty\pgfplots@encode@accum
	\ifnum\csname pgfpl@cm@#1@col@comps\endcsname>4
		\pgfplots@error{Sorry, processing more than 4 color components (as required for color map #1) is unsupported in this context}%
	\fi
	%
	\if m\pgfplots@colormap@access
		% colormap access=map
		\def\pgfplots@order{1}%
		\global\pgf@x=\csname pgfpl@cm@#1@h\endcsname pt %
		\def\pgfplots@C@left{C0}%
		\def\pgfplots@C@right{C1}%
	\else
		\if c\pgfplots@colormap@access
			% colormap access=piecewise constant
			% This has a dedicated special interval at the rightmost
			% end. 
			% See comments in pgfplotscolormapfindpiecewiseconst@precomputed@
			\def\pgfplots@order{0}%
			%
			% advance the number of intervals by 1.
			% To this end, we have to compute 
			% N = n+1
			% and 
			% H = 1000 / (N-1) = 1000/n.
			% Store H in \pgf@x:
			\c@pgf@countd=\pgfplotscolormapsizeof{#1}\relax
			%
			\global\pgf@x=1000pt %
			\global\divide\pgf@x by\c@pgf@countd
			%
			% interchange the sequence in the PDF function as it is
			% defined as 
			% y = C0 + x^0 * (C1 - C0) = C1
			% -> we want the left interval boundary.
			\def\pgfplots@C@left{C1}%
			\def\pgfplots@C@right{C0}%
		\else
			% colormap access=direct
			\pgfplots@error{This should never happen}%
			% ?
			\def\pgfplots@order{0}%
			\global\pgf@x=\csname pgfpl@cm@#1@h\endcsname pt %
			\def\pgfplots@C@left{C0}%
			\def\pgfplots@C@right{C1}%
		\fi
	\fi
	%
	\pgfplotsarrayforeachungrouped{pgfpl@cm@#1}\as\cdata{%
		\edef\cdata{%
			\expandafter
			\expandafter
			\csname pgfplotscolormaptopdffunction@convertcdata@\csname pgfpl@cm@#1@col@comps\endcsname\endcsname
			\cdata\relax
		}%
		\ifnum\c@pgf@counta>0
			\edef\pgfplots@loc@TMPd{%
				<<
					/FunctionType 2
					/Domain [0 \pgfplotscolormappdfmax] 
					/\pgfplots@C@left\space [\pgfplots@loc@TMPa] /\pgfplots@C@right\space [\cdata] 
					/N \pgfplots@order
				>>
			}%
			\expandafter\pgfplotsapplistXpushback\expandafter{\pgfplots@loc@TMPd}\to\pgfplots@resultaccum
			\ifnum\c@pgf@counta<\c@pgf@countb\relax
				\pgf@xa=\pgf@x
				\multiply\pgf@xa by\c@pgf@counta\relax
				\divide\pgf@xa by1000 % we want [ 0 1 ] not [0 1000] as domain XXX : why!?
				\edef\pgfplots@loc@TMPc{ \pgf@sys@tonumber\pgf@xa}%
				\expandafter\pgfplotsapplistXpushback\expandafter{\pgfplots@loc@TMPc}\to\pgfplots@bounds@accum%
			\fi
			\pgfplotsapplistXpushback{ 0 1}\to\pgfplots@encode@accum%
		\fi
		\let\pgfplots@loc@TMPa=\cdata
		\advance\c@pgf@counta by1
	}%
	\if 0\pgfplots@order
		% add an artificial interval.
		\edef\pgfplots@loc@TMPd{%
			<<
				/FunctionType 2
				/Domain [0 \pgfplotscolormappdfmax] 
				/\pgfplots@C@left\space [\cdata] /\pgfplots@C@right\space [\cdata] 
				/N \pgfplots@order
			>>
		}%
		\expandafter\pgfplotsapplistXpushback\expandafter{\pgfplots@loc@TMPd}\to\pgfplots@resultaccum
		\pgf@xa=\pgf@x
		\advance\c@pgf@counta by-1 %
		\multiply\pgf@xa by\c@pgf@counta\relax
		\divide\pgf@xa by1000 % we want [ 0 1 ] not [0 1000] as domain XXX : why!?
		\edef\pgfplots@loc@TMPc{ \pgf@sys@tonumber\pgf@xa}%
		\expandafter\pgfplotsapplistXpushback\expandafter{\pgfplots@loc@TMPc}\to\pgfplots@bounds@accum%
		\pgfplotsapplistXpushback{ 0 1}\to\pgfplots@encode@accum%
	\fi
	\pgfplotsapplistXlet\pgfplotsretval=\pgfplots@resultaccum
	\pgfplotsapplistXlet\pgfplotsbounds=\pgfplots@bounds@accum
	\pgfplotsapplistXlet\pgfplotsencode=\pgfplots@encode@accum
	\t@pgfplots@toka=\expandafter{\pgfplotsretval}%
	\t@pgfplots@tokb=\expandafter{\pgfplotsbounds}%
	\t@pgfplots@tokc=\expandafter{\pgfplotsencode}%
	\xdef\pgfplots@glob@TMPa{%
			\the\t@pgfplots@toka
			]
			/Bounds [\the\t@pgfplots@tokb]
			/Encode [\the\t@pgfplots@tokc]
		>>
	}%
	\endgroup
	\let\pgfplotsretval=\pgfplots@glob@TMPa
}%
\expandafter\def\csname pgfplotscolormaptopdffunction@convertcdata@1\endcsname#1\relax{#1}%
\expandafter\def\csname pgfplotscolormaptopdffunction@convertcdata@2\endcsname#1,#2\relax{#1 #2}%
\expandafter\def\csname pgfplotscolormaptopdffunction@convertcdata@3\endcsname#1,#2,#3\relax{#1 #2 #3}%
\expandafter\def\csname pgfplotscolormaptopdffunction@convertcdata@4\endcsname#1,#2,#3,#4\relax{#1 #2 #3 #4}%

% Invokes '#2' if a color map named '#1' exists and '#3' if no such color map exists.
\def\pgfplotscolormapifdefined#1#2#3{\pgfplotsarrayifdefined{pgfpl@cm@#1}{#2}{#3}}%

\def\pgfplotscolormapassertexists#1{%
	\pgfplotscolormapifdefined{#1}{}{%
		\pgfutil@ifundefined{pgfplotscolormap@errorissued@#1}{%
			\pgfplots@error{The colormap `#1' is undefined! Maybe you misspelled it?}%
		}{}%
		\expandafter\gdef\csname pgfplotscolormap@errorissued@#1\endcsname{1}%
		\pgfplotscreatecolormap{#1}{color(0cm)=(blue); color(1cm)=(yellow)}%
	}%
}

% Convert a colormap into a PGF shading's color specification for use
% in \pgfdeclare*shading.
%
% #1: the colormap's name.
% #2: the PGF "size" of the shading. It is used to set the right end
% of the color specification.
% #3: a macro which will be filled with the result.
%
% Example:
% \pgfplotscolormaptoshadingspec{my colormap}{4cm}{\output}
% \def\tempb{\pgfdeclarehorizontalshading{myshadingA}{1cm}}
% \expandafter\tempb\expandafter{\temp}	
%
% \pgfuseshading{myshadingA}
\def\pgfplotscolormaptoshadingspec#1#2#3{%
	\pgfplotscolormapassertexists{#1}%
	\begingroup
	\def\pgfplots@rgb{rgb}%
	\pgfmathparse{#2}%
	\expandafter\pgfmathdivide@\expandafter{\pgfmathresult}{\pgfplotscolormaprange}%
	\let\pgfplots@loc@TMPb=\pgfmathresult
	\pgfmathmultiply@{\csname pgfpl@cm@#1@h\endcsname}{\pgfplots@loc@TMPb}%
	\pgf@ya=\pgfmathresult pt
	\c@pgf@counta=0
	\let#3=\pgfutil@empty
	\pgfplotsarrayforeachungrouped{pgfpl@cm@#1}\as\pgfplotscolormaptoshadingspec@TMP{%
		\pgf@yb=\c@pgf@counta\pgf@ya
		\edef\pgfplots@colspace{\csname pgfpl@cm@#1@colspace\endcsname}%
		% FIXME : PGF shadings accept only RGB!
		\if1\pgfplotscolormaptoshadingspectorgb
			\ifx\pgfplots@colspace\pgfplots@rgb
			\else
				\pgfutil@ifundefined{pgfpl@cm@#1@warned}{%
					\expandafter\gdef\csname pgfpl@cm@#1@warned\endcsname{1}%
					\pgfplotswarning{lossy colormap rgb conversion}{#1}{\pgfplots@colspace}\pgfeov%
				}{%
				}%
				\edef\pgf@tempcolor{{\pgfplots@colspace}{\pgfplotscolormaptoshadingspec@TMP}}%
				\expandafter\pgfutil@convertcolorspec\pgf@tempcolor{rgb}{\pgfplotscolormaptoshadingspec@TMP}%
				\def\pgfplots@colspace{rgb}%
			\fi
		\fi
		\edef\pgfplots@loc@TMPc{\pgfplots@colspace(\the\pgf@yb)=(\pgfplotscolormaptoshadingspec@TMP)}%
		\ifx#3\pgfutil@empty
			\t@pgfplots@toka={}%
		\else
			\t@pgfplots@toka=\expandafter{#3; }%
		\fi
		\t@pgfplots@tokb=\expandafter{\pgfplots@loc@TMPc}%
		\edef#3{\the\t@pgfplots@toka \the\t@pgfplots@tokb }%
		\advance\c@pgf@counta by1
	}%
	\pgfmath@smuggleone#3%
	\endgroup
}%
\def\pgfplotscolormaptoshadingspectorgb{1}%

% The same as \pgfplotscolormaptoshadingspec, but this here yields the
% *reversed* sequence.
\def\pgfplotscolormapreversedtoshadingspec#1#2#3{%
	\begingroup
	\let\pgfplotsarrayforeachungrouped=\pgfplotsarrayforeachreversedungrouped
	\pgfplotscolormaptoshadingspec{#1}{#2}{#3}%
	\pgfmath@smuggleone#3%
	\endgroup
}%


% Expands to the transformed range's right end of every colormap. The left
% end is fixed to '0'.
\def\pgfplotscolormaprange{1000}

\def\pgfplotscolormapgetcolorspace#1{%
	\edef\pgfplotsretval{\csname pgfpl@cm@#1@colspace\endcsname}%
}%
\def\pgfplotscolormapgetcolorcomps#1{%
	\edef\pgfplotsretval{\csname pgfpl@cm@#1@col@comps\endcsname}%
}%
\def\pgfplotscolormapcolorspaceof#1{%
	\csname pgfpl@cm@#1@colspace\endcsname%
}%
\def\pgfplotscolormapcolorcompsof#1{%
	\csname pgfpl@cm@#1@col@comps\endcsname%
}%


% Linearly maps the number #4 into the colormap #5 and returns the
% interpolated colors into \pgfmathresult. The result will be a triple
% for an RGB colormap and it will contain four numbers for CMYK. The
% components of the result will be in the range [0:1].
% 
% [#1:#2]  the number range of the data source (i.e. of #4). This is required to
%          map into the colormap.
% [#3]     (optional) the quantity 
% 			 s := \pgfplotscolormaprange / (#2-#1).
% 		   Precomputing this quantity may save a lot of time because
% 		   divisions are expensive in TeX. You can omit [#3] or
% 		   provide an empty string here.
% #4       the number to map.
% #5       the colormap's name. Must be defined with
%          \pgfplotscreatecolormap before.
%
% Example:
% \pgfplotscolormapfind[-1:1]{0.2}{my colormap}
\def\pgfplotscolormapfind[#1:#2]{%
	\pgfutil@ifnextchar[{%
		\pgfplotscolormapfind@precomputed[#1:#2]%
	}{%
		\pgfplotscolormapfind@precomputed[#1:#2][]%
	}%
}%
\def\pgfplotscolormapfind@precomputed[#1:#2][#3]#4#5{%
	\ifpgfplots@LUA@supported
		\edef\pgfmathresult{%
			\pgfplotsutil@directlua{%
				pgfplots.texColorMapPrecomputed("#5", #1,#2,#4)
			}%
		}%
		\ifx\pgfmathresult\pgfutil@empty
			\pgfplotscolormapfind@precomputed@warn@LUA{#5}%
			\pgfplotscolormapfind@precomputed@[#1:#2][#3]{#4}{#5}%
		\else
			%\message{LUA colormapfind[#1:#2](#4)(#5) = \pgfmathresult^^J}%
		\fi
	\else
		\pgfplotscolormapfind@precomputed@[#1:#2][#3]{#4}{#5}%
	\fi
}

\def\pgfplotscolormapfind@precomputed@warn@LUA#1{%
	\expandafter\ifx\csname pgfplotscolormapfind@precomputed@warn@LUA@#1\endcsname\relax
		\pgfplotswarning{lua colormap unavailable}{#1}\pgfeov%
		\expandafter\gdef\csname pgfplotscolormapfind@precomputed@warn@LUA@#1\endcsname{1}%
	\fi
}%
\def\pgfplotscolormapfind@@@[#1:#2][#3]#4#5{%
	% Step 0: compute #3 if it is missing and write it into
	% \pgfplots@loc@TMPa.
	\def\pgfplots@loc@TMPa{#3}%
	\ifx\pgfplots@loc@TMPa\pgfutil@empty
		\pgfmathsubtract@{#2}{#1}%
		\let\pgfplots@loc@TMPb=\pgfmathresult
		\pgfmathdivide@{\pgfplotscolormaprange}{\pgfplots@loc@TMPb}%
		\let\pgfplots@loc@TMPa=\pgfmathresult
	\fi
%\message{mapping '#4' into colormap '#5' ... ^^J}%
	\def\pgfplots@loc@samerange{0:1000}%
	\ifx\pgfplots@loc@TMPa\pgfplots@loc@samerange
		% we have phi(#4) = #4  because #4 in [0:1000].
		\def\pgfmathresult{#4}%
	\else
		% Step 1: perform lookup. Map #4 into the colormap's range
		% using the linear trafo
		% phi(#4) = ( #4 - #1 ) / (#2-#1) * colormaprange(#5).
		% This, determine the INTERVAL number into which #4 falls.
		\pgfmathsubtract@{#4}{#1}%
		\expandafter\pgfmathmultiply@\expandafter{\pgfmathresult}{\pgfplots@loc@TMPa}%
	\fi
	\ifdim\pgfmathresult pt<0pt
		\def\pgfmathresult{0}%
	\else
		\ifdim\pgfmathresult pt>1000pt
			\def\pgfmathresult{1000}%
		\fi
	\fi
}%
\def\pgfplotscolormapfind@precomputed@[#1:#2][#3]#4#5{%
	\pgfplotscolormapassertexists{#5}%
	\begingroup
	\pgfplotscolormapfind@@@[#1:#2][#3]{#4}{#5}%
	%
	% Now, we have it in [0,1000]. Find the correct interval:
	\let\pgfplotscolormapfind@transformedx=\pgfmathresult
	\pgfmathmultiply@{\pgfplotscolormapfind@transformedx}{\csname pgfpl@cm@#5@invh\endcsname}%
	\let\pgfplotscolormapfind@transformedx@divh=\pgfmathresult
	\expandafter\pgfmathfloor@\expandafter{\pgfmathresult}%
	% assign \pgfplotscolormapfind@intervalno := \pgfmathresult
	% without '.0' suffix:
	\def\pgfplots@discardperiod##1.##2\relax{\def\pgfplotscolormapfind@intervalno{##1}}%
	\expandafter\pgfplots@discardperiod\pgfmathresult\relax
	% Step 2: interpolate the desired RGB value using vector valued
	% interpolation on the identified interval.
	\c@pgf@counta=\pgfplotscolormapfind@intervalno\relax
%\message{mapping [#1,#2] -> [0,\pgfplotscolormaprange]  yielded phi(#4) = \pgfplotscolormapfind@transformedx, situated in interval no \the\c@pgf@counta.^^J}%
	\pgfplotsarrayselect\c@pgf@counta\of{pgfpl@cm@#5}\to\pgfplotscolormapfind@rgb@LEFT
	%
	% do the stuff which is specific to linear interpolation:
	%
	\pgfmathsubtract@{\pgfplotscolormapfind@transformedx@divh}{\pgfplotscolormapfind@intervalno}%
	\let\pgfplots@loc@factor=\pgfmathresult
	\pgfmathsubtract@{1}{\pgfplots@loc@factor}%
	\let\pgfplots@loc@factor@two=\pgfmathresult
	%
	\pgfplotsarraysize{pgfpl@cm@#5}\to\c@pgf@countb
	\advance\c@pgf@countb-1 %
	\ifnum\c@pgf@counta<\c@pgf@countb
		% Ah: (selected index < length-1)  . That means: we have an interval.
		\advance\c@pgf@counta by1
		\pgfplotsarrayselect\c@pgf@counta\of{pgfpl@cm@#5}\to\pgfplotscolormapfind@rgb@RIGHT
%\message{After lookup: the corresponding RGB interval boundaries are [\pgfplotscolormapfind@rgb@LEFT: \pgfplotscolormapfind@rgb@RIGHT].^^J}%
		%
		%
		% color^m(x) = ( x/h - i ) * ( c_{i+1}^m - c_{i}^m ) + c_i^m 
		%            = s * c_{i+1}^m  + S * c_i^m
		%
		%  s= x_h / h -i 
		%  S = 1 - s
		\pgfplotscolorzero{\csname pgfpl@cm@#5@col@comps\endcsname}%
		\pgfplotscoloraddweighted
			{\csname pgfpl@cm@#5@col@comps\endcsname}
			{\pgfplotsretval}%
			{\pgfplots@loc@factor@two}%
			{\pgfplotscolormapfind@rgb@LEFT}%
		\pgfplotscoloraddweighted
			{\csname pgfpl@cm@#5@col@comps\endcsname}
			{\pgfplotsretval}%
			{\pgfplots@loc@factor}%
			{\pgfplotscolormapfind@rgb@RIGHT}%
		\let\pgfmathresult=\pgfplotsretval
	\else
		% Ah: selected index = last index. Use the rightmost color!
		\let\pgfmathresult=\pgfplotscolormapfind@rgb@LEFT
	\fi
	\pgfmath@smuggleone\pgfmathresult
	\endgroup
%\message{-> got finally mapping(#4, #5) = RGB'\pgfmathresult'.}%
}%


% Maps the number #4 into the colormap #5 using piecewise constant
% interpolation. It returns the interpolated colors into \pgfmathresult. The result will be a triple
% for an RGB colormap and it will contain four numbers for CMYK. The
% components of the result will be in the range [0:1].
% 
% [#1:#2]  the number range of the data source (i.e. of #4). This is required to
%          map into the colormap.
% [#3]     (optional) the quantity 
% 			 s := \pgfplotscolormaprange / (#2-#1).
% 		   Precomputing this quantity may save a lot of time because
% 		   divisions are expensive in TeX. You can omit [#3] or
% 		   provide an empty string here.
% #4       the number to map.
% #5       the colormap's name. Must be defined with
%          \pgfplotscreatecolormap before.
%
% Example:
% \pgfplotscolormapfindpiecewiseconst[-1:1]{0.2}{my colormap}
\def\pgfplotscolormapfindpiecewiseconst[#1:#2]{%
	\pgfutil@ifnextchar[{%
		\pgfplotscolormapfindpiecewiseconst@precomputed[#1:#2]%
	}{%
		\pgfplotscolormapfindpiecewiseconst@precomputed[#1:#2][]%
	}%
}%
\def\pgfplotscolormapfindpiecewiseconst@precomputed[#1:#2][#3]#4#5{%
	%\ifpgfplots@LUA@supported
	\iffalse
		% FIXME : implement
		\edef\pgfmathresult{%
			\pgfplotsutil@directlua{%
				pgfplots.texColorMapPrecomputed("#5", #1,#2,#4)
			}%
		}%
		\ifx\pgfmathresult\pgfutil@empty
			\pgfplotscolormapfindpiecewiseconst@precomputed@warn@LUA{#5}%
			\pgfplotscolormapfindpiecewiseconst@precomputed@[#1:#2][#3]{#4}{#5}%
		\else
			%\message{LUA colormapfindpiecewiseconst[#1:#2](#4)(#5) = \pgfmathresult^^J}%
		\fi
	\else
		\pgfplotscolormapfindpiecewiseconst@precomputed@[#1:#2][#3]{#4}{#5}%
	\fi
}

\def\pgfplotscolormapfindpiecewiseconst@precomputed@warn@LUA#1{%
	\expandafter\ifx\csname pgfplotscolormapfindpiecewiseconst@precomputed@warn@LUA@#1\endcsname\relax
		\pgfplotswarning{lua colormap unavailable}{#1}\pgfeov%
		\expandafter\gdef\csname pgfplotscolormapfindpiecewiseconst@precomputed@warn@LUA@#1\endcsname{1}%
	\fi
}%

\def\pgfplotscolormapfindpiecewiseconst@precomputed@[#1:#2][#3]#4#5{%
	\pgfplotscolormapassertexists{#5}%
	\begingroup
	\pgfplotscolormapfind@@@[#1:#2][#3]{#4}{#5}%
	\let\pgfplotscolormapfind@transformedx=\pgfmathresult
	%
	% Now, we have it in [0,1000]. Find the correct interval:
	% In principle, we could easily find the correct interval. But the
	% native approach has one distinct disadvantage: we will never
	% assign the rightmost color unless #4=1000! That means that our
	% colormap is not used entirely.
	%
	% I decided to enlarge the colormap artificially such that the
	% rightmost color receives its own interval.
	% 
	% To this end, I recompute the coordinates at which colors "live".
	% The operations sounds more involved than it is: I simply need a NEW mesh width locally 
	% inside of this function. The new mesh width H can be computed as
	% follows, assuming that the OLD colormap has mesh width h.
	%
	% H := 1000/ (N-1)
	%
	% N := n+1
	% n := 1000/h + 1 = \pgfplotscolormapsizeof{#5}
	%
	% what I really need is:
	%  1/H = N/1000 = (1000 h^{-1} +1)/ 1000 = h^{-1} + 1/1000
	%
	% compute 1/H here:
	\pgfmathadd@{\csname pgfpl@cm@#5@invh\endcsname}{0.001}%
	\let\pgfplots@piecewiseconst@invh=\pgfmathresult%
	\pgfmathmultiply@{\pgfplotscolormapfind@transformedx}{\pgfplots@piecewiseconst@invh}%
	\let\pgfplotscolormapfind@transformedx@divh=\pgfmathresult
	\expandafter\pgfmathfloor@\expandafter{\pgfmathresult}%
	% assign \pgfplotscolormapfind@intervalno := \pgfmathresult
	% without '.0' suffix:
	\def\pgfplots@discardperiod##1.##2\relax{\def\pgfplotscolormapfind@intervalno{##1}}%
	\expandafter\pgfplots@discardperiod\pgfmathresult\relax
	% Step 2: interpolate the desired RGB value using vector valued
	% interpolation on the identified interval.
	\c@pgf@counta=\pgfplotscolormapfind@intervalno\relax
	%
	\ifnum\c@pgf@counta=\pgfplotscolormapsizeof{#5}\relax
		% we have artificially increased the "h" (see the comments
		% above) -- meaning that this 'if' can happen. 
		% ->Map the rightmost point to the rightmost interval:
		\advance\c@pgf@counta by-1
	\fi
	%
%\message{mapping [#1,#2] -> [0,\pgfplotscolormaprange]  yielded phi(#4) = \pgfplotscolormapfind@transformedx, situated in interval no \the\c@pgf@counta.^^J}%
	\pgfplotsarrayselect\c@pgf@counta\of{pgfpl@cm@#5}\to\pgfplotscolormapfind@rgb@LEFT
	\let\pgfmathresult=\pgfplotscolormapfind@rgb@LEFT
	\pgfmath@smuggleone\pgfmathresult
	\endgroup
}%


% Performs a direct color access into color map '#2' using an index
% '#1'.
%
% #1: an index in the range 0 ... len(#2)-1.
%  If it does not match, it will be pruned. If #1 is a real number, it
%  will be truncated.
% #2: a color map name.
%
% The resulting RGB value will be written to \pgfmathresult.
\def\pgfplotscolormapgetindex#1#2{%
	\pgfplotscolormapassertexists{#2}%
	\begingroup
	\afterassignment\pgfplots@gobble@until@relax
	\c@pgf@counta=#1\relax
	\ifnum\c@pgf@counta<0
		\c@pgf@counta=0
	\else
		\pgfplotsarraysizetomacro{pgfpl@cm@#2}\to\pgfplotscolormapgetindex@
		\ifnum\c@pgf@counta<\pgfplotscolormapgetindex@\relax
		\else
			\c@pgf@counta=\pgfplotscolormapgetindex@\relax
			\advance\c@pgf@counta by-1
		\fi
	\fi
	\pgfplotsarrayselect\c@pgf@counta\of{pgfpl@cm@#2}\to\pgfmathresult
	\pgfmath@smuggleone\pgfmathresult
	\endgroup
}

% Invokes one of \pgfplotscolormapfind,
% \pgfplotscolormapfindpiecewiseconst, or \pgfplotscolormapgetindex,
% depending on the value of '/pgfplots/colormap access'.
\def\pgfplotscolormapaccess[#1:#2]{%
	\pgfutil@ifnextchar[{%
		\pgfplotscolormapaccess@precomputed[#1:#2]%
	}{%
		\pgfplotscolormapaccess@precomputed[#1:#2][]%
	}%
}
\def\pgfplotscolormapaccess@precomputed[#1:#2][#3]#4#5{%
	\if m\pgfplots@colormap@access
		% colormap access=map
		\pgfplotscolormapfind@precomputed[#1:#2][#3]{#4}{#5}%
	\else
		\if c\pgfplots@colormap@access
			% colormap access=piecewise constant
			\pgfplotscolormapfindpiecewiseconst@precomputed[#1:#2][#3]{#4}{#5}%
		\else
			% colormap access=direct
			\pgfplotscolormapgetindex{#4}{#5}%
		\fi
	\fi
}%


\pgfutil@definecolor{mapped color}{rgb}{0,0,0}% make sure this color exists. It will be overwritten if needed.
% ATTENTION: replicated in pgfplots.code.tex :
\pgfplotscreatecolormap{hot}{color(0cm)=(blue); color(1cm)=(yellow); color(2cm)=(orange); color(3cm)=(red)}


\def\pgfplotspointmetatransformedrange{0:1000}

% Defines the 'mapped color' on the basis of
% the current color map.
%
% #1: is the value which should be mapped into the color map; it
% is expected in the range [0,1000] (like point meta).
\def\pgfplotscolormapdefinemappedcolor#1{%
	\expandafter\pgfplotscolormapaccess\expandafter[\pgfplotspointmetatransformedrange]
		[1.0]
		{#1}
		{\pgfkeysvalueof{/pgfplots/colormap name}}
%\message{Color for current point is RGB '\pgfmathresult' (determined using meta 'phi(\pgfplotspointmeta) = \pgfplotspointmetatransformed')^^J}%
	\def\pgfplots@loc@TMPb{\pgfutil@definecolor{mapped color}{\csname pgfpl@cm@\pgfkeysvalueof{/pgfplots/colormap name}@colspace\endcsname}}%
	\expandafter\pgfplots@loc@TMPb\expandafter{\pgfmathresult}%
}%