forked from kodamail/gscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xcbar.gs
641 lines (571 loc) · 16.2 KB
/
xcbar.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
*
* Help is in the end of this script.
*
function xcbar( args )
_version = '0.09r1'
rc = gsfallow( 'on' )
if( args = '' )
help()
* return
endif
***** Default value *****
xmin = 'none'
xmax = 'none'
ymin = 'none'
ymax = 'none'
direction = 'none'
line = 'on'
caption = ''
cnum = -1
fxoffset = 0
fyoffset = 0
xoffset = 0
yoffset = 0
fformat = ''
***** Arguement *****
i = 1
arg = 'dummy'
while( 1 )
arg = subwrd( args, i )
i = i + 1
if( arg = '' ); break; endif
while( 1 )
if( arg = '-caption' | arg = '-c' )
caption = subwrd( args, i )
i = i + 1
while ( 1 )
tmp = subwrd( args, i )
if( tmp = '' ) ; break ; endif
caption = caption % ' ' % tmp
i = i + 1
endwhile
break
endif
if( arg = '-direction' | arg = '-dir' | arg = '-d' )
direction = subwrd( args, i )
if( direction = 'h' ); direction = 'horizontal'; endif
if( direction = 'v' ); direction = 'vertical'; endif
i = i + 1
break
endif
if( arg = '-edge' )
edge = subwrd( args, i )
i = i + 1
break
endif
if( arg = '-fwidth' | arg = '-fw' )
fwidth = subwrd( args, i )
i = i + 1
break
endif
if( arg = '-fheight' | arg = '-fh' )
fheight = subwrd( args, i )
i = i + 1
break
endif
if( arg = '-fthickness' | arg = '-ft' )
fthickness = subwrd( args, i )
i = i + 1
break
endif
if( arg = '-fskip' | arg = '-fstep' | arg = '-fs' )
fstep = subwrd( args, i )
i = i + 1
break
endif
if( arg = '-foffset' | arg = '-fo' )
foffset = subwrd( args, i )
i = i + 1
break
endif
if( arg = '-fcolor' | arg = '-fc' )
fcolor = subwrd( args, i )
i = i + 1
break
endif
if( arg = '-fxoffset' | arg = '-fx' )
fxoffset = subwrd( args, i )
i = i + 1
break
endif
if( arg = '-fyoffset' | arg = '-fy' )
fyoffset = subwrd( args, i )
i = i + 1
break
endif
if( arg = '-fformat' | arg = '-ffmt' )
fformat = subwrd( args, i )
i = i + 1
break
endif
if( arg = '-line' )
tmp = subwrd( args, i )
if( tmp = 'on' | tmp = 'off' )
line = tmp
i = i + 1
else
line = 'on'
endif
break
endif
if( arg = '-linecolor' | arg = '-lc' )
linecolor = subwrd( args, i )
i = i + 1
break
endif
if( arg = '-levcol' )
* color(1) level(1) color(2) level(2) ... level(cnum-1) color(cnum)
cnum = 1
while( 1 )
col.cnum = subwrd( args, i )
if( valnum(col.cnum) = 0 )
say 'Error in xcbar.gs: Syntax error in -levcol'
return
endif
i = i + 1
hi.cnum = subwrd( args, i )
if( valnum(hi.cnum) = 0 ); break; endif
cnum = cnum + 1
i = i + 1
endwhile
break
endif
if( arg = '-xoffset' | arg = '-xo' )
xoffset = subwrd( args, i )
i = i + 1
break
endif
if( arg = '-yoffset' | arg = '-yo' )
yoffset = subwrd( args, i )
i = i + 1
break
endif
if( valnum(arg) != 0 & xmin = 'none' )
xmin = arg
break
endif
if( valnum(arg) != 0 & xmax = 'none' )
xmax = arg
break
endif
if( valnum(arg) != 0 & ymin = 'none' )
ymin = arg
break
endif
if( valnum(arg) != 0 & ymax = 'none' )
ymax = arg
break
endif
say 'syntax error: 'arg
return
endwhile
endwhile
*** get shade information ***
if( cnum = -1 )
'q shades'
shdinfo = result
if ( subwrd( shdinfo, 1 ) = 'None' )
say 'Error in xcbar.gs: No shading information'
return
endif
* number of colors
cnum = subwrd( shdinfo, 5 )
if( cnum <= 0 )
say 'Error in xcbar.gs: Number of color is zero'
return
endif
* color and (higher) levels
i = 1
while( i <= cnum )
rec = sublin( shdinfo, i+1 )
col.i = subwrd( rec, 1 )
hi.i = subwrd( rec, 3 )
i = i + 1
endwhile
endif
*** determine xmin, xmax, ymin, ymax if necessary ***
** ( following cbar.gs )
if( xmin = 'none' | xmax = 'none' | ymin = 'none' | ymax = 'none' )
* 'q gxinfo'
* sline = sublin( result, 2 )
* xsize = subwrd( sline, 4 )
* ysize = subwrd( sline, 6 )
* xline = sublin( result, 3 )
* yline = sublin( result, 4 )
* xlmin = subwrd( xline, 4 )
* xlmax = subwrd( xline, 6 )
* xlwid = xlmax - xlmin
* ylmin = subwrd( yline, 4 )
* ylmax = subwrd( yline, 6 )
* ylwid = ylmax - ylmin
*
** vertical
* if( ylmin < 0.6 | xsize-xlmax > 1.5 )
* direction = 'vertical'
* xmin = xlmax + ( xsize - xlmax ) / 2 - 0.4
* xmax = xmin + 0.2
*
* y1wid = 0.5
* if ( y1wid * cnum > ysize * 0.8 )
* y1wid = ysize * 0.8 / cnum
* endif
* ymin = ysize / 2 - y1wid * cnum / 2
* ymax = ysize / 2 + y1wid * cnum / 2
*
** horizontal
* else
* direction = 'horizontal'
* ymin = ylmin / 2
* ymax = ymin + 0.2
*
* x1wid = 0.8
* if ( x1wid * cnum > xsize * 0.8 )
* x1wid = xsize * 0.8 / cnum
* endif
* xmin = xsize / 2 - x1wid * cnum / 2
* xmax = xsize / 2 + x1wid * cnum / 2
*
* endif
'q gxinfo'
sline = sublin( result, 2 )
xsize = subwrd( sline, 4 )
ysize = subwrd( sline, 6 )
xline = sublin( result, 3 )
yline = sublin( result, 4 )
xlmin = subwrd( xline, 4 )
xlmax = subwrd( xline, 6 )
xlwid = xlmax - xlmin
ylmin = subwrd( yline, 4 )
ylmax = subwrd( yline, 6 )
ylwid = ylmax - ylmin
* vertical
if( ( ylmin < 0.6 | xsize-xlmax > 1.5 ) & direction != 'horizontal' )
direction = 'vertical'
dx = xlwid / 30
xmin = xlmax + 0.2
xmax = xmin + dx
ymin = ylmin
ymax = ylmax
* horizontal
else
direction = 'horizontal'
dy = ylwid / 20
ymax = ylmin - 0.3
ymin = ymax - dy
xmin = xlmin
xmax = xlmax
endif
endif
xmin = xmin + xoffset
xmax = xmax + xoffset
xmid = ( xmax + xmin ) / 2
ymin = ymin + yoffset
ymax = ymax + yoffset
ymid = ( ymax + ymin ) / 2
*** determine direction if necessary ***
if( direction != 'horizontal' & direction != 'vertical' )
if( xmax - xmin >= ymax - ymin )
direction = 'horizontal'
else
direction = 'vertical'
endif
endif
if( direction = 'horizontal' )
xdir = 1
ydir = 0
else
xdir = 0
ydir = 1
endif
*** other default value *****
if( edge != 'box' & edge != 'triangle' & edge != 'circle' )
edge = 'box'
endif
if( valnum(fwidth) = 0 )
fwidth = 0.12
endif
if( valnum(fheight) = 0 )
fheight = 0.13
endif
if( valnum(fthickness) = 0 )
fthickness = fheight * 40
endif
if( valnum(fstep) = 0 )
fstep = 1
endif
if( foffset = 'center' )
if( math_fmod(cnum,2) = 0 )
foffset = math_fmod( cnum / 2 - 1, fstep )
else
foffset = math_fmod( (cnum+1) / 2 - 1, fstep )
endif
* say foffset
endif
if( valnum(foffset) = 0 )
foffset = 0
* foffset = fstep - 1
endif
if( valnum(fcolor) = 0 )
fcolor = 1
endif
if( line != 'on' & line != 'off' )
line = 'off'
endif
if( valnum(linecolor) = 0 )
linecolor = 1
endif
*** get other constant ***
* width of color bar
xdif = xdir * (xmax-xmin) / cnum
ydif = ydir * (ymax-ymin) / cnum
*** draw ***
i = 1
x1 = xmin - xdif
x2 = xmin * xdir + xmax * ydir
y1 = ymin - ydif
y2 = ymin * ydir + ymax * xdir
maxstr = 0
while( i <= cnum )
x1 = x1 + xdif
x2 = x2 + xdif
y1 = y1 + ydif
y2 = y2 + ydif
xmoji = x2 + ( 0.5 * fwidth ) * ydir + fxoffset
ymoji = ( y1 - 0.5 * fheight ) * xdir + y2 * ydir + fyoffset
'set line 'col.i
'set strsiz 'fwidth' 'fheight
*** draw color bar ***
if( edge = 'box' )
'draw recf 'x1' 'y1' 'x2' 'y2
if( line = 'on' ) ; drawrec( linecolor, x1, y1, x2, y2 ) ; endif
endif
if( edge = 'triangle' )
if( i != 1 & i != cnum )
'draw recf 'x1' 'y1' 'x2' 'y2
if( line = 'on' ) ; drawrec( linecolor, x1, y1, x2, y2 ) ; endif
endif
if( direction = 'horizontal' )
if( i = 1 )
ymed = 0.5 * ( y1 + y2 )
poly = x1' 'ymed' 'x2' 'y1' 'x2' 'y2
'draw polyf 'poly
if( line = 'on' ) ; drawpoly( linecolor' 'poly ) ; endif
endif
if( i = cnum )
ymed = 0.5 * ( y1 + y2 )
poly = x1' 'y1' 'x1' 'y2' 'x2' 'ymed
'draw polyf 'poly
if( line = 'on' ) ; drawpoly( linecolor' 'poly ) ; endif
endif
endif
if( direction = 'vertical' )
if( i = 1 )
xmed = 0.5 * ( x1 + x2 )
poly = xmed' 'y1' 'x1' 'y2' 'x2' 'y2
'draw polyf 'poly
if( line = 'on' ) ; drawpoly( linecolor' 'poly ) ; endif
endif
if( i = cnum )
xmed = 0.5 * ( x1 + x2 )
poly = x1' 'y1' 'x2' 'y1' 'xmed' 'y2
'draw polyf 'poly
if( line = 'on' ) ; drawpoly( linecolor' 'poly ) ; endif
endif
endif
endif
* end of triangle
if( edge = 'circle' )
if( i != 1 & i != cnum )
'draw recf 'x1' 'y1' 'x2' 'y2
if( line = 'on' ) ; drawrec( linecolor, x1, y1, x2, y2 ) ; endif
endif
if( direction = 'horizontal' )
if( i = 1 )
xc = x1 + ( y2 - y1 )
yc = y1 + 0.5 * ( y2 - y1 )
radius = 0.5 * ( y2 - y1 )
if( xc < x2 )
'draw recf 'xc' 'y1' 'x2' 'y2
else
xc = x2
endif
circle = circle( xc, yc, radius, 90, 270, 6 )
'draw polyf 'circle
if( line = 'on' ) ; drawpoly( linecolor' 'x2' 'y2' 'circle' 'x2' 'y1 ) ; endif
endif
if( i = cnum )
xc = x2 - ( y2 - y1 )
yc = y1 + 0.5 * ( y2 - y1 )
radius = 0.5 * ( y2 - y1 )
if( x1 < xc )
'draw recf 'x1' 'y1' 'xc' 'y2
else
xc = x1
endif
circle = circle( xc, yc, radius, 270, 450, 6 )
'draw polyf 'circle
if( line = 'on' ) ; drawpoly( linecolor' 'x1' 'y1' 'circle' 'x1' 'y2) ; endif
endif
endif
if( direction = 'vertical' )
if( i = 1 )
xc = x1 + 0.5 * ( x2 - x1 )
yc = y1 + 0.5 * ( x2 - x1 )
radius = 0.5 * ( x2 - x1 )
if( yc < y2 )
'draw recf 'x1' 'yc' 'x2' 'y2
else
yc = y2
endif
circle = circle( xc, yc, radius, 180, 360, 6 )
'draw polyf 'circle
if( line = 'on' ) ; drawpoly( linecolor' 'x1' 'y2' 'circle' 'x2' 'y2 ) ; endif
endif
if( i = cnum )
xc = x1 + 0.5 * ( x2 - x1 )
yc = y2 - 0.5 * ( x2 - x1 )
radius = 0.5 * ( x2 - x1 )
if( y1 < yc )
'draw recf 'x1' 'y1' 'x2' 'yc
else
yc = y1
endif
circle = circle( xc, yc, radius, 0, 180, 6 )
'draw polyf 'circle
if( line = 'on' ) ; drawpoly( linecolor' 'x2' 'y1' 'circle' 'x1' 'y1 ) ; endif
endif
endif
endif
* end of circle
* draw labels
if( i != cnum & i-foffset > 0 & math_int((i-1-foffset)/fstep)*fstep = i-1-foffset )
if( direction = 'horizontal' )
'set string 'fcolor' tc 'fthickness' 0'
else
'set string 'fcolor' l 'fthickness' 0'
endif
str = hi.i
if( fformat != '' )
str = math_format( fformat, hi.i )
endif
'draw string 'xmoji' 'ymoji' 'str
endif
if( math_strlen(str) > maxstr )
maxstr = math_strlen(str)
endif
i = i + 1
endwhile
* caption = 'Temperature [K]'
if( caption != '' )
'set strsiz 'fwidth*1.2' 'fheight*1.2
if( direction = 'horizontal' )
'set string 'fcolor' tc 'fthickness' 0'
* 'draw string 'xmid' 'ymin-fheight-0.2' 'str2
'draw string 'xmid' 'ymin-fheight*2' 'caption
else
'set string 'fcolor' tc 'fthickness' 90'
'draw string 'xmax+fwidth*(2+maxstr)' 'ymid' 'caption
endif
endif
return
*
* angle = 0 : x(+) direction
* angle = 90 : y(+) direction
*
function circle( xc, yc, radius, amin, amax, astep )
circle = ''
angle = amin
while( angle <= amax )
x = xc + radius * math_cos( angle * 3.14 / 180.0 )
y = yc + radius * math_sin( angle * 3.14 / 180.0 )
circle = circle % x % ' ' % y % ' '
angle = angle + astep
endwhile
return ( circle )
function drawrec( linecolor, xmin, ymin, xmax, ymax )
drawpoly( linecolor' 'xmin' 'ymin' 'xmin' 'ymax' 'xmax' 'ymax' 'xmax' 'ymin )
return
function drawpoly( args )
linecolor = subwrd( args, 1 )
xstart = subwrd( args, 2 )
ystart = subwrd( args, 3 )
xmin = xstart
ymin = ystart
i = 4
while( 1 = 1 )
xmax = subwrd( args, i )
ymax = subwrd( args, i+1 )
if( xmax = '' | ymax = '' ) ; break ; endif
'set cthick 1'
'set line 'linecolor
'draw line 'xmin' 'ymin' 'xmax' 'ymax
xmin = xmax
ymin = ymax
i = i + 2
endwhile
'set cthick 1'
'set line 'linecolor
'draw line 'xmin' 'ymin' 'xstart' 'ystart
return
*
* help
*
function help()
say ' Name:'
say ' xcbar '_version' - Draw color bar at any position and size.'
say ' '
say ' Usage:'
say ' xcbar [ xmin xmax ymin ymax ]'
say ' [ ( -xoffset | -xo ) xoffset ] [ ( -yoffset | -yo ) yoffset ]'
say ' [ ( -fwidth | -fw ) fwidth ]'
say ' [ ( -fheight | -fh ) hfeight ]'
say ' [ ( -fthickness | -ft ) fthickness ]'
say ' [ ( -fskip | -fstep | -fs ) fskip ]'
say ' [ ( -foffset | -fo ) ( foffset | center ) ]'
say ' [ ( -fcolor | -fc ) fcolor ]'
say ' [ ( -fxoffset | -fx ) fxoffset ] [ ( -fyoffset | -fy ) fyoffset ]'
say ' [ ( -fformat | -ffmt ) fformat ]'
say ' [ ( -direction | -dir ) ( horizontal | h | vertical | v ) ]'
say ' [ -edge ( box | triangle | circle ) ]'
say ' [ -line [ on | off ] ]'
say ' [ -levcol c(1) l(1) c(2) level(2) ... l(cnum-1) c(cnum) ]'
say ' [ ( -caption | -c ) caption... ]'
say ''
say ' xmin : color bar position (left side)'
say ' xmax : color bar position (right side)'
say ' ymin : color bar position (bottom side)'
say ' ymax : color bar position (top side)'
say ' without xmin, xmax, ymin or ymax, '
say ' position will be determined automatically'
say ' following cbar.gs manner.'
say ' xoffset : x-position offset (default=0)'
say ' yoffset : y-position offset (default=0)'
say ' fwidth : font width (default=0.12)'
say ' fheight : font height (default=0.13)'
say ' fthickness : font thickness (default=fheight*40)'
say ' fskip : label interval (default=1)'
say ' foffset : label offset for fstep (default=0)'
say ' to put labels at the center, specify "center".'
say ' fcolor : label color number (default=1)'
say ' fxoffset : x-direction offset for a label (default=0)'
say ' fyoffset : y-direction offset for a label (default=0)'
say ' fformat : font format (e.g. %5.2e, %.2f'
say ' -direction : horizontal ("h" in short) or vertical ("v" in short)'
say ' color bar (default=auto)'
say ' -edge : shape of edge (default=box)'
say ' -line : lines between each color box. (default="on")'
say ' -levcol : color of lines between each color box (default=1)'
say ' c(1) l(1) c(2) level(2) ... l(cnum-1) c(cnum)'
say ' : color numbers and levels. By using this option,'
say ' you can draw color bar without drawing figure'
say ''
say ' Note:'
say ' [arg-name] : specify if needed'
say ' (arg1 | arg2) : arg1 or arg2 must be specified'
say ''
say ' xcbar is based on cbar.gs'
say ''
say ' Copyright (C) 2011-2021 Chihiro Kodama'
say ' Distributed under GNU GPL (http://www.gnu.org/licenses/gpl.html)'
say ''
return