Learn R Programming

spacesRGB (version 1.1-1)

spacesRGB-package: Calculate with RGB spaces

Description

RGB spaces are confusing because for each RGB space, up to three RGB vectors are involved:

  • linear scene RGB that corresponds to light from a scene

  • non-linear signal RGB encoded from scene RGB

  • linear display RGB that corresponds to light emitted from a display

Without qualification, the non-linear signal RGB is usually meant. This is the case for grDevices::rgb(), grDevices::hsv(), grDevices::convertColor(), etc. This diagram shows the full processing system.

There are two XYZ vectors. Scene XYZ is converted to scene RGB by a 3x3 matrix \(M\). This matrix is uniquely determined by the xy chromaticities of the RGB primaries and the XYZ of the whitepoint (3*2 + 3 = 9 total numbers). Display RGB is converted to display XYZ through multiplication by \(M^{-1}\). In this package \(M\) and \(M^{-1}\) are computed with full precision; the matrices that may be published in the standard are not used. All 4 vectors - scene RGB, scene XYZ, display RGB, and display XYZ - are called linear because they are all linear functions of the spectral energy distrubution of light.

The terminology below is mostly taken from BT.709, BT.1886, and BT.2100

In this package, both linear RGB vectors are normalized to the cube [0,1]\(^3\); they are usually thought of as optical in nature.

Signal RGB can be thought of as electrical in nature. It is common to think of this signal as an 8-bit number stored in the frame buffer, so the cube [0,255]\(^3\) is very commmon for signal RGB. This package allows the user to specify any positive number as the upper limit of the interval. The relevant function argument is maxSignal.

The conversion function from optical scene RGB (linear) to electrical signal RGB (non-linear) is called the Opto-Electronic Transfer Function (OETF). Each primary component (R,G, and B) is transformed independently and has the same OETF. In this package, the OETF is always defined as a map of [0,1] to itself. The classical OETF is the "\(1/\gamma\) power law". For Adobe RGB, the OETF is classical with \(\gamma = 563/256 \approx 2.2\). For the most popular RGB space - sRGB - the OETF is a continuous function defined in 2 pieces. In ICC profiles, it is called a Tone Response Curve (TRC), or shaper curve.

All the transfer functions in this package are continuous and strictly increasing functions that take [0,1] to itself. In fancy terminology they are orientation preserving homeomorphisms of [0,1]. All transfer functions are either power laws, or well-approximated by power laws. The exponent giving the best match is called the effective gamma, the approximate gamma, or the best-fit exponent for the transfer function. The function summaryRGB() displays the best-fit exponent for the transfer functions of all the installed RGB spaces.

The conversion function from electrical signal RGB (non-linear) to optical display RGB (linear) is called the Electro-Optical Transfer Function (EOTF). Each primary component (R,G, and B) is transformed independently and has the same EOTF. In this package, the EOTF is always defined as a map of [0,1] to itself. The classical EOTF is the "\(\gamma\) power law". That is \(R' = R^\gamma\), where \(R'\) is display R and \(R\) is signal R.

All the RGB spaces in Lindbloom and all the pre-installed RGB spaces in this package (except for the space 'HD+2.4'), have EOTF = OETF\(^{-1}\). This implies that scene RGB and display RGB are the same, and that scene XYZ and display XYZ are the same. In SMPTE 240M, the OETF is the transfer function of the reference camera, and the EOTF is the transfer function of the reference reproducer, and they are inverses. Stated another way the Opto-optical Transfer Function (OOTF), which is the composition of the OETF and the EOTF, $$OOTF = EOTF \circ OETF$$ is the identity, i.e. trivial. In some contexts one says that the system gamma is 1, or that the end-to-end exponent is 1. However, due to the Hunt effect and the Stevens effect, color reproduction is more accurate when the the system gamma is *not* 1; see Color Appearance Model. See Examples for creation of 'HD+2.4' - an RGB space with a non-trivial OOTF.

The following RGB space features are outside the scope of this package:

  • black offset, as in BT.1886

  • artistic adjustments, as in BT.2100

  • high-dynamic range, as in BT.2100

  • Academy Color Encoding System (ACES)

An excellent reference for all this is Poynton.

Arguments

The API

The API is small. There are functions to

  • convert between signal RGB and XYZ (both scene and display)

  • convert between signal RGB and linear RGB (both scene and display)

  • install, uninstall, and query the dictionary of RGB spaces

The XYZ vector is always relative to the XYZ of the white point of the RGB space; there are no functions for chromatic adaptation, as in Pascale.

It is also possible to convert between XYZ and linear RGB in one function call by overriding the OETF and EOTF by setting TF=1 as follows RGBfromXYZ( XYZ, TF=1 ) XYZfromRGB( XYZ, TF=1 ) In the first case, besides being more efficient, there is also no clamping to the unit cube, so one can tell how far outside the cube that the linear RGB really is.

Other Packages

Package colorspace has similar functionality, and is much faster because it is compiled. But it only supports one RGB space - sRGB.

Package colorscience has very flexible conversion functions, which include gamma override and chromatic adaptation, and it has all the RGB spaces in Lindbloom. But is structured so it only allows one space with non-classical EOTF/OETF, namely sRGB.

References

BT.709. Parameter values for the HDTV standards for production and international programme exchange. June 2015.

BT.1886. Reference electro-optical transfer function for flat panel displays used in HDTV studio production. March 2011.

BT.2020. Parameter values for ultra-high definition television systems for production and international programme exchange. October 2015.

BT.2100. Image parameter values for high dynamic range television for use in production and international programme exchange. June 2017

Lindbloom, Bruce. RGB/XYZ Matrices. http://brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html

Pascale, Danny. A Review of RGB Color Spaces ...from xyY to R'G'B'. http://www.babelcolor.com/download/A%20review%20of%20RGB%20color%20spaces.pdf. 2003.

Poynton, Charles. Digital Video and HD - Algorithms and Interfaces. Morgan Kaufmann. Second Edition. 2012.

Wikipedia. Adobe RGB (1998). http://en.wikipedia.org/wiki/Adobe_RGB_color_space.

Wikipedia. Color Appearance Model. https://en.wikipedia.org/wiki/Color_appearance_model

Wikipedia. ProPhoto RGB. http://en.wikipedia.org/wiki/ProPhoto_RGB_color_space.

Wikipedia. sRGB. http://en.wikipedia.org/wiki/SRGB.

See Also

summaryRGB(), installRGB()

Examples

Run this code
# NOT RUN {
# Install an RGB space named 'HD+2.4', with encoding from BT.709 and display from BT.1886.
# the OOTF for this space is non-trivial

prim    = matrix( c(0.64,0.33,  0.30,0.60,  0.15,0.06 ), 3, 2, byrow=TRUE )
white   = c( 0.3127, 0.3290 )
    
installRGB( "HD+2.4", prim, white, OETF='BT.709', EOTF=2.4, overwrite=TRUE )
# }

Run the code above in your browser using DataLab