Skip to content

Commit 8433e1a

Browse files
committed
allow numeric colour
1 parent b0f8843 commit 8433e1a

File tree

4 files changed

+48
-4
lines changed

4 files changed

+48
-4
lines changed

R/properties.R

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ property_fontface <- function(allow_null = TRUE, default = NULL) {
5858
S7::new_property(
5959
class = class,
6060
validator = validator,
61-
default = NULL
61+
default = default
6262
)
6363
}
6464

@@ -68,3 +68,34 @@ property_nullable <- function(class = S7::class_any, ...) {
6868
...
6969
)
7070
}
71+
72+
property_colour <- function(
73+
allow_null = TRUE,
74+
pattern = FALSE,
75+
default = NULL
76+
) {
77+
# TODO: remove numeric option for editioning
78+
class <- S7::new_union(
79+
S7::class_character, # Hex codes and colour names, e.g. #FF000 or "red"
80+
S7::class_logical, # For allowing NA, which means 'transparent'
81+
S7::class_numeric # For `grDevices::palette()` indexing
82+
)
83+
if (isTRUE(pattern)) {
84+
class <- S7::new_union(class, S7::new_S3_class("GridPattern"))
85+
}
86+
if (isTRUE(allow_null)) {
87+
class <- S7::new_union(class, NULL)
88+
}
89+
validator <- function(value) {
90+
if (is.numeric(value) && !is_integerish(value)) {
91+
return("cannot be a decimal number, but could be an integer.")
92+
}
93+
character()
94+
}
95+
S7::new_property(
96+
class = class,
97+
validator = validator,
98+
default = default
99+
)
100+
}
101+

R/theme-elements.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ element_blank <- S7::new_class(
103103
#' @include properties.R
104104
#' @include margins.R
105105
element_props <- list(
106-
fill = property_nullable(S7::class_character | S7::new_S3_class("GridPattern") | S7::class_logical),
107-
colour = property_nullable(S7::class_character | S7::class_logical),
106+
fill = property_colour(pattern = TRUE),
107+
colour = property_colour(pattern = FALSE),
108108
family = property_nullable(S7::class_character),
109109
hjust = property_nullable(S7::class_numeric),
110110
vjust = property_nullable(S7::class_numeric),
@@ -119,7 +119,7 @@ element_props <- list(
119119
linejoin = property_choice(c("round", "mitre", "bevel"), allow_null = TRUE),
120120
shape = property_nullable(S7::class_numeric | S7::class_character),
121121
arrow = property_nullable(S7::new_S3_class("arrow") | S7::class_logical),
122-
arrow.fill = property_nullable(S7::class_character | S7::class_logical),
122+
arrow.fill = property_colour(pattern = FALSE),
123123
debug = property_boolean(allow_null = TRUE, default = NULL),
124124
inherit.blank = property_boolean(default = FALSE),
125125
# These are reserved for future use

tests/testthat/_snaps/theme.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,15 @@
106106
Error in `as_margin()`:
107107
! `margin` must be a <margin> class, not a number.
108108

109+
---
110+
111+
Code
112+
element_text(colour = sqrt(2))
113+
Condition
114+
Error:
115+
! <ggplot2::element_text> object properties are invalid:
116+
- @colour cannot be a decimal number, but could be an integer.
117+
109118
# Theme validation behaves as expected
110119

111120
The `aspect.ratio` theme element must be a <numeric/integer> object.

tests/testthat/test-theme.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,10 @@ test_that("element_text throws appropriate conditions", {
561561
element_text(margin = 5),
562562
error = TRUE
563563
)
564+
expect_snapshot(
565+
element_text(colour = sqrt(2)),
566+
error = TRUE
567+
)
564568
})
565569

566570
test_that("Theme validation behaves as expected", {

0 commit comments

Comments
 (0)