image_from_function: Create image based on color function

Description Usage Arguments Note Author(s) Examples

View source: R/image-from-function.R

Description

This function takes in a function that maps every location over some rectangular area to a color, and saves a PNG image based on the output of the function.

Usage

1
2
3
4
5
6
7
8
image_from_function(
  filename,
  color.function,
  bounds = c(-1, 1, -1, 1),
  width,
  height,
  num.portions = 1
)

Arguments

filename

The filename of the output PNG file. It does not need to end in ".png".

color.function

A function that takes in an n x 2 data frame of locations inside the rectangle specified in bounds, and returns one color per location. The data frame has columns named x and y. The function can return the colors in one of the following formats:

  • A vector of n hex codes, e.g. "#FF0000" or "#123456AB" (where "AB" in the latter is alpha / transparency)

  • A vector of n greyscale values between 0 and 1, where 0 is black and 1 is white

  • An n x 2 matrix of values between 0 and 1, where the first column is the grayscale value and the second is alpha

  • An n x 3 matrix of values between 0 and 1, where the columns correspond to R, G and B

  • An n x 4 matrix of values between 0 and 1, where the columns correspond to R, G, B and alpha

bounds

A four-element vector that specifies the rectangular area used when evaluating color.function. The first two elements are the lower and upper bounds for the x-coordinate, while the last two are the lower and upper bounds for the y-coordinate.

width, height

The number of columns and rows in the output image, so that the total number of pixels is width*height.

num.portions

When this parameter is greater than 1, the output image will be divided into num.portions vertical slices of approximately the same height. This is useful for reducing memory use when width and height are large. The slices can be merged into the original image with image editing software.

Note

The aspect ratio of the output image (width / height) should be the same as the aspect ratio of bounds. If not, a warning is issued.

Author(s)

Mathias Isaksen mathiasleanderi@gmail.com

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
## Not run: 
# Example with hex values
color.function.hex = function(locations) {
  colors = c("#df361f", "#21747a", "#edd324")
  return(sample(colors, size = nrow(locations), replace = TRUE))
}

image_from_function("hex.png", color.function.hex,
                    width = 1000, height = 1000)

# Example with grayscale
color.function.gray = function(locations) {
  gray = minmax_scaling(locations$x^2 + locations$y^2)
  return(gray)
}

image_from_function("grayscale.png", color.function.gray,
                    width = 1000, height = 1000)


# Example with RGB matrix
color.function.rgb = function(locations) {
  red = minmax_scaling(locations$x^2)
  green = minmax_scaling(sin(pi*locations$y), 0, 0.5)
  blue = minmax_scaling(locations$x*locations$y)
  return(cbind(red, green, blue))
}

image_from_function("rgb.png", color.function.rgb, bounds = c(-2, 2, -1, 1),
                    width = 2000, height = 1000)

# Example with RGBA matrix
color.function.rgba = function(locations) {
  red = minmax_scaling(sin(pi*locations$x))
  green = minmax_scaling(sin(2*pi*locations$x + 0.25))
  blue = minmax_scaling(sin(4*pi*locations$x + 0.5))
  alpha = minmax_scaling(sin(6*pi*locations$x + 0.75))
  return(cbind(red, green, blue, alpha))
}

image_from_function("rgba.png", color.function.rgba,
                    width = 1000, height = 1000)

## End(Not run)

mathiasisaksen/artKIT documentation built on Dec. 21, 2021, 2:52 p.m.