\setupcolors
	[state=start]

\unprotect
\defineMPspecial{60}
  {\startMPshading{14}% type 0
   \setMPshadingcolors{4}{5}{6}{9}{10}{11}%
   \immediate\pdfobj
     {<</FunctionType 0
        /Order 1
        /BitsPerSample 8
        /Domain [\gMPs1 \gMPs2]
        /Decode [ 0 1 0 1 0 1 ]
        /Range [ 0 1 0 1 0 1 ]
        /Size [ 11 ] >>
        stream
           00 00 00
           51 00 96
           71 02 f3
           8c 07 f3
           a1 10 96
           b4 20 00
           c6 37 00
           d5 57 00
           e4 83 00
           f2 ba 00
           ff ff 00
        endstream}%
   \immediate\pdfobj
     {<</ShadingType 2
        /ColorSpace /\MPshadeC\space
        /Function \the\pdflastobj\space 0 R
        /Coords [\gMPs7 \gMPs8 \gMPs{12} \gMPs{13}]
        /Extend [true true]>>}%
   \stopMPshading}

\def\dosetMPsampledlinearshade#1%
  {\immediate\pdfobj
     {<</FunctionType 0
        /Order 1
        /BitsPerSample 8
        /Domain [\gMPs1 \gMPs2]
        /Decode [ 0 1 0 1 0 1 ]
        /Range [ 0 1 0 1 0 1 ]
        /Size [ 11 ] >>
        stream
           00 00 00
           51 00 96
           71 02 f3
           8c 07 f3
           a1 10 96
           b4 20 00
           c6 37 00
           d5 57 00
           e4 83 00
           f2 ba 00
           ff ff 00
        endstream}%
   \immediate\pdfobj
     {<</ShadingType 2
        /ColorSpace /\MPresolvedspace
        /Function \the\pdflastobj\space 0 R
        /Coords [\MPshadeC]
        /Extend [true true]>>}%
   \global\advance\currentPDFshade \plusone
   \appendtoPDFdocumentshades{/Sh\the\currentPDFshade\space\the\pdflastobj\space0 R }%
   \setxvalue{\@@MPSK#1}{\noexpand\dohandleMPshade{\the\currentPDFshade}}}

\defineMPspecial{60}
 {\expanded{\resolveMPrgbcolor{\gMPs4}{\gMPs5}{\gMPs6}}\to\MPshadeA
  \expanded{\resolveMPrgbcolor{\gMPs{9}}{\gMPs{10}}{\gMPs{11}}}\to\MPshadeB
  \edef\MPshadeC{\gMPs7 \gMPs8 \gMPs{12} \gMPs{13}}%
  \dosetMPsampledlinearshade{\gMPs{14}}}



\protect

\starttext

\startMPinclusions
% trace_shades := true;

vardef define_sampled_linear_shade (expr a, b, ca, cb) =
   flush_special(60, 15, "0 1 " & decimal shadefactor & " " &
       dddecimal ca & " " & ddecimal (a shifted shadeoffset) & " " &
       dddecimal cb & " " & ddecimal (b shifted shadeoffset) ) ;
    _special_counter_
enddef ;


% path, type, colors(color1,color2,...,colorN)
def sampled_linear_shade (expr p, n)(text t) =
	begingroup ;
	save a, b, sh ; pair a, b ;

	save col; color col[];
	save num_col; numeric num_col;

	def colors(text tt) =
		num_col = 0;
		for i=tt:
			num_col := num_col+1;
			col[num_col] = i;
		endfor;
	enddef;

	t;

	% leftovers; I want to get it working hardcoded first
	color ca, cb;
	ca:=yellow; cb:=red;

	set_linear_vector(a,b)(p,n) ;
	fill p withshade define_sampled_linear_shade (a,b,ca,cb) ;
	if trace_shades :
		drawarrow a -- b withpen pencircle scaled 1pt ;
	fi ;
	endgroup ;
enddef ;


\stopMPinclusions
\startMPcode
	% working
	linear_shade(fullsquare scaled 3cm, 0, red, blue);
	% what I would like to get working once
	sampled_linear_shade(fullsquare scaled 3cm shifted (4cm,0), 0, colors(black,blue,red,yellow));%
\stopMPcode
\stoptext

