我正在学习 Postscript 我正在尝试创建一种方法来绘制垂直的渐变。
这是我的代码:
%!PS-Adobe-3.0
%%%%%%%%%%%%%%%%%%%%%%%
% draw a RECTANGLE
/Rect {
/h exch def % height
/w exch def % width
w 0 rlineto
0 h rlineto
-1.0 w mul 0 rlineto
0 -1.0 h mul rlineto
} def
%%%%%%%%%%%%%%%%%%%%%%%
% draw a Gradient
/VGrad {
/h exch def % height
/w exch def % width
/c2 exch def %gray-end
/c1 exch def %gray-start
/index 0.0 def %loop-index
0 1 h { %loop over height
gsave
c2 c1 sub index h div mul c1 add setgray
w h index sub Rect
stroke
/index index 1.0 add def % index++
grestore
} for
} def
%%%%%%%%%%%%%%%%%%%%%%%
%test script
200 600 moveto
.1 .9 100 10 VGrad
showpage
但是GS引发了一个错误:
GPL Ghostscript 8.70 (2009-07-31)
Copyright (C) 2009 Artifex Software, Inc. All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
Error: /undefinedresult in --div--
Operand stack:
0 1 2 3 4 5 0.8 5.0 0.0
Execution stack:
%interp_exit .runexec2 --nostringval-- --nostringval-- --nostringval-- 2 %stopped_push --nostringval-- --nostringval-- --nostringval-- false 1 %stopped_push 1862 1 3 %oparray_pop 1861 1 3 %oparray_pop 1845 1 3 %oparray_pop 1739 1 3 %oparray_pop --nostringval-- %errorexec_pop .runexec2 --nostringval-- --nostringval-- --nostringval-- 2 %stopped_push --nostringval-- 6 1 10 --nostringval-- %for_pos_int_continue --nostringval--
Dictionary stack:
--dict:1150/1684(ro)(G)-- --dict:0/20(G)-- --dict:75/200(L)--
Current allocation mode is local
Current file position is 588
GPL Ghostscript GPL Ghostscript 8.708.70: : Unrecoverable error, exit code 1
Unrecoverable error, exit code 1
我哪里错了?
答案 0 :(得分:6)
使用巧妙的堆栈操作和CTM效果,您的程序将更快地执行。
这不像你的平滑渐变,但执行得更快,而且函数计算为单行语句(我只是喜欢更好,无法解释原因)。
另外很好的发布用法和可能的样本页面(当你不需要它时很容易修剪,但是当你做需要它的时候...... ??)无论如何,这是你的程序重写我的方式,FWIW。
正如我所说的那样,它的输出效果还不如现在的形式。但您可以通过使用currenttransfer
和settransfer
更改灰度传递函数来修改渐变和/或将框的计算更改为对数缩放,更改灰度的范围和速度。这些东西应该更容易在“更严格”的代码中看到。堆栈注释可帮助您在每行末尾“检查您的理解”。
编辑:我无法停止玩它!我已经考虑了循环,并取出了一些更多的参数。
编辑:再扩展一次。那漂亮的画面怎么样?
%!
/box { % x y w h
4 2 roll moveto % w h
1 index 0 rlineto % w h
0 exch rlineto % w
neg 0 rlineto %
closepath
} def
/poly { % n
0.5 0 moveto
{ ? rotate 0.5 0 lineto } % n proc
dup 0 360 4 index div put % n {360/n...}
repeat
closepath
} def
% num-slices shapeproc matrix grayproc agrad -
% repeatedly (fill shape, concat matrix, transform currentgray)
/agrad {
3 dict begin /gray exch def /mat exch def /shape exch def
({ //shape exec //mat concat currentgray //gray exec setgray })
token pop exch pop end bind repeat
} def
/shapes [
{ -0.5 -0.5 1 1 box fill } %box shape
{ 0 0 0.5 0 360 arc fill } %circle shape
{ 0 0 0.5 0 180 arc fill } %fan shape
{ 5 poly fill } %pentagon
{ 6 poly fill } %hexagon
] def
/mats [
{1 index 2 exch div 1 exch sub dup matrix scale } %pyramid matrix
{1 index 2 exch div 1 exch sub 1 matrix scale } %horizontal matrix
{1 index 2 exch div 1 exch sub 1 exch matrix scale } %vertical matrix
] def
% mat-no shape-no gray0 grayF n x y w h dograd -
/dograd {
gsave
4 2 roll translate % m sh g0 gF n w h
scale % m sh g0 gF n
3 1 roll % m sh n g0 gF
1 index sub 2 index div % m sh n g0 (gF-g0)/n
[ exch /add cvx ] cvx % m sh n g0 grayproc
3 1 roll setgray % m sh grayproc n
3 -1 roll shapes exch get % m gray n shape
4 -1 roll mats exch get exec % gray n shape mat
4 -1 roll %n shape matrix gray
agrad
grestore
} def
%mat shape g0 gF n x y w h
0 4 .7 .1 20 300 400 600 800 dograd
0 0 0 1 10 100 650 200 200 dograd
1 1 0 1 20 300 650 200 200 dograd
2 2 .5 1 30 500 650 200 200 dograd
0 3 1 0 40 100 400 200 200 dograd
1 4 1 .5 50 300 400 200 200 dograd
2 1 .5 0 60 500 400 200 200 dograd
0 2 .1 .9 10 100 150 200 200 dograd
1 3 .2 .8 20 300 150 200 200 dograd
2 4 .3 .7 30 500 150 200 200 dograd
showpage
答案 1 :(得分:3)
好的,我发现了问题:似乎索引是一个保留字。这是一个功能版本:
/box
{
4 dict begin
/height exch def
/width exch def
/y exch def
/x exch def
x y moveto
width 0 rlineto
0 height rlineto
width -1 mul 0 rlineto
0 height -1 mul rlineto
end
} bind def
/gradient
{
4 dict begin
/height exch def
/width exch def
/y exch def
/x exch def
/i 0 def
height 2 div /i exch def
0 1 height 2 div {
1 i height 2.0 div div sub setgray
newpath
x
y height 2 div i sub add
width
i 2 mul
box
closepath
fill
i 1 sub /i exch def
}for
newpath
0 setgray
0.4 setlinewidth
x y width height box
closepath
stroke
end
} bind def
答案 2 :(得分:2)
我没有尝试完全理解您的代码。但错误消息试图告诉您正在除以零(查看剩余操作数堆栈的顶部元素:“0”)。
只需在h
变量中添加“1”(在1 add
之后插入h
),使您的PostScript程序通过Ghostscript解释器运行并让它绘制一些东西(尽管可能不会看起来像你设想....)。