Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

merging colors..

28 views
Skip to first unread message

Robert Baer

unread,
Mar 31, 2013, 11:19:10 PM3/31/13
to
This is my first "fancy" program, three color-shaded circles.
It would be nice to have the colors appropriately combine/add in the
overlap regions.
Is there a nice way to do that?

%! % three.ps
/inch {72 mul} def % 72 pixels to the inch
/N 500 def % steps
/red 1 def
/green {1 3 div} def
/blue {2 3 div} def
/size {dup inch exch inch scale} def
/circle
{
newpath
exch % order: hue sat on top
0 0 moveto % (circlestack) stack pop % ? where does it go?
1 sethsbcolor % hue sat 1
0 0 1 -270 90 arc % includes a straight line to the initial
point of the arc
% x y r ang1 ang2 % from, to angles
closepath % and a curved section to the end of the arc.
pop
} def

gsave
267 400 translate % middle, landscape
1 1 N % Set up the for operator to iterate
{
gsave % Enclose the ‘‘circle’’ operation in a
gsave - grestore
% radius must shrink as sat grows for proper shading..
dup N exch sub N div dup 3 mul size
1 exch sub
red circle
gsave fill grestore % Save the circle path for use after the
fill.
grestore % Get out of the coordinate system left
by circle.
} for % Close the procedure body and execute
the for operator.
grestore

gsave
400 200 translate % middle, landscape
1 1 N % Set up the for operator to iterate
{
gsave % Enclose the ‘‘circle’’ operation in a
gsave - grestore
% radius must shrink as sat grows for proper shading..
dup N exch sub N div dup 3 mul size
1 exch sub
green circle
gsave fill grestore % Save the circle path for use after the
fill.
grestore % Get out of the coordinate system left
by circle.
} for % Close the procedure body and execute
the for operator.
grestore

gsave
533 400 translate % middle, landscape
1 1 N % Set up the for operator to iterate
{
gsave % Enclose the ‘‘circle’’ operation in a
gsave - grestore
% radius must shrink as sat grows for proper shading..
dup N exch sub N div dup 3 mul size
1 exch sub
blue circle
gsave fill grestore % Save the circle path for use after the
fill.
grestore % Get out of the coordinate system left
by circle.
} for % Close the procedure body and execute
the for operator.
grestore
gsave
/Helvetica findfont
24 scalefont setfont
320 580 moveto
( Linear gradient) show
grestore
showpage

luser- -droog

unread,
Apr 1, 2013, 2:18:55 AM4/1/13
to
On Sunday, March 31, 2013 9:18:49 PM UTC-5, Robert Baer wrote:
> This is my first "fancy" program, three color-shaded circles.
>
> It would be nice to have the colors appropriately combine/add in the
>
> overlap regions.
>
> Is there a nice way to do that?
>


You'll have to define the separate regions separately.
Postscript doesn't have any sort of blending or rasterops.
But remember there's `clip` and `eoclip` which are very powerful.

Btw, I wrote some code for gradients you might find useful.
http://stackoverflow.com/a/7900346/733077

luser- -droog

unread,
Apr 1, 2013, 2:24:47 AM4/1/13
to
On Monday, April 1, 2013 1:18:55 AM UTC-5, luser- -droog wrote:
> On Sunday, March 31, 2013 9:18:49 PM UTC-5, Robert Baer wrote:
>
> > This is my first "fancy" program, three color-shaded circles.
> > It would be nice to have the colors appropriately combine/add in the
> > overlap regions.
> > Is there a nice way to do that?
>
>
> You'll have to define the separate regions separately.
> Postscript doesn't have any sort of blending or rasterops.
> But remember there's `clip` and `eoclip` which are very powerful.
>

Thought it through just after posting...

Yes! Construct the path for one circle, clip.
Construct the path for the second circle, clip.
All further drawing will be bounded by the intersection of these paths.

Even simpler, just clip to the *other* circles. The one you're actually drawing is already "within its own bounds", right?


gernot....@hs-emden-leer.de

unread,
Apr 1, 2013, 5:04:38 AM4/1/13
to
Some examples:

http://docs-hoffmann.de/ciegraph17052004.pdf

%!PS-Adobe-3.0 EPSF-3.0
%%BoundingBox: 0 0 567 567
%%Creator: Gernot Hoffmann
%%Title: CMY-Circles
%%CreationDate: June 07 / 2006

/mm {2.834646 mul} def

/sc 80 mm def
/x0 100 mm def
/y0 100 mm def

/BBox
{ 0 0 567 567 rectstroke
} def

% BBox

/E 0.5 def
/R 0.7 def

/ac 180 def
/am 300 def
/ay 60 def

/xc ac cos E mul def
/yc ac sin E mul def
/xm am cos E mul def
/ym am sin E mul def

