Learn R Programming

colorRamp2 (version 0.1.0)

col2value: Convert back from colors to values

Description

Convert back from colors to values

Usage

col2value(r, g, b, col_fun)

Value

A vector of original numeric values.

Arguments

r

Red channel in sRGB color space. Value should be between 0 and 1. The value can also be a character vector of colors or a three-column matrix with r, g, b as columns. In this case, g and b are ignored,

g

Green channel in sRGB color space. Value should be between 0 and 1.

b

Blue channel in sRGB color space. Value should be between 0 and 1.

col_fun

the color mapping function generated by colorRamp2.

Author

Zuguang Gu <z.gu@dkfz.de>

Details

colorRamp2 maps values to colors and this function does the reversed job. Note for some color spaces, it cannot convert back to the original value perfectly.

Examples

Run this code
x = seq(0, 1, length.out = 11)
col_fun = colorRamp2(c(0, 0.5, 1), c("blue", "white", "red"))
col = col_fun(x)
col2value(col, col_fun = col_fun)
col2value("red", col_fun = col_fun)

col_fun = colorRamp2(c(0, 0.5, 1), c("blue", "white", "red"), space = "sRGB")
col = col_fun(x)
col2value(col, col_fun = col_fun)

Run the code above in your browser using DataLab