From 6e154e9d9015b670232ab641394fb1e632d178b0 Mon Sep 17 00:00:00 2001 From: Andrew Bruce Date: Sat, 19 Oct 2024 11:03:11 +0100 Subject: [PATCH] Make colour/2 a top-level function This makes it more obvious that this function's effect is permanent until the next invocation. --- lib/mudbrick.ex | 13 ++++--------- test/mudbrick/font_test.exs | 12 +++++------- test/mudbrick_test.exs | 15 +++++++-------- 3 files changed, 16 insertions(+), 24 deletions(-) diff --git a/lib/mudbrick.ex b/lib/mudbrick.ex index 599888e..91a4853 100644 --- a/lib/mudbrick.ex +++ b/lib/mudbrick.ex @@ -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 diff --git a/test/mudbrick/font_test.exs b/test/mudbrick/font_test.exs index a687a38..5d21eb9 100644 --- a/test/mudbrick/font_test.exs +++ b/test/mudbrick/font_test.exs @@ -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) =~ """ diff --git a/test/mudbrick_test.exs b/test/mudbrick_test.exs index c6d9fc2..7cd2939 100644 --- a/test/mudbrick_test.exs +++ b/test/mudbrick_test.exs @@ -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)