Skip to content

Commit

Permalink
new naming model
Browse files Browse the repository at this point in the history
  • Loading branch information
mtennekes committed Sep 9, 2024
1 parent 4cdf43e commit 2f3a6d9
Show file tree
Hide file tree
Showing 10 changed files with 308 additions and 229 deletions.
84 changes: 42 additions & 42 deletions R/c4a_gui.R
Original file line number Diff line number Diff line change
Expand Up @@ -340,31 +340,31 @@ c4a_gui = function(type = "cat", n = NA, series = "all") {
#shiny::plotOutput("namePlot", height = "600px", width = "1000px"))),
shiny::fluidRow(
shiny::column(width = 12,
shiny::sliderInput("nameAlpha", "Clarity level", min = .5, max = 3, step = .5, value = 2),
shiny::actionButton("showWeights", "Show weight calibration"))),
shiny::conditionalPanel(
condition = "input.showWeights % 2 == 1",
shiny::fluidRow(
shiny::column(width = 3,
shiny::sliderInput("w_1", "Green", min = 0, max = 1.2, step = 0.01, value = unname(.C4A$boynton_weights[1])),
shiny::sliderInput("w_2", "Blue", min = 0, max = 1.2, step = 0.01, value = unname(.C4A$boynton_weights[2])),
shiny::sliderInput("w_3", "Purple", min = 0, max = 1.2, step = 0.01, value = unname(.C4A$boynton_weights[3])),
shiny::sliderInput("w_4", "Pink", min = 0, max = 1.2, step = 0.01, value = unname(.C4A$boynton_weights[4]))
),
shiny::column(width = 3,
shiny::sliderInput("w_5", "Yellow", min = 0, max = 1.2, step = 0.01, value = unname(.C4A$boynton_weights[5])),
shiny::sliderInput("w_6", "Brown", min = 0, max = 1.2, step = 0.01, value = unname(.C4A$boynton_weights[6])),
shiny::sliderInput("w_7", "Orange", min = 0, max = 1.2, step = 0.01, value = unname(.C4A$boynton_weights[7])),
shiny::sliderInput("w_8", "Red", min = 0, max = 1.2, step = 0.01, value = unname(.C4A$boynton_weights[8]))
),
shiny::column(width = 3,
shiny::sliderInput("w_9", "White", min = 0, max = 1.2, step = 0.01, value = unname(.C4A$boynton_weights[9])),
shiny::sliderInput("w_10", "Gray", min = 0, max = 1.2, step = 0.01, value = unname(.C4A$boynton_weights[10])),
shiny::sliderInput("w_11", "Black", min = 0, max = 1.2, step = 0.01, value = unname(.C4A$boynton_weights[11])),
shiny::actionButton("w_do", "Update naming data")
)
)
)
shiny::sliderInput("nameAlpha", "Clarity level", min = .5, max = .C4A$naming_softmax$a + 2, step = .5, value = .C4A$naming_softmax$a))),
#shiny::actionButton("showWeights", "Show weight calibration"))),
# shiny::conditionalPanel(
# condition = "input.showWeights % 2 == 1",
# shiny::fluidRow(
# shiny::column(width = 3,
# shiny::sliderInput("w_1", "Green", min = 0, max = 1.2, step = 0.01, value = unname(.C4A$boynton_weights[1])),
# shiny::sliderInput("w_2", "Blue", min = 0, max = 1.2, step = 0.01, value = unname(.C4A$boynton_weights[2])),
# shiny::sliderInput("w_3", "Purple", min = 0, max = 1.2, step = 0.01, value = unname(.C4A$boynton_weights[3])),
# shiny::sliderInput("w_4", "Pink", min = 0, max = 1.2, step = 0.01, value = unname(.C4A$boynton_weights[4]))
# ),
# shiny::column(width = 3,
# shiny::sliderInput("w_5", "Yellow", min = 0, max = 1.2, step = 0.01, value = unname(.C4A$boynton_weights[5])),
# shiny::sliderInput("w_6", "Brown", min = 0, max = 1.2, step = 0.01, value = unname(.C4A$boynton_weights[6])),
# shiny::sliderInput("w_7", "Orange", min = 0, max = 1.2, step = 0.01, value = unname(.C4A$boynton_weights[7])),
# shiny::sliderInput("w_8", "Red", min = 0, max = 1.2, step = 0.01, value = unname(.C4A$boynton_weights[8]))
# ),
# shiny::column(width = 3,
# shiny::sliderInput("w_9", "White", min = 0, max = 1.2, step = 0.01, value = unname(.C4A$boynton_weights[9])),
# shiny::sliderInput("w_10", "Gray", min = 0, max = 1.2, step = 0.01, value = unname(.C4A$boynton_weights[10])),
# shiny::sliderInput("w_11", "Black", min = 0, max = 1.2, step = 0.01, value = unname(.C4A$boynton_weights[11])),
# shiny::actionButton("w_do", "Update naming data")
# )
# )
# )

),
shiny::tabPanel("Contrast",
Expand All @@ -381,7 +381,7 @@ c4a_gui = function(type = "cat", n = NA, series = "all") {
shiny::checkboxGroupInput("cr_plot_opts", "", c("Sort" = "sort", "Show WCAG criteria" = "wcag", "Show equiluminant groups" = "equi"), selected = ""))),
shiny::fluidRow(
shiny::column(width = 6,
infoBoxUI("infoCR", "Contrast ratio between colors"),
infoBoxUI("infoCR", "Contrast ratios between colors"),
plotOverlay("table", width = "400px", height = "300px", "aniTable", click = "table_click")),
shiny::column(width = 6,
shiny::markdown("<br></br>
Expand Down Expand Up @@ -1183,7 +1183,7 @@ c4a_gui = function(type = "cat", n = NA, series = "all") {
#############################

output$cr_title = shiny::renderUI({
shiny::markdown(paste0("#### Contrast ratio with ", ifelse(input$dark, "black", "white"), " (background))"))
shiny::markdown(paste0("#### Contrast ratios with ", ifelse(input$dark, "black", "white"), " (background))"))
})

output$ex_plus = shiny::renderPlot({
Expand Down Expand Up @@ -1242,29 +1242,29 @@ c4a_gui = function(type = "cat", n = NA, series = "all") {
## Naming tab
#############################

shiny::observeEvent(input$w_do, {
shinyjs::runjs("$('#container').prop('disabled', true);")

w = c4a_options("boynton_weights")[[1]]
w[1:11] = vapply(1:11, function(i) {
input[[paste0("w_", i)]]
}, FUN.VALUE = numeric(1))
c4a_options(boynton_weights = w)
updatePlot(w)
get_trigger(!get_trigger()) # to update table
shinyjs::runjs("$('#container').prop('disabled', false);")

})
# shiny::observeEvent(input$w_do, {
# shinyjs::runjs("$('#container').prop('disabled', true);")
#
# w = c4a_options("boynton_weights")[[1]]
# w[1:11] = vapply(1:11, function(i) {
# input[[paste0("w_", i)]]
# }, FUN.VALUE = numeric(1))
# c4a_options(boynton_weights = w)
# updatePlot(w)
# get_trigger(!get_trigger()) # to update table
# shinyjs::runjs("$('#container').prop('disabled', false);")
#
# })

output$anaName = shiny::renderPlot({
pal = tab_vals$pal
c4a_plot_names2(pal, dark = input$dark, a = input$nameAlpha)
c4a_plot_names(pal, dark = input$dark, a = input$nameAlpha)
})

updatePlot <- function(result) {
output$anaName = shiny::renderPlot({
pal = tab_vals$pal
c4a_plot_names2(pal, dark = input$dark, a = input$nameAlpha)
c4a_plot_names(pal, dark = input$dark, a = input$nameAlpha)
})
}

Expand Down
111 changes: 79 additions & 32 deletions R/c4a_plot_names.R
Original file line number Diff line number Diff line change
@@ -1,46 +1,93 @@
c4a_plot_names = function(cols = NULL) {
grid::grid.newpage()
grid::pushViewport(grid::viewport(layout = grid::grid.layout(nrow = 3, ncol = 2, widths = c(0.5, 0.5))))
c4a_plot_names = function(cols = NULL, dark = FALSE, a = NA, th = NA) {

if (!is.null(cols)) {
names(cols) = seq_along(cols)
}
n = length(cols)
b = length(.C4A$naming_colors)

dfs = .C4A$name_data

vdf = expand.grid(row = 1:(2*n+3), col = 1:(2*b+3))
#vdf$name = c("Green", "Yellow", "Orange", "Blue", "Purple", "Brown", "Pink", "Red", "", "White", "Gray", "Black")

#vdf = vdf[vdf$name != "", ]

hex = c(cols, boynton)
col_pal_w = 0.07
col_marg1 = 0.01

m = as.data.frame(get_hcl_matrix(hex))
m$hex = hex
col_tot = (1 - col_pal_w - 2 * col_marg1) / b

col_bt_w = col_tot * 0.8
col_marg2 = col_tot * 0.2

Ls = c(40, 60, 80)

labels = c("Luminance 20 to 50",
"Luminance 50 to 70",
"Luminance 70 to 90",
"Luminance max")
row_bt_h = 0.3
row_marg1 = 0.02

m$L2 = sapply(m$L, function(l) Ls[which.min(abs(l - Ls))])
row_tot = (1 - row_bt_h - 2 * row_marg1) / n

row_pal_h = row_tot * 0.8
row_marg2 = row_tot * 0.2


m = c4a_naming_matrix_softmax(cols, a = a, th = th)

grid::grid.newpage()
grid::pushViewport(grid::viewport(layout = grid::grid.layout(nrow = (2*n+3), ncol = (2*b+3),
heights = grid::unit(c(row_marg1, row_bt_h, row_marg1, rep(c(row_pal_h, row_marg2), n)), units = "npc"),
widths = grid::unit(c(col_marg1, col_pal_w, col_marg1, rep(c(col_bt_w, col_marg2), b)), units = "npc"))))

bs = split(m, f = m$L2)
df = expand.grid(row = 1:2, col = 1:2)
bc = if (dark) "#000000" else "#FFFFFF"
fc = if (dark) "#FFFFFF" else "#000000"

for (i in 1:length(bs)) {
bsi = bs[[i]]
lm = paste0("L", names(bs)[i])
h = bsi$hex
names(h) = rownames(bsi)
grid::grid.rect(gp = grid::gpar(fill = bc, col = NA))

cellplot(df$row[i], df$col[i], {
plot_rgb(confusion_lines = FALSE, colors = h, L = lm, newpage = FALSE)
grid::grid.text(labels[i], x = .9, y = 0.05, just = "right")


for (i in 1:b) {
dfi = dfs[[i]]

cellplot(2, i*2+2, {
#grid::pushViewport(grid::viewport(width = grid::unit(1, "npc"), height = grid::unit(1, "npc"), clip = TRUE))
g = grid::pointsGrob(x = grid::unit(dfi$x, "npc"), y = grid::unit(dfi$y * .8 + .2, "npc"), size = grid::unit(0.15, "npc"), pch = 19, gp = grid::gpar(col = dfi$hex))
#grid::grid.rect()
grid::grid.draw(g)
grid::grid.text(names(.C4A$naming_colors)[i], y = .1, gp = grid::gpar(col = fc))
#grid::upViewport()
})
}
cellplot(df$row[4], df$col[4], {
h = m$hex
names(h) = rownames(m)

plot_rgb(confusion_lines = FALSE, colors = h, L = "L100", newpage = FALSE)
grid::grid.text(labels[4], x = .9, y = 0.05, just = "right")
})
for (j in 1:n) {
if (i == 1) {
cellplot(j*2+2, 2, {
grid::grid.text(j, x = 0.15, gp = grid::gpar(col = fc))
grid::grid.rect(x = 0.7, width = 0.6, gp = grid::gpar(fill = cols[j]))
})
}

v = sqrt(m[j,i])

#conflict_vert = sum(m[,i] >0) > 1
#conflict_hori = sum(m[j,] >0) > 1

if (i == 1) {
if (sum(m[j,] >0) > 1) {
cellplot(j*2+2, 4:(b*2+2), {
grid::grid.lines(y = grid::unit(c(0.5, 0.5), "npc"), gp = grid::gpar(col = fc, lwd = 2))
})
}
}
if (j == 1) {
if (sum(m[,i] >0) > 1) {
cellplot(4:(n*2+2), i*2+2, {
grid::grid.lines(x = grid::unit(c(0.5, 0.5), "npc"), gp = grid::gpar(col = fc, lwd = 2))
})
}
}

if (v > 0) {
cellplot(j*2+2, i*2+2, {
grid::grid.points(x = grid::unit(0.5, "npc"), y = grid::unit(0.5, "npc"), pch = 19, size = grid::unit(v, "lines"), gp = grid::gpar(col = fc))
})
}
}
}
grid::upViewport()
}
97 changes: 0 additions & 97 deletions R/c4a_plot_names2.R

This file was deleted.

Loading

0 comments on commit 2f3a6d9

Please sign in to comment.