Background color for diagram image...

8 views
Skip to first unread message

Stuart Hungerford

unread,
Apr 25, 2012, 5:50:19 PM4/25/12
to diagrams...@googlegroups.com
Hi,

I'm creating a Diagram centered with 'centerXY' and padded around the
edges with 'pad'. This is rendered using the Cairo command line
version of 'main' so that the final width and height are chosen at
run-time.

What's the recommended way to set a background color behind the whole
diagram (for PNG rendering) when the final render size is not known
when the diagram is prepared?

Thanks,

Stu

Brent Yorgey

unread,
Apr 25, 2012, 7:14:30 PM4/25/12
to diagrams...@googlegroups.com
Whether the final render size is known is not really relevant. The
basic idea is to create a rectangle of the appropriate size and color
and placing it behind the diagram. The trick is how to compute the
appropriate size, but it can be done using the diagram's envelope.
There is actually already a function to do this in the darcs version
of diagrams-lib (Diagrams.TwoD.Combinators.bg) but not in the released
version of diagrams-lib. Here's the code:


-- | Construct a bounding rectangle for an enveloped object, that is,
-- the smallest axis-aligned rectangle which encloses the object.
boundingRect :: ( Enveloped p, Transformable p, PathLike p, V p ~ R2
, Enveloped a, V a ~ R2
)
=> a -> p
boundingRect = (`boxFit` rect 1 1) . boundingBox

-- | \"Set the background color\" of a diagram. That is, place a
-- diagram atop a bounding rectangle of the given color.
bg :: (Renderable (Path R2) b) => Colour Double -> Diagram b R2 -> Diagram b R2
bg c d = d <> boundingRect d # lw 0 # fc c


-Brent
Reply all
Reply to author
Forward
0 new messages