Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new: mixRGB() for additive color mixing #62

Merged
merged 2 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export(get_continuous_colors)
export(gg_simple_scatter)
export(giottoSankeyPlan)
export(mixHSV)
export(mixRGB)
export(pal_names)
export(plotHeatmap)
export(plotMetaDataCellsHeatmap)
Expand Down
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@

# GiottoVisuls 0.1.5 (TBD)
## new
- `mixHSV()` vectorized color mixing in HSV space for overlapping colors in plots
- `mixRGB()` vectorized additive mixing in RGB space
- `mixHSV()` vectorized color mixing in HSV space

# GiottoVisuals 0.1.4 (2024/01/25)
## bug fixes
Expand Down
58 changes: 56 additions & 2 deletions R/mixcolor.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,34 @@
#' plot(seq(255), y = rep(2, 255), col = a, pch = 15, ylim = c(0, 3), bg = "black")
#' points(seq(255), y = rep(1.5, 255), col = b, pch = 15)
#' points(seq(255), y = rep(1, 255), col = x, pch = 15)
#' @family colormixing functions
NULL

#' @name mixRGB
#' @title Colormixing in RGB space
#' @description
#' Vectorized additive mixing of colors in RGB space.
#' @param c1,c2 Colors 1 and 2. Accepts vector of hex color codes or an rgb matrix

Check notice

Code scanning / lintr

Lines should not be more than 80 characters. This line is 82 characters. Note

Lines should not be more than 80 characters. This line is 82 characters.
#' @param output either "hex" or "rgb". "hex" produces a vector of hex codes
#' for color mixtures. "rgb" returns the rgb matrix.
#' @family colormixing functions
#' @examples
#' # with black background
#' a <- GiottoVisuals::simple_palette_factory(c("green", "black"))(255)
#' b <- GiottoVisuals::simple_palette_factory(c("red", "black", "blue"))(255)
#' x <- mixRGB(a,b)
#'
#' op <- par(no.readonly = TRUE)
#' par(bg = "black")
#'
#' # plot input color vectors
#' plot(seq(255), y = rep(2, 255), col = a, pch = 15, ylim = c(0, 3), bg = "black")

Check notice

Code scanning / lintr

Lines should not be more than 80 characters. This line is 83 characters. Note

Lines should not be more than 80 characters. This line is 83 characters.
#' points(seq(255), y = rep(1.5, 255), col = b, pch = 15)
#' # plot mixture
#' points(seq(255), y = rep(1, 255), col = x, pch = 15)
#'
#' par(op)
NULL


# in HSV, hue can be thought of as a circular set of values from 0 to 1.
Expand Down Expand Up @@ -134,7 +160,7 @@
}

hex2hsv <- function(x) {
rgb2hsv(hex2rgb(x))
grDevices::rgb2hsv(hex2rgb(x))
}

#' @rdname mixHSV
Expand Down Expand Up @@ -168,7 +194,35 @@

switch(
output,
"hex" = return(hsv(i_h, i_s, i_v)),
"hex" = return(grDevices::hsv(i_h, i_s, i_v)),
"hsv" = return(rbind(i_h, i_s, i_v))
)
}



#' @rdname mixRGB
#' @export
mixRGB <- function(c1, c2, output = c("hex", "rgb")) {

Check notice

Code scanning / lintr

Variable and function name style should match snake_case or symbols. Note

Variable and function name style should match snake_case or symbols.

output <- match.arg(output, c("hex", "rgb"))

if (is.character(c1)) c1 <- hex2rgb(c1)
if (is.character(c2)) c2 <- hex2rgb(c2)

# matrix is expected
if (!is.matrix(c1) || !is.matrix(c2)) {
.gstop("c1 and c2 are expected to be 3 x n rgb matrices")
}

rgbm <- c1 + c2
rgbm_adj <- rgbm / max(rgbm)

grDevices::rgb(t(rgbm_adj))
}

Check notice

Code scanning / lintr

Trailing blank lines are superfluous. Note

Trailing blank lines are superfluous.

Check notice

Code scanning / lintr

Trailing blank lines are superfluous. Note

Trailing blank lines are superfluous.

Check notice

Code scanning / lintr

Trailing blank lines are superfluous. Note

Trailing blank lines are superfluous.

Check notice

Code scanning / lintr

Trailing blank lines are superfluous. Note

Trailing blank lines are superfluous.

Check notice

Code scanning / lintr

Trailing blank lines are superfluous. Note

Trailing blank lines are superfluous.

Check notice

Code scanning / lintr

Trailing blank lines are superfluous. Note

Trailing blank lines are superfluous.
1 change: 0 additions & 1 deletion R/package_imports.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#' @importFrom methods slot new
#' @importFrom methods setGeneric setMethod
#' @importFrom colorRamp2 colorRamp2
#' @importFrom grDevices hsv rgb2hsv
#' @import GiottoUtils
#' @import GiottoClass
#' @import ggplot2
Expand Down
5 changes: 5 additions & 0 deletions man/mixHSV.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions man/mixRGB.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading