Skip to content

Possible as_pixeltrix() enhancements #38

@trevorld

Description

@trevorld
  • as_pixeltrix() becomes an S3 generic
  • as_pixeltrix.matrix() adds an optional colours argument.
  • Some more possible methods that wouldn't require adding anything to Suggests (you already use the core {grDevices} elsewhere):
# as returned by `png::readPNG()` and friends
as_pixeltrix.array <- function(x, ...) {
    as_pixeltrix.raster(grDevices::as.raster(x))
}

# {bittermelon} bitmap object
as_pixeltrix.bm_bitmap <- function(x, colours = NULL, ...) {
    m <- as.matrix(x, first_row_is_top = TRUE)
    as_pixeltrix.matrix(m, colours = colours)
}

# {bittermelon} pixmap object
as_pixeltrix.bm_pixmap <- function(x, ...) {
    as_pixeltrix.raster(grDevices::as.raster(x))
}

# `{magick}` image object
`as_pixeltrix.magick-image` <- function(x, ...) {
    as_pixeltrix.raster(grDevices::as.raster(x))
}

as_pixeltrix.raster <- function(x, ...) {
    f <- as.factor(as.matrix(x))
    m <- matrix(as.integer(f) - 1L, nrow = nrow(x), ncol = ncol(x))
    as_pixeltrix.matrix(m, colours = levels(f))
}

# An `as_pixeltrix.nativeRaster()` method would probably need adding 
# {farver} and/or {nara} to `Suggests`...

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions