Code coverage tests

This page documents the degree to which the PARI/GP source code is tested by our public test suite, distributed with the source distribution in directory src/test/. This is measured by the gcov utility; we then process gcov output using the lcov frond-end.

We test a few variants depending on Configure flags on the pari.math.u-bordeaux.fr machine (x86_64 architecture), and agregate them in the final report:

The target is to exceed 90% coverage for all mathematical modules (given that branches depending on DEBUGLEVEL or DEBUGMEM are not covered). This script is run to produce the results below.

LCOV - code coverage report
Current view: top level - graph - plotport.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.12.1 lcov report (development 25819-e703fe1174) Lines: 1186 1344 88.2 %
Date: 2020-09-18 06:10:04 Functions: 110 125 88.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2000  The PARI group.
       2             : 
       3             : This file is part of the PARI/GP package.
       4             : 
       5             : PARI/GP is free software; you can redistribute it and/or modify it under the
       6             : terms of the GNU General Public License as published by the Free Software
       7             : Foundation. It is distributed in the hope that it will be useful, but WITHOUT
       8             : ANY WARRANTY WHATSOEVER.
       9             : 
      10             : Check the License for details. You should have received a copy of it, along
      11             : with the package; see the file 'COPYING'. If not, write to the Free Software
      12             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      13             : 
      14             : /*******************************************************************/
      15             : /*                                                                 */
      16             : /*                         PLOT ROUTINES                           */
      17             : /*                                                                 */
      18             : /*******************************************************************/
      19             : #include "pari.h"
      20             : #include "paripriv.h"
      21             : #include "rect.h"
      22             : 
      23             : static void (*pari_get_plot)(PARI_plot *);
      24             : 
      25             : /* no need for THREAD: OK to share this */
      26             : static hashtable *rgb_colors = NULL;
      27             : 
      28             : THREAD PariRect rectgraph[18]; /*NUMRECT*/
      29             : static THREAD long current_color[18]; /*NUMRECT*/
      30             : 
      31             : static long plotpoint_itype = 0, rectline_itype  = 0;
      32             : 
      33             : const long NUMRECT = 18;
      34             : const long RECUR_MAXDEPTH = 10;
      35             : const double RECUR_PREC = 0.001;
      36             : const long DEFAULT_COLOR = 1, AXIS_COLOR = 2;
      37             : 
      38             : enum {
      39             :   ROt_MV = 1, /* Move */
      40             :   ROt_PT,  /* Point */
      41             :   ROt_LN,  /* Line */
      42             :   ROt_BX,  /* Box */
      43             :   ROt_FBX, /* Filled Box */
      44             :   ROt_MP,  /* Multiple point */
      45             :   ROt_ML,  /* Multiple lines */
      46             :   ROt_ST,  /* String */
      47             :   ROt_PTT,  /* Point type change */
      48             :   ROt_LNT,  /* Line type change */
      49             :   ROt_PTS,  /* Point size change */
      50             :   ROt_NULL, /* To be the start of the chain */
      51             : };
      52             : 
      53             : /* string justification */
      54             : #define RoSTdirLEFT       0x00
      55             : #define RoSTdirRIGHT      0x02
      56             : #define RoSTdirHPOS_mask  0x03
      57             : #define RoSTdirBOTTOM     0x00
      58             : #define RoSTdirTOP        0x08
      59             : #define RoSTdirVPOS_mask  0x0c
      60             : #define RoSTdirHGAP       0x10
      61             : #define RoSTdirVGAP       0x20
      62             : 
      63             : /* ploth flags */
      64             : #define PLOT_PARAMETRIC   0x00001
      65             : #define PLOT_RECURSIVE    0x00002
      66             : #define PLOT_NO_RESCALE   0x00004
      67             : #define PLOT_NO_AXE_X     0x00008
      68             : #define PLOT_NO_AXE_Y     0x00010
      69             : #define PLOT_NO_FRAME     0x00020
      70             : #define PLOT_POINTS       0x00040
      71             : #define PLOT_POINTS_LINES 0x00080
      72             : #define PLOT_SPLINES      0x00100
      73             : #define PLOT_NO_TICK_X    0x00200
      74             : #define PLOT_NO_TICK_Y    0x00400
      75             : #define PLOT_NODOUBLETICK 0x00800
      76             : #define PLOT_COMPLEX      0x01000
      77             : #define PLOT_PARA         0x02000
      78             : 
      79             : INLINE long
      80      158583 : DTOL(double t) { return (long)(t + 0.5); }
      81             : 
      82             : static const long PS_WIDTH = 1120 - 60; /* 1400 - 60 for hi-res */
      83             : static const long PS_HEIGH = 800 - 40; /* 1120 - 60 for hi-res */
      84             : static const long PS_SCALE = 1000; /* Allowing 64x zoom on 500ppi */
      85             : 
      86             : static void
      87           0 : _psdraw_scale(PARI_plot *T, GEN w, GEN x, GEN y)
      88             : {
      89           0 :   pari_sp av = avma;
      90           0 :   FILE *F = fopen(current_psfile, "a");
      91           0 :   if (!F) pari_err_FILE("postscript file",current_psfile);
      92           0 :   fputs(rect2ps(w,x,y,T), F);
      93           0 :   fclose(F); set_avma(av);
      94           0 : }
      95             : static void
      96           0 : _psdraw(PARI_plot *T, GEN w, GEN x, GEN y)
      97           0 : { (void)T; _psdraw_scale(NULL,w,x,y); }
      98             : static void
      99          49 : pari_get_psplot(PARI_plot *T)
     100             : {
     101          49 :   T->width  = PS_WIDTH;
     102          49 :   T->height = PS_HEIGH;
     103          49 :   T->fheight= 15;
     104          49 :   T->fwidth = 6;
     105          49 :   T->hunit  = 5;
     106          49 :   T->vunit  = 5;
     107          49 :   T->dwidth = 0;
     108          49 :   T->dheight= 0;
     109          49 :   T->draw = NULL;
     110          49 : }
     111             : static void
     112          86 : pari_get_svgplot(PARI_plot *T)
     113             : {
     114          86 :   T->width   = 480;
     115          86 :   T->height  = 320;
     116          86 :   T->fheight = 12;
     117          86 :   T->fwidth  = 6;
     118          86 :   T->hunit   = 3;
     119          86 :   T->vunit   = 3;
     120          86 :   T->dwidth  = 0;
     121          86 :   T->dheight = 0;
     122          86 :   T->draw = NULL;
     123          86 : }
     124             : 
     125             : /********************************************************************/
     126             : /**                                                                **/
     127             : /**                      RECTPLOT FUNCTIONS                        **/
     128             : /**                                                                **/
     129             : /********************************************************************/
     130             : void
     131        1690 : pari_init_graphics(void) { pari_get_plot = &pari_get_svgplot; }
     132             : 
     133             : void
     134        1680 : pari_set_plot_engine(void (*plot)(PARI_plot *))
     135             : {
     136             :   long n;
     137        1680 :   pari_get_plot = plot;
     138       31920 :   for (n = 0; n < NUMRECT; n++)
     139             :   {
     140       30240 :     PariRect *e = &rectgraph[n];
     141       30240 :     RHead(e) = RTail(e) = NULL;
     142       30240 :     RXsize(e) = RYsize(e) = 0;
     143             :   }
     144        1680 : }
     145             : 
     146             : void
     147        1680 : pari_kill_plot_engine(void)
     148             : {
     149             :   int i;
     150       31920 :   for (i=0; i<NUMRECT; i++)
     151             :   {
     152       30240 :     PariRect *e = &rectgraph[i];
     153       30240 :     if (RHead(e)) plotkill(i);
     154             :   }
     155        1680 :   if (rgb_colors)
     156             :   {
     157          25 :     pari_free((void*)rgb_colors->table);
     158          25 :     pari_free((void*)rgb_colors);
     159             :   }
     160        1680 : }
     161             : 
     162             : static PariRect *
     163       23698 : check_rect(long ne)
     164             : {
     165       23698 :   const char *f = "graphic function";
     166       23698 :   const long m = NUMRECT-1;
     167       23698 :   if (ne < 0) pari_err_DOMAIN(f, "rectwindow", "<", gen_0, stoi(ne));
     168       23690 :   if (ne > m) pari_err_DOMAIN(f, "rectwindow", ">", stoi(m), stoi(ne));
     169       23686 :   return &rectgraph[ne];
     170             : }
     171             : 
     172             : static PariRect *
     173       23406 : check_rect_init(long ne)
     174             : {
     175       23406 :   PariRect *e = check_rect(ne);
     176       23398 :   if (!RHead(e)) pari_err_TYPE("graphic function [use plotinit()]", stoi(ne));
     177       23398 :   return e;
     178             : }
     179             : static void
     180       33027 : Rchain(PariRect *e, RectObj *z)
     181             : {
     182       33027 :   if (!RHead(e)) RHead(e) = z; else RoNext(RTail(e)) = z;
     183       33027 :   RTail(e) = z;
     184       33027 :   RoNext(z) = NULL;
     185       33027 : }
     186             : 
     187             : static long
     188       10686 : rgb_to_long(long r, long g, long b)
     189       10686 : { return (r << 16) | (g << 8) | b; }
     190             : /* c from graphcolormap */
     191             : static long
     192       10682 : colormap_to_color(long i)
     193             : {
     194       10682 :   GEN c = GP_DATA->colormap;
     195       10682 :   long k = i+1, l = lg(c)-1;
     196             :   int r, g, b;
     197       10682 :   if (k > l)
     198           4 :     pari_err_COMPONENT("graphcolormap",">", stoi(l), stoi(k));
     199       10678 :   color_to_rgb(gel(c, k), &r,&g,&b);
     200       10678 :   return rgb_to_long(r, g, b);
     201             : }
     202             : 
     203             : static void
     204         284 : initrect_i(long ne, long x, long y)
     205             : {
     206             :   PariRect *e;
     207             :   RectObj *z;
     208             : 
     209         284 :   if (x <= 1) pari_err_DOMAIN("plotinit", "x", "<=", gen_1, stoi(x));
     210         284 :   if (y <= 1) pari_err_DOMAIN("plotinit", "y", "<=", gen_1, stoi(y));
     211         284 :   e = check_rect(ne); if (RHead(e)) plotkill(ne);
     212             : 
     213         280 :   current_color[ne] = colormap_to_color(DEFAULT_COLOR);
     214         280 :   z = (RectObj*) pari_malloc(sizeof(RectObj));
     215         280 :   RoType(z) = ROt_NULL;
     216         280 :   Rchain(e, z);
     217         280 :   RXsize(e) = x; RXcursor(e) = 0; RXscale(e) = 1; RXshift(e) = 0;
     218         280 :   RYsize(e) = y; RYcursor(e) = 0; RYscale(e) = 1; RYshift(e) = 0;
     219         280 : }
     220             : static long
     221         148 : initrect_get_arg(GEN x, long dft)
     222             : {
     223         148 :   if (!x) return dft;
     224          78 :   if (typ(x) != t_INT) pari_err_TYPE("plotinit",x);
     225          78 :   return itos(x);
     226             : }
     227             : void
     228          74 : plotinit(long ne, GEN x, GEN y, long flag)
     229             : {
     230          74 :   const long m = NUMRECT-3;
     231             :   long xi, yi;
     232             :   PARI_plot T;
     233             : 
     234          74 :   if (flag)
     235             :   {
     236           0 :     pari_get_plot(&T);
     237           0 :     xi = T.width -1; if (x) xi = DTOL(xi * gtodouble(x));
     238           0 :     yi = T.height-1; if (y) yi = DTOL(yi * gtodouble(y));
     239             :   }
     240             :   else
     241             :   {
     242          74 :     if (!x || !y) pari_get_plot(&T);
     243          74 :     xi = initrect_get_arg(x, T.width -1);
     244          74 :     yi = initrect_get_arg(y, T.height-1);
     245             :   }
     246          74 :   if (ne > m) pari_err_DOMAIN("plotinit", "rectwindow", ">", stoi(m), stoi(ne));
     247          70 :   initrect_i(ne, xi, yi);
     248          66 : }
     249             : 
     250             : GEN
     251          47 : plotcursor(long ne)
     252             : {
     253          47 :   PariRect *e = check_rect_init(ne);
     254          47 :   return mkvec2s((long)RXcursor(e), (long)RYcursor(e));
     255             : }
     256             : 
     257             : static void
     258         132 : plotscale0(long ne, double x1, double x2, double y1, double y2)
     259             : {
     260         132 :   PariRect *e = check_rect_init(ne);
     261             :   double x, y;
     262             : 
     263         132 :   x = RXshift(e) + RXscale(e) * RXcursor(e);
     264         132 :   y = RYshift(e) + RYscale(e) * RYcursor(e);
     265         132 :   RXscale(e) = RXsize(e)/(x2-x1); RXshift(e) = -x1*RXscale(e);
     266         132 :   RYscale(e) = RYsize(e)/(y1-y2); RYshift(e) = -y2*RYscale(e);
     267         132 :   RXcursor(e) = (x - RXshift(e)) / RXscale(e);
     268         132 :   RYcursor(e) = (y - RYshift(e)) / RYscale(e);
     269         132 : }
     270             : void
     271          18 : plotscale(long ne, GEN x1, GEN x2, GEN y1, GEN y2)
     272          18 : { plotscale0(ne, gtodouble(x1), gtodouble(x2), gtodouble(y1), gtodouble(y2)); }
     273             : 
     274             : static void
     275         865 : plotmove0(long ne, double x, double y, long relative)
     276             : {
     277         865 :   PariRect *e = check_rect_init(ne);
     278         857 :   RectObj *z = (RectObj*) pari_malloc(sizeof(RectObj1P));
     279             : 
     280         857 :   if (relative) { RXcursor(e) += x; RYcursor(e) += y; }
     281         839 :   else          { RXcursor(e) = x; RYcursor(e) = y; }
     282         857 :   RoType(z) = ROt_MV;
     283         857 :   RoMVx(z) = RXcursor(e) * RXscale(e) + RXshift(e);
     284         857 :   RoMVy(z) = RYcursor(e) * RYscale(e) + RYshift(e);
     285         857 :   Rchain(e, z);
     286         857 : }
     287             : static void
     288         728 : _move(long ne, double x, double y)
     289         728 : { plotmove0(ne,x,y,0); }
     290             : void
     291         119 : plotmove(long ne, GEN x, GEN y)
     292         119 : { plotmove0(ne,gtodouble(x),gtodouble(y),0); }
     293             : void
     294          18 : plotrmove(long ne, GEN x, GEN y)
     295          18 : { plotmove0(ne,gtodouble(x),gtodouble(y),1); }
     296             : 
     297             : /* ROt_MV/ROt_PT */
     298             : static void
     299          36 : plotpoint0(long ne, double x, double y,long relative)
     300             : {
     301          36 :   PariRect *e = check_rect_init(ne);
     302          36 :   RectObj *z = (RectObj*) pari_malloc(sizeof(RectObj1P));
     303             : 
     304          36 :   if (relative) { RXcursor(e) += x; RYcursor(e) += y; }
     305          18 :   else          { RXcursor(e) = x; RYcursor(e) = y; }
     306          36 :   RoPTx(z) = RXcursor(e)*RXscale(e) + RXshift(e);
     307          36 :   RoPTy(z) = RYcursor(e)*RYscale(e) + RYshift(e);
     308          36 :   RoType(z) = ( DTOL(RoPTx(z)) < 0
     309          36 :                 || DTOL(RoPTy(z)) < 0 || DTOL(RoPTx(z)) > RXsize(e)
     310          72 :                 || DTOL(RoPTy(z)) > RYsize(e) ) ? ROt_MV : ROt_PT;
     311          36 :   Rchain(e, z);
     312          36 :   RoCol(z) = current_color[ne];
     313          36 : }
     314             : static void
     315          18 : plotpoint(long ne, GEN x, GEN y)
     316          18 : { plotpoint0(ne,gtodouble(x),gtodouble(y),0); }
     317             : void
     318          18 : plotrpoint(long ne, GEN x, GEN y)
     319          18 : { plotpoint0(ne,gtodouble(x),gtodouble(y),1); }
     320             : 
     321             : GEN
     322           8 : plotcolor(long ne, GEN c)
     323             : {
     324           8 :   long t = typ(c), n = lg(GP_DATA->colormap)-2;
     325             :   int r, g, b;
     326           8 :   check_rect(ne);
     327           8 :   if (t == t_INT)
     328             :   {
     329           8 :     long i = itos(c);
     330           8 :     if (i < 0) pari_err_DOMAIN("plotcolor", "color", "<", gen_0, c);
     331           8 :     if (i > n) pari_err_DOMAIN("plotcolor", "color", ">", stoi(n), c);
     332           8 :     c = gel(GP_DATA->colormap,i+1);
     333             :   }
     334             :   else
     335             :   {
     336           0 :     if (t == t_VEC) { c = ZV_to_zv(c); t = typ(c); }
     337           0 :     if (t != t_VECSMALL && t != t_STR) pari_err_TYPE("plotcolor",c);
     338             :   }
     339           8 :   color_to_rgb(c, &r,&g,&b);
     340           8 :   current_color[ne] = rgb_to_long(r,g,b);
     341           8 :   return mkvec3s(r, g, b);
     342             : }
     343             : 
     344             : /* ROt_MV/ROt_LN */
     345             : static void
     346         222 : rectline0(long ne, double gx2, double gy2, long relative)
     347             : {
     348             :   double dx, dy, dxy, xmin, xmax, ymin, ymax, x1, y1, x2, y2;
     349         222 :   PariRect *e = check_rect_init(ne);
     350         222 :   RectObj *z = (RectObj*) pari_malloc(sizeof(RectObj2P));
     351         222 :   const double c = 1 + 1e-10;
     352             : 
     353         222 :   x1 = RXcursor(e)*RXscale(e) + RXshift(e);
     354         222 :   y1 = RYcursor(e)*RYscale(e) + RYshift(e);
     355         222 :   if (relative)
     356          18 :     { RXcursor(e)+=gx2; RYcursor(e)+=gy2; }
     357             :   else
     358         204 :     { RXcursor(e)=gx2; RYcursor(e)=gy2; }
     359         222 :   x2 = RXcursor(e)*RXscale(e) + RXshift(e);
     360         222 :   y2 = RYcursor(e)*RYscale(e) + RYshift(e);
     361         222 :   xmin = maxdd(mindd(x1,x2),0); xmax = mindd(maxdd(x1,x2),RXsize(e));
     362         222 :   ymin = maxdd(mindd(y1,y2),0); ymax = mindd(maxdd(y1,y2),RYsize(e));
     363         222 :   dxy = x1*y2 - y1*x2; dx = x2-x1; dy = y2-y1;
     364         222 :   if (dy)
     365             :   {
     366         129 :     double a = (dxy + RYsize(e)*dx) / dy, b = dxy / dy;
     367         129 :     if (dx*dy < 0)
     368          18 :     { xmin=maxdd(xmin,a); xmax=mindd(xmax,b); }
     369             :     else
     370         111 :     { xmin=maxdd(xmin,b); xmax=mindd(xmax,a); }
     371             :   }
     372         222 :   if (dx)
     373             :   {
     374         129 :     double a = (RXsize(e)*dy - dxy) / dx, b = -dxy / dx;
     375         129 :     if (dx*dy < 0)
     376          18 :     { ymin=maxdd(ymin,a); ymax=mindd(ymax,b); }
     377             :     else
     378         111 :     { ymin=maxdd(ymin,b); ymax=mindd(ymax,a); }
     379             :   }
     380         222 :   RoLNx1(z) = xmin;
     381         222 :   RoLNx2(z) = xmax;
     382         222 :   if (dx*dy < 0) { RoLNy1(z) = ymax; RoLNy2(z) = ymin; }
     383         204 :   else         { RoLNy1(z) = ymin; RoLNy2(z) = ymax; }
     384         222 :   RoType(z) = (xmin>xmax*c || ymin>ymax*c) ? ROt_MV : ROt_LN;
     385         222 :   Rchain(e, z);
     386         222 :   RoCol(z) = current_color[ne];
     387         222 : }
     388             : static void
     389         186 : _line(long ne, double x, double y)
     390         186 : { rectline0(ne, x, y, 0); }
     391             : void
     392          18 : plotline(long ne, GEN gx2, GEN gy2)
     393          18 : { rectline0(ne, gtodouble(gx2), gtodouble(gy2),0); }
     394             : void
     395          18 : plotrline(long ne, GEN gx2, GEN gy2)
     396          18 : { rectline0(ne, gtodouble(gx2), gtodouble(gy2),1); }
     397             : 
     398             : enum {
     399             :   TICKS_CLOCKW   = 1, /* Draw in clockwise direction */
     400             :   TICKS_ACLOCKW  = 2, /* Draw in anticlockwise direction */
     401             :   TICKS_ENDSTOO  = 4, /* Draw at endspoints if needed */
     402             :   TICKS_NODOUBLE = 8  /* Do not draw double-length ticks */
     403             : };
     404             : 
     405             : /* Given coordinates of ends of a line, and labels l1 l2 attached to the
     406             :  * ends, plot ticks where the label coordinate takes "round" values */
     407             : static void
     408         456 : rectticks(PARI_plot *WW, long ne, double dx1, double dy1, double dx2,
     409             :           double dy2, double l1, double l2, long flags)
     410             : {
     411             :   long dx, dy, dxy, dxy1, x1, y1, x2, y2, nticks, n, n1, dn;
     412             :   double minstep, maxstep, step, l_min, l_max, minl, maxl, dl, dtx, dty, x, y;
     413             :   double ddx, ddy;
     414         456 :   const double mult[3] = { 2./1., 5./2., 10./5. };
     415         456 :   PariRect *e = check_rect_init(ne);
     416         456 :   int do_double = !(flags & TICKS_NODOUBLE);
     417             : 
     418         456 :   x1 = DTOL(dx1*RXscale(e) + RXshift(e));
     419         456 :   y1 = DTOL(dy1*RYscale(e) + RYshift(e));
     420         456 :   x2 = DTOL(dx2*RXscale(e) + RXshift(e));
     421         456 :   y2 = DTOL(dy2*RYscale(e) + RYshift(e));
     422         456 :   dx = x2 - x1; if (dx < 0) dx = -dx;
     423         456 :   dy = y2 - y1; if (dy < 0) dy = -dy;
     424         456 :   dxy1 = maxss(dx, dy);
     425         456 :   dx /= WW->hunit;
     426         456 :   dy /= WW->vunit;
     427         456 :   if (dx > 1000 || dy > 1000)
     428           0 :     dxy = 1000; /* avoid overflow */
     429             :   else
     430         456 :     dxy = usqrt(dx*dx + dy*dy);
     431         456 :   nticks = (long) ((dxy + 2.5)/4);
     432         456 :   if (!nticks) return;
     433             : 
     434             :   /* Find nticks (or less) "round" numbers between l1 and l2. For our purpose
     435             :    * round numbers have "last significant" decimal digit either
     436             :    *    - any;
     437             :    *    - even;
     438             :    *    - divisible by 5.
     439             :    * We need to choose which alternative is better. */
     440         456 :   if (l1 < l2)
     441         228 :     l_min = l1, l_max = l2;
     442             :   else
     443         228 :     l_min = l2, l_max = l1;
     444         456 :   minstep = (l_max - l_min)/(nticks + 1);
     445         456 :   maxstep = 2.5*(l_max - l_min);
     446         456 :   step = exp(log(10.) * floor(log10(minstep)));
     447         456 :   if (!(flags & TICKS_ENDSTOO)) {
     448         456 :     double d = 2*(l_max - l_min)/dxy1; /* Two pixels off */
     449         456 :     l_min += d;
     450         456 :     l_max -= d;
     451             :   }
     452         456 :   for (n = 0; ; n++)
     453             :   {
     454        1488 :     if (step >= maxstep) return;
     455             : 
     456        1488 :     if (step >= minstep) {
     457         456 :       minl = ceil(l_min/step);
     458         456 :       maxl = floor(l_max/step);
     459         456 :       if (minl <= maxl && maxl - minl + 1 <= nticks) {
     460         456 :         nticks = (long) (maxl - minl + 1);
     461         456 :         l_min = minl * step;
     462         456 :         l_max = maxl * step; break;
     463             :       }
     464             :     }
     465        1032 :     step *= mult[ n % 3 ];
     466             :   }
     467             :   /* Where to position doubleticks. Variants:
     468             :    * small: each 5, double: each 10  ; n=2 mod 3
     469             :    * small: each 2, double: each 10  ; n=1 mod 3
     470             :    * small: each 1, double: each  5 */
     471         456 :   dn = (n % 3 == 2)? 2: 5;
     472         456 :   n1 = ((long)minl) % dn; /* unused if do_double = FALSE */
     473             : 
     474             :   /* now l_min and l_max keep min/max values of l with ticks, and nticks is
     475             :      the number of ticks to draw. */
     476         456 :   if (nticks == 1) ddx = ddy = 0; /* -Wall */
     477             :   else {
     478         456 :     dl = (l_max - l_min)/(nticks - 1);
     479         456 :     ddx = (dx2 - dx1) * dl / (l2 - l1);
     480         456 :     ddy = (dy2 - dy1) * dl / (l2 - l1);
     481             :   }
     482         456 :   x = dx1 + (dx2 - dx1) * (l_min - l1) / (l2 - l1);
     483         456 :   y = dy1 + (dy2 - dy1) * (l_min - l1) / (l2 - l1);
     484             :   /* assume hunit and vunit form a square. For clockwise ticks: */
     485         456 :   dtx = WW->hunit * dy/dxy * (y2 > y1 ? 1 : -1); /* y-coord runs down */
     486         456 :   dty = WW->vunit * dx/dxy * (x2 > x1 ? 1 : -1);
     487       10940 :   for (n = 0; n < nticks; n++, x += ddx, y += ddy) {
     488       10484 :     RectObj *z = (RectObj*) pari_malloc(sizeof(RectObj2P));
     489       10484 :     double lunit = WW->hunit > 1 ? 1.5 : 2;
     490       10484 :     double l = (do_double && (n + n1) % dn == 0) ? lunit: 1;
     491             :     double x1, x2, y1, y2;
     492       10484 :     x1 = x2 = x*RXscale(e) + RXshift(e);
     493       10484 :     y1 = y2 = y*RYscale(e) + RYshift(e);
     494       10484 :     if (flags & TICKS_CLOCKW)  { x1 += dtx*l; y1 -= dty*l; }
     495       10484 :     if (flags & TICKS_ACLOCKW) { x2 -= dtx*l; y2 += dty*l; }
     496       10484 :     RoLNx1(z) = x1; RoLNy1(z) = y1;
     497       10484 :     RoLNx2(z) = x2; RoLNy2(z) = y2;
     498       10484 :     RoType(z) = ROt_LN;
     499       10484 :     Rchain(e, z);
     500       10484 :     RoCol(z) = current_color[ne];
     501             :   }
     502             : }
     503             : 
     504             : static void
     505         150 : rectbox0(long ne, double gx2, double gy2, long relative, long filled)
     506             : {
     507             :   double xx, yy, x1, y1, x2, y2, xmin, ymin, xmax, ymax;
     508         150 :   PariRect *e = check_rect_init(ne);
     509         150 :   RectObj *z = (RectObj*) pari_malloc(sizeof(RectObj2P));
     510             : 
     511         150 :   x1 = RXcursor(e)*RXscale(e) + RXshift(e);
     512         150 :   y1 = RYcursor(e)*RYscale(e) + RYshift(e);
     513         150 :   if (relative)
     514          18 :   { xx = RXcursor(e)+gx2; yy = RYcursor(e)+gy2; }
     515             :   else
     516         132 :   {  xx = gx2; yy = gy2; }
     517         150 :   x2 = xx*RXscale(e) + RXshift(e);
     518         150 :   y2 = yy*RYscale(e) + RYshift(e);
     519         150 :   xmin = maxdd(mindd(x1,x2),0); xmax = mindd(maxdd(x1,x2),RXsize(e));
     520         150 :   ymin = maxdd(mindd(y1,y2),0); ymax = mindd(maxdd(y1,y2),RYsize(e));
     521             : 
     522         150 :   RoType(z) = filled ? ROt_FBX: ROt_BX;
     523         150 :   RoBXx1(z) = xmin; RoBXy1(z) = ymin;
     524         150 :   RoBXx2(z) = xmax; RoBXy2(z) = ymax;
     525         150 :   Rchain(e, z);
     526         150 :   RoCol(z) = current_color[ne];
     527         150 : }
     528             : static void
     529         114 : _box(long ne, double x, double y)
     530         114 : { rectbox0(ne, x, y, 0, 0); }
     531             : void
     532          18 : plotbox(long ne, GEN gx2, GEN gy2, long f)
     533          18 : { rectbox0(ne, gtodouble(gx2), gtodouble(gy2), 0, f); }
     534             : void
     535          18 : plotrbox(long ne, GEN gx2, GEN gy2, long f)
     536          18 : { rectbox0(ne, gtodouble(gx2), gtodouble(gy2), 1, f); }
     537             : 
     538             : static void
     539       33347 : freeobj(RectObj *z) {
     540       33347 :   switch(RoType(z)) {
     541       10166 :     case ROt_MP: case ROt_ML:
     542       10166 :       pari_free(RoMPxs(z));
     543       10166 :       pari_free(RoMPys(z)); break;
     544         504 :     case ROt_ST:
     545         504 :       pari_free(RoSTs(z)); break;
     546             :   }
     547       33347 :   pari_free(z);
     548       33347 : }
     549             : 
     550             : void
     551         280 : plotkill(long ne)
     552             : {
     553             :   RectObj *z, *t;
     554         280 :   PariRect *e = check_rect_init(ne);
     555             : 
     556         280 :   z = RHead(e);
     557         280 :   RHead(e) = RTail(e) = NULL;
     558         280 :   RXsize(e) = RYsize(e) = 0;
     559         280 :   RXcursor(e) = RYcursor(e) = 0;
     560         280 :   RXscale(e) = RYscale(e) = 1;
     561         280 :   RXshift(e) = RYshift(e) = 0;
     562       33583 :   while (z) { t = RoNext(z); freeobj(z); z = t; }
     563         280 : }
     564             : 
     565             : /* ROt_MP */
     566             : static void
     567          36 : plotpoints0(long ne, double *X, double *Y, long lx)
     568             : {
     569             :   double *px, *py;
     570          36 :   long i, cp=0;
     571          36 :   PariRect *e = check_rect_init(ne);
     572          36 :   RectObj *z = (RectObj*) pari_malloc(sizeof(RectObjMP));
     573             : 
     574          36 :   RoMPxs(z) = px = (double*) pari_malloc(lx*sizeof(double));
     575          36 :   RoMPys(z) = py = (double*) pari_malloc(lx*sizeof(double));
     576        3620 :   for (i=0; i<lx; i++)
     577             :   {
     578        3584 :     double x = RXscale(e)*X[i] + RXshift(e);
     579        3584 :     double y = RYscale(e)*Y[i] + RYshift(e);
     580        3584 :     if (x >= 0 && y >= 0 && x <= RXsize(e) && y <= RYsize(e))
     581             :     {
     582        3582 :       px[cp] = x; py[cp] = y; cp++;
     583             :     }
     584             :   }
     585          36 :   RoType(z) = ROt_MP;
     586          36 :   RoMPcnt(z) = cp;
     587          36 :   Rchain(e, z);
     588          36 :   RoCol(z) = current_color[ne];
     589          36 : }
     590             : void
     591          36 : plotpoints(long ne, GEN X, GEN Y)
     592             : {
     593          36 :   pari_sp av = avma;
     594             :   double *px, *py;
     595             :   long i, lx;
     596             : 
     597          36 :   if (!is_vec_t(typ(X)) || !is_vec_t(typ(Y))) { plotpoint(ne, X, Y); return; }
     598          18 :   lx = lg(X); if (lg(Y) != lx) pari_err_DIM("plotpoints");
     599          18 :   lx--; if (!lx) return;
     600             : 
     601          18 :   px = (double*)stack_malloc_align(lx*sizeof(double), sizeof(double)); X++;
     602          18 :   py = (double*)stack_malloc_align(lx*sizeof(double), sizeof(double)); Y++;
     603         198 :   for (i=0; i<lx; i++)
     604             :   {
     605         180 :     px[i] = gtodouble(gel(X,i));
     606         180 :     py[i] = gtodouble(gel(Y,i));
     607             :   }
     608          18 :   plotpoints0(ne,px,py,lx); set_avma(av);
     609             : }
     610             : 
     611             : /* ROt_ML */
     612             : static void
     613       10090 : rectlines0(long ne, double *x, double *y, long lx, long flag)
     614             : {
     615             :   long i,I;
     616             :   double *ptx,*pty;
     617       10090 :   PariRect *e = check_rect_init(ne);
     618       10090 :   RectObj *z = (RectObj*) pari_malloc(sizeof(RectObj2P));
     619             : 
     620       10090 :   I = flag ? lx+1 : lx;
     621       10090 :   ptx = (double*) pari_malloc(I*sizeof(double));
     622       10090 :   pty = (double*) pari_malloc(I*sizeof(double));
     623      222408 :   for (i=0; i<lx; i++)
     624             :   {
     625      212318 :     ptx[i] = RXscale(e)*x[i] + RXshift(e);
     626      212318 :     pty[i] = RYscale(e)*y[i] + RYshift(e);
     627             :   }
     628       10090 :   if (flag)
     629             :   {
     630           0 :     ptx[i] = RXscale(e)*x[0] + RXshift(e);
     631           0 :     pty[i] = RYscale(e)*y[0] + RYshift(e);
     632             :   }
     633       10090 :   Rchain(e, z);
     634       10090 :   RoType(z) = ROt_ML;
     635       10090 :   RoMLcnt(z) = I;
     636       10090 :   RoMLxs(z) = ptx;
     637       10090 :   RoMLys(z) = pty;
     638       10090 :   RoCol(z) = current_color[ne];
     639       10090 : }
     640             : void
     641          36 : plotlines(long ne, GEN X, GEN Y, long flag)
     642             : {
     643          36 :   pari_sp av = avma;
     644             :   double *x, *y;
     645             :   long i, lx;
     646             : 
     647          36 :   if (!is_vec_t(typ(X)) || !is_vec_t(typ(Y))) { plotline(ne, X, Y); return; }
     648          18 :   lx = lg(X); if (lg(Y) != lx) pari_err_DIM("plotlines");
     649          18 :   lx--; if (!lx) return;
     650             : 
     651          18 :   x = (double*)stack_malloc_align(lx*sizeof(double), sizeof(double)); X++;
     652          18 :   y = (double*)stack_malloc_align(lx*sizeof(double), sizeof(double)); Y++;
     653         108 :   for (i=0; i<lx; i++)
     654             :   {
     655          90 :     x[i] = gtodouble(gel(X,i));
     656          90 :     y[i] = gtodouble(gel(Y,i));
     657             :   }
     658          18 :   rectlines0(ne,x,y,lx,flag); set_avma(av);
     659             : }
     660             : 
     661             : /* ROt_ST */
     662             : void
     663         464 : plotstring(long ne, char *str, long dir)
     664             : {
     665         464 :   PariRect *e = check_rect_init(ne);
     666         464 :   RectObj *z = (RectObj*) pari_malloc(sizeof(RectObjST));
     667         464 :   long l = strlen(str);
     668         464 :   char *s = (char *) pari_malloc(l+1);
     669             : 
     670         464 :   memcpy(s,str,l+1);
     671         464 :   RoType(z) = ROt_ST;
     672         464 :   RoSTl(z) = l;
     673         464 :   RoSTs(z) = s;
     674         464 :   RoSTx(z) = RXscale(e)*RXcursor(e)+RXshift(e);
     675         464 :   RoSTy(z) = RYscale(e)*RYcursor(e)+RYshift(e);
     676         464 :   RoSTdir(z) = dir;
     677         464 :   Rchain(e, z);
     678         464 :   RoCol(z) = current_color[ne];
     679         464 : }
     680             : 
     681             : /* ROt_PTT */
     682             : void
     683          18 : plotpointtype(long ne, long type)
     684             : {
     685          18 :  if (ne == -1) plotpoint_itype = type;
     686             :  else {
     687          18 :    PariRect *e = check_rect_init(ne);
     688          18 :    RectObj *z = (RectObj*) pari_malloc(sizeof(RectObjPN));
     689          18 :    RoType(z) = ROt_PTT;
     690          18 :    RoPTTpen(z) = type;
     691          18 :    Rchain(e, z);
     692             :  }
     693          18 : }
     694             : 
     695             : /* ROt_PTS. FIXME: this function is a noop, since no graphic driver implement
     696             :  * this code. ne == -1 (change globally). */
     697             : void
     698           0 : plotpointsize(long ne, GEN size)
     699             : {
     700           0 :  if (ne == -1) { /*do nothing*/ }
     701             :  else {
     702           0 :    PariRect *e = check_rect_init(ne);
     703           0 :    RectObj *z = (RectObj*) pari_malloc(sizeof(RectObjPS));
     704           0 :    RoType(z) = ROt_PTS;
     705           0 :    RoPTSsize(z) = gtodouble(size);
     706           0 :    Rchain(e, z);
     707             :  }
     708           0 : }
     709             : 
     710             : void
     711       10390 : plotlinetype(long ne, long type)
     712             : {
     713       10390 :  if (ne == -1) rectline_itype = type;
     714             :  else {
     715       10390 :    PariRect *e = check_rect_init(ne);
     716       10390 :    RectObj *z = (RectObj*) pari_malloc(sizeof(RectObjPN));
     717       10390 :    RoType(z) = ROt_LNT;
     718       10390 :    RoLNTpen(z) = type;
     719       10390 :    Rchain(e, z);
     720             :  }
     721       10390 : }
     722             : 
     723             : #define RECT_CP_RELATIVE  0x1
     724             : #define RECT_CP_NW        0x0
     725             : #define RECT_CP_SW        0x2
     726             : #define RECT_CP_SE        0x4
     727             : #define RECT_CP_NE        0x6
     728             : 
     729             : static double*
     730           0 : cpd(double* R, size_t t)
     731           0 : { void *o = pari_malloc(t * sizeof(double)); memcpy(o,R,t); return (double*)o; }
     732             : static void*
     733         460 : cp(void* R, size_t t)
     734         460 : { void *o = pari_malloc(t); memcpy(o,R,t); return o; }
     735             : void
     736          36 : plotcopy(long source, long dest, GEN xoff, GEN yoff, long flag)
     737             : {
     738          36 :   PariRect *s = check_rect_init(source), *d = check_rect_init(dest);
     739          36 :   RectObj *R, *tail = RTail(d);
     740             :   long i, x, y;
     741          36 :   if (flag & RECT_CP_RELATIVE) {
     742          32 :     double xd = gtodouble(xoff), yd = gtodouble(yoff);
     743             :     PARI_plot T;
     744          32 :     if (xd > 1) pari_err_DOMAIN("plotcopy","dx",">",gen_1,xoff);
     745          28 :     if (xd < 0) pari_err_DOMAIN("plotcopy","dx","<",gen_0,xoff);
     746          24 :     if (yd > 1) pari_err_DOMAIN("plotcopy","dy",">",gen_1,yoff);
     747          20 :     if (yd < 0) pari_err_DOMAIN("plotcopy","dy","<",gen_0,yoff);
     748          16 :     pari_get_plot(&T);
     749          16 :     x = DTOL(xd * (T.width-1));
     750          16 :     y = DTOL(yd * (T.height-1));
     751             :   } else {
     752           4 :     if (typ(xoff) != t_INT) pari_err_TYPE("plotcopy",xoff);
     753           4 :     if (typ(yoff) != t_INT) pari_err_TYPE("plotcopy",yoff);
     754           4 :     x = itos(xoff);
     755           4 :     y = itos(yoff);
     756             :   }
     757          20 :   switch (flag & ~RECT_CP_RELATIVE)
     758             :   {
     759           8 :     case RECT_CP_NW: break;
     760           4 :     case RECT_CP_SW: y = RYsize(d) - RYsize(s) - y; break;
     761           4 :     case RECT_CP_SE: y = RYsize(d) - RYsize(s) - y; /* fall through */
     762           8 :     case RECT_CP_NE: x = RXsize(d) - RXsize(s) - x; break;
     763             :   }
     764         360 :   for (R = RHead(s); R; R = RoNext(R))
     765             :   {
     766             :     RectObj *o;
     767         340 :     switch(RoType(R))
     768             :     {
     769          40 :       case ROt_PT:
     770          40 :         o = (RectObj*)cp(R, sizeof(RectObj1P));
     771          40 :         RoPTx(o) += x; RoPTy(o) += y;
     772          40 :         break;
     773          80 :       case ROt_LN: case ROt_BX: case ROt_FBX:
     774          80 :         o = (RectObj*)cp(R, sizeof(RectObj2P));
     775          80 :         RoLNx1(o) += x; RoLNy1(o) += y;
     776          80 :         RoLNx2(o) += x; RoLNy2(o) += y;
     777          80 :         break;
     778          40 :       case ROt_MP: case ROt_ML:
     779          40 :         o = (RectObj*)cp(R, sizeof(RectObjMP));
     780          40 :         RoMPxs(o) = (double*)cp(RoMPxs(R), sizeof(double)*RoMPcnt(o));
     781          40 :         RoMPys(o) = (double*)cp(RoMPys(R), sizeof(double)*RoMPcnt(o));
     782         340 :         for (i=0; i<RoMPcnt(o); i++) { RoMPxs(o)[i] += x; RoMPys(o)[i] += y; }
     783          40 :         break;
     784          40 :       case ROt_ST:
     785          40 :         o = (RectObj*)cp(R, sizeof(RectObjST));
     786          40 :         RoSTs(o) = (char*)cp(RoSTs(R),RoSTl(R)+1);
     787          40 :         RoSTx(o) += x; RoSTy(o) += y;
     788          40 :         break;
     789         140 :       default: /* ROt_PTT, ROt_LNT, ROt_PTS */
     790         140 :         o = (RectObj*)cp(R, sizeof(RectObjPN));
     791         140 :         break;
     792             :     }
     793         340 :     RoNext(tail) = o; tail = o;
     794             :   }
     795          20 :   RoNext(tail) = NULL; RTail(d) = tail;
     796          20 : }
     797             : 
     798             : enum {CLIPLINE_NONEMPTY = 1, CLIPLINE_CLIP_1 = 2, CLIPLINE_CLIP_2 = 4};
     799             : /* A simpler way is to clip by 4 half-planes */
     800             : static int
     801         120 : clipline(double xmin, double xmax, double ymin, double ymax,
     802             :          double *x1p, double *y1p, double *x2p, double *y2p)
     803             : {
     804         120 :   int xy_exch = 0, rc = CLIPLINE_NONEMPTY;
     805             :   double t, sl;
     806             :   double xi, xmn, xmx;
     807             :   double yi, ymn, ymx;
     808             :   int x1_is_ymn, x1_is_xmn;
     809         120 :   double x1 = *x1p, x2 = *x2p, y1 = *y1p, y2 = *y2p;
     810             : 
     811         120 :   if ((x1 < xmin &&  x2 < xmin) || (x1 > xmax && x2 > xmax))
     812          40 :     return 0;
     813          80 :   if (fabs(x1 - x2) < fabs(y1 - y2)) { /* Exchange x and y */
     814          24 :     xy_exch = 1;
     815          24 :     dswap(xmin, ymin); dswap(x1, y1);
     816          24 :     dswap(xmax, ymax); dswap(x2, y2);
     817             :   }
     818             : 
     819             :   /* Build y as a function of x */
     820          80 :   xi = x1;
     821          80 :   yi = y1;
     822          80 :   sl = x1==x2? 0: (y2 - yi)/(x2 - xi);
     823             : 
     824          80 :   if (x1 > x2) {
     825          24 :     x1_is_xmn = 0;
     826          24 :     xmn = x2;
     827          24 :     xmx = x1;
     828             :   } else {
     829          56 :     x1_is_xmn = 1;
     830          56 :     xmn = x1;
     831          56 :     xmx = x2;
     832             :   }
     833             : 
     834          80 :   if (xmn < xmin) {
     835           8 :     xmn = xmin;
     836           8 :     rc |= x1_is_xmn? CLIPLINE_CLIP_1: CLIPLINE_CLIP_2;
     837             :   }
     838          80 :   if (xmx > xmax) {
     839          12 :     xmx = xmax;
     840          12 :     rc |= x1_is_xmn? CLIPLINE_CLIP_2: CLIPLINE_CLIP_1;
     841             :   }
     842          80 :   if (xmn > xmx) return 0;
     843             : 
     844          80 :   ymn = yi + (xmn - xi)*sl;
     845          80 :   ymx = yi + (xmx - xi)*sl;
     846             : 
     847          80 :   if (sl < 0) t = ymn, ymn = ymx, ymx = t;
     848          80 :   if (ymn > ymax || ymx < ymin) return 0;
     849             : 
     850          80 :   if (rc & CLIPLINE_CLIP_1) x1 = x1_is_xmn? xmn: xmx;
     851          80 :   if (rc & CLIPLINE_CLIP_2) x2 = x1_is_xmn? xmx: xmn;
     852             : 
     853             :   /* Now we know there is an intersection, need to move x1 and x2 */
     854          80 :   x1_is_ymn = ((sl >= 0) == (x1 < x2));
     855          80 :   if (ymn < ymin) {
     856           4 :     double x = (ymin - yi)/sl + xi; /* slope != 0  ! */
     857           4 :     if (x1_is_ymn) x1 = x, rc |= CLIPLINE_CLIP_1;
     858           0 :     else           x2 = x, rc |= CLIPLINE_CLIP_2;
     859             :   }
     860          80 :   if (ymx > ymax) {
     861           4 :     double x = (ymax - yi)/sl + xi; /* slope != 0  ! */
     862           4 :     if (x1_is_ymn) x2 = x, rc |= CLIPLINE_CLIP_2;
     863           0 :     else           x1 = x, rc |= CLIPLINE_CLIP_1;
     864             :   }
     865          80 :   if (rc & CLIPLINE_CLIP_1) y1 = yi + (x1 - xi)*sl;
     866          80 :   if (rc & CLIPLINE_CLIP_2) y2 = yi + (x2 - xi)*sl;
     867          80 :   if (xy_exch) /* Exchange x and y */
     868          24 :     *x1p = y1, *x2p = y2, *y1p = x1, *y2p = x2;
     869             :   else
     870          56 :     *x1p = x1, *x2p = x2, *y1p = y1, *y2p = y2;
     871          80 :   return rc;
     872             : }
     873             : 
     874             : void
     875          20 : plotclip(long rect)
     876             : {
     877          20 :   PariRect *s = check_rect_init(rect);
     878          20 :   RectObj *next, *R = RHead(s), **prevp = &RHead(s);
     879          20 :   double xmin = 0, xmax = RXsize(s);
     880          20 :   double ymin = 0, ymax = RYsize(s);
     881             : 
     882         380 :   for (; R; R = next) {
     883         360 :     int did_clip = 0;
     884             : #define REMOVE() { *prevp = next; freeobj(R); break; }
     885             : #define NEXT() { prevp = &RoNext(R); break; }
     886             : 
     887         360 :     next = RoNext(R);
     888         360 :     switch(RoType(R)) {
     889          40 :       case ROt_PT:
     890          40 :         if ( DTOL(RoPTx(R)) < xmin || DTOL(RoPTx(R)) > xmax
     891          40 :           || DTOL(RoPTy(R)) < ymin || DTOL(RoPTy(R)) > ymax) REMOVE();
     892          24 :         NEXT();
     893          40 :       case ROt_BX: case ROt_FBX:
     894          40 :         if (RoLNx1(R) < xmin) RoLNx1(R) = xmin, did_clip = 1;
     895          40 :         if (RoLNx2(R) < xmin) RoLNx2(R) = xmin, did_clip = 1;
     896          40 :         if (RoLNy1(R) < ymin) RoLNy1(R) = ymin, did_clip = 1;
     897          40 :         if (RoLNy2(R) < ymin) RoLNy2(R) = ymin, did_clip = 1;
     898          40 :         if (RoLNx1(R) > xmax) RoLNx1(R) = xmax, did_clip = 1;
     899          40 :         if (RoLNx2(R) > xmax) RoLNx2(R) = xmax, did_clip = 1;
     900          40 :         if (RoLNy1(R) > ymax) RoLNy1(R) = ymax, did_clip = 1;
     901          40 :         if (RoLNy2(R) > ymax) RoLNy2(R) = ymax, did_clip = 1;
     902             :         /* Remove zero-size clipped boxes */
     903          40 :         if (did_clip && RoLNx1(R) == RoLNx2(R)
     904           8 :                      && RoLNy1(R) == RoLNy2(R)) REMOVE();
     905          36 :         NEXT();
     906          40 :       case ROt_LN:
     907          40 :         if (!clipline(xmin, xmax, ymin, ymax,
     908             :                       &RoLNx1(R), &RoLNy1(R),
     909           8 :                       &RoLNx2(R), &RoLNy2(R))) REMOVE();
     910          32 :         NEXT();
     911          20 :       case ROt_MP: {
     912          20 :         int c = RoMPcnt(R), f = 0, t = 0;
     913             : 
     914         220 :         while (f < c) {
     915         200 :           if ( DTOL(RoMPxs(R)[f]) >= xmin && DTOL(RoMPxs(R)[f]) <= xmax
     916         120 :             && DTOL(RoMPys(R)[f]) >= ymin && DTOL(RoMPys(R)[f]) <= ymax) {
     917         120 :             if (t != f) {
     918           0 :               RoMPxs(R)[t] = RoMPxs(R)[f];
     919           0 :               RoMPys(R)[t] = RoMPys(R)[f];
     920             :             }
     921         120 :             t++;
     922             :           }
     923         200 :           f++;
     924             :         }
     925          20 :         if (t == 0) REMOVE();
     926          12 :         RoMPcnt(R) = t;
     927          12 :         NEXT();
     928             :       }
     929          20 :       case ROt_ML: {
     930             :         /* Hard case. Break a multiline into several pieces
     931             :          * if some part is clipped. */
     932          20 :         int c = RoMPcnt(R) - 1;
     933          20 :         int f = 0, t = 0, had_lines = 0, had_hole = 0, rc;
     934          20 :         double ox = RoMLxs(R)[0], oy = RoMLys(R)[0], oxn, oyn;
     935             : 
     936         100 :         while (f < c) {
     937             :         /* Endpoint of this segment is startpoint of next one: need to
     938             :          * preserve it if it is clipped. */
     939          80 :           oxn = RoMLxs(R)[f+1];
     940          80 :           oyn = RoMLys(R)[f+1];
     941         160 :           rc = clipline(xmin, xmax, ymin, ymax,
     942             :                   &ox, &oy, /* &RoMLxs(R)[f], &RoMLys(R)[f], */
     943          80 :                   &RoMLxs(R)[f+1], &RoMLys(R)[f+1]);
     944          80 :           RoMLxs(R)[f] = ox; ox = oxn;
     945          80 :           RoMLys(R)[f] = oy; oy = oyn;
     946          80 :           if (!rc) {
     947          32 :             if (had_lines) had_hole = 1;
     948          32 :             f++; continue;
     949             :           }
     950             : 
     951          48 :           if (!had_lines || (!(rc & CLIPLINE_CLIP_1) && !had_hole) ) {
     952             :             /* Continuous */
     953          48 :             had_lines = 1;
     954          48 :             if (t != f) {
     955           0 :               if (t == 0) {
     956           0 :                 RoMPxs(R)[t] = RoMPxs(R)[f];
     957           0 :                 RoMPys(R)[t] = RoMPys(R)[f];
     958             :               }
     959           0 :               RoMPxs(R)[t+1] = RoMPxs(R)[f+1];
     960           0 :               RoMPys(R)[t+1] = RoMPys(R)[f+1];
     961             :             }
     962          48 :             t++;
     963          48 :             f++;
     964          48 :             if (rc & CLIPLINE_CLIP_2) had_hole = 1, RoMLcnt(R) = t+1;
     965          48 :             continue;
     966             :           }
     967             :           /* Is not continuous, automatically R is not pari_free()ed.  */
     968           0 :           t++;
     969           0 :           RoMLcnt(R) = t;
     970           0 :           if (rc & CLIPLINE_CLIP_2) { /* Needs separate entry */
     971           0 :             RectObj *n = (RectObj*) pari_malloc(sizeof(RectObj2P));
     972           0 :             RoType(n) = ROt_LN;
     973           0 :             RoCol(n) = RoCol(R);
     974           0 :             RoLNx1(n) = RoMLxs(R)[f];   RoLNy1(n) = RoMLys(R)[f];
     975           0 :             RoLNx2(n) = RoMLxs(R)[f+1]; RoLNy2(n) = RoMLys(R)[f+1];
     976           0 :             RoNext(n) = next;
     977           0 :             RoNext(R) = n;
     978             :             /* Restore the unclipped value: */
     979           0 :             RoMLxs(R)[f+1] = oxn; RoMLys(R)[f+1] = oyn;
     980           0 :             f++;
     981           0 :             prevp = &RoNext(n);
     982             :           }
     983           0 :           if (f + 1 < c) { /* Are other lines */
     984           0 :             RectObj *n = (RectObj*) pari_malloc(sizeof(RectObjMP));
     985           0 :             RoType(n) = ROt_ML;
     986           0 :             RoCol(n) = RoCol(R);
     987           0 :             RoMLcnt(n) = c - f;
     988           0 :             RoMLxs(n) = cpd(RoMPxs(R) + f, c-f);
     989           0 :             RoMLys(n) = cpd(RoMPys(R) + f, c-f);
     990           0 :             RoMPxs(n)[0] = oxn;
     991           0 :             RoMPys(n)[0] = oyn;
     992           0 :             RoNext(n) = next;
     993           0 :             RoNext(R) = n;
     994           0 :             next = n;
     995             :           }
     996           0 :           break;
     997             :         }
     998          20 :         if (t == 0) REMOVE();
     999          12 :         NEXT();
    1000             :       }
    1001             :     }
    1002             : #undef REMOVE
    1003             : #undef NEXT
    1004         360 :   }
    1005          20 : }
    1006             : 
    1007             : /********************************************************************/
    1008             : /**                                                                **/
    1009             : /**                        HI-RES PLOT                             **/
    1010             : /**                                                                **/
    1011             : /********************************************************************/
    1012             : static void
    1013      177588 : set_xrange(dblPointList *f, double x)
    1014      177588 : { if (x < f->xsml) f->xsml = x;
    1015      177588 :   if (x > f->xbig) f->xbig = x; }
    1016             : static void
    1017      170176 : Appendx(dblPointList *f, dblPointList *l, double x)
    1018      170176 : { (l->d)[l->nb++] = x; set_xrange(f,x); }
    1019             : static void
    1020      242588 : set_yrange(dblPointList *f, double y)
    1021      242588 : { if (y < f->ysml) f->ysml = y;
    1022      242588 :   if (y > f->ybig) f->ybig = y; }
    1023             : static void
    1024      235176 : Appendy(dblPointList *f, dblPointList *l, double y)
    1025      235176 : { (l->d)[l->nb++] = y; set_yrange(f,y); }
    1026             : 
    1027             : static void
    1028      138192 : get_xy(long cplx, GEN t, double *x, double *y)
    1029             : {
    1030             :   GEN a, b;
    1031      138192 :   if (cplx)
    1032             :   {
    1033           0 :     if (typ(t) == t_VEC)
    1034             :     {
    1035           0 :       if (lg(t) != 2) pari_err_DIM("get_xy");
    1036           0 :       t = gel(t,1);
    1037             :     }
    1038           0 :     a = real_i(t); b = imag_i(t);
    1039             :   }
    1040             :   else
    1041             :   {
    1042      138192 :     if (typ(t) != t_VEC || lg(t) != 3) pari_err_DIM("get_xy");
    1043      138192 :     a = gel(t,1); b = gel(t,2);
    1044             :   }
    1045      138192 :   *x = gtodouble(a);
    1046      138192 :   *y = gtodouble(b);
    1047      138192 : }
    1048             : /* t a t_VEC (possibly a scalar if cplx), get next (x,y) coordinate starting
    1049             :  * at index *i [update i] */
    1050             : static void
    1051       40256 : get_xy_from_vec(long cplx, GEN t, long *i, double *x, double *y)
    1052             : {
    1053             :   GEN a, b;
    1054       40256 :   if (cplx)
    1055             :   {
    1056           0 :     if (typ(t) == t_VEC) t = gel(t,*i);
    1057           0 :     a = real_i(t); b = imag_i(t); (*i)++;
    1058             :   }
    1059             :   else
    1060             :   {
    1061       40256 :     a = gel(t, (*i)++);
    1062       40256 :     b = gel(t, (*i)++);
    1063             :   }
    1064       40256 :   *x = gtodouble(a);
    1065       40256 :   *y = gtodouble(b);
    1066       40256 : }
    1067             : /* X,Y t_VEC; next (x,y) coordinate starting at index i; Y ignored if (cplx) */
    1068             : static void
    1069        7412 : get_xy_from_vec2(long cplx, GEN X, GEN Y, long i, double *x, double *y)
    1070             : {
    1071             :   GEN a, b;
    1072        7412 :   if (cplx)
    1073             :   {
    1074           0 :     GEN z = gel(X,i);
    1075           0 :     a = real_i(z); b = imag_i(z);
    1076             :   }
    1077             :   else
    1078             :   {
    1079        7412 :     a = gel(X,i); b = gel(Y,i);
    1080             :   }
    1081        7412 :   *x = gtodouble(a);
    1082        7412 :   *y = gtodouble(b);
    1083        7412 : }
    1084             : 
    1085             : /* Convert data from GEN to double before we call plotrecthrawin. */
    1086             : static dblPointList*
    1087          26 : gtodblList(GEN data, long flags)
    1088             : {
    1089             :   dblPointList *l, *L;
    1090             :   double *X, *Y;
    1091          26 :   long nl=lg(data)-1, lx1, i, j;
    1092          26 :   const long param = (flags & (PLOT_PARAMETRIC|PLOT_COMPLEX));
    1093          26 :   const long cplx = (flags & PLOT_COMPLEX);
    1094             : 
    1095          26 :   if (! is_vec_t(typ(data))) pari_err_TYPE("gtodblList",data);
    1096          26 :   if (!nl) return NULL;
    1097          26 :   lx1 = lg(gel(data,1));
    1098          26 :   if (!param && lx1 == 1) return NULL;
    1099             : 
    1100             :   /* Check input first */
    1101          26 :   if (nl == 1 && !cplx) pari_err_DIM("gtodblList");
    1102          52 :   for (i = 0; i < nl; i += cplx? 1: 2)
    1103             :   {
    1104          26 :     GEN x = gel(data,i+1), y = cplx? NULL: gel(data,i+2);
    1105          26 :     long lx = lg(x);
    1106          26 :     if (!is_vec_t(typ(x))) pari_err_TYPE("gtodblList",x);
    1107          26 :     if (y)
    1108             :     {
    1109          26 :       if (!is_vec_t(typ(y))) pari_err_TYPE("gtodblList",y);
    1110          26 :       if (lg(y) != lx || (!param && lx != lx1)) pari_err_DIM("gtodblList");
    1111             :     }
    1112             :   }
    1113             :   /* Now allocate memory, then convert coord. to double */
    1114          26 :   l = (dblPointList*)pari_malloc((cplx? 2*nl: nl)*sizeof(dblPointList));
    1115          26 :   L = &l[0];
    1116          52 :   for (i = 0; i < nl; i += cplx? 1: 2)
    1117             :   {
    1118          26 :     GEN x = gel(data,i+1), y = cplx? NULL: gel(data,i+2);
    1119          26 :     long lx = lg(x)-1;
    1120          26 :     l[i].d   = X = (double*)pari_malloc(lx*sizeof(double));
    1121          26 :     l[i+1].d = Y = (double*)pari_malloc(lx*sizeof(double));
    1122        7438 :     for (j=1; j<=lx; j++) get_xy_from_vec2(cplx, x, y, j, X+(j-1), Y+(j-1));
    1123          26 :     l[i].nb = l[i+1].nb = lx;
    1124             :   }
    1125             :   /* Compute extremas */
    1126          26 :   if (param)
    1127             :   {
    1128          26 :     L->nb = cplx? nl: nl/2;
    1129          26 :     for (i=0; i < L->nb; i+=2)
    1130          26 :       if (l[i+1].nb) break;
    1131          26 :     if (i >= L->nb) { pari_free(l); return NULL; }
    1132          26 :     L->xsml = L->xbig = l[i  ].d[0];
    1133          26 :     L->ysml = L->ybig = l[i+1].d[0];
    1134          52 :     for (; i < L->nb; i+=2)
    1135             :     {
    1136          26 :       long nbi = l[i+1].nb; X = l[i].d; Y = l[i+1].d;
    1137        7438 :       for (j = 0; j < nbi; j++) { set_xrange(L, X[j]); set_yrange(L, Y[j]); }
    1138             :     }
    1139             :   }
    1140             :   else
    1141             :   {
    1142           0 :     L->nb = nl-1;
    1143           0 :     X = L->d;   L->xsml = L->xbig = X[0];
    1144           0 :     Y = l[1].d; L->ysml = L->ybig = Y[0];
    1145           0 :     for (j=0; j < l[1].nb; j++) set_xrange(L, X[j]);
    1146           0 :     for (i=1; i <= L->nb; i++)
    1147             :     {
    1148           0 :       long nbi = l[i].nb; Y = l[i].d;
    1149           0 :       for (j = 0; j < nbi; j++) set_yrange(L, Y[j]);
    1150             :     }
    1151             :   }
    1152          26 :   return l;
    1153             : }
    1154             : 
    1155             : /* x,y t_REAL; return (x+y)/2,  */
    1156             : static GEN
    1157      208940 : rmiddle(GEN x, GEN y) { GEN z = addrr(x,y); shiftr_inplace(z,-1); return z; }
    1158             : 
    1159             : static void
    1160       91404 : single_recursion(void *E, GEN(*eval)(void*,GEN), dblPointList *pl,
    1161             :                  GEN xl,double yl, GEN xr,double yr,long depth)
    1162             : {
    1163             :   GEN xx;
    1164       91404 :   pari_sp av = avma;
    1165       91404 :   double yy, dy=pl[0].ybig - pl[0].ysml;
    1166             : 
    1167       91404 :   if (depth==RECUR_MAXDEPTH) return;
    1168             : 
    1169       83124 :   xx = rmiddle(xl,xr);
    1170       83124 :   yy = gtodouble(eval(E,xx));
    1171             : 
    1172       83124 :   if (dy && fabs(yl+yr-2*yy) < dy*RECUR_PREC) return;
    1173       43708 :   single_recursion(E,eval, pl,xl,yl, xx,yy, depth+1);
    1174       43708 :   Appendx(&pl[0],&pl[0],rtodbl(xx));
    1175       43708 :   Appendy(&pl[0],&pl[1],yy);
    1176       43708 :   single_recursion(E,eval, pl,xx,yy, xr,yr, depth+1);
    1177       43708 :   set_avma(av);
    1178             : }
    1179             : 
    1180             : static void
    1181      138104 : param_recursion(void *E,GEN(*eval)(void*,GEN), long cplx, dblPointList *pl,
    1182             :   GEN tl,double xl, double yl, GEN tr,double xr,double yr, long depth)
    1183             : {
    1184             :   GEN t;
    1185      138104 :   pari_sp av = avma;
    1186      138104 :   double xx, dy=pl[0].ybig - pl[0].ysml;
    1187      138104 :   double yy, dx=pl[0].xbig - pl[0].xsml;
    1188             : 
    1189      198060 :   if (depth==RECUR_MAXDEPTH) return;
    1190             : 
    1191      125816 :   t = rmiddle(tl,tr);
    1192      125816 :   get_xy(cplx, eval(E,t), &xx,&yy);
    1193             : 
    1194      125816 :   if (dx && dy && fabs(xl+xr-2*xx) < dx*RECUR_PREC
    1195       62848 :                && fabs(yl+yr-2*yy) < dy*RECUR_PREC) return;
    1196       65860 :   param_recursion(E,eval, cplx, pl, tl,xl,yl, t,xx,yy, depth+1);
    1197       65860 :   Appendx(&pl[0],&pl[0],xx);
    1198       65860 :   Appendy(&pl[0],&pl[1],yy);
    1199       65860 :   param_recursion(E,eval,cplx, pl, t,xx,yy, tr,xr,yr, depth+1);
    1200       65860 :   set_avma(av);
    1201             : }
    1202             : 
    1203             : /* Graph 'code' for parameter values in [a,b], using 'N' sample points
    1204             :  * (0 = use a default value); code is either a t_CLOSURE or a t_POL or a
    1205             :  * t_VEC of two t_POLs from rectsplines. Returns a dblPointList of
    1206             :  * (absolute) coordinates. */
    1207             : static dblPointList *
    1208       10080 : plotrecthin(void *E, GEN(*eval)(void*, GEN), GEN a, GEN b, ulong flags,
    1209             :             long N, long prec)
    1210             : {
    1211       10080 :   const double INF = 1.0/0.0;
    1212       10080 :   const long param = flags & (PLOT_PARAMETRIC|PLOT_COMPLEX);
    1213       10080 :   const long recur = flags & PLOT_RECURSIVE;
    1214       10080 :   const long cplx = flags & PLOT_COMPLEX;
    1215             :   GEN t, dx, x;
    1216             :   dblPointList *pl;
    1217       10080 :   long tx, i, j, sig, nc, nl, ncoords, nbpoints, non_vec = 0;
    1218             : 
    1219       10080 :   sig = gcmp(b,a); if (!sig) return NULL;
    1220       10080 :   if (sig < 0) swap(a, b);
    1221       10080 :   if (N == 1) pari_err_DOMAIN("ploth", "#points", "<", gen_2, stoi(N));
    1222       10076 :   if (!N) N = recur? 8: (param? 1500: 1000);
    1223             :   /* compute F(a) to determine nc = #curves; nl = #coord. lists */
    1224       10076 :   x = gtofp(a, prec);
    1225       10076 :   t = eval(E, x); tx = typ(t);
    1226       10076 :   if (cplx) nc = nl = (tx == t_VEC)? lg(t)-1: 1;
    1227       10076 :   else if (param)
    1228             :   {
    1229        6043 :     if (tx != t_VEC) pari_err_TYPE("ploth [not a t_VEC in parametric plot]", t);
    1230        6039 :     nl = lg(t)-1; nc = nl >> 1;
    1231        6039 :     if (odd(nl)) pari_err_TYPE("ploth [odd #components in parametric plot]",t);
    1232             :   }
    1233        4033 :   else if (!is_matvec_t(tx)) { nl = 2; non_vec = 1; nc = 1; }
    1234             :   else
    1235             :   {
    1236          12 :     if (tx != t_VEC) pari_err_TYPE("ploth [not a t_VEC]",t);
    1237          12 :     nl = lg(t);
    1238          12 :     nc = nl-1;
    1239             :   }
    1240       10072 :   if (!nc) return NULL;
    1241       10072 :   if (recur && nc > 1) pari_err_TYPE("ploth [multi-curves + recursive]",t);
    1242             : 
    1243       10068 :   ncoords = cplx? 2*nl: nl;
    1244       10068 :   nbpoints = recur? N << RECUR_MAXDEPTH: N;
    1245       10068 :   pl=(dblPointList*) pari_malloc(ncoords*sizeof(dblPointList));
    1246             :   /* set [xy]sml,[xy]big to default values */
    1247       10068 :   if (param)
    1248             :   {
    1249        6039 :     pl[0].xsml = INF;
    1250        6039 :     pl[0].xbig =-INF;
    1251             :   } else {
    1252        4029 :     pl[0].xsml = gtodouble(a);
    1253        4029 :     pl[0].xbig = gtodouble(b);
    1254             :   }
    1255       10068 :   pl[0].ysml = INF;
    1256       10068 :   pl[0].ybig =-INF;
    1257       30228 :   for (i = 0; i < ncoords; i++)
    1258             :   {
    1259       20160 :     pl[i].d = (double*)pari_malloc((nbpoints+1)*sizeof(double));
    1260       20160 :     pl[i].nb=0;
    1261             :   }
    1262       10068 :   dx = divru(gtofp(gsub(b,a),prec), N-1);
    1263       10068 :   if (recur)
    1264             :   { /* recursive plot */
    1265        9980 :     double yl, yr = 0;
    1266        9980 :     if (param)
    1267             :     {
    1268        5992 :       GEN tl = cgetr(prec), tr = cgetr(prec);
    1269        5992 :       double xl, xr = 0;
    1270        5992 :       pari_sp av2 = avma;
    1271        5992 :       affgr(a, tl);
    1272        5992 :       t = eval(E, tl);
    1273        5992 :       get_xy(cplx,t, &xl,&yl);
    1274       12376 :       for (i=0; i<N-1; i++, set_avma(av2))
    1275             :       {
    1276        6384 :         if (i) { affrr(tr,tl); xl = xr; yl = yr; }
    1277        6384 :         addrrz(tl,dx,tr);
    1278        6384 :         t = eval(E, tr);
    1279        6384 :         get_xy(cplx,t, &xr,&yr);
    1280        6384 :         Appendx(&pl[0],&pl[0],xl);
    1281        6384 :         Appendy(&pl[0],&pl[1],yl);
    1282        6384 :         param_recursion(E,eval, cplx, pl, tl,xl,yl, tr,xr,yr, 0);
    1283             :       }
    1284        5992 :       Appendx(&pl[0],&pl[0],xr);
    1285        5992 :       Appendy(&pl[0],&pl[1],yr);
    1286             :     }
    1287             :     else /* single curve */
    1288             :     {
    1289        3988 :       GEN xl = cgetr(prec), xr = cgetr(prec);
    1290        3988 :       pari_sp av2 = avma;
    1291        3988 :       affgr(a,xl);
    1292        3988 :       yl = gtodouble(eval(E,xl));
    1293        7976 :       for (i=0; i<N-1; i++, set_avma(av2))
    1294             :       {
    1295        3988 :         addrrz(xl,dx,xr);
    1296        3988 :         yr = gtodouble(eval(E,xr));
    1297        3988 :         Appendx(&pl[0],&pl[0],rtodbl(xl));
    1298        3988 :         Appendy(&pl[0],&pl[1],yl);
    1299        3988 :         single_recursion(E,eval, pl,xl,yl,xr,yr,0);
    1300        3988 :         affrr(xr,xl); yl = yr;
    1301             :       }
    1302        3988 :       Appendx(&pl[0],&pl[0],rtodbl(xr));
    1303        3988 :       Appendy(&pl[0],&pl[1],yr);
    1304             :     }
    1305             :   }
    1306             :   else /* non-recursive plot */
    1307             :   {
    1308          88 :     GEN V, X = cgetg(N+1, t_VEC);
    1309       86388 :     for (i = 1; i <= N; i++) { gel(X,i) = x; x = addrr(x,dx); }
    1310          88 :     if (flags & PLOT_PARA && eval == gp_call)
    1311          14 :     {
    1312          14 :       GEN worker = snm_closure(is_entry("_parapply_slice_worker"),
    1313             :                                mkvec((GEN)E));
    1314          14 :       V = gen_parapply_slice(worker, X, mt_nbthreads());
    1315             :     }
    1316             :     else
    1317             :     {
    1318          74 :       V = cgetg(N+1, t_VEC);
    1319       68874 :       for (i = 1; i <= N; i++) gel(V,i) = eval(E,gel(X,i));
    1320             :     }
    1321          88 :     if (param)
    1322             :     {
    1323       40303 :       for (i = 1; i <= N; i++)
    1324             :       {
    1325             :         long nt, k, j;
    1326       40260 :         t = gel(V,i);
    1327       40260 :         if (typ(t) != t_VEC)
    1328             :         {
    1329           4 :           if (cplx) nt = 1;
    1330           4 :           else nt = 0; /* trigger error */
    1331             :         }
    1332             :         else
    1333       40256 :           nt = lg(t)-1;
    1334       40260 :         if (nt != nl) pari_err_DIM("plotrecth");
    1335       40256 :         k = 0; j = 1;
    1336       80512 :         while (j <= nl)
    1337             :         {
    1338             :           double xx, yy;
    1339       40256 :           get_xy_from_vec(cplx, t, &j, &xx, &yy);
    1340       40256 :           Appendx(&pl[0], &pl[k++], xx);
    1341       40256 :           Appendy(&pl[0], &pl[k++], yy);
    1342             :         }
    1343             :       }
    1344             :     }
    1345          41 :     else if (non_vec)
    1346       33033 :       for (i = 1; i <= N; i++)
    1347             :       {
    1348       33000 :         Appendy(&pl[0], &pl[1], gtodouble(gel(V,i)));
    1349       33000 :         pl[0].d[i-1] = gtodouble(gel(X,i));
    1350             :       }
    1351             :     else /* vector of non-parametric curves */
    1352        8008 :       for (i = 1; i <= N; i++)
    1353             :       {
    1354        8000 :         t = gel(V,i);
    1355        8000 :         if (typ(t) != t_VEC || lg(t) != nl) pari_err_DIM("plotrecth");
    1356       40000 :         for (j = 1; j < nl; j++) Appendy(&pl[0], &pl[j], gtodouble(gel(t,j)));
    1357        8000 :         pl[0].d[i-1] = gtodouble(gel(X,i));
    1358             :       }
    1359             :   }
    1360       10064 :   pl[0].nb = nc; return pl;
    1361             : }
    1362             : 
    1363             : static GEN
    1364      234176 : spline_eval(void* E, GEN x) { return gsubst((GEN)E,0,x); }
    1365             : 
    1366             : /* Uses highlevel plotting functions to implement splines as
    1367             :    a low-level plotting function. */
    1368             : static void
    1369           8 : rectsplines(long ne, double *x, double *y, long lx, long flag)
    1370             : {
    1371             :   long i, j;
    1372           8 :   pari_sp av0 = avma;
    1373           8 :   GEN X = pol_x(0), xa = cgetg(lx+1, t_VEC), ya = cgetg(lx+1, t_VEC);
    1374             :   GEN tas, pol3;
    1375           8 :   long param = flag & PLOT_PARAMETRIC;
    1376           8 :   const long fl = param | PLOT_RECURSIVE | PLOT_NO_RESCALE | PLOT_NO_FRAME
    1377             :                         | PLOT_NO_AXE_Y | PLOT_NO_AXE_X;
    1378             : 
    1379           8 :   if (lx < 4) pari_err(e_MISC, "Too few points (%ld) for spline plot", lx);
    1380       10008 :   for (i = 1; i <= lx; i++) {
    1381       10000 :     gel(xa,i) = dbltor(x[i-1]);
    1382       10000 :     gel(ya,i) = dbltor(y[i-1]);
    1383             :   }
    1384           8 :   if (param) {
    1385           4 :     tas = new_chunk(4);
    1386          20 :     for (j = 1; j <= 4; j++) gel(tas,j-1) = utoipos(j);
    1387           4 :     pol3 = cgetg(3, t_VEC);
    1388             :   }
    1389             :   else
    1390           4 :     tas = pol3 = NULL; /* gcc -Wall */
    1391        9984 :   for (i = 0; i <= lx - 4; i++) {
    1392        9976 :     pari_sp av = avma;
    1393             : 
    1394        9976 :     xa++; ya++;
    1395        9976 :     if (param) {
    1396        5988 :       gel(pol3,1) = polintspec(tas, xa, X, 4, NULL);
    1397        5988 :       gel(pol3,2) = polintspec(tas, ya, X, 4, NULL);
    1398             :     } else {
    1399        3988 :       pol3 = polintspec(xa, ya, X, 4, NULL);
    1400        3988 :       tas = xa;
    1401             :     }
    1402             :     /* Start with 3 points */
    1403        9976 :     plotrecth((void*)pol3, &spline_eval, ne,
    1404             :                i== 0 ? gel(tas,0) : gel(tas,1),
    1405        9976 :                i==lx-4 ? gel(tas,3) : gel(tas,2),
    1406             :                fl, 2, DEFAULTPREC);
    1407        9976 :     set_avma(av);
    1408             :   }
    1409           8 :   set_avma(av0);
    1410           8 : }
    1411             : 
    1412             : static void
    1413          71 : pari_get_fmtplot(GEN fmt, PARI_plot *T)
    1414             : {
    1415          71 :   char *f = GSTR(fmt);
    1416          71 :   if (!strcmp(f, "svg")) pari_get_svgplot(T);
    1417          21 :   else if (!strcmp(f, "ps")) pari_get_psplot(T);
    1418           0 :   else pari_err_TYPE("plotexport [unknown format]", fmt);
    1419          71 : }
    1420             : static GEN
    1421         135 : fmt_convert(GEN fmt, GEN w, GEN x, GEN y, PARI_plot *T)
    1422             : {
    1423         135 :   char *f, *s = NULL;
    1424         135 :   if (typ(fmt) != t_STR) pari_err_TYPE("plotexport",fmt);
    1425         135 :   f = GSTR(fmt);
    1426         135 :   if (!strcmp(f, "svg"))
    1427          86 :     s = rect2svg(w,x,y,T);
    1428          49 :   else if (!strcmp(f, "ps"))
    1429          49 :     s = rect2ps(w,x,y,T);
    1430             :   else
    1431           0 :     pari_err_TYPE("plotexport [unknown format]", fmt);
    1432         135 :   return strtoGENstr(s);
    1433             : }
    1434             : 
    1435             : static void
    1436          60 : Draw(PARI_plot *T, GEN w, GEN x, GEN y)
    1437             : {
    1438          60 :   if (!T->draw) pari_err(e_MISC,"high resolution graphics disabled");
    1439          60 :   T->draw(T, w,x,y);
    1440          60 : }
    1441             : static void
    1442       20180 : set_range(double m, double M, double *sml, double *big)
    1443             : {
    1444       20180 :   if (M - m < 1.e-9)
    1445             :   {
    1446           0 :     double d = fabs(m)/10; if (!d) d = 0.1;
    1447           0 :     M += d; m -= d;
    1448             :   }
    1449       20180 :   *sml = m; *big = M;
    1450       20180 : }
    1451             : /* Plot a dblPointList. Complete with axes, bounding box, etc.
    1452             :  *
    1453             :  * data is an array of structs. Its meaning depends on flags :
    1454             :  *
    1455             :  * + data[0] contains global extremas, the number of curves to plot
    1456             :  *   (data[0].nb) and a list of doubles (first set of x-coordinates).
    1457             :  *
    1458             :  * + data[i].nb (i>0) contains the number of points in the list
    1459             :  *   data[i].d (hopefully, data[2i].nb=data[2i+1].nb when i>0...)
    1460             :  *
    1461             :  * + If flags contain PLOT_PARAMETRIC, the array length should be
    1462             :  *   even, and successive pairs (data[2i].d, data[2i+1].d) represent
    1463             :  *   curves to plot.
    1464             :  *
    1465             :  * + If there is no such flag, the first element is an array with
    1466             :  *   x-coordinates and the following ones contain y-coordinates.
    1467             :  * If W != NULL, output wrt this PARI_plot using two drawing rectangles:
    1468             :  * one for labels, another for graphs. Else draw to rectwindow ne without
    1469             :  * labels.
    1470             :  * If fmt != NULL (requires W != NULL), output is a t_STR containing the
    1471             :  * converted picture, else a bounding box */
    1472             : static GEN
    1473       10090 : plotrecthrawin(GEN fmt, PARI_plot *W, long ne, dblPointList *data, long flags)
    1474             : {
    1475       10090 :   const long param = flags & (PLOT_PARAMETRIC|PLOT_COMPLEX);
    1476       10090 :   const long max_graphcolors = lg(GP_DATA->graphcolors)-1;
    1477       10090 :   const pari_sp av = avma;
    1478             :   dblPointList x, y;
    1479             :   double xsml, xbig, ysml, ybig;
    1480             :   long ltype, i, nc, w[3], wx[3], wy[3];
    1481             : 
    1482       10090 :   if (!data) return cgetg(1,t_VEC);
    1483       10090 :   x = data[0]; nc = x.nb;
    1484       10090 :   set_range(x.xsml, x.xbig, &xsml, &xbig);
    1485       10090 :   set_range(x.ysml, x.ybig, &ysml, &ybig);
    1486       10090 :   if (W)
    1487             :   { /* actual output; else output to rectwindow: no labels */
    1488         107 :     const long se = NUMRECT-2;
    1489             :     long lm, rm, tm, bm;
    1490             :     char YBIG[16], YSML[16], XSML[16], XBIG[16];
    1491             :     /* left/right/top/bottom margin */
    1492         107 :     sprintf(YSML,"%.5g", ysml); sprintf(YBIG,"%.5g", ybig);
    1493         107 :     sprintf(XSML,"%.5g", xsml); sprintf(XBIG,"%.5g", xbig);
    1494             :     /* left margin has y labels with hgap on both sides of text */
    1495         107 :     lm = maxss(strlen(YSML),strlen(YBIG))*W->fwidth + 2*W->hunit-1;
    1496         107 :     rm = W->hunit-1;
    1497         107 :     tm = W->vunit-1;
    1498         107 :     bm = W->vunit+W->fheight-1;
    1499         107 :     w[0] = wx[0] = wy[0] = evaltyp(t_VECSMALL) | evallg(3);
    1500         107 :     w[1] = se; wx[1] = 0;  wy[1] = 0;
    1501         107 :     w[2] = ne; wx[2] = lm; wy[2] = tm;
    1502             :    /* Window (width x height) is given in pixels, correct pixels are 0..n-1,
    1503             :     * whereas rect functions work with windows whose pixel range is [0,n] */
    1504         107 :     initrect_i(se, W->width - 1, W->height - 1);
    1505         107 :     initrect_i(ne, W->width - (lm+rm) - 1, W->height - (tm+bm) - 1);
    1506             :     /* draw labels on se */
    1507         107 :     _move(se,lm,0); plotstring(se, YBIG, RoSTdirRIGHT|RoSTdirHGAP|RoSTdirTOP);
    1508         107 :     _move(se,lm,W->height-bm); plotstring(se,YSML, RoSTdirRIGHT|RoSTdirHGAP|RoSTdirVGAP);
    1509         107 :     _move(se,lm,W->height-bm); plotstring(se, XSML, RoSTdirLEFT|RoSTdirTOP);
    1510         107 :     _move(se,W->width-rm-1, W->height-bm); plotstring(se, XBIG, RoSTdirRIGHT|RoSTdirTOP);
    1511             :   }
    1512       10090 :   if (!(flags & PLOT_NO_RESCALE)) plotscale0(ne, xsml, xbig, ysml, ybig);
    1513       10090 :   if (!(flags & PLOT_NO_FRAME))
    1514             :   {
    1515         228 :     long fl = (flags & PLOT_NODOUBLETICK)? TICKS_CLOCKW|TICKS_NODOUBLE
    1516         114 :                                          : TICKS_CLOCKW;
    1517             :     PARI_plot T, *pl;
    1518         114 :     if (W) pl = W; else { pl = &T; pari_get_plot(pl); }
    1519         114 :     plotlinetype(ne, -2); /* frame */
    1520         114 :     current_color[ne] = colormap_to_color(DEFAULT_COLOR);
    1521         114 :     _move(ne,xsml,ysml);
    1522         114 :     _box(ne,xbig,ybig);
    1523         114 :     if (!(flags & PLOT_NO_TICK_X)) {
    1524         114 :       rectticks(pl, ne, xsml, ysml, xbig, ysml, xsml, xbig, fl);
    1525         114 :       rectticks(pl, ne, xbig, ybig, xsml, ybig, xbig, xsml, fl);
    1526             :     }
    1527         114 :     if (!(flags & PLOT_NO_TICK_Y)) {
    1528         114 :       rectticks(pl, ne, xbig, ysml, xbig, ybig, ysml, ybig, fl);
    1529         114 :       rectticks(pl, ne, xsml, ybig, xsml, ysml, ybig, ysml, fl);
    1530             :     }
    1531             :   }
    1532       10090 :   if (!(flags & PLOT_NO_AXE_Y) && (xsml<=0 && xbig >=0))
    1533             :   {
    1534          93 :     plotlinetype(ne, -1); /* axes */
    1535          93 :     current_color[ne] = colormap_to_color(AXIS_COLOR);
    1536          93 :     _move(ne,0.0,ysml);
    1537          93 :     _line(ne,0.0,ybig);
    1538             :   }
    1539       10090 :   if (!(flags & PLOT_NO_AXE_X) && (ysml<=0 && ybig >=0))
    1540             :   {
    1541          93 :     plotlinetype(ne, -1); /* axes */
    1542          93 :     current_color[ne] = colormap_to_color(AXIS_COLOR);
    1543          93 :     _move(ne,xsml,0.0);
    1544          93 :     _line(ne,xbig,0.0);
    1545             :   }
    1546             : 
    1547       10090 :   if (param) {
    1548        6061 :     i = 0;
    1549        6061 :     flags |= PLOT_PARAMETRIC;
    1550        6061 :     flags &= (~PLOT_COMPLEX); /* turn COMPLEX to PARAMETRIC*/
    1551        4029 :   } else i = 1;
    1552       20188 :   for (ltype = 0; ltype < nc; ltype++)
    1553             :   {
    1554       10102 :     long c = GP_DATA->graphcolors[1+(ltype%max_graphcolors)];
    1555       10102 :     current_color[ne] = colormap_to_color(c);
    1556       10098 :     if (param) x = data[i++];
    1557             : 
    1558       10098 :     y = data[i++];
    1559       10098 :     if (flags & (PLOT_POINTS_LINES|PLOT_POINTS)) {
    1560          18 :       plotlinetype(ne, plotpoint_itype + ltype); /* Graphs */
    1561          18 :       plotpointtype(ne,plotpoint_itype + ltype); /* Graphs */
    1562          18 :       plotpoints0(ne, x.d, y.d, y.nb);
    1563          18 :       if (!(flags & PLOT_POINTS_LINES)) continue;
    1564             :     }
    1565             : 
    1566       10080 :     if (flags & PLOT_SPLINES) {
    1567             :       /* rectsplines will call us back with ltype == 0 */
    1568           8 :       int old = rectline_itype;
    1569           8 :       rectline_itype = rectline_itype + ltype;
    1570           8 :       rectsplines(ne, x.d, y.d, y.nb, flags);
    1571           8 :       rectline_itype = old;
    1572             :     } else {
    1573       10072 :       plotlinetype(ne, rectline_itype + ltype); /* Graphs */
    1574       10072 :       rectlines0(ne, x.d, y.d, y.nb, 0);
    1575             :     }
    1576             :   }
    1577       30262 :   for (i--; i>=0; i--) pari_free(data[i].d);
    1578       10086 :   pari_free(data);
    1579             : 
    1580       10086 :   if (W)
    1581             :   {
    1582         103 :     GEN s = NULL;
    1583         103 :     if (fmt) s = fmt_convert(fmt, w, wx, wy, W); else Draw(W, w,wx,wy);
    1584         103 :     plotkill(w[1]);
    1585         103 :     plotkill(w[2]);
    1586         103 :     if (fmt) return s;
    1587             :   }
    1588       10015 :   set_avma(av);
    1589       10015 :   retmkvec4(dbltor(xsml), dbltor(xbig), dbltor(ysml), dbltor(ybig));
    1590             : }
    1591             : 
    1592             : /*************************************************************************/
    1593             : /*                                                                       */
    1594             : /*                          HI-RES FUNCTIONS                             */
    1595             : /*                                                                       */
    1596             : /*************************************************************************/
    1597             : /* If T != NULL, draw using the attached graphic (using rectwindow ne as a temp)
    1598             :  * Else write to rectwindow 'ne'.
    1599             :  * Graph y=f(x), x=a..b, use n points */
    1600             : static GEN
    1601       10080 : plotrecth_i(GEN fmt, void *E, GEN(*f)(void*,GEN), PARI_plot *T, long ne,
    1602             :             GEN a,GEN b, ulong flags,long n, long prec)
    1603             : {
    1604       10080 :   pari_sp av = avma;
    1605       10080 :   dblPointList *pl = plotrecthin(E,f, a,b, flags, n, prec);
    1606       10064 :   set_avma(av); return plotrecthrawin(fmt, T, ne, pl, flags);
    1607             : }
    1608             : GEN
    1609        9983 : plotrecth(void *E, GEN(*f)(void*,GEN), long ne, GEN a,GEN b,
    1610             :           ulong flags, long n, long prec)
    1611        9983 : { return plotrecth_i(NULL, E,f, NULL, ne, a,b, flags&~PLOT_PARA, n, prec); }
    1612             : GEN
    1613           7 : plotrecth0(long ne, GEN a,GEN b,GEN code,ulong flags,long n, long prec)
    1614           7 : { EXPR_WRAP(code, plotrecth(EXPR_ARG, ne, a,b, flags, n, prec)); }
    1615             : static GEN
    1616          44 : _ploth(void *E, GEN(*f)(void*,GEN), GEN a, GEN b,long flags, long n, long prec)
    1617             : {
    1618          44 :   PARI_plot T; pari_get_plot(&T);
    1619          44 :   return plotrecth_i(NULL, E,f, &T, NUMRECT-1, a,b, flags,n, prec);
    1620             : }
    1621             : GEN
    1622          44 : ploth(void *E, GEN(*f)(void*,GEN), GEN a, GEN b,long flags, long n, long prec)
    1623          44 : { return _ploth(E, f, a, b, flags&~PLOT_PARA, n, prec); }
    1624             : GEN
    1625           0 : parploth(GEN a, GEN b, GEN code, long flags, long n, long prec)
    1626           0 : { return _ploth(code, gp_call, a, b, flags|PLOT_PARA, n, prec); }
    1627             : GEN
    1628          44 : ploth0(GEN a, GEN b, GEN code, long flags,long n, long prec)
    1629          44 : { EXPR_WRAP(code, ploth(EXPR_ARG, a,b,flags,n, prec)); }
    1630             : 
    1631             : GEN
    1632           0 : psploth(void *E, GEN(*f)(void*,GEN), GEN a,GEN b, long flags, long n, long prec)
    1633             : {
    1634           0 :   PARI_plot T; pari_get_psplot(&T); T.draw = &_psdraw;
    1635           0 :   return plotrecth_i(NULL, E,f, &T, NUMRECT-1, a,b, flags&~PLOT_PARA,n, prec);
    1636             : }
    1637             : GEN
    1638           0 : psploth0(GEN a, GEN b, GEN code, long flags, long n, long prec)
    1639           0 : { EXPR_WRAP(code, psploth(EXPR_ARG, a, b, flags, n, prec)); }
    1640             : 
    1641             : static GEN
    1642          53 : _plothexport(GEN fmt, void *E, GEN(*f)(void*,GEN), GEN a,GEN b, long flags,
    1643             :             long n, long prec)
    1644             : {
    1645          53 :   pari_sp av = avma;
    1646             :   GEN s;
    1647          53 :   PARI_plot T; pari_get_fmtplot(fmt, &T);
    1648          53 :   s = plotrecth_i(fmt, E,f, &T, NUMRECT-1, a,b, flags,n, prec);
    1649          53 :   return gerepileuptoleaf(av, s);
    1650             : }
    1651             : GEN
    1652          39 : plothexport(GEN fmt, void *E, GEN(*f)(void*,GEN), GEN a,GEN b, long flags,
    1653             :             long n, long prec)
    1654          39 : { return _plothexport(fmt, E, f, a, b, flags&~PLOT_PARA, n, prec); }
    1655             : GEN
    1656          39 : plothexport0(GEN fmt, GEN a, GEN b, GEN code, long flags, long n, long prec)
    1657          39 : { EXPR_WRAP(code, plothexport(fmt, EXPR_ARG, a, b, flags, n, prec)); }
    1658             : GEN
    1659          14 : parplothexport(GEN fmt, GEN a, GEN b, GEN code, long flags, long n, long prec)
    1660          14 : { return _plothexport(fmt, code, gp_call, a, b, flags|PLOT_PARA, n, prec); }
    1661             : 
    1662             : /* Draw list of points */
    1663             : static GEN
    1664          26 : plotrecthraw_i(GEN fmt, PARI_plot *T, long ne, GEN data, long flags)
    1665             : {
    1666          26 :   dblPointList *pl = gtodblList(data,flags);
    1667          26 :   return plotrecthrawin(fmt, T, ne, pl, flags);
    1668             : }
    1669             : static GEN
    1670          26 : plothraw_i(GEN fmt, PARI_plot *T, GEN X, GEN Y, long flag)
    1671             : {
    1672          26 :   pari_sp av = avma;
    1673          26 :   switch (flag) {
    1674          18 :     case 0: flag = PLOT_PARAMETRIC|PLOT_POINTS; break;
    1675           8 :     case 1: flag = PLOT_PARAMETRIC; break;
    1676           0 :     default: flag |= PLOT_PARAMETRIC; break;
    1677             :   }
    1678          26 :   return gerepileupto(av, plotrecthraw_i(fmt, T, NUMRECT-1, mkvec2(X,Y), flag));
    1679             : }
    1680             : GEN
    1681           8 : plothraw(GEN X, GEN Y, long flags)
    1682           8 : { PARI_plot T; pari_get_plot(&T); return plothraw_i(NULL,&T,X,Y,flags); }
    1683             : GEN
    1684           0 : psplothraw(GEN X, GEN Y, long flags)
    1685           0 : { PARI_plot T; pari_get_psplot(&T); T.draw = &_psdraw;
    1686           0 :   return plothraw_i(NULL,&T,X,Y,flags); }
    1687             : GEN
    1688           0 : plotrecthraw(long ne, GEN data, long flags)
    1689           0 : { return plotrecthraw_i(NULL, NULL, ne, data, flags); }
    1690             : GEN
    1691          18 : plothrawexport(GEN fmt, GEN X, GEN Y, long flags)
    1692          18 : { PARI_plot T; pari_get_fmtplot(fmt,&T); return plothraw_i(fmt,&T,X,Y,flags); }
    1693             : 
    1694             : GEN
    1695           4 : plothsizes(long flag)
    1696             : {
    1697           4 :   GEN vect = cgetg(1+8,t_VEC);
    1698             :   PARI_plot T;
    1699             : 
    1700           4 :   pari_get_plot(&T);
    1701           4 :   gel(vect,1) = stoi(T.width);
    1702           4 :   gel(vect,2) = stoi(T.height);
    1703           4 :   if (flag) {
    1704           0 :     gel(vect,3) = dbltor(T.hunit*1.0/T.width);
    1705           0 :     gel(vect,4) = dbltor(T.vunit*1.0/T.height);
    1706           0 :     gel(vect,5) = dbltor(T.fwidth*1.0/T.width);
    1707           0 :     gel(vect,6) = dbltor(T.fheight*1.0/T.height);
    1708             :   } else {
    1709           4 :     gel(vect,3) = stoi(T.hunit);
    1710           4 :     gel(vect,4) = stoi(T.vunit);
    1711           4 :     gel(vect,5) = stoi(T.fwidth);
    1712           4 :     gel(vect,6) = stoi(T.fheight);
    1713             :   }
    1714           4 :   gel(vect,7) = stoi(T.dwidth);
    1715           4 :   gel(vect,8) = stoi(T.dheight);
    1716           4 :   return vect;
    1717             : }
    1718             : 
    1719             : /*************************************************************************/
    1720             : /*                                                                       */
    1721             : /*                         POSTSCRIPT OUTPUT                             */
    1722             : /*                                                                       */
    1723             : /*************************************************************************/
    1724             : static long
    1725          92 : wxy_n(GEN wxy)
    1726             : {
    1727             :   long n;
    1728          92 :   switch(typ(wxy))
    1729             :   {
    1730          22 :     case t_INT: return 1;
    1731          70 :     case t_VEC:
    1732          70 :       n = lg(wxy)-1;
    1733          70 :       if (n%3) pari_err_DIM("plotdraw");
    1734          70 :       return n/3;
    1735             :   }
    1736           0 :   pari_err_TYPE("plotdraw",wxy);
    1737             :   return 0;/*LCOV_EXCL_LINE*/
    1738             : }
    1739             : static void
    1740          92 : wxy_init(GEN wxy, GEN *pW, GEN *pX, GEN *pY, PARI_plot *T)
    1741             : {
    1742          92 :   long i, n = wxy_n(wxy);
    1743             :   GEN W, X, Y;
    1744          92 :   *pW = W = cgetg(n+1, t_VECSMALL); /* win number */
    1745          92 :   *pX = X = cgetg(n+1, t_VECSMALL);
    1746          92 :   *pY = Y = cgetg(n+1, t_VECSMALL); /* (x,y)-offset */
    1747          92 :   if (typ(wxy) == t_INT)
    1748             :   {
    1749          22 :     W[1] = itos(wxy); check_rect_init(W[1]);
    1750          22 :     X[1] = 0;
    1751          22 :     Y[1] = 0; return;
    1752             :   }
    1753         140 :   for (i = 1; i <= n; i++)
    1754             :   {
    1755          70 :     GEN w = gel(wxy,3*i-2), x = gel(wxy,3*i-1), y = gel(wxy,3*i);
    1756          70 :     if (typ(w) != t_INT) pari_err_TYPE("plotdraw",w);
    1757          70 :     W[i] = itos(w); check_rect_init(W[i]);
    1758          70 :     if (T) {
    1759           0 :       X[i] = DTOL(gtodouble(x)*(T->width - 1));
    1760           0 :       Y[i] = DTOL(gtodouble(y)*(T->height - 1));
    1761             :     } else {
    1762          70 :       X[i] = gtos(x);
    1763          70 :       Y[i] = gtos(y);
    1764             :     }
    1765             :   }
    1766             : }
    1767             : /* if flag is set, rescale wrt T */
    1768             : static void
    1769          28 : gendraw(PARI_plot *T, GEN wxy, long flag)
    1770             : {
    1771             :   GEN w, x, y, W, X, Y;
    1772             :   long i, l;
    1773          28 :   wxy_init(wxy, &w,&x,&y, flag? T: NULL);
    1774          28 :   l = lg(w);
    1775             :   /* malloc mandatory in case draw() forks then pari_close(). Done after
    1776             :    * wxy_init to avoid leak on error */
    1777          28 :   W = cgetalloc(t_VECSMALL, l);
    1778          28 :   X = cgetalloc(t_VECSMALL, l);
    1779          28 :   Y = cgetalloc(t_VECSMALL, l);
    1780          56 :   for (i = 1; i < l; i++) { W[i] = w[i]; X[i] = x[i]; Y[i] = y[i]; }
    1781          28 :   Draw(T,W,X,Y);
    1782          28 :   pari_free(W); pari_free(X); pari_free(Y);
    1783          28 : }
    1784             : void
    1785           0 : psdraw(GEN wxy, long flag)
    1786           0 : { PARI_plot T; pari_get_psplot(&T); T.draw = flag? &_psdraw: &_psdraw_scale;
    1787           0 :   gendraw(&T, wxy, flag); }
    1788             : void
    1789          28 : plotdraw(GEN wxy, long flag)
    1790          28 : { PARI_plot T; pari_get_plot(&T); gendraw(&T, wxy, flag); }
    1791             : GEN
    1792          64 : plotexport(GEN fmt, GEN wxy, long flag)
    1793             : {
    1794          64 :   pari_sp av = avma;
    1795             :   GEN w, x, y;
    1796          64 :   PARI_plot _T, *T = flag? &_T: NULL;
    1797          64 :   if (T) pari_get_plot(T);
    1798          64 :   wxy_init(wxy, &w, &x, &y, T);
    1799          64 :   return gerepileuptoleaf(av, fmt_convert(fmt, w, x, y, T));
    1800             : }
    1801             : 
    1802             : /* may be called after pari_close(): don't use the PARI stack */
    1803             : void
    1804         135 : gen_draw(struct plot_eng *eng, GEN w, GEN x, GEN y, double xs, double ys)
    1805             : {
    1806         135 :   void *data = eng->data;
    1807         135 :   long i, j, lw = lg(w);
    1808         135 :   long hgapsize = eng->pl->hunit, fheight = eng->pl->fheight;
    1809         135 :   long vgapsize = eng->pl->vunit,  fwidth = eng->pl->fwidth;
    1810         341 :   for(i = 1; i < lw; i++)
    1811             :   {
    1812         206 :     PariRect *e = &rectgraph[w[i]];
    1813             :     RectObj *R;
    1814         206 :     long x0 = x[i], y0 = y[i];
    1815        9776 :     for (R = RHead(e); R; R = RoNext(R))
    1816             :     {
    1817        9570 :       long col = RoCol(R);
    1818        9570 :       switch(RoType(R))
    1819             :       {
    1820          64 :       case ROt_PT:
    1821          64 :         eng->sc(data,col);
    1822          64 :         eng->pt(data, DTOL((RoPTx(R)+x0)*xs), DTOL((RoPTy(R)+y0)*ys));
    1823          64 :         break;
    1824        7670 :       case ROt_LN:
    1825        7670 :         eng->sc(data,col);
    1826        7670 :         eng->ln(data, DTOL((RoLNx1(R)+x0)*xs), DTOL((RoLNy1(R)+y0)*ys),
    1827        7670 :                       DTOL((RoLNx2(R)+x0)*xs), DTOL((RoLNy2(R)+y0)*ys));
    1828        7670 :         break;
    1829         160 :       case ROt_BX:
    1830         160 :         eng->sc(data,col);
    1831         160 :         eng->bx(data,
    1832         160 :                 DTOL((RoBXx1(R)+x0)*xs),
    1833         160 :                 DTOL((RoBXy1(R)+y0)*ys),
    1834         160 :                 DTOL((RoBXx2(R)-RoBXx1(R))*xs),
    1835         160 :                 DTOL((RoBXy2(R)-RoBXy1(R))*ys));
    1836         160 :         break;
    1837           0 :       case ROt_FBX:
    1838           0 :         eng->sc(data,col);
    1839           0 :         eng->fb(data,
    1840           0 :                 DTOL((RoBXx1(R)+x0)*xs),
    1841           0 :                 DTOL((RoBXy1(R)+y0)*ys),
    1842           0 :                 DTOL((RoBXx2(R)-RoBXx1(R))*xs),
    1843           0 :                 DTOL((RoBXy2(R)-RoBXy1(R))*ys));
    1844           0 :         break;
    1845          53 :       case ROt_MP:
    1846             :         {
    1847          53 :           double *ptx = RoMPxs(R);
    1848          53 :           double *pty = RoMPys(R);
    1849          53 :           long     nb = RoMPcnt(R);
    1850             :           struct plot_points *points =
    1851          53 :             (struct plot_points *) pari_malloc(sizeof(*points)*nb);
    1852        1841 :           for(j=0;j<nb;j++)
    1853             :           {
    1854        1788 :             points[j].x = DTOL((ptx[j]+x0)*xs);
    1855        1788 :             points[j].y = DTOL((pty[j]+y0)*ys);
    1856             :           }
    1857          53 :           eng->sc(data,col);
    1858          53 :           eng->mp(data, nb, points);
    1859          53 :           pari_free(points);
    1860          53 :           break;
    1861             :         }
    1862         103 :       case ROt_ML:
    1863             :         {
    1864         103 :           double *ptx = RoMLxs(R);
    1865         103 :           double *pty = RoMLys(R);
    1866         103 :           long     nb = RoMLcnt(R);
    1867             :           struct plot_points *points =
    1868         103 :             (struct plot_points *) pari_malloc(sizeof(*points)*nb);
    1869       60202 :           for(j=0;j<nb;j++)
    1870             :           {
    1871       60099 :             points[j].x = DTOL((ptx[j]+x0)*xs);
    1872       60099 :             points[j].y = DTOL((pty[j]+y0)*ys);
    1873             :           }
    1874         103 :           eng->sc(data,col);
    1875         103 :           eng->ml(data, nb, points);
    1876         103 :           pari_free(points);
    1877         103 :           break;
    1878             :         }
    1879         320 :       case ROt_ST:
    1880             :         {
    1881         320 :           long dir = RoSTdir(R);
    1882         320 :           long h = dir & RoSTdirHPOS_mask, hgap  = 0;
    1883         320 :           long v = dir & RoSTdirVPOS_mask, vgap  = 0;
    1884         320 :           long x, y, l = RoSTl(R);
    1885         320 :           long shift = (h == RoSTdirLEFT ? 0 : (h == RoSTdirRIGHT? 2: 1));
    1886         320 :           long vshift= (v == RoSTdirBOTTOM? 0: (v == RoSTdirTOP? 2: 1));
    1887         320 :           if (dir & RoSTdirHGAP)
    1888         142 :             hgap = (h == RoSTdirLEFT) ? hgapsize : -hgapsize;
    1889         320 :           if (dir & RoSTdirVGAP)
    1890          71 :             vgap = (v == RoSTdirBOTTOM) ? 2*vgapsize : -2*vgapsize;
    1891         320 :           x = DTOL(xs * (RoSTx(R) + x0 + hgap - (l * fwidth * shift)/2));
    1892         320 :           y = DTOL(ys * (RoSTy(R) + y0 - (vgap - vshift*(fheight-1))/2));
    1893         320 :           eng->sc(data,col);
    1894         320 :           eng->st(data, x, y, RoSTs(R), l);
    1895         320 :           break;
    1896             :         }
    1897        1200 :       default:
    1898        1200 :         break;
    1899             :       }
    1900             :     }
    1901             :   }
    1902         135 : }
    1903             : /*************************************************************************/
    1904             : /*                               SVG                                     */
    1905             : /*************************************************************************/
    1906             : 
    1907             : struct svg_data {
    1908             :   pari_str str;
    1909             :   char hexcolor[8];  /* "#rrggbb\0" */
    1910             : };
    1911             : #define data_str(d) (&((struct svg_data*)(d))->str)
    1912             : #define data_hexcolor(d) (((struct svg_data*)(d))->hexcolor)
    1913             : 
    1914             : /* Work with precision 1/scale */
    1915             : static const float SVG_SCALE = 1024.0;
    1916             : 
    1917             : static float
    1918      127122 : svg_rescale(float x) { return x / SVG_SCALE; }
    1919             : 
    1920             : static void
    1921         992 : svg_point(void *data, long x, long y)
    1922             : {
    1923         992 :   pari_str *S = data_str(data);
    1924             : 
    1925         992 :   str_printf(S, "<circle cx='%.2f' cy='%.2f' r='0.5' ",
    1926         992 :     svg_rescale(x), svg_rescale(y));
    1927         992 :   str_printf(S, "style='fill:%s;stroke:none;'/>", data_hexcolor(data));
    1928         992 : }
    1929             : 
    1930             : static void
    1931        4905 : svg_line(void *data, long x1, long y1, long x2, long y2)
    1932             : {
    1933        4905 :   pari_str *S = data_str(data);
    1934             : 
    1935        4905 :   str_printf(S, "<line x1='%.2f' y1='%.2f' x2='%.2f' y2='%.2f' ",
    1936        4905 :     svg_rescale(x1), svg_rescale(y1), svg_rescale(x2), svg_rescale(y2));
    1937        4905 :   str_printf(S, "style='fill:none;stroke:%s;'/>", data_hexcolor(data));
    1938        4905 : }
    1939             : 
    1940             : static void
    1941         104 : svg_rect(void *data, long x, long y, long w, long h)
    1942             : {
    1943         104 :   pari_str *S = data_str(data);
    1944             : 
    1945         104 :   str_printf(S, "<rect x='%.2f' y='%.2f' width='%.2f' height='%.2f' ",
    1946         104 :     svg_rescale(x), svg_rescale(y), svg_rescale(w), svg_rescale(h));
    1947         104 :   str_printf(S, "style='fill:none;stroke:%s;'/>", data_hexcolor(data));
    1948         104 : }
    1949             : 
    1950             : static void
    1951           0 : svg_fillrect(void *data, long x, long y, long w, long h)
    1952             : {
    1953           0 :   pari_str *S = data_str(data);
    1954           0 :   const char * color = data_hexcolor(data);
    1955           0 :   str_printf(S, "<rect x='%.2f' y='%.2f' width='%.2f' height='%.2f' ",
    1956           0 :     svg_rescale(x), svg_rescale(y), svg_rescale(w), svg_rescale(h));
    1957           0 :   str_printf(S, "style='fill:%s;stroke:%s;'/>", color, color);
    1958           0 : }
    1959             : 
    1960             : static void
    1961          32 : svg_points(void *data, long nb, struct plot_points *p)
    1962             : {
    1963             :   long i;
    1964         981 :   for (i = 0; i < nb; i++)
    1965         949 :     svg_point(data, p[i].x, p[i].y);
    1966          32 : }
    1967             : 
    1968             : static void
    1969        5467 : svg_color(void *data, long col)
    1970             : {
    1971             :   static const char hex[] = "0123456789abcdef";
    1972        5467 :   char *c = data_hexcolor(data);
    1973             :   int r, g, b;
    1974        5467 :   long_to_rgb(col, &r, &g, &b);
    1975        5467 :   c[0] = '#';
    1976        5467 :   c[1] = hex[r / 16];
    1977        5467 :   c[2] = hex[r & 15];
    1978        5467 :   c[3] = hex[g / 16];
    1979        5467 :   c[4] = hex[g & 15];
    1980        5467 :   c[5] = hex[b / 16];
    1981        5467 :   c[6] = hex[b & 15];
    1982        5467 :   c[7] = '\0';
    1983        5467 : }
    1984             : 
    1985             : static void
    1986          75 : svg_lines(void *data, long nb, struct plot_points *p)
    1987             : {
    1988             :   long i;
    1989          75 :   pari_str *S = data_str(data);
    1990             : 
    1991          75 :   str_printf(S, "<polyline points='");
    1992       52404 :   for (i = 0; i < nb; i++)
    1993             :   {
    1994       52329 :     if (i > 0) str_printf(S, " ");
    1995       52329 :     str_printf(S, "%.2f,%.2f", svg_rescale(p[i].x), svg_rescale(p[i].y));
    1996             :   }
    1997          75 :   str_printf(S, "' style='fill:none;stroke:%s;'/>", data_hexcolor(data));
    1998          75 : }
    1999             : 
    2000             : static void
    2001         222 : svg_text(void *data, long x, long y, char *text, long numtext)
    2002             : {
    2003         222 :   pari_str *S = data_str(data);
    2004             :   (void)numtext;
    2005         222 :   str_printf(S, "<text x='%.5f' y='%.5f' font-size='%ld' style='fill:%s;'>%s</text>",
    2006         222 :     svg_rescale(x),svg_rescale(y), 12, data_hexcolor(data), text);
    2007         222 : }
    2008             : 
    2009             : static void
    2010          86 : svg_head(PARI_plot *T, pari_str *S)
    2011             : {
    2012          86 :   str_printf(S, "<svg width='%ld' height='%ld' version='1.1' xmlns='http://www.w3.org/2000/svg'>", T->width, T->height);
    2013          86 : }
    2014             : 
    2015             : static void
    2016          86 : svg_tail(pari_str *S)
    2017             : {
    2018          86 :   str_printf(S, "</svg>");
    2019          86 : }
    2020             : 
    2021             : char *
    2022          86 : rect2svg(GEN w, GEN x, GEN y, PARI_plot *T)
    2023             : {
    2024             :   struct plot_eng pl;
    2025             :   struct svg_data data;
    2026             :   PARI_plot U;
    2027             : 
    2028          86 :   str_init(&data.str, 1);
    2029          86 :   svg_color(&data, 0);
    2030          86 :   if (!T)
    2031             :   {
    2032          36 :     long i, l = lg(w), xmax = 0, ymax = 0;
    2033          36 :     T = &U; pari_get_svgplot(T);
    2034          72 :     for (i = 1; i < l; i++)
    2035             :     {
    2036          36 :       PariRect *e = check_rect_init(w[i]);
    2037          36 :       xmax = maxss(xmax, RXsize(e) + x[i]);
    2038          36 :       ymax = maxss(ymax, RYsize(e) + y[i]);
    2039             :     }
    2040          36 :     T->width = xmax;
    2041          36 :     T->height = ymax;
    2042             :   }
    2043          86 :   pl.data = &data;
    2044          86 :   pl.sc = &svg_color;
    2045          86 :   pl.pt = &svg_point;
    2046          86 :   pl.ln = &svg_line;
    2047          86 :   pl.bx = &svg_rect;
    2048          86 :   pl.fb = &svg_fillrect;
    2049          86 :   pl.mp = &svg_points;
    2050          86 :   pl.ml = &svg_lines;
    2051          86 :   pl.st = &svg_text;
    2052          86 :   pl.pl = T;
    2053             : 
    2054          86 :   svg_head(T, &data.str);
    2055          86 :   gen_draw(&pl, w, x, y, SVG_SCALE, SVG_SCALE);
    2056          86 :   svg_tail(&data.str);
    2057             : 
    2058          86 :   return data.str.string;
    2059             : }
    2060             : 
    2061             : /*************************************************************************/
    2062             : /*                            POSTSCRIPT                                 */
    2063             : /*************************************************************************/
    2064             : static void
    2065        2989 : ps_sc(void *data, long col)
    2066             : {
    2067        2989 :   pari_str *S = (pari_str*)data;
    2068        2989 :   int r, g, b; long_to_rgb(col, &r, &g, &b);
    2069        2989 :   if (!r && !g && !b)
    2070        2940 :     str_puts(S,"c0\n");
    2071             :   else
    2072          49 :     str_printf(S,"%.6f %.6f %.6f c\n", r/255., g/255., b/255.);
    2073        2989 : }
    2074             : 
    2075             : static void
    2076         860 : ps_point(void *data, long x, long y)
    2077             : {
    2078         860 :   pari_str *S = (pari_str*)data;
    2079         860 :   str_printf(S,"%ld %ld p\n",y,x);
    2080         860 : }
    2081             : 
    2082             : static void
    2083        2765 : ps_line(void *data, long x1, long y1, long x2, long y2)
    2084             : {
    2085        2765 :   pari_str *S = (pari_str*)data;
    2086        2765 :   str_printf(S,"%ld %ld m %ld %ld l\n",y1,x1,y2,x2);
    2087        2765 :   str_printf(S,"stroke\n");
    2088        2765 : }
    2089             : 
    2090             : static void
    2091          56 : ps_rect(void *data, long x, long y, long w, long h)
    2092             : {
    2093          56 :   pari_str *S = (pari_str*)data;
    2094          56 :   str_printf(S,"%ld %ld m %ld %ld l %ld %ld l %ld %ld l closepath currentlinejoin 0 setlinejoin stroke setlinejoin\n",
    2095             :              y,x, y,x+w, y+h,x+w, y+h,x);
    2096          56 : }
    2097             : 
    2098             : static void
    2099           0 : ps_fillrect(void *data, long x, long y, long w, long h)
    2100             : {
    2101           0 :   pari_str *S = (pari_str*)data;
    2102           0 :   str_printf(S,"%ld %ld m %ld %ld l %ld %ld l %ld %ld l closepath currentlinejoin 0 setlinejoin fill setlinejoin\n",
    2103             :              y,x, y,x+w, y+h,x+w, y+h,x);
    2104           0 : }
    2105             : 
    2106             : static void
    2107          21 : ps_points(void *data, long nb, struct plot_points *p)
    2108             : {
    2109             :   long i;
    2110         860 :   for (i=0; i<nb; i++) ps_point(data, p[i].x, p[i].y);
    2111          21 : }
    2112             : 
    2113             : static void
    2114          28 : ps_lines(void *data, long nb, struct plot_points *p)
    2115             : {
    2116          28 :   pari_str *S = (pari_str*)data;
    2117             :   long i;
    2118          28 :   str_printf(S,"%ld %ld m\n",p[0].y,p[0].x);
    2119        7770 :   for (i=1; i<nb; i++) str_printf(S, "%ld %ld l\n", p[i].y, p[i].x);
    2120          28 :   str_printf(S,"stroke\n");
    2121          28 : }
    2122             : 
    2123             : static void
    2124          98 : ps_string(void *data, long x, long y, char *s, long length)
    2125             : {
    2126          98 :   pari_str *S = (pari_str*)data;
    2127             :   (void)length;
    2128          98 :   if (strpbrk(s, "(\\)")) {
    2129           7 :     str_printf(S,"(");
    2130          42 :     while (*s) {
    2131          35 :       if ( *s=='(' || *s==')' || *s=='\\' ) str_putc(S,'\\');
    2132          35 :       str_putc(S, *s);
    2133          35 :       s++;
    2134             :     }
    2135             :   } else
    2136          91 :     str_printf(S,"(%s", s);
    2137          98 :   str_printf(S,") %ld %ld m 90 rotate show -90 rotate\n", y, x);
    2138          98 : }
    2139             : 
    2140             : char *
    2141          49 : rect2ps_i(GEN w, GEN x, GEN y, PARI_plot *T, int plotps)
    2142             : {
    2143             :   struct plot_eng pl;
    2144             :   PARI_plot U;
    2145             :   pari_str S;
    2146          49 :   double xs = 0.65*PS_SCALE, ys = 0.65*PS_SCALE;
    2147          49 :   if (T) /* res wrt T dimens */
    2148             :   {
    2149          21 :     if (plotps)
    2150           0 :       xs = ys = PS_SCALE;
    2151             :     else
    2152             :     {
    2153          21 :       xs *= ((double)PS_WIDTH) / T->width;
    2154          21 :       ys *= ((double)PS_HEIGH) / T->height;
    2155             :     }
    2156             :   }
    2157             :   else
    2158             :   {
    2159          28 :     T = &U; pari_get_psplot(T);
    2160             :   }
    2161          49 :   str_init(&S, 1);
    2162             :   /* Definitions taken from post terminal of Gnuplot. */
    2163          49 :   str_printf(&S, "%%!\n\
    2164             : 50 50 translate\n\
    2165             : 1 %d div 1 %d div scale\n\
    2166             : 1 setlinejoin\n\
    2167             : /p {moveto 0 2 rlineto 2 0 rlineto 0 -2 rlineto closepath fill} def\n\
    2168             : /c0 {0 0 0 setrgbcolor} def\n\
    2169             : /c {setrgbcolor} def\n\
    2170             : /l {lineto} def\n\
    2171             : /m {moveto} def\n"
    2172             : "/Times-Roman findfont %ld scalefont setfont\n",
    2173          49 : PS_SCALE, PS_SCALE, DTOL(T->fheight * xs));
    2174             : 
    2175          49 :   pl.sc = &ps_sc;
    2176          49 :   pl.pt = &ps_point;
    2177          49 :   pl.ln = &ps_line;
    2178          49 :   pl.bx = &ps_rect;
    2179          49 :   pl.fb = &ps_fillrect;
    2180          49 :   pl.mp = &ps_points;
    2181          49 :   pl.ml = &ps_lines;
    2182          49 :   pl.st = &ps_string;
    2183          49 :   pl.pl = T;
    2184          49 :   pl.data = (void*)&S;
    2185             : 
    2186          49 :   if (plotps) str_printf(&S,"0 %ld translate -90 rotate\n", (T->height - 50)*PS_SCALE);
    2187          49 :   gen_draw(&pl, w, x, y, xs, ys);
    2188          49 :   str_puts(&S,"stroke showpage\n");
    2189          49 :   *S.cur = 0; return S.string;
    2190             : }
    2191             : char *
    2192          49 : rect2ps(GEN w, GEN x, GEN y, PARI_plot *T)
    2193          49 : { return rect2ps_i(w,x,y,T,0); }
    2194             : 
    2195             : void
    2196           0 : pari_plot_by_file(const char *env, const char *suf, const char *img)
    2197             : {
    2198           0 :   const char *cmd, *s = pari_unique_filename_suffix("plotfile", suf);
    2199           0 :   FILE *f = fopen(s, "w");
    2200           0 :   if (!f) pari_err_FILE("image file", s);
    2201           0 :   fputs(img, f); (void)fclose(f);
    2202           0 :   cmd = os_getenv(env);
    2203             : #ifdef GP_MIME_OPEN
    2204           0 :   if (!cmd) cmd = GP_MIME_OPEN;
    2205             : #else
    2206             :   if (!cmd) cmd = "open -W";
    2207             : #endif
    2208           0 :   cmd = pari_sprintf("%s \"%s\" 2>/dev/null", cmd, s);
    2209           0 :   gpsystem(cmd);
    2210           0 :   pari_unlink(s);
    2211           0 :   pari_free((char*)s);
    2212           0 : }
    2213             : 
    2214             : /*************************************************************************/
    2215             : /*                                                                       */
    2216             : /*                           RGB COLORS                                  */
    2217             : /*                                                                       */
    2218             : /*************************************************************************/
    2219             : /* generated from /etc/X11/rgb.txt by the following perl script
    2220             : #!/usr/bin/perl
    2221             : while(<>)
    2222             : {
    2223             :   ($hex, $name) = split(/\t\t/, $_);
    2224             :   $hex =~ s/^ +//; chomp($name); $name =~ s, *,,g;
    2225             :   $hex = sprintf("0x%02x%02x%02x", split(/\s+/, $hex));
    2226             :   $name = lc($name); next if ($done{$name});
    2227             :   $done{$name} = 1;
    2228             :   print "COL(\"$name\", $hex),\n";
    2229             : }
    2230             : */
    2231             : 
    2232             : #define COL(x,y) {(void*)x,(void*)y,0,NULL}
    2233             : static hashentry col_list[] = {
    2234             : COL("", 0x000000),
    2235             : COL("snow", 0xfffafa),
    2236             : COL("ghostwhite", 0xf8f8ff),
    2237             : COL("whitesmoke", 0xf5f5f5),
    2238             : COL("gainsboro", 0xdcdcdc),
    2239             : COL("floralwhite", 0xfffaf0),
    2240             : COL("oldlace", 0xfdf5e6),
    2241             : COL("linen", 0xfaf0e6),
    2242             : COL("antiquewhite", 0xfaebd7),
    2243             : COL("papayawhip", 0xffefd5),
    2244             : COL("blanchedalmond", 0xffebcd),
    2245             : COL("bisque", 0xffe4c4),
    2246             : COL("peachpuff", 0xffdab9),
    2247             : COL("navajowhite", 0xffdead),
    2248             : COL("moccasin", 0xffe4b5),
    2249             : COL("cornsilk", 0xfff8dc),
    2250             : COL("ivory", 0xfffff0),
    2251             : COL("lemonchiffon", 0xfffacd),
    2252             : COL("seashell", 0xfff5ee),
    2253             : COL("honeydew", 0xf0fff0),
    2254             : COL("mintcream", 0xf5fffa),
    2255             : COL("azure", 0xf0ffff),
    2256             : COL("aliceblue", 0xf0f8ff),
    2257             : COL("lavender", 0xe6e6fa),
    2258             : COL("lavenderblush", 0xfff0f5),
    2259             : COL("mistyrose", 0xffe4e1),
    2260             : COL("white", 0xffffff),
    2261             : COL("black", 0x000000),
    2262             : COL("darkslategray", 0x2f4f4f),
    2263             : COL("darkslategrey", 0x2f4f4f),
    2264             : COL("dimgray", 0x696969),
    2265             : COL("dimgrey", 0x696969),
    2266             : COL("slategray", 0x708090),
    2267             : COL("slategrey", 0x708090),
    2268             : COL("lightslategray", 0x778899),
    2269             : COL("lightslategrey", 0x778899),
    2270             : COL("gray", 0xbebebe),
    2271             : COL("grey", 0xbebebe),
    2272             : COL("lightgrey", 0xd3d3d3),
    2273             : COL("lightgray", 0xd3d3d3),
    2274             : COL("midnightblue", 0x191970),
    2275             : COL("navy", 0x000080),
    2276             : COL("navyblue", 0x000080),
    2277             : COL("cornflowerblue", 0x6495ed),
    2278             : COL("darkslateblue", 0x483d8b),
    2279             : COL("slateblue", 0x6a5acd),
    2280             : COL("mediumslateblue", 0x7b68ee),
    2281             : COL("lightslateblue", 0x8470ff),
    2282             : COL("mediumblue", 0x0000cd),
    2283             : COL("royalblue", 0x4169e1),
    2284             : COL("blue", 0x0000ff),
    2285             : COL("dodgerblue", 0x1e90ff),
    2286             : COL("deepskyblue", 0x00bfff),
    2287             : COL("skyblue", 0x87ceeb),
    2288             : COL("lightskyblue", 0x87cefa),
    2289             : COL("steelblue", 0x4682b4),
    2290             : COL("lightsteelblue", 0xb0c4de),
    2291             : COL("lightblue", 0xadd8e6),
    2292             : COL("powderblue", 0xb0e0e6),
    2293             : COL("paleturquoise", 0xafeeee),
    2294             : COL("darkturquoise", 0x00ced1),
    2295             : COL("mediumturquoise", 0x48d1cc),
    2296             : COL("turquoise", 0x40e0d0),
    2297             : COL("cyan", 0x00ffff),
    2298             : COL("lightcyan", 0xe0ffff),
    2299             : COL("cadetblue", 0x5f9ea0),
    2300             : COL("mediumaquamarine", 0x66cdaa),
    2301             : COL("aquamarine", 0x7fffd4),
    2302             : COL("darkgreen", 0x006400),
    2303             : COL("darkolivegreen", 0x556b2f),
    2304             : COL("darkseagreen", 0x8fbc8f),
    2305             : COL("seagreen", 0x2e8b57),
    2306             : COL("mediumseagreen", 0x3cb371),
    2307             : COL("lightseagreen", 0x20b2aa),
    2308             : COL("palegreen", 0x98fb98),
    2309             : COL("springgreen", 0x00ff7f),
    2310             : COL("lawngreen", 0x7cfc00),
    2311             : COL("green", 0x00ff00),
    2312             : COL("chartreuse", 0x7fff00),
    2313             : COL("mediumspringgreen", 0x00fa9a),
    2314             : COL("greenyellow", 0xadff2f),
    2315             : COL("limegreen", 0x32cd32),
    2316             : COL("yellowgreen", 0x9acd32),
    2317             : COL("forestgreen", 0x228b22),
    2318             : COL("olivedrab", 0x6b8e23),
    2319             : COL("darkkhaki", 0xbdb76b),
    2320             : COL("khaki", 0xf0e68c),
    2321             : COL("palegoldenrod", 0xeee8aa),
    2322             : COL("lightgoldenrodyellow", 0xfafad2),
    2323             : COL("lightyellow", 0xffffe0),
    2324             : COL("yellow", 0xffff00),
    2325             : COL("gold", 0xffd700),
    2326             : COL("lightgoldenrod", 0xeedd82),
    2327             : COL("goldenrod", 0xdaa520),
    2328             : COL("darkgoldenrod", 0xb8860b),
    2329             : COL("rosybrown", 0xbc8f8f),
    2330             : COL("indianred", 0xcd5c5c),
    2331             : COL("saddlebrown", 0x8b4513),
    2332             : COL("sienna", 0xa0522d),
    2333             : COL("peru", 0xcd853f),
    2334             : COL("burlywood", 0xdeb887),
    2335             : COL("beige", 0xf5f5dc),
    2336             : COL("wheat", 0xf5deb3),
    2337             : COL("sandybrown", 0xf4a460),
    2338             : COL("tan", 0xd2b48c),
    2339             : COL("chocolate", 0xd2691e),
    2340             : COL("firebrick", 0xb22222),
    2341             : COL("brown", 0xa52a2a),
    2342             : COL("darksalmon", 0xe9967a),
    2343             : COL("salmon", 0xfa8072),
    2344             : COL("lightsalmon", 0xffa07a),
    2345             : COL("orange", 0xffa500),
    2346             : COL("darkorange", 0xff8c00),
    2347             : COL("coral", 0xff7f50),
    2348             : COL("lightcoral", 0xf08080),
    2349             : COL("tomato", 0xff6347),
    2350             : COL("orangered", 0xff4500),
    2351             : COL("red", 0xff0000),
    2352             : COL("hotpink", 0xff69b4),
    2353             : COL("deeppink", 0xff1493),
    2354             : COL("pink", 0xffc0cb),
    2355             : COL("lightpink", 0xffb6c1),
    2356             : COL("palevioletred", 0xdb7093),
    2357             : COL("maroon", 0xb03060),
    2358             : COL("mediumvioletred", 0xc71585),
    2359             : COL("violetred", 0xd02090),
    2360             : COL("magenta", 0xff00ff),
    2361             : COL("violet", 0xee82ee),
    2362             : COL("plum", 0xdda0dd),
    2363             : COL("orchid", 0xda70d6),
    2364             : COL("mediumorchid", 0xba55d3),
    2365             : COL("darkorchid", 0x9932cc),
    2366             : COL("darkviolet", 0x9400d3),
    2367             : COL("blueviolet", 0x8a2be2),
    2368             : COL("purple", 0xa020f0),
    2369             : COL("mediumpurple", 0x9370db),
    2370             : COL("thistle", 0xd8bfd8),
    2371             : COL("snow1", 0xfffafa),
    2372             : COL("snow2", 0xeee9e9),
    2373             : COL("snow3", 0xcdc9c9),
    2374             : COL("snow4", 0x8b8989),
    2375             : COL("seashell1", 0xfff5ee),
    2376             : COL("seashell2", 0xeee5de),
    2377             : COL("seashell3", 0xcdc5bf),
    2378             : COL("seashell4", 0x8b8682),
    2379             : COL("antiquewhite1", 0xffefdb),
    2380             : COL("antiquewhite2", 0xeedfcc),
    2381             : COL("antiquewhite3", 0xcdc0b0),
    2382             : COL("antiquewhite4", 0x8b8378),
    2383             : COL("bisque1", 0xffe4c4),
    2384             : COL("bisque2", 0xeed5b7),
    2385             : COL("bisque3", 0xcdb79e),
    2386             : COL("bisque4", 0x8b7d6b),
    2387             : COL("peachpuff1", 0xffdab9),
    2388             : COL("peachpuff2", 0xeecbad),
    2389             : COL("peachpuff3", 0xcdaf95),
    2390             : COL("peachpuff4", 0x8b7765),
    2391             : COL("navajowhite1", 0xffdead),
    2392             : COL("navajowhite2", 0xeecfa1),
    2393             : COL("navajowhite3", 0xcdb38b),
    2394             : COL("navajowhite4", 0x8b795e),
    2395             : COL("lemonchiffon1", 0xfffacd),
    2396             : COL("lemonchiffon2", 0xeee9bf),
    2397             : COL("lemonchiffon3", 0xcdc9a5),
    2398             : COL("lemonchiffon4", 0x8b8970),
    2399             : COL("cornsilk1", 0xfff8dc),
    2400             : COL("cornsilk2", 0xeee8cd),
    2401             : COL("cornsilk3", 0xcdc8b1),
    2402             : COL("cornsilk4", 0x8b8878),
    2403             : COL("ivory1", 0xfffff0),
    2404             : COL("ivory2", 0xeeeee0),
    2405             : COL("ivory3", 0xcdcdc1),
    2406             : COL("ivory4", 0x8b8b83),
    2407             : COL("honeydew1", 0xf0fff0),
    2408             : COL("honeydew2", 0xe0eee0),
    2409             : COL("honeydew3", 0xc1cdc1),
    2410             : COL("honeydew4", 0x838b83),
    2411             : COL("lavenderblush1", 0xfff0f5),
    2412             : COL("lavenderblush2", 0xeee0e5),
    2413             : COL("lavenderblush3", 0xcdc1c5),
    2414             : COL("lavenderblush4", 0x8b8386),
    2415             : COL("mistyrose1", 0xffe4e1),
    2416             : COL("mistyrose2", 0xeed5d2),
    2417             : COL("mistyrose3", 0xcdb7b5),
    2418             : COL("mistyrose4", 0x8b7d7b),
    2419             : COL("azure1", 0xf0ffff),
    2420             : COL("azure2", 0xe0eeee),
    2421             : COL("azure3", 0xc1cdcd),
    2422             : COL("azure4", 0x838b8b),
    2423             : COL("slateblue1", 0x836fff),
    2424             : COL("slateblue2", 0x7a67ee),
    2425             : COL("slateblue3", 0x6959cd),
    2426             : COL("slateblue4", 0x473c8b),
    2427             : COL("royalblue1", 0x4876ff),
    2428             : COL("royalblue2", 0x436eee),
    2429             : COL("royalblue3", 0x3a5fcd),
    2430             : COL("royalblue4", 0x27408b),
    2431             : COL("blue1", 0x0000ff),
    2432             : COL("blue2", 0x0000ee),
    2433             : COL("blue3", 0x0000cd),
    2434             : COL("blue4", 0x00008b),
    2435             : COL("dodgerblue1", 0x1e90ff),
    2436             : COL("dodgerblue2", 0x1c86ee),
    2437             : COL("dodgerblue3", 0x1874cd),
    2438             : COL("dodgerblue4", 0x104e8b),
    2439             : COL("steelblue1", 0x63b8ff),
    2440             : COL("steelblue2", 0x5cacee),
    2441             : COL("steelblue3", 0x4f94cd),
    2442             : COL("steelblue4", 0x36648b),
    2443             : COL("deepskyblue1", 0x00bfff),
    2444             : COL("deepskyblue2", 0x00b2ee),
    2445             : COL("deepskyblue3", 0x009acd),
    2446             : COL("deepskyblue4", 0x00688b),
    2447             : COL("skyblue1", 0x87ceff),
    2448             : COL("skyblue2", 0x7ec0ee),
    2449             : COL("skyblue3", 0x6ca6cd),
    2450             : COL("skyblue4", 0x4a708b),
    2451             : COL("lightskyblue1", 0xb0e2ff),
    2452             : COL("lightskyblue2", 0xa4d3ee),
    2453             : COL("lightskyblue3", 0x8db6cd),
    2454             : COL("lightskyblue4", 0x607b8b),
    2455             : COL("slategray1", 0xc6e2ff),
    2456             : COL("slategray2", 0xb9d3ee),
    2457             : COL("slategray3", 0x9fb6cd),
    2458             : COL("slategray4", 0x6c7b8b),
    2459             : COL("lightsteelblue1", 0xcae1ff),
    2460             : COL("lightsteelblue2", 0xbcd2ee),
    2461             : COL("lightsteelblue3", 0xa2b5cd),
    2462             : COL("lightsteelblue4", 0x6e7b8b),
    2463             : COL("lightblue1", 0xbfefff),
    2464             : COL("lightblue2", 0xb2dfee),
    2465             : COL("lightblue3", 0x9ac0cd),
    2466             : COL("lightblue4", 0x68838b),
    2467             : COL("lightcyan1", 0xe0ffff),
    2468             : COL("lightcyan2", 0xd1eeee),
    2469             : COL("lightcyan3", 0xb4cdcd),
    2470             : COL("lightcyan4", 0x7a8b8b),
    2471             : COL("paleturquoise1", 0xbbffff),
    2472             : COL("paleturquoise2", 0xaeeeee),
    2473             : COL("paleturquoise3", 0x96cdcd),
    2474             : COL("paleturquoise4", 0x668b8b),
    2475             : COL("cadetblue1", 0x98f5ff),
    2476             : COL("cadetblue2", 0x8ee5ee),
    2477             : COL("cadetblue3", 0x7ac5cd),
    2478             : COL("cadetblue4", 0x53868b),
    2479             : COL("turquoise1", 0x00f5ff),
    2480             : COL("turquoise2", 0x00e5ee),
    2481             : COL("turquoise3", 0x00c5cd),
    2482             : COL("turquoise4", 0x00868b),
    2483             : COL("cyan1", 0x00ffff),
    2484             : COL("cyan2", 0x00eeee),
    2485             : COL("cyan3", 0x00cdcd),
    2486             : COL("cyan4", 0x008b8b),
    2487             : COL("darkslategray1", 0x97ffff),
    2488             : COL("darkslategray2", 0x8deeee),
    2489             : COL("darkslategray3", 0x79cdcd),
    2490             : COL("darkslategray4", 0x528b8b),
    2491             : COL("aquamarine1", 0x7fffd4),
    2492             : COL("aquamarine2", 0x76eec6),
    2493             : COL("aquamarine3", 0x66cdaa),
    2494             : COL("aquamarine4", 0x458b74),
    2495             : COL("darkseagreen1", 0xc1ffc1),
    2496             : COL("darkseagreen2", 0xb4eeb4),
    2497             : COL("darkseagreen3", 0x9bcd9b),
    2498             : COL("darkseagreen4", 0x698b69),
    2499             : COL("seagreen1", 0x54ff9f),
    2500             : COL("seagreen2", 0x4eee94),
    2501             : COL("seagreen3", 0x43cd80),
    2502             : COL("seagreen4", 0x2e8b57),
    2503             : COL("palegreen1", 0x9aff9a),
    2504             : COL("palegreen2", 0x90ee90),
    2505             : COL("palegreen3", 0x7ccd7c),
    2506             : COL("palegreen4", 0x548b54),
    2507             : COL("springgreen1", 0x00ff7f),
    2508             : COL("springgreen2", 0x00ee76),
    2509             : COL("springgreen3", 0x00cd66),
    2510             : COL("springgreen4", 0x008b45),
    2511             : COL("green1", 0x00ff00),
    2512             : COL("green2", 0x00ee00),
    2513             : COL("green3", 0x00cd00),
    2514             : COL("green4", 0x008b00),
    2515             : COL("chartreuse1", 0x7fff00),
    2516             : COL("chartreuse2", 0x76ee00),
    2517             : COL("chartreuse3", 0x66cd00),
    2518             : COL("chartreuse4", 0x458b00),
    2519             : COL("olivedrab1", 0xc0ff3e),
    2520             : COL("olivedrab2", 0xb3ee3a),
    2521             : COL("olivedrab3", 0x9acd32),
    2522             : COL("olivedrab4", 0x698b22),
    2523             : COL("darkolivegreen1", 0xcaff70),
    2524             : COL("darkolivegreen2", 0xbcee68),
    2525             : COL("darkolivegreen3", 0xa2cd5a),
    2526             : COL("darkolivegreen4", 0x6e8b3d),
    2527             : COL("khaki1", 0xfff68f),
    2528             : COL("khaki2", 0xeee685),
    2529             : COL("khaki3", 0xcdc673),
    2530             : COL("khaki4", 0x8b864e),
    2531             : COL("lightgoldenrod1", 0xffec8b),
    2532             : COL("lightgoldenrod2", 0xeedc82),
    2533             : COL("lightgoldenrod3", 0xcdbe70),
    2534             : COL("lightgoldenrod4", 0x8b814c),
    2535             : COL("lightyellow1", 0xffffe0),
    2536             : COL("lightyellow2", 0xeeeed1),
    2537             : COL("lightyellow3", 0xcdcdb4),
    2538             : COL("lightyellow4", 0x8b8b7a),
    2539             : COL("yellow1", 0xffff00),
    2540             : COL("yellow2", 0xeeee00),
    2541             : COL("yellow3", 0xcdcd00),
    2542             : COL("yellow4", 0x8b8b00),
    2543             : COL("gold1", 0xffd700),
    2544             : COL("gold2", 0xeec900),
    2545             : COL("gold3", 0xcdad00),
    2546             : COL("gold4", 0x8b7500),
    2547             : COL("goldenrod1", 0xffc125),
    2548             : COL("goldenrod2", 0xeeb422),
    2549             : COL("goldenrod3", 0xcd9b1d),
    2550             : COL("goldenrod4", 0x8b6914),
    2551             : COL("darkgoldenrod1", 0xffb90f),
    2552             : COL("darkgoldenrod2", 0xeead0e),
    2553             : COL("darkgoldenrod3", 0xcd950c),
    2554             : COL("darkgoldenrod4", 0x8b6508),
    2555             : COL("rosybrown1", 0xffc1c1),
    2556             : COL("rosybrown2", 0xeeb4b4),
    2557             : COL("rosybrown3", 0xcd9b9b),
    2558             : COL("rosybrown4", 0x8b6969),
    2559             : COL("indianred1", 0xff6a6a),
    2560             : COL("indianred2", 0xee6363),
    2561             : COL("indianred3", 0xcd5555),
    2562             : COL("indianred4", 0x8b3a3a),
    2563             : COL("sienna1", 0xff8247),
    2564             : COL("sienna2", 0xee7942),
    2565             : COL("sienna3", 0xcd6839),
    2566             : COL("sienna4", 0x8b4726),
    2567             : COL("burlywood1", 0xffd39b),
    2568             : COL("burlywood2", 0xeec591),
    2569             : COL("burlywood3", 0xcdaa7d),
    2570             : COL("burlywood4", 0x8b7355),
    2571             : COL("wheat1", 0xffe7ba),
    2572             : COL("wheat2", 0xeed8ae),
    2573             : COL("wheat3", 0xcdba96),
    2574             : COL("wheat4", 0x8b7e66),
    2575             : COL("tan1", 0xffa54f),
    2576             : COL("tan2", 0xee9a49),
    2577             : COL("tan3", 0xcd853f),
    2578             : COL("tan4", 0x8b5a2b),
    2579             : COL("chocolate1", 0xff7f24),
    2580             : COL("chocolate2", 0xee7621),
    2581             : COL("chocolate3", 0xcd661d),
    2582             : COL("chocolate4", 0x8b4513),
    2583             : COL("firebrick1", 0xff3030),
    2584             : COL("firebrick2", 0xee2c2c),
    2585             : COL("firebrick3", 0xcd2626),
    2586             : COL("firebrick4", 0x8b1a1a),
    2587             : COL("brown1", 0xff4040),
    2588             : COL("brown2", 0xee3b3b),
    2589             : COL("brown3", 0xcd3333),
    2590             : COL("brown4", 0x8b2323),
    2591             : COL("salmon1", 0xff8c69),
    2592             : COL("salmon2", 0xee8262),
    2593             : COL("salmon3", 0xcd7054),
    2594             : COL("salmon4", 0x8b4c39),
    2595             : COL("lightsalmon1", 0xffa07a),
    2596             : COL("lightsalmon2", 0xee9572),
    2597             : COL("lightsalmon3", 0xcd8162),
    2598             : COL("lightsalmon4", 0x8b5742),
    2599             : COL("orange1", 0xffa500),
    2600             : COL("orange2", 0xee9a00),
    2601             : COL("orange3", 0xcd8500),
    2602             : COL("orange4", 0x8b5a00),
    2603             : COL("darkorange1", 0xff7f00),
    2604             : COL("darkorange2", 0xee7600),
    2605             : COL("darkorange3", 0xcd6600),
    2606             : COL("darkorange4", 0x8b4500),
    2607             : COL("coral1", 0xff7256),
    2608             : COL("coral2", 0xee6a50),
    2609             : COL("coral3", 0xcd5b45),
    2610             : COL("coral4", 0x8b3e2f),
    2611             : COL("tomato1", 0xff6347),
    2612             : COL("tomato2", 0xee5c42),
    2613             : COL("tomato3", 0xcd4f39),
    2614             : COL("tomato4", 0x8b3626),
    2615             : COL("orangered1", 0xff4500),
    2616             : COL("orangered2", 0xee4000),
    2617             : COL("orangered3", 0xcd3700),
    2618             : COL("orangered4", 0x8b2500),
    2619             : COL("red1", 0xff0000),
    2620             : COL("red2", 0xee0000),
    2621             : COL("red3", 0xcd0000),
    2622             : COL("red4", 0x8b0000),
    2623             : COL("debianred", 0xd70751),
    2624             : COL("deeppink1", 0xff1493),
    2625             : COL("deeppink2", 0xee1289),
    2626             : COL("deeppink3", 0xcd1076),
    2627             : COL("deeppink4", 0x8b0a50),
    2628             : COL("hotpink1", 0xff6eb4),
    2629             : COL("hotpink2", 0xee6aa7),
    2630             : COL("hotpink3", 0xcd6090),
    2631             : COL("hotpink4", 0x8b3a62),
    2632             : COL("pink1", 0xffb5c5),
    2633             : COL("pink2", 0xeea9b8),
    2634             : COL("pink3", 0xcd919e),
    2635             : COL("pink4", 0x8b636c),
    2636             : COL("lightpink1", 0xffaeb9),
    2637             : COL("lightpink2", 0xeea2ad),
    2638             : COL("lightpink3", 0xcd8c95),
    2639             : COL("lightpink4", 0x8b5f65),
    2640             : COL("palevioletred1", 0xff82ab),
    2641             : COL("palevioletred2", 0xee799f),
    2642             : COL("palevioletred3", 0xcd6889),
    2643             : COL("palevioletred4", 0x8b475d),
    2644             : COL("maroon1", 0xff34b3),
    2645             : COL("maroon2", 0xee30a7),
    2646             : COL("maroon3", 0xcd2990),
    2647             : COL("maroon4", 0x8b1c62),
    2648             : COL("violetred1", 0xff3e96),
    2649             : COL("violetred2", 0xee3a8c),
    2650             : COL("violetred3", 0xcd3278),
    2651             : COL("violetred4", 0x8b2252),
    2652             : COL("magenta1", 0xff00ff),
    2653             : COL("magenta2", 0xee00ee),
    2654             : COL("magenta3", 0xcd00cd),
    2655             : COL("magenta4", 0x8b008b),
    2656             : COL("orchid1", 0xff83fa),
    2657             : COL("orchid2", 0xee7ae9),
    2658             : COL("orchid3", 0xcd69c9),
    2659             : COL("orchid4", 0x8b4789),
    2660             : COL("plum1", 0xffbbff),
    2661             : COL("plum2", 0xeeaeee),
    2662             : COL("plum3", 0xcd96cd),
    2663             : COL("plum4", 0x8b668b),
    2664             : COL("mediumorchid1", 0xe066ff),
    2665             : COL("mediumorchid2", 0xd15fee),
    2666             : COL("mediumorchid3", 0xb452cd),
    2667             : COL("mediumorchid4", 0x7a378b),
    2668             : COL("darkorchid1", 0xbf3eff),
    2669             : COL("darkorchid2", 0xb23aee),
    2670             : COL("darkorchid3", 0x9a32cd),
    2671             : COL("darkorchid4", 0x68228b),
    2672             : COL("purple1", 0x9b30ff),
    2673             : COL("purple2", 0x912cee),
    2674             : COL("purple3", 0x7d26cd),
    2675             : COL("purple4", 0x551a8b),
    2676             : COL("mediumpurple1", 0xab82ff),
    2677             : COL("mediumpurple2", 0x9f79ee),
    2678             : COL("mediumpurple3", 0x8968cd),
    2679             : COL("mediumpurple4", 0x5d478b),
    2680             : COL("thistle1", 0xffe1ff),
    2681             : COL("thistle2", 0xeed2ee),
    2682             : COL("thistle3", 0xcdb5cd),
    2683             : COL("thistle4", 0x8b7b8b),
    2684             : COL("gray0", 0x000000),
    2685             : COL("grey0", 0x000000),
    2686             : COL("gray1", 0x030303),
    2687             : COL("grey1", 0x030303),
    2688             : COL("gray2", 0x050505),
    2689             : COL("grey2", 0x050505),
    2690             : COL("gray3", 0x080808),
    2691             : COL("grey3", 0x080808),
    2692             : COL("gray4", 0x0a0a0a),
    2693             : COL("grey4", 0x0a0a0a),
    2694             : COL("gray5", 0x0d0d0d),
    2695             : COL("grey5", 0x0d0d0d),
    2696             : COL("gray6", 0x0f0f0f),
    2697             : COL("grey6", 0x0f0f0f),
    2698             : COL("gray7", 0x121212),
    2699             : COL("grey7", 0x121212),
    2700             : COL("gray8", 0x141414),
    2701             : COL("grey8", 0x141414),
    2702             : COL("gray9", 0x171717),
    2703             : COL("grey9", 0x171717),
    2704             : COL("gray10", 0x1a1a1a),
    2705             : COL("grey10", 0x1a1a1a),
    2706             : COL("gray11", 0x1c1c1c),
    2707             : COL("grey11", 0x1c1c1c),
    2708             : COL("gray12", 0x1f1f1f),
    2709             : COL("grey12", 0x1f1f1f),
    2710             : COL("gray13", 0x212121),
    2711             : COL("grey13", 0x212121),
    2712             : COL("gray14", 0x242424),
    2713             : COL("grey14", 0x242424),
    2714             : COL("gray15", 0x262626),
    2715             : COL("grey15", 0x262626),
    2716             : COL("gray16", 0x292929),
    2717             : COL("grey16", 0x292929),
    2718             : COL("gray17", 0x2b2b2b),
    2719             : COL("grey17", 0x2b2b2b),
    2720             : COL("gray18", 0x2e2e2e),
    2721             : COL("grey18", 0x2e2e2e),
    2722             : COL("gray19", 0x303030),
    2723             : COL("grey19", 0x303030),
    2724             : COL("gray20", 0x333333),
    2725             : COL("grey20", 0x333333),
    2726             : COL("gray21", 0x363636),
    2727             : COL("grey21", 0x363636),
    2728             : COL("gray22", 0x383838),
    2729             : COL("grey22", 0x383838),
    2730             : COL("gray23", 0x3b3b3b),
    2731             : COL("grey23", 0x3b3b3b),
    2732             : COL("gray24", 0x3d3d3d),
    2733             : COL("grey24", 0x3d3d3d),
    2734             : COL("gray25", 0x404040),
    2735             : COL("grey25", 0x404040),
    2736             : COL("gray26", 0x424242),
    2737             : COL("grey26", 0x424242),
    2738             : COL("gray27", 0x454545),
    2739             : COL("grey27", 0x454545),
    2740             : COL("gray28", 0x474747),
    2741             : COL("grey28", 0x474747),
    2742             : COL("gray29", 0x4a4a4a),
    2743             : COL("grey29", 0x4a4a4a),
    2744             : COL("gray30", 0x4d4d4d),
    2745             : COL("grey30", 0x4d4d4d),
    2746             : COL("gray31", 0x4f4f4f),
    2747             : COL("grey31", 0x4f4f4f),
    2748             : COL("gray32", 0x525252),
    2749             : COL("grey32", 0x525252),
    2750             : COL("gray33", 0x545454),
    2751             : COL("grey33", 0x545454),
    2752             : COL("gray34", 0x575757),
    2753             : COL("grey34", 0x575757),
    2754             : COL("gray35", 0x595959),
    2755             : COL("grey35", 0x595959),
    2756             : COL("gray36", 0x5c5c5c),
    2757             : COL("grey36", 0x5c5c5c),
    2758             : COL("gray37", 0x5e5e5e),
    2759             : COL("grey37", 0x5e5e5e),
    2760             : COL("gray38", 0x616161),
    2761             : COL("grey38", 0x616161),
    2762             : COL("gray39", 0x636363),
    2763             : COL("grey39", 0x636363),
    2764             : COL("gray40", 0x666666),
    2765             : COL("grey40", 0x666666),
    2766             : COL("gray41", 0x696969),
    2767             : COL("grey41", 0x696969),
    2768             : COL("gray42", 0x6b6b6b),
    2769             : COL("grey42", 0x6b6b6b),
    2770             : COL("gray43", 0x6e6e6e),
    2771             : COL("grey43", 0x6e6e6e),
    2772             : COL("gray44", 0x707070),
    2773             : COL("grey44", 0x707070),
    2774             : COL("gray45", 0x737373),
    2775             : COL("grey45", 0x737373),
    2776             : COL("gray46", 0x757575),
    2777             : COL("grey46", 0x757575),
    2778             : COL("gray47", 0x787878),
    2779             : COL("grey47", 0x787878),
    2780             : COL("gray48", 0x7a7a7a),
    2781             : COL("grey48", 0x7a7a7a),
    2782             : COL("gray49", 0x7d7d7d),
    2783             : COL("grey49", 0x7d7d7d),
    2784             : COL("gray50", 0x7f7f7f),
    2785             : COL("grey50", 0x7f7f7f),
    2786             : COL("gray51", 0x828282),
    2787             : COL("grey51", 0x828282),
    2788             : COL("gray52", 0x858585),
    2789             : COL("grey52", 0x858585),
    2790             : COL("gray53", 0x878787),
    2791             : COL("grey53", 0x878787),
    2792             : COL("gray54", 0x8a8a8a),
    2793             : COL("grey54", 0x8a8a8a),
    2794             : COL("gray55", 0x8c8c8c),
    2795             : COL("grey55", 0x8c8c8c),
    2796             : COL("gray56", 0x8f8f8f),
    2797             : COL("grey56", 0x8f8f8f),
    2798             : COL("gray57", 0x919191),
    2799             : COL("grey57", 0x919191),
    2800             : COL("gray58", 0x949494),
    2801             : COL("grey58", 0x949494),
    2802             : COL("gray59", 0x969696),
    2803             : COL("grey59", 0x969696),
    2804             : COL("gray60", 0x999999),
    2805             : COL("grey60", 0x999999),
    2806             : COL("gray61", 0x9c9c9c),
    2807             : COL("grey61", 0x9c9c9c),
    2808             : COL("gray62", 0x9e9e9e),
    2809             : COL("grey62", 0x9e9e9e),
    2810             : COL("gray63", 0xa1a1a1),
    2811             : COL("grey63", 0xa1a1a1),
    2812             : COL("gray64", 0xa3a3a3),
    2813             : COL("grey64", 0xa3a3a3),
    2814             : COL("gray65", 0xa6a6a6),
    2815             : COL("grey65", 0xa6a6a6),
    2816             : COL("gray66", 0xa8a8a8),
    2817             : COL("grey66", 0xa8a8a8),
    2818             : COL("gray67", 0xababab),
    2819             : COL("grey67", 0xababab),
    2820             : COL("gray68", 0xadadad),
    2821             : COL("grey68", 0xadadad),
    2822             : COL("gray69", 0xb0b0b0),
    2823             : COL("grey69", 0xb0b0b0),
    2824             : COL("gray70", 0xb3b3b3),
    2825             : COL("grey70", 0xb3b3b3),
    2826             : COL("gray71", 0xb5b5b5),
    2827             : COL("grey71", 0xb5b5b5),
    2828             : COL("gray72", 0xb8b8b8),
    2829             : COL("grey72", 0xb8b8b8),
    2830             : COL("gray73", 0xbababa),
    2831             : COL("grey73", 0xbababa),
    2832             : COL("gray74", 0xbdbdbd),
    2833             : COL("grey74", 0xbdbdbd),
    2834             : COL("gray75", 0xbfbfbf),
    2835             : COL("grey75", 0xbfbfbf),
    2836             : COL("gray76", 0xc2c2c2),
    2837             : COL("grey76", 0xc2c2c2),
    2838             : COL("gray77", 0xc4c4c4),
    2839             : COL("grey77", 0xc4c4c4),
    2840             : COL("gray78", 0xc7c7c7),
    2841             : COL("grey78", 0xc7c7c7),
    2842             : COL("gray79", 0xc9c9c9),
    2843             : COL("grey79", 0xc9c9c9),
    2844             : COL("gray80", 0xcccccc),
    2845             : COL("grey80", 0xcccccc),
    2846             : COL("gray81", 0xcfcfcf),
    2847             : COL("grey81", 0xcfcfcf),
    2848             : COL("gray82", 0xd1d1d1),
    2849             : COL("grey82", 0xd1d1d1),
    2850             : COL("gray83", 0xd4d4d4),
    2851             : COL("grey83", 0xd4d4d4),
    2852             : COL("gray84", 0xd6d6d6),
    2853             : COL("grey84", 0xd6d6d6),
    2854             : COL("gray85", 0xd9d9d9),
    2855             : COL("grey85", 0xd9d9d9),
    2856             : COL("gray86", 0xdbdbdb),
    2857             : COL("grey86", 0xdbdbdb),
    2858             : COL("gray87", 0xdedede),
    2859             : COL("grey87", 0xdedede),
    2860             : COL("gray88", 0xe0e0e0),
    2861             : COL("grey88", 0xe0e0e0),
    2862             : COL("gray89", 0xe3e3e3),
    2863             : COL("grey89", 0xe3e3e3),
    2864             : COL("gray90", 0xe5e5e5),
    2865             : COL("grey90", 0xe5e5e5),
    2866             : COL("gray91", 0xe8e8e8),
    2867             : COL("grey91", 0xe8e8e8),
    2868             : COL("gray92", 0xebebeb),
    2869             : COL("grey92", 0xebebeb),
    2870             : COL("gray93", 0xededed),
    2871             : COL("grey93", 0xededed),
    2872             : COL("gray94", 0xf0f0f0),
    2873             : COL("grey94", 0xf0f0f0),
    2874             : COL("gray95", 0xf2f2f2),
    2875             : COL("grey95", 0xf2f2f2),
    2876             : COL("gray96", 0xf5f5f5),
    2877             : COL("grey96", 0xf5f5f5),
    2878             : COL("gray97", 0xf7f7f7),
    2879             : COL("grey97", 0xf7f7f7),
    2880             : COL("gray98", 0xfafafa),
    2881             : COL("grey98", 0xfafafa),
    2882             : COL("gray99", 0xfcfcfc),
    2883             : COL("grey99", 0xfcfcfc),
    2884             : COL("gray100", 0xffffff),
    2885             : COL("grey100", 0xffffff),
    2886             : COL("darkgrey", 0xa9a9a9),
    2887             : COL("darkgray", 0xa9a9a9),
    2888             : COL("darkblue", 0x00008b),
    2889             : COL("darkcyan", 0x008b8b),
    2890             : COL("darkmagenta", 0x8b008b),
    2891             : COL("darkred", 0x8b0000),
    2892             : COL("lightgreen", 0x90ee90),
    2893             : COL(NULL,0) /* sentinel */
    2894             : };
    2895             : #undef COL
    2896             : 
    2897             : void
    2898       19142 : long_to_rgb(long c, int *r, int *g, int *b)
    2899             : {
    2900       19142 :   *b = c & 0xff; c >>= 8;
    2901       19142 :   *g = c & 0xff; c >>= 8;
    2902       19142 :   *r = c;
    2903       19142 : }
    2904             : static int
    2905           0 : hex2(const char *s)
    2906             : {
    2907           0 :   int m = 0, c = 0, i;
    2908           0 :   for (i = 0; i < 2; i++, s++)
    2909             :   {
    2910           0 :     if (*s >= '0' && *s <= '9')
    2911           0 :       c = *s - '0';
    2912           0 :     else if (*s >= 'A' && *s <= 'F')
    2913           0 :       c = *s - 'A' + 10;
    2914           0 :     else if (*s >= 'a' && *s <= 'f')
    2915           0 :       c = *s - 'a' + 10;
    2916           0 :     else pari_err(e_MISC,"incorrect hexadecimal number: %s", s);
    2917           0 :     m = 16*m + c;
    2918             :   }
    2919           0 :   return m;
    2920             : }
    2921             : void
    2922       10686 : colorname_to_rgb(const char *s, int *r, int *g, int *b)
    2923             : {
    2924       10686 :   if (!rgb_colors) rgb_colors = hashstr_import_static(col_list, 1000);
    2925       10686 :   if (*s == '#' && strlen(s) == 7)
    2926             :   {
    2927           0 :     *r = hex2(s+1);
    2928           0 :     *g = hex2(s+3);
    2929           0 :     *b = hex2(s+5);
    2930             :   }
    2931             :   else
    2932             :   {
    2933       10686 :     hashentry *ep = hash_search(rgb_colors, (void*)s);
    2934       10686 :     if (!ep) pari_err(e_MISC, "unknown color %s", s);
    2935       10686 :     long_to_rgb((long)ep->val, r,g,b);
    2936             :   }
    2937       10686 : }
    2938             : 
    2939             : static void
    2940           0 : chk_8bit(int v, GEN c)
    2941           0 : { if (v & ~0xff) pari_err(e_MISC, "invalid RGB code: %Ps", c); }
    2942             : void
    2943       10686 : color_to_rgb(GEN c, int *r, int *g, int *b)
    2944             : {
    2945       10686 :   switch(typ(c))
    2946             :   {
    2947       10686 :     case t_STR:
    2948       10686 :       colorname_to_rgb(GSTR(c), r,g,b);
    2949       10686 :       break;
    2950           0 :     default: /* t_VECSMALL: */
    2951           0 :       *r = c[1]; chk_8bit(*r, c);
    2952           0 :       *g = c[2]; chk_8bit(*g, c);
    2953           0 :       *b = c[3]; chk_8bit(*b, c);
    2954           0 :       break;
    2955             :   }
    2956       10686 : }

Generated by: LCOV version 1.13