Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions lib/matplotex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,6 @@ defmodule Matplotex do
bar(pos, values, width, [])
end

def bar(pos, values, width, opts) do
BarChart.create(%Figure{axes: %BarChart{}}, {pos, values, width}, opts)
end

@doc """
Adds an additional dataset to a bar plot in the given `%Figure{}`.

Expand Down Expand Up @@ -251,6 +247,13 @@ defmodule Matplotex do
|> M.bar(0, values2, width, label: "Dataset2", color: "#D3D3D3")
|> M.bar(width, values2, width, label: "Dataset3", color: "green")
"""

def bar(%Figure{} = figure, values, width, opts), do: bar(figure, width, values, width, opts)

def bar(pos, values, width, opts) do
BarChart.create(%Figure{axes: %BarChart{}}, {pos, values, width}, opts)
end

def bar(%Figure{} = figure, pos, values, width, opts) do
figure
|> show_legend()
Expand Down Expand Up @@ -752,6 +755,10 @@ defmodule Matplotex do
Figure.show_legend(figure)
end

def hide_legend(figure) do
Figure.hide_legend(figure)
end

def set_options(figure, opts) do
PlotOptions.set_options_in_figure(figure, opts)
end
Expand Down
20 changes: 8 additions & 12 deletions lib/matplotex/colorscheme/blender.ex
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
defmodule Matplotex.Colorscheme.Blender do
@moduledoc false
alias Matplotex.Colorscheme.Rgb
@moduledoc false
alias Matplotex.Colorscheme.Rgb

@rgb_fields [:red, :green, :blue, :alpha]

@type color :: %Rgb{
red: :float,
green: :float,
blue: :float,
alpha: :float
}

red: :float,
green: :float,
blue: :float,
alpha: :float
}

def mix(color1, color2, weight \\ 0.5) do

p = weight
w = p * 2 - 1
a = color1.alpha - color2.alpha
Expand All @@ -30,10 +28,10 @@ alias Matplotex.Colorscheme.Rgb
alpha = get_alpha(color1) * p + get_alpha(color2) * (1 - p)
rgb(r, g, b, alpha)
end

defdelegate rgb(red, green, blue), to: Rgb
defdelegate rgb(red, green, blue, alpha), to: Rgb


@doc """
Gets the `:red` property of the color.
"""
Expand Down Expand Up @@ -87,6 +85,4 @@ alias Matplotex.Colorscheme.Rgb
end

defp cast_color_by_attribute(color, attribute) when attribute in @rgb_fields, do: color


end
15 changes: 8 additions & 7 deletions lib/matplotex/colorscheme/colormap.ex
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
defmodule Matplotex.Colorscheme.Colormap do
@moduledoc false
@moduledoc false
defstruct [:color, :offset, opacity: 1]

def viridis do
["#fde725","#21918c","#3b528b","#440154"]
["#fde725", "#21918c", "#3b528b", "#440154"]
end

def plasma do
["#F7E425","#ED6925", "#9C179E", "#0C0786" ]
["#F7E425", "#ED6925", "#9C179E", "#0C0786"]
end

def inferno do
["#FCFFA4","#F56C3E","#B12A90","#000004"]
["#FCFFA4", "#F56C3E", "#B12A90", "#000004"]
end

def magma do
["#FCFDBF","#FB8861", "#B73779", "#000004"]
["#FCFDBF", "#FB8861", "#B73779", "#000004"]
end

def fetch_cmap(cmap) when is_binary(cmap), do: cmap |> String.to_atom() |> fetch_cmap()

def fetch_cmap(cmap) do
apply(__MODULE__, cmap, []) |> make_colormap()
end

def make_colormap(colors) do
size = length(colors)

colors
|> Enum.with_index()
|> Enum.map(&colormap(&1, size))
Expand All @@ -35,6 +38,4 @@ defmodule Matplotex.Colorscheme.Colormap do
offset = idx / size * 100
%__MODULE__{color: color, offset: offset}
end


