| 1 |
codes <- c("#404040", "#8FBC8F", "#FFFFE0", "#7AC5CD", "#66CDAA", "#1E90FF", |
| 2 |
"#CDC0B0", "#CD0000", "#7A67EE", "#FFFACD") |
| 3 |
|
| 4 |
codes_alpha <- paste0(codes, c('', '1f', '44', 'fe', '', '9e', 'aa', 'b2', '0c', '21')) |
| 5 |
|
| 6 |
cols <- decode_colour(codes) |
| 7 |
alpha <- decode_colour(codes_alpha, TRUE)[, 'alpha'] |
| 8 |
cols_mod <- cols |
| 9 |
|
| 10 |
cols_lch <- convert_colour(cols, 'rgb' ,'lch') |
| 11 |
cols_lch_mod <- cols_lch |
| 12 |
|
| 13 |
test_that("setting channel works", { |
| 14 |
cols_mod[, 'g'] <- 1:10 |
| 15 |
expect_equal(set_channel(codes, 'g', 1:10), encode_colour(cols_mod)) |
| 16 |
|
| 17 |
cols_lch_mod[, 'l'] <- 1:10 |
| 18 |
expect_equal(set_channel(codes, 'l', 1:10, 'lch'), encode_colour(cols_lch_mod, from = 'lch')) |
| 19 |
|
| 20 |
expect_equal(set_channel(codes_alpha, 'alpha', (1:10)/10), encode_colour(cols, alpha = (1:10)/10)) |
| 21 |
}) |
| 22 |
|
| 23 |
test_that("adding channel works", { |
| 24 |
cols_mod[, 'r'] <- cols_mod[, 'r'] + 1:10 |
| 25 |
expect_equal(add_to_channel(codes, 'r', 1:10), encode_colour(cols_mod)) |
| 26 |
|
| 27 |
cols_lch_mod[, 'c'] <- cols_lch_mod[, 'c'] + 1:10 |
| 28 |
expect_equal(add_to_channel(codes, 'c', 1:10, 'lch'), encode_colour(cols_lch_mod, from = 'lch')) |
| 29 |
|
| 30 |
skip_on_os('linux') # Rounding difference on someones aarch64/ppc64le processor |
| 31 |
expect_equal(add_to_channel(codes_alpha, 'alpha', (1:10)/10), encode_colour(cols, alpha = alpha + (1:10)/10)) |
| 32 |
}) |
| 33 |
|
| 34 |
test_that("multiply channel works", { |
| 35 |
cols_mod[, 'b'] <- cols_mod[, 'b'] * 1:10 |
| 36 |
expect_equal(multiply_channel(codes, 'b', 1:10), encode_colour(cols_mod)) |
| 37 |
|
| 38 |
cols_lch_mod[, 'h'] <- cols_lch_mod[, 'h'] * 1:10 |
| 39 |
expect_equal(multiply_channel(codes, 'h', 1:10, 'lch'), encode_colour(cols_lch_mod, from = 'lch')) |
| 40 |
|
| 41 |
expect_equal(multiply_channel(codes_alpha, 'alpha', 1:10), encode_colour(cols, alpha = alpha * 1:10)) |
| 42 |
}) |
| 43 |
|
| 44 |
test_that("raising channel works", { |
| 45 |
cols_mod[, 'g'] <- ifelse(cols_mod[, 'g'] < 200, 200, cols_mod[, 'g']) |
| 46 |
expect_equal(raise_channel(codes, 'g', 200), encode_colour(cols_mod)) |
| 47 |
|
| 48 |
cols_lch_mod[, 'l'] <- ifelse(cols_lch_mod[, 'l'] < 50, 50, cols_lch_mod[, 'l']) |
| 49 |
expect_equal(raise_channel(codes, 'l', 50, 'lch'), encode_colour(cols_lch_mod, from = 'lch')) |
| 50 |
|
| 51 |
expect_equal(raise_channel(codes_alpha, 'alpha', 0.5), encode_colour(cols, alpha = ifelse(alpha < 0.5, 0.5, alpha))) |
| 52 |
}) |
| 53 |
|
| 54 |
test_that("capping channel works", { |
| 55 |
cols_mod[, 'g'] <- ifelse(cols_mod[, 'g'] > 200, 200, cols_mod[, 'g']) |
| 56 |
expect_equal(cap_channel(codes, 'g', 200), encode_colour(cols_mod)) |
| 57 |
|
| 58 |
cols_lch_mod[, 'l'] <- ifelse(cols_lch_mod[, 'l'] > 50, 50, cols_lch_mod[, 'l']) |
| 59 |
expect_equal(cap_channel(codes, 'l', 50, 'lch'), encode_colour(cols_lch_mod, from = 'lch')) |
| 60 |
|
| 61 |
expect_equal(cap_channel(codes_alpha, 'alpha', 0.5), encode_colour(cols, alpha = ifelse(alpha > 0.5, 0.5, alpha))) |
| 62 |
}) |