RGBA_2_gray: Converts RGBA/RGB values to grayscales

View source: R/RGBA_2_gray.R

RGBA_2_grayR Documentation

Converts RGBA/RGB values to grayscales

Description

Converts RGB or RGBA values in R numeric formats to grayscale values of the same format

Usage

RGBA_2_gray(red.ch, green.ch, blue.ch, alp.ch, bg = 1, alp = T)
RGBA_2_gray(red.ch, green.ch, blue.ch, alp = F)
RGBA_2_gray(red.ch, green.ch, blue.ch)

Arguments

red.ch

The red channel of the data that you want to convert.
This can be either a vector, a matrix or an array.

green.ch

The green channel of the data that you want to convert.
This can be either a vector, a matrix or an array.

blue.ch

The blue channel of the data that you want to convert.
This can be either a vector, a matrix or an array.

alp.ch

This argument is only needed when alp = T.
This argument denotes the transparency of the RGB/RGBA data.
The alpha channel of the data that you want to convert.
This can be either a vector, a matrix or an array.

bg

This argument is only needed when alp = T.
This argument has to be a single number between 0 and 1.
Denotes the background for implementing the transparency.
bg = 0 denotes black, while bg = 1 denotes white.
bg = 1 by default, hence default background is white.

alp

This argument denotes whether an alpha value is needed for the conversion.
alp = T, by default.

Details

red.ch, green.ch, blue.ch and alp.ch (if needed), can be have structures of matrix, vectors(NOT lists) and arrays.
red.ch, green.ch, blue.ch and alp.ch (if needed) all need to have the same structures and dimensions.
Dataframes will not be handled.

Value

The return value will have the same structure and dimension of red.ch, green.ch and blue.ch.
It will defintely be a numeric as non-numeric values are not entertained as inputs.
The gray values are defined as shown below......
gray_val <- 0.2989*red.ch + 0.5870*green.ch + 0.1140*blue.ch
gray_val <- alp.ch*gray_val + (1 - alp.ch)*bg

Author(s)

Chitran Ghosal

Examples

##---- Should be DIRECTLY executable !! ----
##-- ==>  Define data, use random,
##--	or standard data sets, see data().
#write a program to test RGBA_2_Gray
library(StatsChitran)
r.ch <- matrix(data = runif(n = 10^4, min = 0, max = 2), nrow = 100, byrow = T)
g.ch <- matrix(data = runif(n = 10^4, min = 0, max = 2), nrow = 100, byrow = T)
b.ch <- matrix(data = runif(n = 10^4, min = 0, max = 2), nrow = 100, byrow = T)
a.ch <- matrix(data = runif(n = 10^4, min = 0, max = 2), nrow = 100, byrow = T)

gray_mat1 <- RGBA_2_gray(red.ch = r.ch, green.ch = g.ch, blue.ch = b.ch, alp.ch = a.ch, bg=0, alp = T)
gray_mat2 <- RGBA_2_gray(red.ch = r.ch, green.ch = g.ch, blue.ch = b.ch, alp = F )
subplot(c(2, 1))
plot2D.mat(X = seq(1, 100), Y = seq(1, 100), Z = gray_mat1)
plot2D.mat(X = seq(1, 100), Y = seq(1, 100), Z = gray_mat2)
subplot(c(1, 1))


Chitran1987/StatsChitran documentation built on Feb. 23, 2025, 8:30 p.m.