/
draw.ex
192 lines (157 loc) · 3.67 KB
/
draw.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
defmodule Image.Options.Draw do
@moduledoc """
Options and options validation for the
drawing functionss.
"""
alias Image.Color
alias Image.CombineMode
@type circle :: [
{:fill, boolean()}
| {:color, Color.t()}
]
@type rect :: [
{:fill, boolean()}
| {:color, Color.t()}
| {:stroke_width, pos_integer()}
]
@type point :: [
{:color, Color.t()}
]
@type flood :: [
{:equal, boolean()}
| {:color, Color.t()}
]
@type mask :: [
{:color, Color.t()}
]
@type line :: [
{:color, Color.t()}
]
@type smudge :: []
@type image :: [
{:mode, CombineMode.t()}
]
@doc false
def default_options(:circle) do
[
color: :black,
fill: true,
stroke_width: 1
]
end
@doc false
def default_options(:rect) do
[
color: :black,
fill: true,
stroke_width: 1
]
end
@doc false
def default_options(:line) do
[
color: :black
]
end
@doc false
def default_options(:point) do
[
color: :black
]
end
@doc false
def default_options(:mask) do
[
color: :black
]
end
@doc false
def default_options(:flood) do
[
color: :black,
equal: false
]
end
@doc false
def default_options(:image) do
[
mode: :VIPS_COMBINE_MODE_SET
]
end
@doc false
def default_options(:smudge) do
[]
end
@doc """
Validate the options for `Image.Draw`.
"""
def validate_options(type, options) do
options = Keyword.merge(default_options(type), options)
options =
case Enum.reduce_while(options, options, &validate_option(type, &1, &2)) do
{:error, value} ->
{:error, value}
options ->
{:ok, options}
end
case options do
{:ok, options} ->
options
|> Map.new()
|> wrap(:ok)
other ->
other
end
end
defp validate_option(type, {:fill, fill}, options) when type in [:circle, :rect] do
if fill do
{:cont, Keyword.put(options, :fill, true)}
else
{:cont, Keyword.put(options, :fill, false)}
end
end
defp validate_option(type, {:color, color}, options)
when type in [:mask, :point, :circle, :rect, :line, :flood] do
case Color.rgb_color(color) do
{:ok, color} ->
rgb = if Keyword.keyword?(color), do: Keyword.fetch!(color, :rgb), else: color
{:cont, Keyword.put(options, :color, rgb)}
{:error, reason} ->
{:halt, {:error, reason}}
end
end
defp validate_option(:flood, {:equal, equal}, options) do
if equal do
{:cont, Keyword.put(options, :equal, true)}
else
{:cont, Keyword.put(options, :equal, false)}
end
end
defp validate_option(:image, {:mode, mode}, options) do
case Image.CombineMode.validate(mode) do
{:ok, mode} ->
{:cont, Keyword.put(options, :mode, mode)}
{:error, reason} ->
{:halt, {:error, reason}}
end
end
defp validate_option(type, {:stroke_width, stroke_width}, options)
when type in [:rect, :circle]
when is_integer(stroke_width) and stroke_width > 0 do
{:cont, options}
end
defp validate_option(type, {option, value}, _options) do
{:halt, {:error, invalid_option(type, option, value)}}
end
@doc false
def invalid_option(type, option) do
"Invalid option or option value for draw_#{type}: #{inspect(option)}"
end
@doc false
def invalid_option(type, option, value) do
"Invalid option or option value for draw_#{type}: #{option}: #{inspect(value)}"
end
defp wrap(term, atom) do
{atom, term}
end
end