Convert sRGB-encoded color data to linear light (and vice versa).
render_gamma_linear(color, srgb_to_linear = TRUE, as_hex = FALSE)A rayimg, hex color string (or vector), length-3 numeric RGB vector,
or RGB/RGBA matrix.
Default TRUE. If TRUE, converts sRGB-encoded data to linear light.
If FALSE, converts linear-light data to sRGB.
Default FALSE. When srgb_to_linear = FALSE and input is an RGB/RGBA
matrix or array, returns hex: for matrices a length-n vector; for arrays a
length-prod(dim(x)[1:2]) vector in column-major order. Alpha (if present) is passed through.
Same shape/type as input unless as_hex = TRUE on matrix/array with srgb_to_linear = FALSE,
in which case a character vector of hex codes is returned.
# Numeric RGB vector (sRGB -> linear -> back to sRGB)
srgb_vec = c(0.2, 0.4, 0.6)
linear_vec = render_gamma_linear(srgb_vec)
render_gamma_linear(linear_vec, srgb_to_linear = FALSE)
#> [1] 0.2 0.4 0.6
# Hex input (single color)
linear_from_hex = render_gamma_linear("#336699")
render_gamma_linear(linear_from_hex, srgb_to_linear = FALSE, as_hex = TRUE)
#> [1] "#336699"
# Greyscale matrix (treated as image plane)
grey_image = matrix(c(0.1, 0.5, 0.9, 0.3), nrow = 2)
linear_grey = render_gamma_linear(grey_image)
render_gamma_linear(linear_grey, srgb_to_linear = FALSE, as_hex = TRUE)
#> [1] "#1A1A1A" "#808080" "#E6E6E6" "#4C4C4C"
# Array with a single greyscale channel
grey_pixels = array(c(0.2, 0.7), dim = c(1, 2, 1))
linear_grey_arr = render_gamma_linear(grey_pixels)
render_gamma_linear(linear_grey_arr, srgb_to_linear = FALSE, as_hex = TRUE)
#> [1] "#333333" "#B2B2B2"
# Array with greyscale + alpha
grey_alpha = array(c(0.3, 0.9, 0.6, 0.4), dim = c(1, 2, 2))
linear_grey_alpha = render_gamma_linear(grey_alpha)
render_gamma_linear(linear_grey_alpha, srgb_to_linear = FALSE, as_hex = TRUE)
#> [1] "#4C4C4C99" "#E6E6E666"
# RGB array (3 channels)
rgb_pixels = array(
c(
0.2, 0.4, 0.6,
0.7, 0.1, 0.3
),
dim = c(1, 2, 3)
)
linear_rgb = render_gamma_linear(rgb_pixels)
render_gamma_linear(linear_rgb, srgb_to_linear = FALSE, as_hex = TRUE)
#> [1] "#33991A" "#66B24C"
# RGBA array (alpha channel preserved)
rgba_pixels = array(
c(
0.2, 0.4, 0.6, 0.5,
0.7, 0.1, 0.3, 0.8
),
dim = c(1, 2, 4)
)
linear_rgba = render_gamma_linear(rgba_pixels)
render_gamma_linear(linear_rgba, srgb_to_linear = FALSE, as_hex = TRUE)
#> [1] "#3399B24C" "#66801ACC"
# rayimg input retains metadata and supports hex conversion
linear_img = render_gamma_linear(ray_read_image(rgba_pixels))
render_gamma_linear(linear_img, srgb_to_linear = FALSE, as_hex = TRUE)
#> [1] "#3399B24C" "#66801ACC"