Can I access the color in which a box is drawn?

Or more generally: Can I access other arguments of a function?

I have custom defined boxes where the stroke and fill have different colors. Now I want to define for example a command myline which, if called inside a box, draws a line for example of the color that is the box’s fill color.

Is this possible in Typst?

One way to I tried to do it was with

#show: mybox => {
  set myline(stroke: red)
  mybox
  }

The problem is, that then also the stroke of normal #line commands gets colored red, not just the one of the custom myline.

I think this could make sense, since the show rule does not only trigger in mybox, but triggers everywhere, since mybox is just a dummy variable name in this case, but I am not sure.

First of all, I want to say that the code you provided doesn’t work (it cannot be compiled successfully) because you used set myline(stroke: red), but currently, the set statement only works with official elements.

Regarding your question, I suspect that your mybox has a parameter for controlling the box’s fill. In this case, you just need to use set line(stroke: fill) inside mybox. If the fill color of your mybox is fixed, you can simply set the value in the above statement to the corresponding constant.

Here is an example:

#set page(width: auto, height: auto)


#let mybox(fill: none, content) = {
  set line(stroke: fill) if fill != none
  box(stroke: blue, inset: 0.5em, fill: fill, content)
}

#mybox(
  fill: red,
  [
    Hello
    #place(
      center,
      dy: -0.3em,
      line(length: 3em),
    )
  ],
)
Preview

image