Skip to content

Commit

Permalink
Make colour/2 a top-level function
Browse files Browse the repository at this point in the history
This makes it more obvious that this function's effect is permanent
until the next invocation.
  • Loading branch information
camelpunch committed Oct 19, 2024
1 parent e7c1b00 commit 6e154e9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 24 deletions.
13 changes: 4 additions & 9 deletions lib/mudbrick.ex
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,11 @@ defmodule Mudbrick do
|> ContentStream.add(ContentStream.Td, tx: x, ty: y)
end

def text(context, text, opts \\ []) do
context =
case Keyword.get(opts, :colour) do
{r, g, b} ->
ContentStream.add(context, ContentStream.Rg, r: r, g: g, b: b)

_ ->
context
end
def colour(context, {r, g, b}) do
ContentStream.add(context, ContentStream.Rg, r: r, g: g, b: b)
end

def text(context, text, opts \\ []) do
ContentStream.write_text(context, text, opts)
end

Expand Down
12 changes: 5 additions & 7 deletions test/mudbrick/font_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,11 @@ defmodule Mudbrick.FontTest do
)
|> font(:helvetica, size: 10)
|> text("black and ")
|> text(
"""
red
text\
""",
colour: {1.0, 0.0, 0.0}
)
|> colour({1.0, 0.0, 0.0})
|> text("""
red
text\
""")

assert show(content_stream) =~
"""
Expand Down
15 changes: 7 additions & 8 deletions test/mudbrick_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ defmodule MudbrickTest do
)
|> text_position(200, 700)
|> font(:bodoni, size: 14)
|> text("CO₂ ", colour: {1, 0, 0}, align: :right)
|> text(
"""
is Carbon Dioxide
and HNO₃ is Nitric Acid
""",
colour: {0, 0, 0}
)
|> colour({1, 0, 0})
|> text("CO₂ ", align: :right)
|> colour({0, 0, 0})
|> text("""
is Carbon Dioxide
and HNO₃ is Nitric Acid
""")
|> text("wide stuff", align: :right)
|> text("wider stuff", align: :right)
|> text("z", align: :right)
Expand Down

0 comments on commit 6e154e9

Please sign in to comment.