Teachpacks for How to Design Programs

# Images

### image.ss

This teachpack provides primitives for constructing and manipulating images. These functions create basic shapes. The mode can be either `'solid` or `"solid"`, meaning the shape is filled in, or `'outline` or `"outline"`, meaning the shape is hollow. Image colors can be either symbols (like `'blue`), strings (like `"blue"`), or color structs (like `(make-color 0 0 255)`) -- see below for more information about color structs.
- `rectangle` : int int mode image-color -> image
  to create a rectangle using the given width, height, mode, and color
- `circle` : int mode image-color -> image
  to create a circle using the given radius, mode, and color
- `ellipse` : int int mode image-color -> image
  to create an ellipse using the given width, height, and color
- `triangle` : int mode iamge-color -> image
  to create an upward pointing equilateral triangle using the given edge size and color
- `line` : number number image-color -> image
  to create an image with a colored line from (0,0) to the point with the given coordinates
- `add-line` : image number number number number image-color -> image
  to add a line to an existing image, drawn between the two given points
- `text` : string size image-color -> image
  to create an image of the text in the given string, with the point size, and color specified by the last two arguments
These functions build complex images from the basic shapes. When two images are laid on top of each other, the are lined up at their pinhole. Most shapes have their pinholes in the middle. The exceptions are `text` and `line` which have their pinholes in the top-left corner.
- `overlay` : image image image ... -> image
  to add the pixels of the second image onto the first image, lining up the pinholes
- `overlay/xy` : image int int image -> image
  to add the pixels of the second image onto the first image. Instead of lining up on the pinhole, the second image's pinhole is lined up with an offset from the first image's pinhole. The two coordinates specify how far down and to the right the offset should be. The pinhole of the resulting image is the same place as the pinhole in the first image.
After an image has been overlaid on another, it is possible to recover the position of overlaid image, using the next two functions.
- `image-inside?` : image image -> boolean
  to determine whether the pixels of the second image appear in the first

  Be careful when using this function with jpeg images. If you use an image-editing program to crop a jpeg image and then save it, `image-inside?` will not recognize the cropped image, due to jpeg's compression.

  Use png images instead.

- `find-image` : image image -> posn
  to determine where the pixels of the second image appear in the first, with respect to the pinhole of the first image.
The shrink functions trim an image by eliminating extraneous pixels.
- `shrink-tl` : image number number -> image
  to shrink the image, starting from the top-left corner. The two numbers indicate how many pixels to save. The pinhole of the resulting image is in the middle of the image.
- `shrink-tr` : image number number -> image
  to shrink the image, starting from the top-right corner. The two numbers indicate how many pixels to save. The pinhole of the resulting image is in the middle of the image.
- `shrink-bl` : image number number -> image
  to shrink the image, starting from the bottom-left corner. The two numbers indicate how many pixels to save. The pinhole of the resulting image is in the middle of the image.
- `shrink-br` : image number number -> image
  to shrink the image, starting from the bottom-right corner. The two numbers indicate how many pixels to save. The pinhole of the resulting image is in the middle of the image.
- `shrink` : image number number number number -> image
  to shrink an image around its pinhole. The numbers are the pixels to save to left, above, to the right, and below the pinhole, respectively. The pixel directly on the pinhole is always saved.
These functions provide information about the image's size.
- `image-width` : image -> number
  to obtain an image's width in pixels
- `image-height` : image -> number
  to obtain an image's height in pixels
This functions provide information and manipulate an image's pinhole.
- `pinhole-x` : image -> number
  to determine the x coordinate of the pinhole, measuring from the left of the image
- `pinhole-y` : image -> number
  to determine the y coordinate of the pinhole, measuring down from the left of the image
- `move-pinhole` : image number number -> image
  to move the pinhole down and to the right (by the specified amounts) of its current location. Use negative numbers to move it up or to the left.
- `put-pinhole` : image number number -> image
  to put the pinhole in the location specified by the arguments, counting from the left and down from the top, respectively.
This function precisely specifies what a valid image color is.
- `image-color?` : anything -> boolean
  to determine if the input is a valid image color
The next functions separate an image into its consitiuent colors and combine pixels together to build an image.
- `image->color-list` : image -> list-of-color
  to convert an image to a list of colors
- `color-list->image` : list-of-color int int int int -> image
  to convert a list of colors to an image with the given width and height, and pinhole coordinates (the pinhole coordinates are with respect to the top-left of the image).
- `make-color` : int int int -> color
  to construct a color
- `color?` : anything -> boolean
  to determine if its input is a color
- `color-red` : color -> int
  to extract the red component of a color
- `color-green` : color -> int
  to extract the green component of a color
- `color-blue` : color -> int
  to extract the blue component of a color
Like the last group of functions, these functions separate an image into its consitiuent colors and combine pixels together to build an image, but these provide alpha-channel information as well. Alpha channels are a measure of transparency; 0 indicates fully opaque and 255 indicates fully transparent.
- `image->alpha-color-list` : image -> list-of-alpha-color
  to convert an image to a list of alpha colors
- `alpha-color-list->image` : list-of-alpha-color int int int int -> image
  to convert a list of alpha colors to an image with the given width and height, and pinhole coordinates (the pinhole coordinates are with respect to the top-left of the image).
- `make-alpha-color` : int int int int -> color
  to construct an alpha color
- `alpha-color?` : anything -> boolean
  to determine if its input is a color
- `alpha-color-alpha` : color -> int
  to extract the alpha value of a color
- `alpha-color-red` : color -> int
  to extract the red component of a color
- `alpha-color-green` : color -> int
  to extract the green component of a color
- `alpha-color-blue` : color -> int
  to extract the blue component of a color"