11defmodule Matplotex.Colorscheme.Rgb do
22 @ moduledoc false
3- alias Matplotex.Colorscheme.Colormap
3+ alias Matplotex.Colorscheme.Colormap
4+
45 defstruct [
5- red: 0.0 , # 0-255
6- green: 0.0 , # 0-255
7- blue: 0.0 , # 0-255
8- alpha: 1.0 # 0-1
6+ # 0-255
7+ red: 0.0 ,
8+ # 0-255
9+ green: 0.0 ,
10+ # 0-255
11+ blue: 0.0 ,
12+ # 0-1
13+ alpha: 1.0
914 ]
1015
11- def rgb ( red , green , blue , alpha \\ 1.0 )
12- def rgb ( { red , :percent } , { green , :percent } , { blue , :percent } , alpha ) do
16+ def rgb ( red , green , blue , alpha \\ 1.0 )
17+
18+ def rgb ( { red , :percent } , { green , :percent } , { blue , :percent } , alpha ) do
1319 rgb ( red * 255 , green * 255 , blue * 255 , alpha )
1420 end
15- def rgb ( red , green , blue , alpha ) do
21+
22+ def rgb ( red , green , blue , alpha ) do
1623 % __MODULE__ {
1724 red: cast ( red , :red ) ,
1825 green: cast ( green , :green ) ,
@@ -21,65 +28,64 @@ alias Matplotex.Colorscheme.Colormap
2128 }
2229 end
2330
24- def to_string ( struct , type \\ nil )
31+ def to_string ( struct , type \\ nil )
32+
33+ def to_string ( struct , nil ) do
34+ type =
35+ case struct . alpha do
36+ 1.0 -> :hex
37+ _ -> :rgba
38+ end
2539
26- def to_string ( struct , :nil ) do
27- type = case struct . alpha do
28- 1.0 -> :hex
29- _ -> :rgba
30- end
3140 to_string ( struct , type )
3241 end
3342
3443 def to_string ( % __MODULE__ { red: r , green: g , blue: b , alpha: alpha } , :rgba ) do
3544 "rgba(#{ round ( r ) } , #{ round ( g ) } , #{ round ( b ) } , #{ alpha } )"
3645 end
46+
3747 def to_string ( % __MODULE__ { red: r , green: g , blue: b , alpha: 1.0 } , :hex ) do
3848 "#" <> to_hex ( r ) <> to_hex ( g ) <> to_hex ( b )
3949 end
4050
4151 def cast ( value , field ) when field in [ :red , :green , :blue ] do
42- value / 1
52+ ( value / 1 )
4353 |> min ( 255.0 )
4454 |> max ( 0.0 )
4555 end
56+
4657 def cast ( value , :alpha ) do
47- value / 1
58+ ( value / 1 )
4859 |> min ( 1.0 )
4960 |> max ( 0.0 )
5061 end
5162
52- defp to_hex ( value ) when is_float ( value ) , do:
53- to_hex ( round ( value ) )
54- defp to_hex ( value ) when value < 16 , do:
55- "0" <> Integer . to_string ( value , 16 )
56- defp to_hex ( value ) when is_integer ( value ) , do:
57- Integer . to_string ( value , 16 )
58-
59- def from_hex! ( input ) do
60- { :ok , color } = from_hex ( input )
61- color
62- end
63+ defp to_hex ( value ) when is_float ( value ) , do: to_hex ( round ( value ) )
64+ defp to_hex ( value ) when value < 16 , do: "0" <> Integer . to_string ( value , 16 )
65+ defp to_hex ( value ) when is_integer ( value ) , do: Integer . to_string ( value , 16 )
6366
64- def from_cmap! ( % Colormap { color: color } = cmap ) do
65- % Colormap { cmap | color: from_hex! ( color ) }
66- end
67+ def from_hex! ( input ) do
68+ { :ok , color } = from_hex ( input )
69+ color
70+ end
6771
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
72+ def from_cmap! ( % Colormap { color: color } = cmap ) do
73+ % Colormap { cmap | color: from_hex! ( color ) }
74+ end
7475
75- defp parse_hex ( s ) , do: String . to_integer ( s , 16 )
76+ def from_hex ( "#" <> << r :: binary - size ( 2 ) , g :: binary - size ( 2 ) , b :: binary - size ( 2 ) >> ) do
77+ { :ok , rgb ( parse_hex ( r ) , parse_hex ( g ) , parse_hex ( b ) ) }
78+ end
7679
80+ def from_hex ( "#" <> << r :: binary - size ( 1 ) , g :: binary - size ( 1 ) , b :: binary - size ( 1 ) >> ) do
81+ { :ok , rgb ( parse_hex ( r <> r ) , parse_hex ( g <> g ) , parse_hex ( b <> b ) ) }
82+ end
7783
84+ defp parse_hex ( s ) , do: String . to_integer ( s , 16 )
7885end
7986
80-
8187defimpl String.Chars , for: CssColors.RGB do
82- def to_string ( struct ) do
83- Matplotex.Colorscheme.Rgb . to_string ( struct )
84- end
88+ def to_string ( struct ) do
89+ Matplotex.Colorscheme.Rgb . to_string ( struct )
90+ end
8591end
0 commit comments