后记:画一个渐变

Pie*_*rre 3 gradient postscript ghostscript

我正在学习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
Run Code Online (Sandbox Code Playgroud)

但是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
Run Code Online (Sandbox Code Playgroud)

我哪里错了?

lus*_*oog 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
Run Code Online (Sandbox Code Playgroud)

png输出