/xy ay cos E mul def
/yy ay sin E mul def

x0 y0 translate
sc sc scale

gsave
1 0 0 0 setcmykcolor
xc yc R 0 360 arc fill

0 1 0 0 setcmykcolor
xm ym R 0 360 arc fill

0 0 1 0 setcmykcolor
xy yy R 0 360 arc fill

grestore
gsave
newpath xc yc R 0 360 arc clip
newpath xm ym R 0 360 arc clip

1 1 0 0 setcmykcolor
xc yc R 0 360 arc fill

grestore
gsave
newpath xm ym R 0 360 arc clip
newpath xy yy R 0 360 arc clip

0 1 1 0 setcmykcolor
xm ym R 0 360 arc fill

grestore
gsave
newpath xy yy R 0 360 arc clip
newpath xc yc R 0 360 arc clip

1 0 1 0 setcmykcolor
xy yy R 0 360 arc fill

grestore
gsave
newpath xc yc R 0 360 arc clip
newpath xm ym R 0 360 arc clip
newpath xy yy R 0 360 arc clip

1 1 1 0 setcmykcolor
xc yc R 0 360 arc fill

showpage

---------------------

%!PS-Adobe-3.0 EPSF-3.0
%%BoundingBox: 0 0 567 567
%%Creator: Gernot Hoffmann
%%Title: RGB-Circles
%%CreationDate: June 07 / 2006

/mm {2.834646 mul} def

/sc 80 mm def
/x0 100 mm def
/y0 100 mm def /BBox
{ 0 0 567 567 rectstroke
} def

% BBox

/E 0.5 def
/R 0.7 def

/ar 0 def
/ag 120 def
/ab 240 def

/xr ar cos E mul def
/yr ar sin E mul def
/xg ag cos E mul def
/yg ag sin E mul def

/xb ab cos E mul def
/yb ab sin E mul def

x0 y0 translate
sc sc scale

gsave
1 0 0 setrgbcolor
xr yr R 0 360 arc fill

0 1 0 setrgbcolor
xg yg R 0 360 arc fill

0 0 1 setrgbcolor
xb yb R 0 360 arc fill

grestore
gsave
newpath xr yr R 0 360 arc clip
newpath xg yg R 0 360 arc clip

1 1 0 setrgbcolor
xr yr R 0 360 arc fill

grestore
gsave
newpath xg yg R 0 360 arc clip
newpath xb yb R 0 360 arc clip

0 1 1 setrgbcolor
xg yg R 0 360 arc fill

grestore
gsave
newpath xb yb R 0 360 arc clip
newpath xr yr R 0 360 arc clip

1 0 1 setrgbcolor
xb yb R 0 360 arc fill

grestore
gsave
newpath xr yr R 0 360 arc clip
newpath xg yg R 0 360 arc clip
newpath xb yb R 0 360 arc clip

1 1 1 setrgbcolor
xr yr R 0 360 arc fill

showpage

John Deubert

unread,
Apr 1, 2013, 10:09:25 AM4/1/13
to
The simplest algorithm for the "three-circle-overlap" I know goes
something like this:

Given three overlapping circles A, B, and C:
Do A; fill red
Do B; gsave; clip; fill green
Do A; Fill yellow
grestore
Do C; clip; Fill blue
Do A; fill magenta
Do B; clip; fill cyan
Do A; fill white

This is for the classic red-green-blue circles (yielding overlaps of c,
m, y, and white); I sometimes present this as an extra-credit problem
in the PostScript Foundations class.

Here's a PostScript implementation:

% ========= cut here ========
/CircleA
{ 300 500 100 0 360 arc } bind def

/CircleB
{ 375 425 100 0 360 arc } bind def

/CircleC
{ 225 425 100 0 360 arc } bind def

CircleA
1 0 0 setrgbcolor fill

gsave
CircleB
clip
0 1 0 setrgbcolor
fill

CircleA
1 1 0 setrgbcolor
fill
grestore

CircleC
clip
0 0 1 setrgbcolor
fill

CircleA
1 0 1 setrgbcolor
fill

CircleB
clip
0 1 1 setrgbcolor
fill

CircleA
1 setgray
fill

showpage
% ============ cut here ============

You can pretty easily adapt this for your quite nice-looking radial gradients.

- John

P.S. If you like gradients, you might want to look into PostScript's
smooth shading. The Acumen Journal has a brief introduction to the
topic way back in issue #1, November 2000. The Journal is free for the
downloading at www.acumentraining.com/acumenjournal.html.

- John



--
========
John Deubert
Acumen Training
PostScript & PDF Engineering Classes & Consulting
www.acumentraining.com

Learn PostScript programming techniques
Read the free Acumen Journal
acumentraining.com/acumenjournal.html

John Deubert

