R/nr-color.R

Defines functions nr_color_replace nr_threshold nr_dither nr_desaturate

Documented in nr_color_replace nr_desaturate nr_dither nr_threshold

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' Move image colors to gray
#' 
#' @inheritParams nr_fill
#' @param factor desaturation factor. Default: 1 (fully desaturate)
#' 
#' @return Invisibly return the supplied native raster image which was been
#'         modified in-place
#'         
#' @examples
#' plot(deer[[1]])
#' nr <- deer[[1]] |> nr_copy()
#' 
#' nr_desaturate(nr)
#' plot(nr)
#' @family color manipulation functions
#' @export
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
nr_desaturate <- function(nr, factor = 1) {
  invisible(
    .Call(nr_desaturate_, nr, factor)
  )
}



#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' Dither to binary image
#' 
#' @inheritParams nr_fill
#' @param value Threshold value. Default: 0.5  (valid range [0, 1])
#' @param algo Dithering algorithm. 'fs' (floyd-steinberg) or 'atkinson'. 
#'        Default: 'fs'
#'        
#' @return Invisibly return the supplied native raster image which was been
#'         modified in-place
#'         
#' @examples
#' nr <- nr_copy(deer[[1]])
#' plot(nr)
#' 
#' nr_dither(nr, 0.99)
#' plot(nr)
#' @family color manipulation functions
#' @export
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
nr_dither <- function(nr, value = 0.5, algo = 'fs') {
  invisible(
    .Call(nr_dither_, nr, value, algo)
  )
}



#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' Threshold to binary image
#' 
#' @inheritParams nr_fill
#' @param value Threshold value. Default: 0.5  (valid range [0, 1])
#' 
#' @return Invisibly return the supplied native raster image which was been
#'         modified in-place
#'         
#' @examples
#' nr <- nr_copy(deer[[1]])
#' plot(nr)
#' 
#' nr_threshold(nr, 0.9)
#' plot(nr)
#' @family color manipulation functions
#' @export
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
nr_threshold <- function(nr, value = 0.5) {
  invisible(
    .Call(nr_threshold_, nr, value)
  )
}



#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' Replace colors in a native raster image
#' 
#' @inheritParams nr_fill
#' @param old Vector of old colors
#' @param new Vector of replacement colors
#' 
#' @return Invisibly return the supplied native raster image which was been
#'         modified in-place
#' 
#' @examples
#' nr <- nr_new(10, 10, 'hotpink')
#' nr_color_replace(nr, 'hotpink', 'grey80')
#' plot(nr)
#' @family color manipulation functions
#' @export
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
nr_color_replace <- function(nr, old, new) {
  invisible(
    .Call(nr_color_replace_, nr, old, new)
  )
}

Try the nara package in your browser

Any scripts or data that you put into this service are public.

nara documentation built on May 28, 2026, 5:07 p.m.