Skip to content

Commit 3071b2c

Browse files
author
Mohammed Sadique
committed
cmap
1 parent 20c6e8c commit 3071b2c

5 files changed

Lines changed: 37 additions & 7 deletions

File tree

lib/matplotex/colorscheme/colormap.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ defmodule Matplotex.Colorscheme.Colormap do
1919
def fetch_cmap(cmap) when is_binary(cmap), do: cmap |> String.to_atom() |> fetch_cmap()
2020

2121
def fetch_cmap(cmap) do
22-
apply(Colormap, cmap, []) |> make_colormap()
22+
apply(__MODULE__, cmap, []) |> make_colormap()
2323
end
2424
def make_colormap(colors) do
2525
size = length(colors)

lib/matplotex/colorscheme/garner.ex

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
defmodule Matplotex.Colorscheme.Garner do
2+
alias Matplotex.Colorscheme.Rgb
23
alias Matplotex.Colorscheme.Blender
34
alias Matplotex.InputError
45
alias Matplotex.Colorscheme.Colormap
@@ -16,29 +17,34 @@ alias Matplotex.Colorscheme.Colormap
1617
defp fetch_from_cmap(cmap) do
1718
cmap
1819
|>Colormap.fetch_cmap()
20+
|>to_rgb()
1921
|>place_edges()
2022
end
2123

2224
defp put_range(%__MODULE__{} = garner, range, cue) do
2325
%__MODULE__{garner | range: range, color_cue: cue}
2426
end
2527

28+
defp to_rgb(color_map) do
29+
Enum.map(color_map, &Rgb.from_cmap!(&1))
30+
end
31+
2632
defp place_edges([preceeding, minor, major,final]) do
27-
%__MODULE__{preceeding: preceeding, minor: minor, major: major, final: final}
33+
%__MODULE__{preceeding: preceeding.color, minor: minor.color, major: major.color, final: final.color}
2834
end
2935
defp place_edges(_) do
3036
raise InputError, message: "Invalid colormap"
3137
end
3238

3339
defp point_color(%__MODULE__{color_cue: cue, preceeding: preceeding, minor: minor}) when cue < minor do
34-
Blender.mix(minor, preceeding, cue)
40+
minor|> Blender.mix(preceeding, cue)|> Rgb.to_string()
3541
end
3642

3743
defp point_color(%__MODULE__{color_cue: cue, minor: minor, major: major}) when cue < major do
38-
Blender.mix(minor, major, cue)
44+
major|> Blender.mix(minor, cue)|> Rgb.to_string()
3945
end
4046

4147
defp point_color(%__MODULE__{color_cue: cue, major: major, final: final}) when cue >= major do
42-
final
48+
final|> Blender.mix(major)|> Rgb.to_string()
4349
end
4450
end

lib/matplotex/colorscheme/rgb.ex

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
defmodule Matplotex.Colorscheme.Rgb do
22
@moduledoc false
3-
3+
alias Matplotex.Colorscheme.Colormap
44
defstruct [
55
red: 0.0, # 0-255
66
green: 0.0, # 0-255
@@ -56,8 +56,28 @@ defmodule Matplotex.Colorscheme.Rgb do
5656
defp to_hex(value) when is_integer(value), do:
5757
Integer.to_string(value, 16)
5858

59+
def from_hex!(input) do
60+
{:ok, color} = from_hex(input)
61+
color
62+
end
63+
64+
def from_cmap!(%Colormap{color: color} = cmap) do
65+
%Colormap{cmap | color: from_hex!(color) }
66+
end
67+
68+
def from_hex("#" <> <<r :: binary-size(2), g :: binary-size(2), b :: binary-size(2)>>) do
69+
{:ok, rgb(parse_hex(r), parse_hex(g), parse_hex(b))}
70+
end
71+
def from_hex("#" <> <<r :: binary-size(1), g :: binary-size(1), b :: binary-size(1)>>) do
72+
{:ok, rgb(parse_hex(r <> r), parse_hex(g <> g), parse_hex(b <> b))}
73+
end
74+
75+
defp parse_hex(s), do: String.to_integer(s, 16)
76+
77+
5978
end
6079

80+
6181
defimpl String.Chars, for: CssColors.RGB do
6282
def to_string(struct) do
6383
Matplotex.Colorscheme.Rgb.to_string(struct)

test/matplotex/colorscheme/garner_test.exs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ defmodule Matplotex.Colorscheme.GarnerTest do
66
test "garn the color for specific point in the number" do
77
colorset = [3,4,5,6,6,4,9]
88
color = Garner.garn_color(Enum.min_max(colorset), Enum.min(colorset), "viridis")
9-
assert is_binary(color)
9+
assert is_color(color)
1010
end
1111
end
12+
13+
defp is_color("#" <> <<_r :: binary-size(2), _g :: binary-size(2), _b :: binary-size(2)>>), do: true
14+
defp is_color(_), do: false
1215
end

test/matplotex/figure/areal/scatter_test.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ defmodule Matplotex.Figure.Areal.ScatterTest do
1212
assert %Figure{axes: %{data: {x, _y}, element: elements}} = Scatter.materialize(figure)
1313
assert Enum.count(elements, fn elem -> elem.type == "plot.marker" end) == length(x)
1414
end
15+
1516
end
1617

1718
describe "generate_ticks/2" do

0 commit comments

Comments
 (0)