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

Generated by: LCOV version 1.13