3.19 image
The Pyret images library is based on the images teachpack in HtDP, and borrows much of the language for documentation. You can find documentation for the teachpack here:
3.19.1 Data Types
- name-to-color :: (
- name :: String
- )
- -> Color
Looks up the given string in the list of predefined colors.
3.19.2 Basic Images
- circle :: (
- radius :: Number,
- mode :: Mode,
- color :: ImageColor
- )
- -> Image
Constructs a circle with the given radius, mode and color.
Constructs an ellipse with the given width, height, mode and color.
Draws an image of a line that connects the point (0,0) to the point (x,y).
Creates a new image like img with a line added starting from the point (x1,y1) and going to the point (x2,y2). Unlike scene-line, if the line passes outside of img, the image gets larger to accommodate the line.
3.19.3 Text
- text :: (
- string :: String,
- font-size :: Number,
- color :: ImageColor
- )
- -> Image
Constructs an image of string, using the given font size and color.
font-face is system-dependent because different computers and operating systems have different fonts installed. You can try different options for the names of fonts on your machine, and text-font will fall back to a default in the given family if it can’t find the one provided.
- text-font :: (
- string :: String,
- size :: Number,
- color :: ImageColor,
- font-face :: String,
- font-family :: FontFamily,
- style :: FontStyle,
- weight :: FontWeight,
- underline :: Boolean
- )
- -> Image
Like text, constructs an image that draws the given string, but makes use of a complete font specification. The various style options are described below.
"default"
"decorative"
"roman"
"script"
"swiss"
"modern"
"symbol"
"system"
3.19.4 Polygons
- triangle :: (
- side-length :: Number,
- mode :: Mode,
- color :: ImageColor
- )
- -> Image
Constructs an image of an upward-pointing equilateral triangle. Each side will be of length side-length.
- right-triangle :: (
- side-length1 :: Number,
- side-length2 :: Number,
- mode :: Mode,
- color :: ImageColor
- )
- -> Image
Constructs an image of a triangle with a right angle at the bottom-left corner and where the two sides adjacent to the right angle have lengths side-length1 and side-length2.
- isosceles-triangle :: (
- side-length :: Number,
- angle-c :: Number,
- mode :: Mode,
- color :: ImageColor
- )
- -> Image
Constructs an image of a triangle with two equal-length sides, of length side-length where the angle between those two sides is angle-c. if the angle is less than 180, then the triangle will point up; otherwise, the triangle will point down.
- triangle-sss :: (
- side-a :: Number,
- side-b :: Number,
- side-c :: Number,
- mode :: Mode,
- color :: ImageColor
- )
- -> Image
Constructs an image of a triangle using the three given sides.
- triangle-ass :: (
- angle-a :: Number,
- side-b :: Number,
- side-c :: Number,
- mode :: Mode,
- color :: ImageColor
- )
- -> Image
Constructs an image of a triangle using the given angle and two sides.
Constructs an image of a triangle using the given angle and two sides.
Constructs an image of a triangle using the given angle and two sides.
Constructs an image of a triangle using the two given angles and side.
Constructs an image of a triangle using the two given angles and side.
Constructs an image of a triangle using the two given angles and sides.
Constructs an image of a square with the given side length, mode and color.
Constructs an image of a rectangle with the given side width, height, mode and color.
Constructs a four-sided polygon whose sides are of length side-length and thus has angles equal to their opposites. The top and bottom pair of angles is angle and the left and right pair is 180 - angle.
Constructs a five-pointed star with sides of length side-length, and with the given mode and color.
Constructs a star with point-count points. The outer points will lie a distance of outer from the center of the star, while the inner points will lie a distance of inner from the center.
Same as radial-star.
Constructs an image of an arbitrary regular star polygon. The polygon is enclosed by a regular polygon with side-count sides each side-length long. The polygon is actually constructed by going from vertex to vertex around the regular polygon, but connecting every step-count-th vertex (i.e., skipping every step-count - 1 vertices).
Constructs an image of a regular polygon with side-count sides.
3.19.5 Overlaying Images
Overlays img1 on img2 like overlay, but uses place-x and place-y to determine where the images should line up.
Overlays img1 on img2 like overlay, but initially lines up the two images upper-left corners and then shifts img2 to the right by dx pixels, and then down by dy pixels.
Underlays img1 beneath img2 like underlay, but uses place-x and place-y to determine where the images should line up.
Underlays img1 beneath img2 like underlay, but initially lines up the two images upper-left corners and then shifts img2 to the right by x pixels, and then down by y pixels.
Constructs an image by placing img1 to the left of img2.
- beside-align :: (
- place-y :: YPlace,
- img1 :: Image,
- img2 :: Image
- )
- -> Image
Constructs an image by placing img1 to the left of img2, and aligning the two images as indicated by place-y.
- above-align :: (
- place-x :: XPlace,
- img1 :: Image,
- img2 :: Image
- )
- -> Image
Constructs an image by placing img1 above img2, and aligning the two images as indicated by place-x.
3.19.6 Placing Images & Scenes
- empty-scene :: (
- width :: Number,
- height :: Number
- )
- -> Scene
Construct an empty scene of given width and height.
Places the image img on the scene background so that its center is located at the coordinates (x,y), cropping the resulting image as necessary to maintain the size of background. The coordinates are relative to the bottom-left of background.
Places the image img on the scene background so that its center is located at the coordinates (x,y), cropping the resulting image as necessary to maintain the size of background. The coordinates are relative to the top-left of background.
Functions like place-image, but uses place-x and place-y to determine where to anchor img, instead of using the center.
Draws a line from (x1,y1) to (x2,y2) on the scene background. Unlike add-line, this function crops the resulting image to be the same size as background.
3.19.7 Rotating, Scaling, Flipping, Cropping and Framing Images
Scales by x-factor horizontally and by y-factor vertically.
- flip-horizontal :: (
- img :: Image
- )
- -> Image
Flips img left to right.
- flip-vertical :: (
- img :: Image
- )
- -> Image
Flips img top to bottom.
Crops img to the rectangle with the upper left at the point (x,y) and with width width and height height.
Construct an image similar to img, but with a black, single pixel frame draw around the bounding box of the image.
3.19.8 Bitmaps
- open-image-url :: (
- url :: String
- )
- -> Image
Loads the image specified by url.
- bitmap-url :: (
- url :: String
- )
- -> Image
Loads the image specified by url.
- image-to-color-list :: (
- image :: Image
- )
- -> List<Color>
Returns a list of colors that correspond to the colors in the image, reading from left to right, top to bottom.
Given a list of colors, creates an image with the given width width and height height.
Given a list of colors, creates an image with the given width width and height height.
3.19.9 Image Properties
- image-width :: (
- img :: Image
- )
- -> Number
Returns the width of img.
- image-height :: (
- img :: Image
- )
- -> Number
Returns the height of img.
- image-baseline :: (
- img :: Image
- )
- -> Number
Returns the distance from the top of img to its baseline. The baseline of an image is the place where the bottoms of letters line up, without counting the descender, such as the tails on "y", "g" or "j".
3.19.10 Image Predicates
- is-image-color :: (
- maybe-color :: Any
- )
- -> Boolean
Checks if maybe-color can be used as a color. Strings, if names of colors (e.g. "red" or "green") can also be used, if they exist in the color database.
- is-y-place :: (
- maybe-y-place :: Any
- )
- -> Boolean
Checks if maybe-y-place can be used as y-place in appropriate functions. Valid strings are top, bottom, middle, center, baseline and pinhole.
- is-x-place :: (
- maybe-x-place :: String
- )
- -> Boolean
Checks if maybe-x-place can be used as x-place in appropriate functions. Valid strings are left, right, middle, center and pinhole.
Checks if maybe-angle is an angle, namely a real number. All angles in the library are in degrees.
- is-side-count :: (
- side-count :: Any
- )
- -> Boolean
Checks if maybe-side-count is an integer greater than or equal to 3.
- is-step-count :: (
- step-count :: Number
- )
- -> Boolean
Checks if maybe-step-count is an integer greater than or equal to 1.
3.19.11 Image Equality
- images-equal :: (
- image1 :: Image,
- image2 :: Image
- )
- -> Boolean
Compares two images for equality.
Compares two images for approximate equality. Returns left if they aren’t the same size (and are this incomparable). Returns right otherwise, with a number representing how far off they are.
Numbers range from 0-255, where around 255 indicates completely different images, and numbers below 20 or so are very difficult to distinguish at a glance. Useful for testing against reference images (especially cross-browser).