render_gamma_linear: Render Gamma/Linear

View source: R/render_gamma_linear.R

render_gamma_linearR Documentation

Render Gamma/Linear

Description

Convert sRGB-encoded color data to linear light (and vice versa).

Usage

render_gamma_linear(color, srgb_to_linear = TRUE, as_hex = FALSE)

Arguments

color

A rayimg, hex color string (or vector), length-3 numeric RGB vector, or RGB/RGBA matrix.

srgb_to_linear

Default TRUE. If TRUE, converts sRGB-encoded data to linear light. If FALSE, converts linear-light data to sRGB.

as_hex

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.

Value

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.

Examples

# 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)

# Hex input (single color)
linear_from_hex = render_gamma_linear("#336699")
render_gamma_linear(linear_from_hex, srgb_to_linear = FALSE, as_hex = TRUE)

# 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)

# 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)

# 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)

# 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)

# 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)

# 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)

rayimage documentation built on June 12, 2026, 5:06 p.m.