GenericMappingTools / gmt

The Generic Mapping Tools
https://www.generic-mapping-tools.org
Other
850 stars 356 forks source link

Color gradients in polygons #5480

Open joa-quim opened 3 years ago

joa-quim commented 3 years ago

This is something we miss and apparently doesn't seem complicated (apart the PS language itself) http://redgrittybrick.org/gradient.html

The linking page talks also about a shfill oprator in PS Level 3

gradient

joa-quim commented 2 years ago

Ping

PaulWessel commented 2 years ago

Well, there is no shfill operator in PS Level 3, so that is only in his head I am afraid. I think this particular case (plane 2-D polygon where you desire a gradient fill) could be relatively done. He is drawing lines of different colors. It still imposes a pixel-size via the pen width, so if you zoom in you can tell. Making it very thin would then help, but gets slower. An alternative would be to create a grid with 0 (color 1) to 1 (color 2), then use grdimage inside a clip path [720 dpi as in PDF]. How do you specify a color gradient? Must need color1, color2, direction from 1 to 2, and then the tangent line coming in from direction 1 starts at color1 and then increases to reach color2 as we exit the polygon? What would the syntax be?-Gyellow:orange:-45. ?

As you know from example 19 we can fake these things already.

PaulWessel commented 2 years ago

Take that back: https://www.adobe.com/content/dam/acom/en/devnet/postscript/pdfs/TN5600.SmoothShading.pdf

PaulWessel commented 2 years ago

Cannot find any example online using shfill. Not listed in online Adobe PostSCript manual which still says 1999...

PaulWessel commented 2 years ago

OK, I am learning new skills:

%!PS
/inch {72 mul} def

2 inch 2 inch moveto
5 inch 0 rlineto
0 5 inch rlineto
-5 inch 0 rlineto
closepath

gsave
    clip
    newpath
    << /ShadingType 2
        /ColorSpace /DeviceGray
        /Coords [ 0 0 8.5 inch 11 inch]
        /Function << /FunctionType 2
            /Domain [0 1]
            /C0 0
            /C1 1
            /N 1
        >>
    >>
    shfill
grestore
showpage

a

Esteban82 commented 2 years ago

Could this feature use to make diffuse/soft shadow effect?

PaulWessel commented 2 years ago

Don't know, seems like that will require transparency.

PaulWessel commented 2 years ago

Same but with RGB gradient:

b

joa-quim commented 2 years ago

The Matlab syntax uses a color vector assigning colors to each vertex https://www.mathworks.com/help/matlab/ref/patch.html Is it feasible in your scheme to have more than 2 vertex colored and interpolate between them (the Ml triangle example)? But even if only two that could be a viable syntax, that could be set as an extra column in file or a list of color names on command line.

PaulWessel commented 2 years ago

Triangles can do stuff like that. If we are to just paint a regular polygon then the standard gradients (linear or radial) are the ones on tap. You can browse the PDF above I posted. There would be much work in rewriting grdview and perhaps easier in triangulate to use this scheme but perhaps that is the way forward. As for just applying the gradient in -G for plot it may not be too hard. But need to allow either axial or radial gradient syntax.

PaulWessel commented 2 years ago

The axial gradient specification takes

2 colors
2 sets of coordinates defining the blending line = 4 parameters

The radial gradient requires

2 colors
2 circles (center coordinates and radius) = 6 parameters

The latter if often used to simulate a 3-D sphere, for instance. Perhaps that is the cheaper way for us to plot ellipsoids and spheres in plot3d.

Wild draft ideas for -G syntax before I hit the road:

with gradient.def having things like

color1:  red
color2: 233/16/99
mode: linear
start: 3 3.4
stop: 6.3 -0.5

The coordinates should probably be user coordinates like anything else.

PaulWessel commented 2 years ago

FYI, there seems to be no way to combine this with transparencies other than at the layer level.

PaulWessel commented 2 years ago

I see I can probably implement spheres in plot3d doing something like this:

t

and perhaps transparent ellipsoids like this:

t