-
Notifications
You must be signed in to change notification settings - Fork 0
/
colorjam-remap-colors.R
155 lines (144 loc) · 4.14 KB
/
colorjam-remap-colors.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#' Remap colors to a new colorjam preset
#'
#' Remap colors to a new colorjam preset, using existing preset
#' when available.
#'
#' This function is experimental, and is intended to convert a set
#' of categorical colors to a colorjam preset. When the existing
#' colors have attribute `"preset"` it is used to inform the starting
#' preset. The output of `rainbowJam()` includes the preset as
#' `attr(x, "preset")`.
#'
#' The HCL and HSL color conversions are blended together to improve
#' the imperfect result from either method alone. The end result
#' is imperfect, but better than the alternatives.
#'
#' @family colorjam hue warp
#'
#' @returns `character` vector of hexadecimal colors
#'
#' @examples
#' x <- rainbowJam(12)
#' x_new <- remap_colorjam_preset(x, preset="ryb2", do_plot=TRUE)
#'
#' x <- rainbowJam(12, preset="ryb2")
#' x_new <- remap_colorjam_preset(x, preset="dichromat2", do_plot=TRUE)
#'
#' x <- rainbowJam(12, preset="ryb")
#' x_new <- remap_colorjam_preset(x, preset="dichromat2", do_plot=TRUE)
#'
#' x <- rainbowJam(12, preset="dichromat2")
#' x_new <- remap_colorjam_preset(x, preset="ryb", do_plot=TRUE)
#'
#' x <- rainbowJam(12, preset="rgb")
#' x_new <- remap_colorjam_preset(x, preset="ryb", do_plot=TRUE)
#'
#' @export
remap_colorjam_preset <- function
(x,
preset=NULL,
preset_from=NULL,
do_plot=FALSE,
plot_debug=FALSE,
...)
{
# determine HCL hue
x_hcl <- jamba::col2hcl(x);
x_hcl_hue <- x_hcl["H",];
if (TRUE %in% plot_debug) {
print("x_hcl_hue (input):");print(x_hcl_hue);# debug
plot(x=seq_along(x), y=x_hcl_hue, ylim=c(0, 360), pch="1");
}
# accept preset encoded as attribute name
if (length(preset_from) == 0 && "preset" %in% names(attributes(x))) {
preset_from <- attr(x, "preset");
}
# optionally decode input preset
if (length(preset_from) > 0) {
x_hcl_hue <- round(h2hw(preset=preset_from,
h=x_hcl_hue), digits=1) %% 360
if (TRUE %in% plot_debug) {
print("x_hcl_hue (preset_from):");print(x_hcl_hue);# debug
points(x=seq_along(x), y=x_hcl_hue, pch="2");
}
}
# adjust hue
new_hcl_hue <- hw2h(preset=preset,
h=x_hcl_hue)
if (TRUE %in% plot_debug) {
print("new_hcl_hue (output):");print(new_hcl_hue);# debug
points(x=seq_along(x), y=new_hcl_hue, pch="3");
}
# determine HSL hue
x_hsl_hue <- hcl_to_hsl_hue(new_hcl_hue);
# determine HSL values
x_hsl <- jamba::col2hsl(x)
# create colors using HSL with new hue, existing S,L
new_hsl_colors <- jamba::hsl2col(H=x_hsl_hue,
S=x_hsl["S", ],
L=x_hsl["L", ],
alpha=x_hsl["alpha", ])
# create colors using HCL with new hue, existing C,L
new_hcl_colors <- jamba::hcl2col(H=new_hcl_hue,
C=x_hcl["C", ],
L=x_hcl["L", ],
alpha=x_hsl["alpha", ])
# blend them together
new_hsl_colors <- sapply(seq_along(new_hsl_colors), function(i){
blend_colors(x=c(new_hsl_colors[i],
new_hcl_colors[i],
new_hcl_colors[i]))
})
# optional plot
if (TRUE %in% do_plot && !TRUE %in% plot_debug) {
color_list <- list(
input=x,
output=new_hsl_colors);
if (length(preset_from) > 0) {
names(color_list)[1] <- preset_from;
}
names(color_list)[2] <- preset;
jamba::showColors(color_list)
}
return(new_hsl_colors)
}
#' Convert HCL hue to HSL hue
#'
#' @family colorjam hue warp
#'
#' @returns `numeric` hue with values in range `c(0, 360)`
#' intended for use with `jamba::hsl2col()`
#'
#' @param x `numeric` hue with values in range `c(0, 360)`
#' intended for use with `jamba::hcl2col()`
#' @param ... additional arguments are ignored.
#'
#' @export
hcl_to_hsl_hue <- function
(x,
...)
{
#
approx_degrees(preset="hcl_to_hsl",
h=x)
}
#' Convert HSL hue to HCL hue
#'
#' @family colorjam hue warp
#'
#' @returns `numeric` hue with values in range `c(0, 360)`
#' intended for use with `jamba::hcl2col()`
#'
#' @param x `numeric` hue with values in range `c(0, 360)`
#' intended for use with `jamba::hsl2col()`
#' @param ... additional arguments are ignored.
#'
#' @export
hsl_to_hcl_hue <- function
(x,
...)
{
#
hw2h(preset="hcl_to_hsl",
h=x);
}