From de490a4a1c34873d14eb9fc1c84d75702302529e Mon Sep 17 00:00:00 2001 From: Mikael Fangel <34864484+MikaelFangel@users.noreply.github.com> Date: Mon, 3 Jun 2024 19:08:34 +0000 Subject: [PATCH] color: generate functions using a macro (#4) * color: change to generating the function via a macro * version: bump patch * color: simplify doc generation --- README.md | 2 +- lib/catppuccin/color.ex | 175 +++------------------------------------- mix.exs | 2 +- 3 files changed, 11 insertions(+), 168 deletions(-) diff --git a/README.md b/README.md index 5024c6cd6..757b846e8 100644 --- a/README.md +++ b/README.md @@ -799,7 +799,7 @@ ```elixir def deps do [ - {:catppuccin, github: "MikaelFangel/elixir", tag: "v0.1.1"} + {:catppuccin, github: "MikaelFangel/elixir", tag: "v0.1.2"} ] end ``` diff --git a/lib/catppuccin/color.ex b/lib/catppuccin/color.ex index 9ed3fb18f..c59424476 100644 --- a/lib/catppuccin/color.ex +++ b/lib/catppuccin/color.ex @@ -5,6 +5,8 @@ defmodule Catppuccin.Color do alias Catppuccin.Flavor + @color_names ~w(rosewater flamingo pink mauve red maroon peach yellow green teal sky sapphire blue lavender text subtext1 subtext0 overlay2 overlay1 overlay0 surface2 surface1 surface0 base mantle crust) + @typedoc """ Data structure representing a color i hex, rgb, and hsl. """ @@ -97,172 +99,13 @@ defmodule Catppuccin.Color do Map.get(colors, color_name) end - @doc """ - Returns the color data for the rosewater color for a given flavor. - - ## Examples - - iex> Catppuccin.latte |> Catppuccin.Color.rosewater - %Catppuccin.Color{ - name: "Rosewater", - order: 0, - hex: "#dc8a78", - rgb: {220, 138, 120}, - hsl: {10.799999999999995, 0.5882352941176472, 0.6666666666666667}, - accent: true - } + for color_name <- @color_names do + Module.register_attribute(__MODULE__, :doc, accumulate: true) - """ - @spec rosewater({atom(), Flavor.t()}) :: Color.t() | nil - def rosewater(flavor), do: color_accessor(:rosewater, flavor) - - @doc """ - Returns the color data for the flamingo color for a given flavor. - """ - @spec flamingo({atom(), Flavor.t()}) :: Color.t() | nil - def flamingo(flavor), do: color_accessor(:flamingo, flavor) - - @doc """ - Returns the color data for the pink color for a given flavor. - """ - @spec pink({atom(), Flavor.t()}) :: Color.t() | nil - def pink(flavor), do: color_accessor(:pink, flavor) - - @doc """ - Returns the color data for the mauve color for a given flavor. - """ - @spec mauve({atom(), Flavor.t()}) :: Color.t() | nil - def mauve(flavor), do: color_accessor(:mauve, flavor) - - @doc """ - Returns the color data for the red color for a given flavor. - """ - @spec red({atom(), Flavor.t()}) :: Color.t() | nil - def red(flavor), do: color_accessor(:red, flavor) - - @doc """ - Returns the color data for the maroon color for a given flavor. - """ - @spec maroon({atom(), Flavor.t()}) :: Color.t() | nil - def maroon(flavor), do: color_accessor(:maroon, flavor) + @doc """ + Returns the `#{color_name}` color for the given `flavor`. + """ - @doc """ - Returns the color data for the peach color for a given flavor. - """ - @spec peach({atom(), Flavor.t()}) :: Color.t() | nil - def peach(flavor), do: color_accessor(:peach, flavor) - - @doc """ - Returns the color data for the yellow color for a given flavor. - """ - @spec yellow({atom(), Flavor.t()}) :: Color.t() | nil - def yellow(flavor), do: color_accessor(:yellow, flavor) - - @doc """ - Returns the color data for the green color for a given flavor. - """ - @spec green({atom(), Flavor.t()}) :: Color.t() | nil - def green(flavor), do: color_accessor(:green, flavor) - - @doc """ - Returns the color data for the teal color for a given flavor. - """ - @spec teal({atom(), Flavor.t()}) :: Color.t() | nil - def teal(flavor), do: color_accessor(:teal, flavor) - - @doc """ - Returns the color data for the sky color for a given flavor. - """ - @spec sky({atom(), Flavor.t()}) :: Color.t() | nil - def sky(flavor), do: color_accessor(:sky, flavor) - - @doc """ - Returns the color data for the sapphire color for a given flavor. - """ - @spec sapphire({atom(), Flavor.t()}) :: Color.t() | nil - def sapphire(flavor), do: color_accessor(:sapphire, flavor) - - @doc """ - Returns the color data for the blue color for a given flavor. - """ - @spec blue({atom(), Flavor.t()}) :: Color.t() | nil - def blue(flavor), do: color_accessor(:blue, flavor) - - @doc """ - Returns the color data for the lavender color for a given flavor. - """ - @spec lavender({atom(), Flavor.t()}) :: Color.t() | nil - def lavender(flavor), do: color_accessor(:lavender, flavor) - - @doc """ - Returns the color data for the test color for a given flavor. - """ - @spec text({atom(), Flavor.t()}) :: Color.t() | nil - def text(flavor), do: color_accessor(:text, flavor) - - @doc """ - Returns the color data for the subtext1 color for a given flavor. - """ - @spec subtext1({atom(), Flavor.t()}) :: Color.t() | nil - def subtext1(flavor), do: color_accessor(:subtext1, flavor) - - @doc """ - Returns the color data for the subtext0 color for a given flavor. - """ - @spec subtext0({atom(), Flavor.t()}) :: Color.t() | nil - def subtext0(flavor), do: color_accessor(:subtext0, flavor) - - @doc """ - Returns the color data for the overlay2 color for a given flavor. - """ - @spec overlay2({atom(), Flavor.t()}) :: Color.t() | nil - def overlay2(flavor), do: color_accessor(:overlay2, flavor) - - @doc """ - Returns the color data for the overlay1 color for a given flavor. - """ - @spec overlay1({atom(), Flavor.t()}) :: Color.t() | nil - def overlay1(flavor), do: color_accessor(:overlay1, flavor) - - @doc """ - Returns the color data for the overlay0 color for a given flavor. - """ - @spec overlay0({atom(), Flavor.t()}) :: Color.t() | nil - def overlay0(flavor), do: color_accessor(:overlay0, flavor) - - @doc """ - Returns the color data for the surface2 color for a given flavor. - """ - @spec surface2({atom(), Flavor.t()}) :: Color.t() | nil - def surface2(flavor), do: color_accessor(:surface2, flavor) - - @doc """ - Returns the color data for the surface1 color for a given flavor. - """ - @spec surface1({atom(), Flavor.t()}) :: Color.t() | nil - def surface1(flavor), do: color_accessor(:surface1, flavor) - - @doc """ - Returns the color data for the surface0 color for a given flavor. - """ - @spec surface0({atom(), Flavor.t()}) :: Color.t() | nil - def surface0(flavor), do: color_accessor(:surface0, flavor) - - @doc """ - Returns the color data for the base color for a given flavor. - """ - @spec base({atom(), Flavor.t()}) :: Color.t() | nil - def base(flavor), do: color_accessor(:base, flavor) - - @doc """ - Returns the color data for the mantle color for a given flavor. - """ - @spec mantle({atom(), Flavor.t()}) :: Color.t() | nil - def mantle(flavor), do: color_accessor(:mantle, flavor) - - @doc """ - Returns the color data for the crust color for a given flavor. - """ - @spec crust({atom(), Flavor.t()}) :: Color.t() | nil - def crust(flavor), do: color_accessor(:crust, flavor) + def unquote(:"#{color_name}")(flavor), do: color_accessor(unquote(:"#{color_name}"), flavor) + end end diff --git a/mix.exs b/mix.exs index f143c1876..74ce6366e 100644 --- a/mix.exs +++ b/mix.exs @@ -4,7 +4,7 @@ defmodule Catppuccin.MixProject do def project do [ app: :catppuccin, - version: "0.1.1", + version: "0.1.2", elixir: "~> 1.15", start_permanent: Mix.env() == :prod, deps: deps(),