end
36 changes: 22 additions & 14 deletions lib/matplotex/colorscheme/garner.ex
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
defmodule Matplotex.Colorscheme.Garner do
@moduledoc false
alias Matplotex.Colorscheme.Rgb
alias Matplotex.Colorscheme.Blender
alias Matplotex.InputError
@moduledoc false
alias Matplotex.Colorscheme.Rgb
alias Matplotex.Colorscheme.Blender
alias Matplotex.InputError

defstruct [:range, :color_cue, :cmap, :preceeding, :minor, :major, :final]
defstruct [:range, :color_cue, :cmap, :preceeding, :minor, :major, :final]

def garn_color({min, max} = range, point, cmap) when max != min do
cue = (point - min) / (max - min)

cmap
|> make_from_cmap()
|> put_range(range, cue)
Expand All @@ -16,8 +17,8 @@ alias Matplotex.InputError

defp make_from_cmap(cmap) do
cmap
|>to_rgb()
|>place_edges()
|> to_rgb()
|> place_edges()
end

defp put_range(%__MODULE__{} = garner, range, cue) do
Expand All @@ -28,22 +29,29 @@ alias Matplotex.InputError
Enum.map(color_map, &Rgb.from_cmap!(&1))
end

defp place_edges([preceeding, minor, major,final]) do
%__MODULE__{preceeding: preceeding.color, minor: minor.color, major: major.color, final: final.color}
defp place_edges([preceeding, minor, major, final]) do
%__MODULE__{
preceeding: preceeding.color,
minor: minor.color,
major: major.color,
final: final.color
}
end

defp place_edges(_) do
raise InputError, message: "Invalid colormap"
end

defp point_color(%__MODULE__{color_cue: cue, preceeding: preceeding, minor: minor}) when cue < minor do
minor|> Blender.mix(preceeding, cue)|> Rgb.to_string()
defp point_color(%__MODULE__{color_cue: cue, preceeding: preceeding, minor: minor})
when cue < minor do
minor |> Blender.mix(preceeding, cue) |> Rgb.to_string()
end

defp point_color(%__MODULE__{color_cue: cue, minor: minor, major: major}) when cue < major do
major|> Blender.mix(minor, cue)|> Rgb.to_string()
major |> Blender.mix(minor, cue) |> Rgb.to_string()
end

defp point_color(%__MODULE__{color_cue: cue, major: major, final: final}) when cue >= major do
final|> Blender.mix(major)|> Rgb.to_string()
final |> Blender.mix(major) |> Rgb.to_string()
end
end
end
88 changes: 47 additions & 41 deletions lib/matplotex/colorscheme/rgb.ex
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
defmodule Matplotex.Colorscheme.Rgb do
@moduledoc false
alias Matplotex.Colorscheme.Colormap
alias Matplotex.Colorscheme.Colormap

defstruct [
red: 0.0, # 0-255
green: 0.0, # 0-255
blue: 0.0, # 0-255
alpha: 1.0 # 0-1
# 0-255
red: 0.0,
# 0-255
green: 0.0,
# 0-255
blue: 0.0,
# 0-1
alpha: 1.0
]

def rgb(red, green, blue, alpha\\1.0)
def rgb({red, :percent}, {green, :percent}, {blue, :percent}, alpha) do
def rgb(red, green, blue, alpha \\ 1.0)

def rgb({red, :percent}, {green, :percent}, {blue, :percent}, alpha) do
rgb(red * 255, green * 255, blue * 255, alpha)
end
def rgb(red, green, blue, alpha) do

def rgb(red, green, blue, alpha) do
%__MODULE__{
red: cast(red, :red),
green: cast(green, :green),
Expand All @@ -21,65 +28,64 @@ alias Matplotex.Colorscheme.Colormap
}
end

def to_string(struct, type\\nil)
def to_string(struct, type \\ nil)

def to_string(struct, nil) do
type =
case struct.alpha do
1.0 -> :hex
_ -> :rgba
end

