#' RGB color indices
#'
#' Computes multiple RGB-based color indices from a three-band RGB image.
#'
#' @param image A RasterBrick or RasterStack object with three bands (R, G, B).
#'
#' @return
#' A RasterStack object where each layer represents a specific RGB-based color index,
#' including Grey, BI, HI, RI, SI, CI, CIVE, VEG, IPCA, GLAI, SAT, OHI, TCVI, COM1, and COM2.
#'
#' @examples
#' set.seed(123)
#' r <- raster::raster(matrix(runif(50*50), 50, 50))
#' g <- raster::raster(matrix(runif(50*50), 50, 50))
#' b <- raster::raster(matrix(runif(50*50), 50, 50))
#' img <- raster::stack(r, g, b)
#' rgb_color(img)
#'
#' @export
rgb_color <- function(image){
  suppressWarnings({
    img <- raster::brick(image)
    R <- img[[1]]; G <- img[[2]]; B <- img[[3]]
    r <- R/(R+G+B); g <- G/(R+G+B); b <- B/(R+G+B)
    
    Grey <- 0.2898*r + 0.5870*g + 0.1140*b
    BI <- sqrt((R^2+G^2+B^2)/3)
    HI <- (2*R-G-B)/(G-B)
    RI <- R^2/(B*G^3)
    SI <- 2*(R-G-B)/(G-B)
    CI <- (R-B)/R
    CIVE <- 0.441*R - 0.811*G + 0.385*B + 18.78745
    VEG <- G/(R^0.667 * B^0.333)
    IPCA <- 0.994*(R-B)+0.961*(G-B)+0.914*(G-R)
    GLAI <- 25*(G-R)/(G+R-B)+1.25
    SAT <- (abs(R-G)+abs(R-B)+abs(G-B))/(3*(R+G+B)+1e-10)
    OHI <- atan(2*(R-G-B)/(30.5*(G-B)))
    TCVI <- 1.4*(2*R-2*B)/(2*R-G-2*B+255*0.4)
    ExG <- 2*g-r-b
    ExGR <- 3*g-2.4*r-b
    COM1 <- ExG + CIVE + ExGR + VEG
    COM2 <- 0.36*ExG + 0.47*CIVE + 0.17*VEG
    
    out <- raster::stack(Grey,BI,HI,RI,SI,CI,CIVE,VEG,IPCA,GLAI,SAT,OHI,TCVI,COM1,COM2)
    names(out) <- c("Grey","BI","HI","RI","SI","CI","CIVE","VEG","IPCA","GLAI","SAT","OHI","TCVI","COM1","COM2")
    out[!is.finite(out)] <- NA
    out
  })
}