Description Usage Arguments Note Author(s) Examples
View source: R/image-from-function.R
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.
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
)
|
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 |
A four-element vector that specifies the rectangular area used
when evaluating |
width, height |
The number of columns and rows in the output image, so that
the total number of pixels is |
num.portions |
When this parameter is greater than 1, the output image
will be divided into |
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.
Mathias Isaksen mathiasleanderi@gmail.com
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)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.