Title: | Generate Color Mapping Functions |
---|---|
Description: | A color mapping is generated according to the break values and corresponding colors. Other colors are generated by interpolating in a certain color space. |
Authors: | Zuguang Gu [aut, cre] |
Maintainer: | Zuguang Gu <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.0.1 |
Built: | 2024-11-11 02:57:59 UTC |
Source: | https://github.com/jokergoo/colorramp2 |
Add transparency to colors
add_transparency(col, transparency = 0)
add_transparency(col, transparency = 0)
col |
A vector of colors. |
transparency |
Transparency, numeric value between 0 and 1. |
A vector of colors.
add_transparency("red", 0.5) add_transparency(1, 0.5) add_transparency("#FF000080", 0.2)
add_transparency("red", 0.5) add_transparency(1, 0.5) add_transparency("#FF000080", 0.2)
Convert back from colors to values
col2value(r, g, b, col_fun)
col2value(r, g, b, col_fun)
r |
Red channel in |
g |
Green channel in |
b |
Blue channel in |
col_fun |
the color mapping function generated by |
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.
A vector of original numeric values.
Zuguang Gu <[email protected]>
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)
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)
Generate color mapping functions
colorRamp2(breaks, colors, transparency = 0, space = "LAB", hcl_palette = NULL, reverse = FALSE)
colorRamp2(breaks, colors, transparency = 0, space = "LAB", hcl_palette = NULL, reverse = FALSE)
breaks |
A vector of numeric break values. |
colors |
A vector of colors which correspond to values in |
transparency |
A single value in |
space |
Color space in which colors are interpolated. Value should be one of "RGB", "LAB", "XYZ", "sRGB", "LUV", see |
hcl_palette |
Name of the HCL palette. Value should be supported in |
reverse |
Whether should the colors in |
Colors are linearly interpolated according to the break values and corresponding colors through a certain color space. Values exceeding breaks will be assigned with corresponding maximum or minimum colors.
A function which accepts a vector of numeric values and returns interpolated colors.
col2value
converts back to the original values by providing the color mapping function generated by colorRamp2
.
col_fun = colorRamp2(c(-1, 0, 1), c("green", "white", "red")) col_fun(c(-2, -1, -0.5, 0, 0.5, 1, 2))
col_fun = colorRamp2(c(-1, 0, 1), c("green", "white", "red")) col_fun(c(-2, -1, -0.5, 0, 0.5, 1, 2))
Generate random colors
rand_color(n, hue = NULL, luminosity = "random", transparency = 0, friendly = FALSE)
rand_color(n, hue = NULL, luminosity = "random", transparency = 0, friendly = FALSE)
n |
Number of colors |
hue |
The hue of the generated color. You can use following default color name: |
luminosity |
it controls the luminosity of the generated color. The value should be a string containing |
transparency |
Transparency, numeric value between 0 and 1. |
friendly |
If it is true, light random colors will not be generated. |
The code is adapted from randomColor.js (https://github.com/davidmerfield/randomColor ).
Zuguang Gu <[email protected]>
plot(NULL, xlim = c(1, 10), ylim = c(1, 8), axes = FALSE, ann = FALSE) points(1:10, rep(1, 10), pch = 16, cex = 5, col = rand_color(10)) points(1:10, rep(2, 10), pch = 16, cex = 5, col = rand_color(10, luminosity = "bright")) points(1:10, rep(3, 10), pch = 16, cex = 5, col = rand_color(10, luminosity = "light")) points(1:10, rep(4, 10), pch = 16, cex = 5, col = rand_color(10, luminosity = "dark")) points(1:10, rep(5, 10), pch = 16, cex = 5, col = rand_color(10, hue = "red", luminosity = "bright")) points(1:10, rep(6, 10), pch = 16, cex = 5, col = rand_color(10, hue = "green", luminosity = "bright")) points(1:10, rep(7, 10), pch = 16, cex = 5, col = rand_color(10, hue = "blue", luminosity = "bright")) points(1:10, rep(8, 10), pch = 16, cex = 5, col = rand_color(10, hue = "monochrome", luminosity = "bright"))
plot(NULL, xlim = c(1, 10), ylim = c(1, 8), axes = FALSE, ann = FALSE) points(1:10, rep(1, 10), pch = 16, cex = 5, col = rand_color(10)) points(1:10, rep(2, 10), pch = 16, cex = 5, col = rand_color(10, luminosity = "bright")) points(1:10, rep(3, 10), pch = 16, cex = 5, col = rand_color(10, luminosity = "light")) points(1:10, rep(4, 10), pch = 16, cex = 5, col = rand_color(10, luminosity = "dark")) points(1:10, rep(5, 10), pch = 16, cex = 5, col = rand_color(10, hue = "red", luminosity = "bright")) points(1:10, rep(6, 10), pch = 16, cex = 5, col = rand_color(10, hue = "green", luminosity = "bright")) points(1:10, rep(7, 10), pch = 16, cex = 5, col = rand_color(10, hue = "blue", luminosity = "bright")) points(1:10, rep(8, 10), pch = 16, cex = 5, col = rand_color(10, hue = "monochrome", luminosity = "bright"))