-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathdshade.gs
513 lines (402 loc) · 11.3 KB
/
dshade.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
*
* Help is in the end of this script
*
function dshade( args )
_version = '0.01b2'
rc = gsfallow( 'on' )
if( args = '' )
help()
return
endif
sw = subwrd( args, 1 )
****************************************
* set color levels
****************************************
if( sw = 'set' )
***** arguements *****
* default
alpha = '0 255'
* user-specified arguemtnts
sw2 = subwrd( args, 2 )
colorarg = '-ret'
i = 3
while( i <= 1000 )
word = subwrd( args, i )
if( word = '' ) ; break ; endif
if( word = '-alpha' )
i = i + 1
alpha = subwrd( args, i )
i = i + 1
alpha = alpha % ' ' % subwrd( args, i )
else
colorarg = colorarg % ' ' % word
endif
i = i + 1
endwhile
* check arguements
if( sw2 != 'back' & sw2 != 'over' )
say 'error: second arguement must be "back" or "over" when the first arguement is "set".'
exit
endif
* get and save color information
colorret = color( colorarg )
if( sw2 = 'back' )
setstr( colback, colorret )
else
setstr( colover, colorret )
setstr( alphaover, alpha )
endif
return
endif
****************************************
* draw double-shaded figure
****************************************
if( sw = 'draw' )
***** arguements *****
* default
* user-specified arguemtnts
vback = subwrd( args, 2 )
'tmpvback = 'vback
vover = subwrd( args, 3 )
'tmpvover = 'vover
colback = getstr( colback )
colover = getstr( colover )
alphaover = getstr( alphaover )
key.1 = colback
key.2 = colover
f = 1
while( f <= 2 )
n = 1
while( n <= 1000 )
lev.f.n = sep( key.f, 'clevs', n )
r.f.n = sep( key.f, 'rgb', 4*n-2 )
g.f.n = sep( key.f, 'rgb', 4*n-1 )
b.f.n = sep( key.f, 'rgb', 4*n-0 )
say f % ',' % n % ' : ' % lev.f.n % ' ' % r.f.n % ' ' % g.f.n % ' ' % b.f.n
if( lev.f.n = '' ) ; break ; endif
n = n + 1
endwhile
* say f % ',' % n % ' : ' % 'NON ' % r.f.n % ' ' % g.f.n % ' ' % b.f.n
cnum.f = n
* say n
f = f + 1
endwhile
a.2.1 = subwrd( alphaover, 1 )
n = cnum.2
a.2.n = subwrd( alphaover, 2 )
c = 2
while( c <= n - 1 )
a.2.c = a.2.1 + ( a.2.n - a.2.1 ) / ( n - 1 ) * ( c - 1 )
c = c + 1
endwhile
c = 1
while( c <= cnum.1 )
cmm = c - 1
'set gxout shaded'
d = 1
ccols = ''
clevs = ''
while( d <= cnum.2 )
w = a.2.d / 255.0
r = ( 1 - w ) * r.1.c + w * r.2.d
g = ( 1 - w ) * g.1.c + w * g.2.d
b = ( 1 - w ) * b.1.c + w * b.2.d
temp = d + 15
'set rgb 'temp' 'r' 'g' 'b
ccols = ccols % ' ' % (d+15)
if( d < cnum.2 ) ; clevs = clevs % ' ' % lev.2.d ; endif
d = d + 1
endwhile
'set ccols 'ccols
'set clevs 'clevs
if( c = 1 )
'd maskout( tmpvover, 'lev.1.c'-tmpvback )'
endif
if( c = cnum.1 )
'd maskout( tmpvover, tmpvback-'lev.1.cmm' )'
endif
if( c != 1 & c != cnum.1 )
'd maskout( tmpvover, -(tmpvback-'lev.1.cmm')*(tmpvback-'lev.1.c') )'
endif
c = c + 1
endwhile
* fill boundary
* another better way exist???
c = 1
while( c <= cnum.1 - 1 )
'c = maskout( tmpvover, tmpvback-'lev.1.c' )'
'int1 = cdiff( c, x ) + cdiff( c, y ) + cdiff( cdiff( c, x ), y ) + c'
'int1 = const( int1, 1 )'
'int1 = const( int1, -1, -u )'
'c = maskout( tmpvover, 'lev.1.c'-tmpvback )'
'int2 = cdiff( c, x ) + cdiff( c, y ) + cdiff( cdiff( c, x ), y ) + c'
'int2 = const( int2, 1 )'
'int2 = const( int2, -1, -u )'
'bound1 = maskout( maskout( tmpvback, -int1 ), -int2 )'
'bound1 = const( const( bound1, 1 ), -1, -u )'
if( c = 1 )
'bound = bound1 + 1'
else
'bound = bound + ( bound1 + 1 )'
endif
c = c + 1
endwhile
*'bound = maskout( 'tmpvback', (bound1+1) + (bound2+1) - 1 )'
'bound = maskout( 'tmpvback', bound - 1 )'
'bound = const( const( bound, 1 ), -1, -u )'
'set gxout grfill'
*'d maskout( b, bound )'
'tmpvover = maskout( tmpvover, bound )'
c = 1
while( c <= cnum.1 )
cmm = c - 1
* kind = '(0,127,0)->(255,255,255)'
* kind = '('r.1.c','g.1.c','b.1.c')->('r.2.12','r.2.12','r.2.12')'
* 'color 0.1 20.1 2 -kind 'kind
'set gxout grfill'
d = 1
ccols = ''
clevs = ''
while( d <= cnum.2 )
w = a.2.d / 255.0
r = ( 1 - w ) * r.1.c + w * r.2.d
g = ( 1 - w ) * g.1.c + w * g.2.d
b = ( 1 - w ) * b.1.c + w * b.2.d
temp = d + 15
'set rgb 'temp' 'r' 'g' 'b
ccols = ccols % ' ' % (d+15)
if( d < cnum.2 ) ; clevs = clevs % ' ' % lev.2.d ; endif
d = d + 1
endwhile
'set ccols 'ccols
'set clevs 'clevs
if( c = 1 )
'd maskout( tmpvover, 'lev.1.c'-tmpvback )'
endif
if( c = cnum.1 )
'd maskout( tmpvover, tmpvback-'lev.1.cmm' )'
endif
if( c != 1 & c != cnum.1 )
'd maskout( tmpvover, -(tmpvback-'lev.1.cmm')*(tmpvback-'lev.1.c') )'
endif
c = c + 1
endwhile
*******************************************************************
exit
'!sleep 3s'
****************************************
* reverse background & over-paint to smooth boundary
c = 1
while( c <= cnum.2 )
cmm = c - 1
* kind = '(0,127,0)->(255,255,255)'
* kind = '('r.1.c','g.1.c','b.1.c')->('r.2.12','r.2.12','r.2.12')'
* 'color 0.1 20.1 2 -kind 'kind
'set gxout shaded'
d = 1
ccols = ''
clevs = ''
while( d <= cnum.1 )
w = a.2.c / 255.0
r = ( 1 - w ) * r.1.d + w * r.2.d
g = ( 1 - w ) * g.1.d + w * g.2.d
b = ( 1 - w ) * b.1.d + w * b.2.d
temp = d + 15
'set rgb 'temp' 'r' 'g' 'b
ccols = ccols % ' ' % (d+15)
if( d < cnum.1 ) ; clevs = clevs % ' ' % lev.1.d ; endif
d = d + 1
endwhile
* say ccols
* say clevs
'set ccols 'ccols
'set clevs 'clevs
if( c = 1 )
'd maskout( maskout( vback, 'lev.2.c'-vover ), bound )'
endif
if( c = cnum.2 )
'd maskout( maskout( vback, vover-'lev.2.cmm' ), bound )'
endif
if( c != 1 & c != cnum.2 )
'd maskout( maskout( vback, -(vover-'lev.2.cmm')*(vover-'lev.2.c') ), bound )'
endif
c = c + 1
endwhile
****************************************
exit
* lon=126 (x=127), lat=11 (y=102)
'temp = maskout( b, const( b, -1, -a ) )'
'set defval temp 127 102 1'
'set defval temp 128 102 2'
*'set defval temp 129 102 3'
'set defval temp 127 103 3'
'set defval temp 128 103 4'
*'set defval temp 129 103 5'
*'set defval temp 127 104 3'
*'set defval temp 128 104 4'
*'set defval temp 129 104 5'
exit
****************************************
c = 1
while( c <= cnum.1 )
cmm = c - 1
* kind = '(0,127,0)->(255,255,255)'
kind = '('r.1.c','g.1.c','b.1.c')->('r.2.12','r.2.12','r.2.12')'
'color 0.1 20.1 2 -kind 'kind
'set gxout grfill'
if( c = 1 )
'd maskout( vover, 'lev.1.c'-vback )'
endif
if( c = cnum.1 )
'd maskout( vover, vback-'lev.1.cmm' )'
endif
if( c != 1 & c != cnum.1 )
'd maskout( vover, -(vback-'lev.1.cmm')*(vback-'lev.1.c') )'
endif
c = c + 1
endwhile
exit
*min.1 = 'infty' ; max.1 = 100
*min.2 = 100 ; max.2 = '1000'
*min.3 = 1000 ; max.3 = 'infty'
c = 1
while( c <= cnum.1 )
cpp = c + 1
* kind = '(0,127,0)->(255,255,255)'
kind = '('r.1.c','g.1.c','b.1.c')->('r.2.6','r.2.6','r.2.6')'
'color 0.1 20.1 2 -kind 'kind
'set gxout grfill'
* if( min.c = 'infty' )
if( c = 1 )
'd maskout( 'vover', 'max.c'-'vback' )'
endif
* if( max.c = 'infty' )
if( c = cnum.1 )
'd maskout( 'vover', 'vback'-'min.c' )'
endif
* if( max.c != 'infty' & min.c != 'infty' )
if( c != 1 & c != cnum.1 )
'd maskout( 'vover', -('vback'-'min.c')*('vback'-'max.c') )'
endif
c = c + 1
endwhile
exit
clev = 1
while( clev <= 3 )
* kind = '(0,127,0)->(255,255,255)'
kind = '('r.1.clev','g.1.clev','b.1.clev')->('r.2.6','r.2.6','r.2.6')'
'color 0.1 20.1 2 -kind 'kind
'set gxout grfill'
if( max.clev = 'infty' )
'd maskout( 'vover', 'vback'-'min.clev' )'
endif
if( min.clev = 'infty' )
'd maskout( 'vover', 'max.clev'-'vback' )'
endif
if( max.clev != 'infty' & min.clev != 'infty' )
'd maskout( 'vover', -('vback'-'min.clev')*('vback'-'max.clev') )'
endif
clev = clev + 1
endwhile
exit
exit
* land
*'color 1 1 1 -kind (165,42,42)->(0,0,255)'
*'color 1 1 1 -kind (0,255,0)->(255,255,255)'
*'set rgb 16 0 127 0'
*'set rgb 17 255 255 255'
'color 0.1 20.1 2 -kind (0,127,0)->(255,255,255)'
*'set ccols 16 17'
*'set clevs 1'
*'d maskout( lterp(prate.2*24*3600,hgs.1(t=1)), hgs.1(t=1)-100 )'
*'d maskout( lterp(prate.2*24*3600,dummy), lterp(hgs.1(t=1)-100,dummy) )'
*'set gxout grfill'
'd maskout( b, a-100 )'
* ocean
*'color 1 1 1 -kind (255,255,255)->(0,0,255)'
*'set rgb 16 0 0 0'
*'set rgb 17 255 255 255'
*'set ccols 16 17'
*'set clevs 1'
'color 0.1 20.1 2 -kind (0,0,255)->(255,255,255)'
*'d maskout( lterp(prate.2*24*3600,hgs.1(t=1)), 100-hgs.1(t=1) )'
*'d maskout( lterp(prate.2*24*3600,dummy), lterp(200-hgs.1(t=1),dummy) )'
*'set gxout grfill'
'd maskout( b, 100-a )'
exit
*'c = maskout( 'vover', 'vback'-1000 )'
*'int1 = cdiff( c, x ) + cdiff( c, y ) + cdiff( cdiff( c, x ), y ) + c'
*'int1 = const( int1, 1 )'
*'int1 = const( int1, -1, -u )'
*'c = maskout( 'vover', 1000-'vback' )'
*'int2 = cdiff( c, x ) + cdiff( c, y ) + cdiff( cdiff( c, x ), y ) + c'
*'int2 = const( int2, 1 )'
*'int2 = const( int2, -1, -u )'
*'bound1 = maskout( maskout( 'vback', -int1 ), -int2 )'
*'bound1 = const( const( bound1, 1 ), -1, -u )'
*'c = maskout( 'vover', 'vback'-100 )'
*'int1 = cdiff( c, x ) + cdiff( c, y ) + cdiff( cdiff( c, x ), y ) + c'
*'int1 = const( int1, 1 )'
*'int1 = const( int1, -1, -u )'
*'c = maskout( 'vover', 100-'vback' )'
*'int2 = cdiff( c, x ) + cdiff( c, y ) + cdiff( cdiff( c, x ), y ) + c'
*'int2 = const( int2, 1 )'
*'int2 = const( int2, -1, -u )'
*'bound2 = maskout( maskout( 'vback', -int1 ), -int2 )'
*'bound2 = const( const( bound2, 1 ), -1, -u )'
return
endif
say 'error: dshade.gs did nothing'
return
function sep( colorret, id, num )
* say 'sep: ' % colorret
* say id % ' ' % num
i = 1
n = 1
flag = 0
while( i <= 1000 )
word = subwrd( colorret, i )
if( flag = 1 & valnum(word) != 0 )
if( n = num ) ; return word ; endif
n = n + 1
else
flag = 0
endif
if( word = id )
flag = 1
endif
i = i + 1
endwhile
return ''
*
* help
*
function help()
say ' Name:'
say ' dshade '_version' - draw transparent shadings'
say ' '
say ' Usage(1): set background color'
say ' dshade set back color-args...'
say ''
say ' Usage(2): set overlay color'
say ' dshade set over [-alpha min max] color-args...'
say ''
say ' Usage(3): draw'
say ' dshade draw var-back var-over'
say ''
say ' color-args : same as color.gs'
say ' -alpha min max : min. and max. transparency'
say ' var-back : background variable'
say ' var-over : variable to overlay'
say ''
say ' Note:'
say ' [arg-name] : specify if needed'
say ' (arg1 | arg2) : arg1 or arg2 must be specified'
say ''
say ' dshades.gs needs setstr.gsf, getstr.gsf, strmem.gsf'
say ' dshades.gs needs color.gs whose version is more than 0.06r3'
say ''
say ' Copyright (C) 2010-2010 Chihiro Kodama'
say ' Distributed under GNU GPL (http://www.gnu.org/licenses/gpl.html)'
say ''
return