def to_string(struct, :nil) do
type = case struct.alpha do
1.0 -> :hex
_ -> :rgba
end
to_string(struct, type)
end

def to_string(%__MODULE__{red: r, green: g, blue: b, alpha: alpha}, :rgba) do
"rgba(#{round(r)}, #{round(g)}, #{round(b)}, #{alpha})"
end

def to_string(%__MODULE__{red: r, green: g, blue: b, alpha: 1.0}, :hex) do
"#" <> to_hex(r) <> to_hex(g) <> to_hex(b)
end

def cast(value, field) when field in [:red, :green, :blue] do
value/1
(value / 1)
|> min(255.0)
|> max(0.0)
end

def cast(value, :alpha) do
value/1
(value / 1)
|> min(1.0)
|> max(0.0)
end

defp to_hex(value) when is_float(value), do:
to_hex(round(value))
defp to_hex(value) when value < 16, do:
"0" <> Integer.to_string(value, 16)
defp to_hex(value) when is_integer(value), do:
Integer.to_string(value, 16)

def from_hex!(input) do
{:ok, color} = from_hex(input)
color
end
defp to_hex(value) when is_float(value), do: to_hex(round(value))
defp to_hex(value) when value < 16, do: "0" <> Integer.to_string(value, 16)
defp to_hex(value) when is_integer(value), do: Integer.to_string(value, 16)

def from_cmap!(%Colormap{color: color} = cmap) do
%Colormap{cmap | color: from_hex!(color) }
end
def from_hex!(input) do
{:ok, color} = from_hex(input)
color
end

def from_hex("#" <> <<r :: binary-size(2), g :: binary-size(2), b :: binary-size(2)>>) do
{:ok, rgb(parse_hex(r), parse_hex(g), parse_hex(b))}
end
def from_hex("#" <> <<r :: binary-size(1), g :: binary-size(1), b :: binary-size(1)>>) do
{:ok, rgb(parse_hex(r <> r), parse_hex(g <> g), parse_hex(b <> b))}
end
def from_cmap!(%Colormap{color: color} = cmap) do
%Colormap{cmap | color: from_hex!(color)}
end

defp parse_hex(s), do: String.to_integer(s, 16)
def from_hex("#" <> <<r::binary-size(2), g::binary-size(2), b::binary-size(2)>>) do
{:ok, rgb(parse_hex(r), parse_hex(g), parse_hex(b))}
end

def from_hex("#" <> <<r::binary-size(1), g::binary-size(1), b::binary-size(1)>>) do
{:ok, rgb(parse_hex(r <> r), parse_hex(g <> g), parse_hex(b <> b))}
end

defp parse_hex(s), do: String.to_integer(s, 16)
end


defimpl String.Chars, for: CssColors.RGB do
def to_string(struct) do
Matplotex.Colorscheme.Rgb.to_string(struct)
end
def to_string(struct) do
Matplotex.Colorscheme.Rgb.to_string(struct)
end
end
2 changes: 1 addition & 1 deletion lib/matplotex/element.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ defmodule Matplotex.Element do

defimpl String.Chars, for: __MODULE__ do
def to_string(%module{} = element) do
module.assemble(element)
module.assemble(element)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotex/element/cmap.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule Matplotex.Element.Cmap do
@moduledoc false
@moduledoc false
alias Matplotex.Element.Rect
alias Matplotex.Element
use Element
Expand Down
3 changes: 3 additions & 0 deletions lib/matplotex/figure.ex
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ defmodule Matplotex.Figure do
def show_legend(%__MODULE__{axes: %module{} = axes} = figure),
do: %{figure | axes: module.show_legend(axes)}

def hide_legend(%__MODULE__{axes: %module{} = axes} = figure),
do: %{figure | axes: module.hide_legend(axes)}

def set_figure_size(%__MODULE__{margin: margin, axes: axes} = figure, {fwidth, fheight} = fsize) do
frame_size = {fwidth - fwidth * 2 * margin, fheight - fheight * 2 * margin}

Expand Down
Loading
Loading