Hi guys,
I’ve been working with 2htdp/image and been struck by its monitor-oriented
coordinate system and its image-centric perspective, both of which have proven
a rich bed for new tools. I’ve found these two functions have been useful to me
and might be useful for others. I’ve particularly found them useful as my
racket sessions use the “white on black” color schemes, and with my eyesight a
pixel’s contrast makes a difference.
The center-crop function facilitates the image-cntric perspective of the
library by centering the cropping rectangle on the image center. The frame
function replaces the library’s existing function with one that does the same
thing, but also provides parameters for setting the pixel frame coloring,
background coloring within the frame, and x/y offsets that work with
center-crop to expand or shrink the framing of the image.
;; center-crop: width height image -> image?
;; crops image in a rectangle of width x height whose center is image center.
(define (center-crop width height img)
(crop (- (quotient (image-width img) 2) (quotient width 2))
(- (quotient (image-height img) 2) (quotient height 2))
width
height
img))
;; frame: image frame-option ... -> image?
;; Returns an image just like image, except with a frame-color'd, single pixel
frame
;; around the bounding box of the image. The background-color indicates the
color
;; inside the frame over which the image is laid. If an offset is provided it
;; indicates a center-crop in that dimension. A positive value extends the crop
;; beyond the image bounding box, a negative value center-crops the image within
;; the bounding box.
(define (frame img
#:frame-color (frame-color 'black)
#:background-color (background-color 'transparent)
#:frame-offset (frame-offset 0)
#:frame-x-offset (frame-x-offset frame-offset)
#:frame-y-offset (frame-y-offset frame-offset))
(let ([width (+ (image-width img) frame-x-offset)]
[height (+ (image-height img) frame-y-offset)])
(overlay (rectangle width height 'outline frame-color)
(center-crop width height img)
(rectangle width height 'solid background-color))))
-Kevin
_________________________
Racket Developers list:
http://lists.racket-lang.org/dev