diff --git a/R/brickr.R b/R/brickr.R index 6156057..61d3e14 100644 --- a/R/brickr.R +++ b/R/brickr.R @@ -14,7 +14,8 @@ if(getRversion() >= "2.15.1") { "brick_area", "brick_width", "brick_height", "channel", "col2rgb", "color", "Color", "color_hex", "dist", "elevation", "G", "G_lego", - "ggproto", "height", "hex", "layer", + "ggproto", "height", "hex", + "id", "layer", "lego", "Lego_color", "LEGO_color", "lego_colors", "Lego_name", "Level", "lum", "Lum", "median", "mid_level", "n", diff --git a/R/bricks-from-mosaic.R b/R/bricks-from-mosaic.R index 8d39669..35cc336 100644 --- a/R/bricks-from-mosaic.R +++ b/R/bricks-from-mosaic.R @@ -1,7 +1,6 @@ #' Convert a 2D LEGO mosaic into a 'brickr' 3D object #' #' Stacks LEGO plates to create a 3D version of the 2D brick mosaics. -#' #' Height of bricks determined by brightness of color. #' #' @param mosaic_list List output from image_to_bricks(). Contains an element \code{Img_lego}. @@ -10,18 +9,37 @@ #' @return A list with elements \code{Img_lego} to pass to \code{\link{build_bricks}}. #' @family 3D Models #' @export -#' @examples \donttest{ -#' # Create a random 24x24 'image'. -#' # Otherwise, use a jpeg or png -#' demo_image = array(scales::rescale(runif(24*24*3), c(0, 1)), dim=c(24, 24, 3)) -#' -#' demo_image %>% -#' image_to_mosaic(img_size = 24) %>% -#' bricks_from_mosaic() %>% -#' build_bricks() +#' @examples +#' # Import a jpeg or png +#' demo_file <- system.file("extdata", "demo_img.jpg", +#' package = "brickr", mustWork = TRUE) +#' demo_image <- jpeg::readJPEG(demo_file) #' -#' rgl::clear3d() -#'} +#' #Begin with a 24x24 mosaic object +#' \donttest{ +#' mosaic <- demo_image %>% +#' image_to_mosaic(24) +#' } +#' +#' #Pass the mosaic object to bricks_from_mosaic() to convert to 3D specifications +#' \donttest{ +#' mosaic %>% +#' bricks_from_mosaic() %>% +#' build_bricks() +#' +#' rgl::clear3d() +#' } +#' +#' #In this image, the background is a light color. +#' # Change the 'highest_el' to make dark colors highest +#' # Change mosaic height to change the number of layers +#' \donttest{ +#' mosaic %>% +#' bricks_from_mosaic(mosaic_height = 3, highest_el = "dark") %>% +#' build_bricks() +#' +#' rgl::clear3d() +#' } bricks_from_mosaic <- function(mosaic_list, mosaic_height = 6, highest_el = "light"){ #Get previous data diff --git a/R/bricks-from-tables.R b/R/bricks-from-tables.R index 86736c7..e7e761a 100644 --- a/R/bricks-from-tables.R +++ b/R/bricks-from-tables.R @@ -53,11 +53,14 @@ #' X1 = c("b", "b", "p", "p"), #b is brick (default), p is plate #' X2 = c("b", "b", "p", "p") #') +#' +#'\donttest{ #'brick %>% #' bricks_from_table(piece_matrix=pieces) %>% #' build_bricks() #' #' rgl::clear3d() +#' } #' #' #Provide a custom table of colors #' custom_colors <- data.frame( @@ -65,18 +68,22 @@ #' Color = c("Bright orange", "Dark green") #' ) #' +#' \donttest{ #'brick %>% #' bricks_from_table(color_guide = custom_colors) %>% #' build_bricks() #' #' rgl::clear3d() +#' } #' #'#Limit the size of bricks used in the model with use_bricks +#'\donttest{ #'brick %>% #' bricks_from_table(use_bricks = "2x1") %>% #Only use 2x1 bricks. #' build_bricks() #' #' rgl::clear3d() +#' } bricks_from_table <- function(matrix_table, color_guide = brickr::lego_colors, piece_matrix = NULL, @@ -402,27 +409,31 @@ bricks_from_excel <- function(excel_table, #' #' #This is a lot of bricks #' bricks <- expand.grid( -#' x = 1:8, -#' y = 1:4, +#' x = 1:4, +#' y = 1:2, #' z = 1:3) #' #' #Color them in sets of these 3 options -#' bricks$color <- rep(rep(c("Bright yellow", "Bright red", "Tr. green"), each=4), 8) +#' bricks$color <- rep(rep(c("Bright yellow", "Bright red", "Tr. green"), each=4), 2) #' +#' \donttest{ #' bricks %>% #' bricks_from_coords() %>% #' build_bricks() #' #' rgl::clear3d() +#' } #' #'#Use different brick shapes by added a 'piece_type' column #'bricks$piece_type <- "c1" #Make all the pieces cylinders #' +#'\donttest{ #'bricks %>% #' bricks_from_coords() %>% #' build_bricks() #' #' rgl::clear3d() +#' } #' bricks_from_coords <- function(coord_table, use_bricks = NULL, diff --git a/R/build-bricks-rgl.R b/R/build-bricks-rgl.R index 5ba906b..e7fd014 100644 --- a/R/build-bricks-rgl.R +++ b/R/build-bricks-rgl.R @@ -1,6 +1,6 @@ #' Build 3D brick model with 'rgl' #' -#' Render the output of any of the \code{bricks_from_*} functions as a 3D model. +#' Render the output of any of the \code{bricks_from_*} functions as a 3D model. Opens an 'rgl' window. #' #' @param brick_list List output from a \code{bricks_from_*} function. Contains an element \code{Img_lego}. #' @param background_color Default 'white'. Color of the background. @@ -9,7 +9,7 @@ #' Set to 'TRUE' and rgl_lit='FALSE' for cartoon-looking bricks. #' @param trans_alpha Default 0.5. Alpha level for transparent bricks. #' @param view_levels Numeric array of Levels/z values to display. Leave as 'NULL' to include all. -#' @examples \donttest{ +#' @examples #' #This is a brick #'brick <- data.frame( #' Level="A", @@ -17,12 +17,23 @@ #' X2 = rep(3,4) #') #' -#'brick %>% -#' bricks_from_table() %>% -#' build_bricks() +#'#Convert the dataframe to a list object that can be rendered +#'brick_object <- brick %>% +#' bricks_from_table() #' +#'#Render it +#'brick_object %>% +#' build_bricks() +#' +#' rgl::clear3d() +#' +#'#Combine the option rgl_lit=FALSE & outline_bricks=TRUE +#'# This makes the rendering look like a drawing +#' brick_object %>% +#' build_bricks(outline_bricks = TRUE, rgl_lit = FALSE, +#' background_color = "#99e7ff") #'rgl::clear3d() -#' } +#' #' @return 3D brick model rendered in the 'rgl' package. #' @family 3D Models #' @export diff --git a/R/build-instructions.R b/R/build-instructions.R index 5d56c5f..81a38e8 100644 --- a/R/build-instructions.R +++ b/R/build-instructions.R @@ -13,15 +13,29 @@ #' @return A single plot object of steps to build brickr model or mosaic. #' @family Resources #' @export -#' @examples \donttest{ -#' # Create a random 24x24 'image'. -#' # Otherwise, use a jpeg or png -#' demo_image = array(scales::rescale(runif(24*24*3), c(0, 1)), dim=c(24, 24, 3)) -#' -#' demo_image %>% -#' image_to_mosaic(img_size = 24) %>% -#' build_instructions() -#'} +#' @examples +#' # Import a jpeg or png +#' demo_file <- system.file("extdata", "demo_img.jpg", +#' package = "brickr", mustWork = TRUE) +#' demo_image <- jpeg::readJPEG(demo_file) +#' +#' #Create a mosaic oject +#' \donttest{ +#' mosaic <- demo_image %>% +#' image_to_mosaic(img_size = 24) +#' } +#' +#' #Rather than drawing the mosaic, use build_instructions() to draw instructions +#' \donttest{ +#' mosaic %>% +#' build_instructions() +#' } +#' +#' #Change the number of steps for more detail +#' \donttest{ +#' mosaic %>% +#' build_instructions(num_steps = 9) +#' } #' build_instructions <- function(brickr_obj, num_steps=6) { diff --git a/R/build-mosaic.R b/R/build-mosaic.R index 852a487..d6d9344 100644 --- a/R/build-mosaic.R +++ b/R/build-mosaic.R @@ -10,11 +10,23 @@ #' @examples #' #' # Import a jpeg or png -#' demo_image = system.file("extdata", "demo_img.jpg", package = "brickr", mustWork = TRUE) -#' -#' jpeg::readJPEG(demo_image) %>% -#' image_to_mosaic() %>% +#' demo_file <- system.file("extdata", "demo_img.jpg", +#' package = "brickr", mustWork = TRUE) +#' demo_image <- jpeg::readJPEG(demo_file) +#' +#' #Build a very small 12x12 mosaic. +#' \donttest{ +#' demo_image %>% +#' image_to_mosaic(12) %>% #' build_mosaic() +#' } +#' +#' #Build a mosaic in the default size of 48x48 studs with title +#' \donttest{ +#' demo_image %>% +#' image_to_mosaic() %>% +#' build_mosaic("Demo mosaic") +#' } build_mosaic <- function(brick_obj, title=NULL){ in_list <- brick_obj diff --git a/R/collect-bricks.R b/R/collect-bricks.R index fa721bf..96d7ee4 100644 --- a/R/collect-bricks.R +++ b/R/collect-bricks.R @@ -1,6 +1,9 @@ -#' Consolidate 1x1 bricks into larger ones of the same color. Internal function. +#' Consolidate 1x1 bricks into larger ones of the same color +#' +#' Internal function for collecting single bricks into large ones. Used in both mosaics and 3D models. +#' Automatically generate missing piece_type or mid_level. #' -#' @param image_list List output from legoize(). Contains an element \code{Img_lego}. +#' @param image_list List output containing an element \code{Img_lego}. #' @param use_bricks Array of brick sizes to use in mosaic. Defaults to \code{c('4x2', '2x2', '3x1', '2x1', '1x1')}`. #' @param default_piece_type Piece type to use in absence of piece_type column. #' @return A list with element \code{Img_bricks} containing a data frame of the x- & y-coordinates, R, G, B channels, and brick ID. Other helper elements. diff --git a/R/image-to-mosaic.R b/R/image-to-mosaic.R index fb35d88..a27bb4e 100644 --- a/R/image-to-mosaic.R +++ b/R/image-to-mosaic.R @@ -5,9 +5,8 @@ #' @param img Image matrix to convert into mosaic. Usually from \code{\link[jpeg]{readJPEG}} or \code{\link[png]{readPNG}}. #' @param img_size Size of output image in pixel, where one pixel = one 'brick'. Use a single value (e.g. \code{48}) for a square image with 48 pixels on each side. #' Use an array of two values for a rectangular image \code{c(width, height)}. -#' @param method The method to use for comparison. Either 'brickr_classic', 'euclidean', 'cie1976', 'cie94', 'cie2000', or 'cmc'. -#' 'brickr_classic' is an explicit euclidean distance formula, but yield different results than 'euclidean' in {farver}. -#' See \code{farver::compare_colour}. +#' @param method The method to use for comparison. Options are 'euclidean', 'cie1976', 'cie94', 'cie2000', or 'cmc'. +#' See \code{\link[farver]{compare_colour}}. #' @param color_table Defaults to \code{lego_colors}. Data frame of brick colors to map onto image. Must contain Name and R, G, B channels. #' See attached data \code{lego_colors} as examples. #' @param color_palette Brick color rarity to use. Defaults to all colors: 'universal' (most common), 'generic', and 'special' (least common). This is useful when trying to build the mosaic out of real bricks. @@ -21,20 +20,55 @@ #' @family Mosaics #' @export #' @examples -#' \donttest{ -#' # Create a random 24x24 'image'. -#' # Otherwise, use a jpeg or png -#' demo_image = array(scales::rescale(runif(24*24*3), c(0, 1)), dim=c(24, 24, 3)) -#' -#' demo_image %>% -#' image_to_mosaic(img_size = 24) %>% -#' build_mosaic() +#' # Import a jpeg or png +#' demo_file <- system.file("extdata", "demo_img.jpg", +#' package = "brickr", mustWork = TRUE) +#' demo_image <- jpeg::readJPEG(demo_file) #' -#' #Only use the most common "universal" LEGO colors +#' #Create a 24x24 mosaic +#' \donttest{ #' demo_image %>% -#' image_to_mosaic(img_size = 24, color_palette = "universal") %>% +#' image_to_mosaic(img_size = 24) %>% #' build_mosaic() -#' } +#' } +#' +#' #Only use the two more common tiers of colors +#' \donttest{ +#' demo_image %>% +#' image_to_mosaic(img_size = 24, +#' color_palette = c("universal", "generic")) %>% +#' build_mosaic() +#' } +#' +#' #Be more prescriptive with colors using 'color_table'. +#' # Here, we prevent all blues from being used +#' lego_colors_no_blue = lego_colors %>% +#' dplyr::filter(!grepl("[Bb]lue|[Aa]zur", Color)) +#' \donttest{ +#' demo_image %>% +#' image_to_mosaic(img_size = 24, +#' color_table = lego_colors_no_blue) %>% +#' build_mosaic() +#' } +#' +#' #Color matching is done with the 'farver' package. There are different method. +#' # Change the method to euclidean (RGB distance matching) +#' \donttest{ +#' demo_image %>% +#' image_to_mosaic(img_size = 24, +#' method = "euclidean") %>% +#' build_mosaic() +#' } +#' +#' #Change the default size of bricks to use. +#' # In this extreme example, use 4x6s for the background, 1x1 everywhere else +#' \donttest{ +#' demo_image %>% +#' image_to_mosaic(img_size = 24, +#' use_bricks = c("4x6")) %>% +#' build_mosaic() +#' } +#' #' image_to_mosaic <- function(img, img_size = 48, color_table = NULL, method = "cie94", diff --git a/R/image-to-prep-mosaic.R b/R/image-to-prep-mosaic.R index c6e0686..c602cf0 100644 --- a/R/image-to-prep-mosaic.R +++ b/R/image-to-prep-mosaic.R @@ -1,4 +1,4 @@ -#' Scale an image raster array to a small number of pixels. +#' Scale an image raster array to a small number of pixels #' #' Decrease the size of an image, in pixel. Process into a data frame. Internal function. #' diff --git a/R/piece-count.R b/R/piece-count.R index 30752cc..15198ea 100644 --- a/R/piece-count.R +++ b/R/piece-count.R @@ -1,4 +1,4 @@ -#' Generate required bricks as a data frame. +#' Generate required bricks as a data frame #' #' Create a dataframe of brick colors and sizes used in a brick mosaic or model. #' @@ -6,15 +6,23 @@ #' @return Data frame of piece counts by LEGO color name and size. #' @family Resources #' @export -#' @examples \donttest{ -#' # Create a random 24x24 'image'. -#' # Otherwise, use a jpeg or png -#' demo_image = array(scales::rescale(runif(24*24*3), c(0, 1)), dim=c(24, 24, 3)) -#' -#' demo_image %>% -#' image_to_mosaic(img_size = 24) %>% -#' build_pieces_table() -#'} +#' @examples +#' # Import a jpeg or png +#' demo_file <- system.file("extdata", "demo_img.jpg", +#' package = "brickr", mustWork = TRUE) +#' demo_image <- jpeg::readJPEG(demo_file) +#' #Create a mosaic oject +#' \donttest{ +#' mosaic <- demo_image %>% +#' image_to_mosaic(img_size = 24) +#' } +#' +#' #Rather than drawing the mosaic, use build_pieces_table() to produce piece table +#' \donttest{ +#' mosaic %>% +#' build_pieces_table() +#' } + build_pieces_table <- function(brick_obj){ pcs <- brick_obj$pieces @@ -25,7 +33,7 @@ build_pieces_table <- function(brick_obj){ dplyr::rename(`LEGO Brick Color` = Lego_name) } -#' Display bricks required to build model or mosaic. +#' Display bricks required to build model or mosaic #' #' Create a chart of brick colors and sizes used in a brick mosaic or model. #' @@ -33,16 +41,22 @@ build_pieces_table <- function(brick_obj){ #' @return Plot object of required bricks by color and size. #' @family Resources #' @export -#' @examples \donttest{ -#' # Create a random 24x24 'image'. -#' # Otherwise, use a jpeg or png -#' demo_image = array(scales::rescale(runif(24*24*3), c(0, 1)), dim=c(24, 24, 3)) -#' -#' demo_image %>% -#' image_to_mosaic(img_size = 24) %>% -#' build_pieces() -#'} -#' +#' @examples +#' # Import a jpeg or png +#' demo_file <- system.file("extdata", "demo_img.jpg", +#' package = "brickr", mustWork = TRUE) +#' demo_image <- jpeg::readJPEG(demo_file) +#' #Create a mosaic oject +#' \donttest{ +#' mosaic <- demo_image %>% +#' image_to_mosaic(img_size = 24) +#' } +#' +#' #Rather than drawing the mosaic, use build_pieces_table() to draw piece chart +#' \donttest{ +#' mosaic %>% +#' build_pieces() +#' } build_pieces <- function(brick_obj){ in_list <- brick_obj diff --git a/cran-comments.md b/cran-comments.md index 55b7378..86481a2 100644 --- a/cran-comments.md +++ b/cran-comments.md @@ -17,5 +17,8 @@ This is my first CRAN submission. Re-submit with improvements to documentation. +Most examples that produce the mosaics or 3D models take more than 5 seconds to calculate. +Those lines of script have been wrapped in donttest. There are a few cases where I got the examples to run quickly without donttest. + ## Downstream dependencies No downstream dependencies \ No newline at end of file diff --git a/man/bricks_from_coords.Rd b/man/bricks_from_coords.Rd index 1cc3b4d..aa856b4 100644 --- a/man/bricks_from_coords.Rd +++ b/man/bricks_from_coords.Rd @@ -65,27 +65,31 @@ brick \%>\% #This is a lot of bricks bricks <- expand.grid( -x = 1:8, -y = 1:4, +x = 1:4, +y = 1:2, z = 1:3) #Color them in sets of these 3 options -bricks$color <- rep(rep(c("Bright yellow", "Bright red", "Tr. green"), each=4), 8) +bricks$color <- rep(rep(c("Bright yellow", "Bright red", "Tr. green"), each=4), 2) +\donttest{ bricks \%>\% bricks_from_coords() \%>\% build_bricks() rgl::clear3d() + } #Use different brick shapes by added a 'piece_type' column bricks$piece_type <- "c1" #Make all the pieces cylinders +\donttest{ bricks \%>\% bricks_from_coords() \%>\% build_bricks() rgl::clear3d() + } } \seealso{ diff --git a/man/bricks_from_mosaic.Rd b/man/bricks_from_mosaic.Rd index 8cf6d54..1e66181 100644 --- a/man/bricks_from_mosaic.Rd +++ b/man/bricks_from_mosaic.Rd @@ -18,23 +18,39 @@ A list with elements \code{Img_lego} to pass to \code{\link{build_bricks}}. } \description{ Stacks LEGO plates to create a 3D version of the 2D brick mosaics. -} -\details{ Height of bricks determined by brightness of color. } \examples{ -\donttest{ -# Create a random 24x24 'image'. -# Otherwise, use a jpeg or png - demo_image = array(scales::rescale(runif(24*24*3), c(0, 1)), dim=c(24, 24, 3)) - -demo_image \%>\% - image_to_mosaic(img_size = 24) \%>\% - bricks_from_mosaic() \%>\% - build_bricks() +# Import a jpeg or png + demo_file <- system.file("extdata", "demo_img.jpg", + package = "brickr", mustWork = TRUE) + demo_image <- jpeg::readJPEG(demo_file) - rgl::clear3d() -} + #Begin with a 24x24 mosaic object + \donttest{ + mosaic <- demo_image \%>\% + image_to_mosaic(24) + } + + #Pass the mosaic object to bricks_from_mosaic() to convert to 3D specifications + \donttest{ + mosaic \%>\% + bricks_from_mosaic() \%>\% + build_bricks() + + rgl::clear3d() + } + + #In this image, the background is a light color. + # Change the 'highest_el' to make dark colors highest + # Change mosaic height to change the number of layers + \donttest{ + mosaic \%>\% + bricks_from_mosaic(mosaic_height = 3, highest_el = "dark") \%>\% + build_bricks() + + rgl::clear3d() + } } \seealso{ Other 3D Models: diff --git a/man/bricks_from_table.Rd b/man/bricks_from_table.Rd index 8fcb590..87175ab 100644 --- a/man/bricks_from_table.Rd +++ b/man/bricks_from_table.Rd @@ -90,11 +90,14 @@ pieces <- data.frame( X1 = c("b", "b", "p", "p"), #b is brick (default), p is plate X2 = c("b", "b", "p", "p") ) + +\donttest{ brick \%>\% bricks_from_table(piece_matrix=pieces) \%>\% build_bricks() rgl::clear3d() + } #Provide a custom table of colors custom_colors <- data.frame( @@ -102,18 +105,22 @@ custom_colors <- data.frame( Color = c("Bright orange", "Dark green") ) +\donttest{ brick \%>\% bricks_from_table(color_guide = custom_colors) \%>\% build_bricks() rgl::clear3d() + } #Limit the size of bricks used in the model with use_bricks +\donttest{ brick \%>\% bricks_from_table(use_bricks = "2x1") \%>\% #Only use 2x1 bricks. build_bricks() rgl::clear3d() + } } \seealso{ Other 3D Models: diff --git a/man/build_bricks.Rd b/man/build_bricks.Rd index 6672d02..23ee1aa 100644 --- a/man/build_bricks.Rd +++ b/man/build_bricks.Rd @@ -31,10 +31,9 @@ Set to 'TRUE' and rgl_lit='FALSE' for cartoon-looking bricks.} 3D brick model rendered in the 'rgl' package. } \description{ -Render the output of any of the \code{bricks_from_*} functions as a 3D model. +Render the output of any of the \code{bricks_from_*} functions as a 3D model. Opens an 'rgl' window. } \examples{ -\donttest{ #This is a brick brick <- data.frame( Level="A", @@ -42,12 +41,23 @@ brick <- data.frame( X2 = rep(3,4) ) -brick \%>\% - bricks_from_table() \%>\% - build_bricks() +#Convert the dataframe to a list object that can be rendered +brick_object <- brick \%>\% + bricks_from_table() +#Render it +brick_object \%>\% + build_bricks() + rgl::clear3d() -} + +#Combine the option rgl_lit=FALSE & outline_bricks=TRUE +# This makes the rendering look like a drawing +brick_object \%>\% + build_bricks(outline_bricks = TRUE, rgl_lit = FALSE, + background_color = "#99e7ff") +rgl::clear3d() + } \seealso{ Other 3D Models: diff --git a/man/build_colors.Rd b/man/build_colors.Rd index ae6aaed..2510c10 100644 --- a/man/build_colors.Rd +++ b/man/build_colors.Rd @@ -15,16 +15,15 @@ build_colors(.names_only = FALSE, include_transparent = TRUE) An array or ggplot of brick colors & ID numbers. } \description{ -Generates a plot of available brick colors. +Generates a plot of available brick colors. These names must be used exactly when creating custom name lists. +There are 41 solid brick color names and 13 transparent colors. Transparent colors are not used in mosaics. } \details{ Use .names_only = TRUE to get a list of color names. } \examples{ #Generate plot of colors -\donttest{ -build_colors() -} +build_colors(include_transparent = FALSE) #Print list of colors build_colors(TRUE) diff --git a/man/build_instructions.Rd b/man/build_instructions.Rd index 676eba8..dd0ad5f 100644 --- a/man/build_instructions.Rd +++ b/man/build_instructions.Rd @@ -25,15 +25,28 @@ This makes it easier to follow each row when building an actual brick mosaic. The current model level is clearly displayed, while the previous level is shows as transparent. } \examples{ -\donttest{ -# Create a random 24x24 'image'. -# Otherwise, use a jpeg or png - demo_image = array(scales::rescale(runif(24*24*3), c(0, 1)), dim=c(24, 24, 3)) +# Import a jpeg or png + demo_file <- system.file("extdata", "demo_img.jpg", + package = "brickr", mustWork = TRUE) + demo_image <- jpeg::readJPEG(demo_file) + + #Create a mosaic oject + \donttest{ + mosaic <- demo_image \%>\% + image_to_mosaic(img_size = 24) + } + + #Rather than drawing the mosaic, use build_instructions() to draw instructions + \donttest{ + mosaic \%>\% + build_instructions() + } -demo_image \%>\% - image_to_mosaic(img_size = 24) \%>\% - build_instructions() -} +#Change the number of steps for more detail +\donttest{ + mosaic \%>\% + build_instructions(num_steps = 9) + } } \seealso{ diff --git a/man/build_mosaic.Rd b/man/build_mosaic.Rd index a27c67b..8b4a065 100644 --- a/man/build_mosaic.Rd +++ b/man/build_mosaic.Rd @@ -20,11 +20,23 @@ Render a plot image of the 2D brick mosaic with optional title. \examples{ # Import a jpeg or png - demo_image = system.file("extdata", "demo_img.jpg", package = "brickr", mustWork = TRUE) - -jpeg::readJPEG(demo_image) \%>\% - image_to_mosaic() \%>\% + demo_file <- system.file("extdata", "demo_img.jpg", + package = "brickr", mustWork = TRUE) + demo_image <- jpeg::readJPEG(demo_file) + +#Build a very small 12x12 mosaic. + \donttest{ +demo_image \%>\% + image_to_mosaic(12) \%>\% build_mosaic() + } + +#Build a mosaic in the default size of 48x48 studs with title +\donttest{ +demo_image \%>\% + image_to_mosaic() \%>\% + build_mosaic("Demo mosaic") + } } \seealso{ Other Mosaics: diff --git a/man/build_pieces.Rd b/man/build_pieces.Rd index 7cfa295..703dd2d 100644 --- a/man/build_pieces.Rd +++ b/man/build_pieces.Rd @@ -2,7 +2,7 @@ % Please edit documentation in R/piece-count.R \name{build_pieces} \alias{build_pieces} -\title{Display bricks required to build model or mosaic.} +\title{Display bricks required to build model or mosaic} \usage{ build_pieces(brick_obj) } @@ -16,16 +16,21 @@ Plot object of required bricks by color and size. Create a chart of brick colors and sizes used in a brick mosaic or model. } \examples{ -\donttest{ -# Create a random 24x24 'image'. -# Otherwise, use a jpeg or png - demo_image = array(scales::rescale(runif(24*24*3), c(0, 1)), dim=c(24, 24, 3)) - -demo_image \%>\% - image_to_mosaic(img_size = 24) \%>\% - build_pieces() -} - +# Import a jpeg or png + demo_file <- system.file("extdata", "demo_img.jpg", + package = "brickr", mustWork = TRUE) + demo_image <- jpeg::readJPEG(demo_file) +#Create a mosaic oject + \donttest{ + mosaic <- demo_image \%>\% + image_to_mosaic(img_size = 24) + } + + #Rather than drawing the mosaic, use build_pieces_table() to draw piece chart + \donttest{ + mosaic \%>\% + build_pieces() + } } \seealso{ Other Resources: diff --git a/man/build_pieces_table.Rd b/man/build_pieces_table.Rd index d9d14d7..26b8561 100644 --- a/man/build_pieces_table.Rd +++ b/man/build_pieces_table.Rd @@ -2,7 +2,7 @@ % Please edit documentation in R/piece-count.R \name{build_pieces_table} \alias{build_pieces_table} -\title{Generate required bricks as a data frame.} +\title{Generate required bricks as a data frame} \usage{ build_pieces_table(brick_obj) } @@ -16,15 +16,21 @@ Data frame of piece counts by LEGO color name and size. Create a dataframe of brick colors and sizes used in a brick mosaic or model. } \examples{ -\donttest{ -# Create a random 24x24 'image'. -# Otherwise, use a jpeg or png - demo_image = array(scales::rescale(runif(24*24*3), c(0, 1)), dim=c(24, 24, 3)) - -demo_image \%>\% - image_to_mosaic(img_size = 24) \%>\% - build_pieces_table() -} +# Import a jpeg or png + demo_file <- system.file("extdata", "demo_img.jpg", + package = "brickr", mustWork = TRUE) + demo_image <- jpeg::readJPEG(demo_file) +#Create a mosaic oject + \donttest{ + mosaic <- demo_image \%>\% + image_to_mosaic(img_size = 24) + } + + #Rather than drawing the mosaic, use build_pieces_table() to produce piece table + \donttest{ + mosaic \%>\% + build_pieces_table() + } } \seealso{ Other Resources: diff --git a/man/collect_bricks.Rd b/man/collect_bricks.Rd index 0ea842a..dce8a77 100644 --- a/man/collect_bricks.Rd +++ b/man/collect_bricks.Rd @@ -2,12 +2,12 @@ % Please edit documentation in R/collect-bricks.R \name{collect_bricks} \alias{collect_bricks} -\title{Consolidate 1x1 bricks into larger ones of the same color. Internal function.} +\title{Consolidate 1x1 bricks into larger ones of the same color} \usage{ collect_bricks(image_list, use_bricks = NULL, default_piece_type = "b") } \arguments{ -\item{image_list}{List output from legoize(). Contains an element \code{Img_lego}.} +\item{image_list}{List output containing an element \code{Img_lego}.} \item{use_bricks}{Array of brick sizes to use in mosaic. Defaults to \code{c('4x2', '2x2', '3x1', '2x1', '1x1')}`.} @@ -17,6 +17,7 @@ collect_bricks(image_list, use_bricks = NULL, default_piece_type = "b") A list with element \code{Img_bricks} containing a data frame of the x- & y-coordinates, R, G, B channels, and brick ID. Other helper elements. } \description{ -Consolidate 1x1 bricks into larger ones of the same color. Internal function. +Internal function for collecting single bricks into large ones. Used in both mosaics and 3D models. +Automatically generate missing piece_type or mid_level. } \keyword{internal} diff --git a/man/image_to_mosaic.Rd b/man/image_to_mosaic.Rd index c12025e..db55010 100644 --- a/man/image_to_mosaic.Rd +++ b/man/image_to_mosaic.Rd @@ -26,9 +26,8 @@ Use an array of two values for a rectangular image \code{c(width, height)}.} \item{color_table}{Defaults to \code{lego_colors}. Data frame of brick colors to map onto image. Must contain Name and R, G, B channels. See attached data \code{lego_colors} as examples.} -\item{method}{The method to use for comparison. Either 'brickr_classic', 'euclidean', 'cie1976', 'cie94', 'cie2000', or 'cmc'. -'brickr_classic' is an explicit euclidean distance formula, but yield different results than 'euclidean' in {farver}. -See \code{farver::compare_colour}.} +\item{method}{The method to use for comparison. Options are 'euclidean', 'cie1976', 'cie94', 'cie2000', or 'cmc'. +See \code{\link[farver]{compare_colour}}.} \item{color_palette}{Brick color rarity to use. Defaults to all colors: 'universal' (most common), 'generic', and 'special' (least common). This is useful when trying to build the mosaic out of real bricks. Use "bw" for only grayscale bricks. Ignored if a \code{color_table} is supplied.} @@ -50,20 +49,55 @@ A list with element \code{Img_lego} containing a data frame of the x- & y-coordi Generate brick mosaics from an image or matrix with customization options. } \examples{ -\donttest{ -# Create a random 24x24 'image'. -# Otherwise, use a jpeg or png - demo_image = array(scales::rescale(runif(24*24*3), c(0, 1)), dim=c(24, 24, 3)) - -demo_image \%>\% - image_to_mosaic(img_size = 24) \%>\% - build_mosaic() +# Import a jpeg or png + demo_file <- system.file("extdata", "demo_img.jpg", + package = "brickr", mustWork = TRUE) + demo_image <- jpeg::readJPEG(demo_file) - #Only use the most common "universal" LEGO colors + #Create a 24x24 mosaic + \donttest{ demo_image \%>\% - image_to_mosaic(img_size = 24, color_palette = "universal") \%>\% + image_to_mosaic(img_size = 24) \%>\% build_mosaic() -} + } + + #Only use the two more common tiers of colors + \donttest{ + demo_image \%>\% + image_to_mosaic(img_size = 24, + color_palette = c("universal", "generic")) \%>\% + build_mosaic() + } + + #Be more prescriptive with colors using 'color_table'. + # Here, we prevent all blues from being used + lego_colors_no_blue = lego_colors \%>\% + dplyr::filter(!grepl("[Bb]lue|[Aa]zur", Color)) + \donttest{ + demo_image \%>\% + image_to_mosaic(img_size = 24, + color_table = lego_colors_no_blue) \%>\% + build_mosaic() + } + + #Color matching is done with the 'farver' package. There are different method. + # Change the method to euclidean (RGB distance matching) + \donttest{ + demo_image \%>\% + image_to_mosaic(img_size = 24, + method = "euclidean") \%>\% + build_mosaic() + } + + #Change the default size of bricks to use. + # In this extreme example, use 4x6s for the background, 1x1 everywhere else + \donttest{ + demo_image \%>\% + image_to_mosaic(img_size = 24, + use_bricks = c("4x6")) \%>\% + build_mosaic() + } + } \seealso{ diff --git a/man/image_to_scaled.Rd b/man/image_to_scaled.Rd index 8e83c7c..661ba3d 100644 --- a/man/image_to_scaled.Rd +++ b/man/image_to_scaled.Rd @@ -2,7 +2,7 @@ % Please edit documentation in R/image-to-prep-mosaic.R \name{image_to_scaled} \alias{image_to_scaled} -\title{Scale an image raster array to a small number of pixels.} +\title{Scale an image raster array to a small number of pixels} \arguments{ \item{image}{A raster array from an image.} diff --git a/vignettes/mosaics.Rmd b/vignettes/mosaics.Rmd index d80a150..0172f01 100644 --- a/vignettes/mosaics.Rmd +++ b/vignettes/mosaics.Rmd @@ -105,10 +105,10 @@ png::readPNG(demo_img) %>% brickr uses the [`farver`](https://CRAN.R-project.org/package=farver) package to match image colors to the subset of LEGO colors. Technical details of the different algorithms can be found on [Wikipedia](https://en.wikipedia.org/wiki/Color_difference). -The default algorithm is 'cie94', though the other farver options are available using the `method` input. The farver 'euclidean' is not very accurate, so 'brickr_classic' returns a manually calculated version for Euclidean RGB distance matching. +The default algorithm is 'cie94', though the other farver options are available using the `method` input. ```{r c_methods, fig.height=5, fig.width=5} -c("cie94", "cie2000", "euclidean", "brickr_classic") %>% +c("cie94", "cie2000", "euclidean", "cmc") %>% purrr::map(~png::readPNG(demo_img) %>% image_to_mosaic(24, method =.x) %>% build_mosaic(title = .x )) -> mosaics_by_method