unread,
Apr 1, 2013, 12:27:09 PM4/1/13
to
The Smooth Shading version of this was calling to me; I had to do it. Had to!

% =========== Snip =========

/FcnDict <<
/FunctionType 2
/Domain [ 0 1 ]
/Range [ 0 1 0 1 0 1 ]
/C0 [ 1 0 0 ]
/C1 [ 1 1 1 ]
/N 1.5
>> def

/DoShfill % << shade dict >> [ r g b ] => ---
{
FcnDict /C0 3 -1 roll put
shfill
} bind def

/CircleA
{ 300 500 100 0 360 arc } bind def

/CircleAShadingDict <<
/ShadingType 3
/ColorSpace [ /DeviceRGB ]
/Coords [ 300 500 0 300 500 100 ]
/Function FcnDict
>> def

/CircleB
{ 375 425 100 0 360 arc } bind def

/CircleBShadingDict <<
/ShadingType 3
/ColorSpace [ /DeviceRGB ]
/Coords [ 375 425 0 375 425 100 ]
/Function FcnDict
>> def

/CircleC
{ 225 425 100 0 360 arc } bind def

/CircleCShadingDict <<
/ShadingType 3
/ColorSpace [ /DeviceRGB ]
/Coords [ 225 425 0 225 425 100 ]
/Function FcnDict
>> def

CircleAShadingDict [ 1 0 0 ] DoShfill

gsave
CircleB
clip newpath
CircleBShadingDict [ 0 1 0 ] DoShfill

CircleAShadingDict [ 1 1 0 ] DoShfill
grestore

CircleC
clip newpath
CircleCShadingDict [ 0 0 1 ] DoShfill

CircleAShadingDict [ 1 0 1 ] DoShfill

CircleB
clip newpath
CircleBShadingDict [ 0 1 1 ] DoShfill

CircleAShadingDict [ 1 1 1 ] DoShfill

showpage

% =========== Snip =========

This can probably be streamlined some.

- J

Robert Baer

unread,
Apr 2, 2013, 12:48:14 AM4/2/13
to
Verrrry nice!
Now have a lot of reading to do.
Thanks!

Don Lancaster

unread,
Apr 2, 2013, 8:03:07 PM4/2/13
to
>> gsave % Enclose the ��circle�� operation in a
>>
>> gsave - grestore
>>
>> % radius must shrink as sat grows for proper shading..
>>
>> dup N exch sub N div dup 3 mul size
>>
>> 1 exch sub
>>
>> red circle
>>
>> gsave fill grestore % Save the circle path for use after the
>>
>> fill.
>>
>> grestore % Get out of the coordinate system left
>>
>> by circle.
>>
>> } for % Close the procedure body and execute
>>
>> the for operator.
>>
>> grestore
>>
>>
>>
>> gsave
>>
>> 400 200 translate % middle, landscape
>>
>> 1 1 N % Set up the for operator to iterate
>>
>> {
>>
>> gsave % Enclose the ��circle�� operation in a
>>
>> gsave - grestore
>>
>> % radius must shrink as sat grows for proper shading..
>>
>> dup N exch sub N div dup 3 mul size
>>
>> 1 exch sub
>>
>> green circle
>>
>> gsave fill grestore % Save the circle path for use after the
>>
>> fill.
>>
>> grestore % Get out of the coordinate system left
>>
>> by circle.
>>
>> } for % Close the procedure body and execute
>>
>> the for operator.
>>
>> grestore
>>
>>
>>
>> gsave
>>
>> 533 400 translate % middle, landscape
>>
>> 1 1 N % Set up the for operator to iterate
>>
>> {
>>
>> gsave % Enclose the ��circle�� operation in a
< http://www.tinaja.com/glib/pstrans2.pdf > , of course.

Otherwise, you have to work with the HSB color space and make sure you
do not go the wrong way around the circle.

As in < http://www.tinaja.com/glib/postproc.pdf >

--
Many thanks,

Don Lancaster voice phone: (928)428-4073
Synergetics 3860 West First Street Box 809 Thatcher, AZ 85552
rss: http://www.tinaja.com/whtnu.xml email: d...@tinaja.com

Please visit my GURU's LAIR web site at http://www.tinaja.com

luser- -droog

unread,
Apr 3, 2013, 12:06:11 AM4/3/13
to
On Monday, April 1, 2013 10:47:58 PM UTC-5, Robert Baer wrote:
> luser- -droog wrote:
>
>
> > Btw, I wrote some code for gradients you might find useful.
>
> > http://stackoverflow.com/a/7900346/733077
>
> Verrrry nice!
>
> Now have a lot of reading to do.
>
> Thanks!

I'm assuming this is a "blanket" thank you to Gernot and John for contributing very nice code rather than links. :) And you're welcome, I'm sure.

We'd love to see the result when it's finished (or help more if you need it).
0 new messages