R/color_name.R

Defines functions lx_check_color print.lx_color_string lx_color_name

Documented in lx_check_color lx_color_name

#' picking a color by name or hsbk
#' @template param_colors
#' @template param_color_name
#' @param check if FALSE does not call the API to check if the color is valid
#' @template param_token
#' @export
lx_color_name <- function(hue = NULL, saturation = NULL, brightness = NULL, kelvin = NULL, color_name = NULL, check = TRUE, token = lx_get_token()) {

    inputs <- list(hue, saturation, brightness, kelvin)
    names(inputs) <- c("hue", "saturation", "brightness", "kelvin")
    inputs <- unlist(inputs)
    # generate a string of color information as the lifx api expects it (https://api.developer.lifx.com/docs/colors)
    if (length(inputs) == 0) {
        color_string <- ""
    } else {
        color_string <- paste(names(inputs), ":", inputs, collapse = " ", sep = "")
    }

    color_string <- paste(color_name, color_string)
    # call api to validate color
    if(check){
    if (!lx_check_color(color_string, token = token)) {
        stop("invalid color specification.")
    }
    }
    class(color_string) <- c("lx_color_string", class(color_string))
    return(color_string)
}


#' @method print lx_color_string
#' @export
print.lx_color_string <- function(x, ...) {
    cat(crayon::silver("lx color:"))
    cat(crayon::italic(x))
    invisible(x)
}




#' check if lifx color name is valid
#' @param color_name a color string in lifx api format (can be made with \code{\link{lx_color_name}} )
#' @template param_token
#' @export
lx_check_color <- function(color_name,  token = lx_get_token()) {
  url <- paste0("https://api.lifx.com/v1/color?string=", utils::URLencode(color_name))
  header <- lx_auth(token)
  response <- httr::GET(url, header)
  if (response$status_code == 422) {
    return(FALSE)
  }
  if (response$status_code == 200) {
    return(TRUE)
  }
  if (response$status_code == 401) {
    message("bad token - could not check color")
    return(FALSE)
  }
  print(response)
  stop(paste("Error:", response$status_code))
  return(color_name)
}
mabafaba/lifx documentation built on June 8, 2020, 3:46 a.m.