首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Postscript:绘制渐变

Postscript:绘制渐变
EN

Stack Overflow用户
提问于 2011-09-21 13:00:19
回答 3查看 2.2K关注 0票数 3

我正在学习Postscript,我正在尝试创建一个方法,用于绘制垂直梯度

这是我的代码:

代码语言:javascript
复制
%!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引发了一个错误:

代码语言:javascript
复制
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

我哪里错了?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-09-25 19:50:56

好的,我发现了问题:索引似乎是一个保留字。以下是一个功能版本:

代码语言:javascript
复制
/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
票数 4
EN

Stack Overflow用户

发布于 2011-10-26 08:31:53

通过使用巧妙的堆栈操作和CTM效果,您的程序将执行得更快。

这不是像您这样的平滑梯度,但是执行速度要快得多,函数被计算为一行语句(我更喜欢它,无法解释为什么)。

此外,张贴与用法和可能的样本页很好(当你不需要它时很容易修剪,但当你确实需要它.?!)不管怎么说,这是你的程序改写了我的方式,FWIW。

就像我说过的,它的输出在目前的形式上不那么漂亮。但是,您可以通过使用currenttransfersettransfer更改灰度传递函数来修改梯度,并/或将盒的计算更改为逻辑缩放,改变灰度的范围和速度。在“更紧密”的代码中,应该更容易看到这些东西。堆栈注释帮助您在每一行的末尾“检查您的理解”。

编辑:,我无法停止玩它!我已经把循环分解出来,并找出了更多的参数。

编辑:再做一次扩展。再来张漂亮的照片怎么样?

代码语言:javascript
复制
%!

/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

票数 6
EN

Stack Overflow用户

发布于 2011-09-21 21:49:27

我没有完全理解你的代码。但是,错误消息试图告诉您,您是除以零(查看其余操作数堆栈的顶部元素:"0")。

只需在h变量中添加"1“(在h之后插入1 add ),就可以使PostScript程序通过Ghostscript解释器运行,并让它绘制一些东西(尽管这看起来可能不像您设想的那样.)。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7500185

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档