88
99import itertools
1010from math import floor
11+
1112from mathics .builtin .base import Builtin
12- from mathics .builtin .colors .color_directives import (
13- _ColorObject ,
14- ColorError ,
15- RGBColor ,
16- )
13+ from mathics .builtin .colors .color_directives import ColorError , RGBColor , _ColorObject
1714from mathics .builtin .colors .color_internals import convert_color
18- from mathics .builtin .drawing .image import _ImageBuiltin , Image
19-
20- from mathics .core .atoms import (
21- Integer ,
22- MachineReal ,
23- Rational ,
24- Real ,
25- )
15+ from mathics .builtin .drawing .image import Image , _ImageBuiltin
16+ from mathics .core .atoms import Integer , MachineReal , Rational , Real
2617from mathics .core .convert .expression import to_expression , to_mathics_list
2718from mathics .core .expression import Expression
2819from mathics .core .list import ListExpression
3223_image_requires = ("numpy" , "PIL" )
3324
3425try :
35- import PIL .ImageOps
3626 import numpy
27+ import PIL .ImageOps
3728
3829 _enabled = True
3930except ImportError :
4233
4334class Blend (Builtin ):
4435 """
36+ <url>:WMA link:https://reference.wolfram.com/language/ref/Blend.html</url>
37+
4538 <dl>
46- <dt>'Blend[{$c1$, $c2$}]'
47- <dd>represents the color between $c1$ and $c2$.
48- <dt>'Blend[{$c1$, $c2$}, $x$]'
49- <dd>represents the color formed by blending $c1$ and $c2$ with
50- factors 1 - $x$ and $x$ respectively.
51- <dt>'Blend[{$c1$, $c2$, ..., $cn$}, $x$]'
52- <dd>blends between the colors $c1$ to $cn$ according to the
53- factor $x$.
39+ <dt>'Blend[{$c1$, $c2$}]'
40+ <dd>represents the color between $c1$ and $c2$.
41+
42+ <dt>'Blend[{$c1$, $c2$}, $x$]'
43+ <dd>represents the color formed by blending $c1$ and $c2$ with
44+ factors 1 - $x$ and $x$ respectively.
45+
46+ <dt>'Blend[{$c1$, $c2$, ..., $cn$}, $x$]'
47+ <dd>blends between the colors $c1$ to $cn$ according to the
48+ factor $x$.
5449 </dl>
5550
5651 >> Blend[{Red, Blue}]
@@ -109,7 +104,7 @@ def do_blend(self, colors, values):
109104 result = [r + p for r , p in zip (result , part )]
110105 return type (components = result )
111106
112- def apply (self , colors , u , evaluation ):
107+ def eval (self , colors , u , evaluation ):
113108 "Blend[{colors___}, u_]"
114109
115110 colors_orig = colors
@@ -154,10 +149,13 @@ def apply(self, colors, u, evaluation):
154149
155150class ColorConvert (Builtin ):
156151 """
152+ <url>:WMA link:
153+ https://reference.wolfram.com/language/ref/ColorConvert.html</url>
154+
157155 <dl>
158- <dt>'ColorConvert[$c$, $colspace$]'
159- <dd>returns the representation of $c$ in the color space $colspace$. $c$
160- may be a color or an image.
156+ <dt>'ColorConvert[$c$, $colspace$]'
157+ <dd>returns the representation of $c$ in the color space $colspace$. $c$ \
158+ may be a color or an image.
161159 </dl>
162160
163161 Valid values for $colspace$ are:
@@ -178,15 +176,15 @@ class ColorConvert(Builtin):
178176 }
179177 summary_text = "convert between color models"
180178
181- def apply (self , input , colorspace , evaluation ):
179+ def eval (self , input , colorspace , evaluation ):
182180 "ColorConvert[input_, colorspace_String]"
183181
184182 if isinstance (input , Image ):
185183 return input .color_convert (colorspace .get_string_value ())
186184 else :
187185 from mathics .builtin .colors .color_directives import (
188- expression_to_color ,
189186 color_to_expression ,
187+ expression_to_color ,
190188 )
191189
192190 py_color = expression_to_color (input )
@@ -208,6 +206,8 @@ def apply(self, input, colorspace, evaluation):
208206
209207class ColorNegate (_ImageBuiltin ):
210208 """
209+ <url>:WMA link:https://reference.wolfram.com/language/ref/ColorNegate.html</url>
210+
211211 <dl>
212212 <dt>'ColorNegate[$image$]'
213213 <dd>returns the negative of $image$ in which colors have been negated.
@@ -223,7 +223,7 @@ class ColorNegate(_ImageBuiltin):
223223
224224 summary_text = "the negative color of a given color"
225225
226- def apply_for_color (self , color , evaluation ):
226+ def eval_for_color (self , color , evaluation ):
227227 "ColorNegate[color_RGBColor]"
228228 # Get components
229229 r , g , b = [element .to_python () for element in color .elements ]
@@ -232,13 +232,15 @@ def apply_for_color(self, color, evaluation):
232232 # Reconstitute
233233 return Expression (SymbolRGBColor , Real (r ), Real (g ), Real (b ))
234234
235- def apply_for_image (self , image , evaluation ):
235+ def eval_for_image (self , image , evaluation ):
236236 "ColorNegate[image_Image]"
237237 return image .filter (lambda im : PIL .ImageOps .invert (im ))
238238
239239
240240class Darker (Builtin ):
241241 """
242+ <url>:WMA link:https://reference.wolfram.com/language/ref/Darker.html</url>
243+
242244 <dl>
243245 <dt>'Darker[$c$, $f$]'
244246 <dd>is equivalent to 'Blend[{$c$, Black}, $f$]'.
@@ -262,23 +264,33 @@ class Darker(Builtin):
262264
263265class DominantColors (_ImageBuiltin ):
264266 """
267+ <url>:WMA link:https://reference.wolfram.com/language/ref/DominantColors.html</url>
268+
265269 <dl>
266- <dt>'DominantColors[$image$]'
270+ <dt>'DominantColors[$image$]'
267271 <dd>gives a list of colors which are dominant in the given image.
268- <dt>'DominantColors[$image$, $n$]'
272+
273+ <dt>'DominantColors[$image$, $n$]'
269274 <dd>returns at most $n$ colors.
270- <dt>'DominantColors[$image$, $n$, $prop$]'
271- <dd>returns the given property $prop$, which may be "Color" (return RGB colors), "LABColor" (return
272- LAB colors), "Count" (return the number of pixels a dominant color covers), "Coverage" (return the
273- fraction of the image a dominant color covers), or "CoverageImage" (return a black and white image
274- indicating with white the parts that are covered by a dominant color).
275+
276+ <dt>'DominantColors[$image$, $n$, $prop$]'
277+ <dd>returns the given property $prop$, which may be:
278+ <ul>
279+ <li>"Color": return RGB colors,
280+ <li> "LABColor": return LAB colors,
281+ <li> "Count": return the number of pixels a dominant color covers,
282+ <li> "Coverage": return the fraction of the image a dominant color \
283+ covers, or
284+ <li> "CoverageImage": return a black and white image indicating with \
285+ white the parts that are covered by a dominant color.
286+ </ul>
275287 </dl>
276288
277- The option "ColorCoverage" specifies the minimum amount of coverage needed to include a dominant color
278- in the result.
289+ The option "ColorCoverage" specifies the minimum amount of coverage needed to \
290+ include a dominant color in the result.
279291
280- The option "MinColorDistance" specifies the distance (in LAB color space) up to which colors are merged
281- and thus regarded as belonging to the same dominant color.
292+ The option "MinColorDistance" specifies the distance (in LAB color space) up \
293+ to which colors are merged and thus regarded as belonging to the same dominant color.
282294
283295 >> img = Import["ExampleData/lena.tif"]
284296 = -Image-
@@ -316,7 +328,7 @@ class DominantColors(_ImageBuiltin):
316328 options = {"ColorCoverage" : "Automatic" , "MinColorDistance" : "Automatic" }
317329 summary_text = "find a list of dominant colors"
318330
319- def apply (self , image , n , prop , evaluation , options ):
331+ def eval (self , image , n , prop , evaluation , options ):
320332 "DominantColors[image_Image, n_Integer, prop_String, OptionsPattern[%(name)s]]"
321333
322334 py_prop = prop .get_string_value ()
@@ -379,9 +391,9 @@ def apply(self, image, n, prop, evaluation, options):
379391 num_pixels = im .size [0 ] * im .size [1 ]
380392
381393 from mathics .algorithm .clusters import (
382- agglomerate ,
383- PrecomputedDistances ,
384394 FixedDistanceCriterion ,
395+ PrecomputedDistances ,
396+ agglomerate ,
385397 )
386398
387399 norm = numpy .linalg .norm
@@ -416,7 +428,7 @@ def result():
416428 elif py_prop == "Coverage" :
417429 yield Rational (int (count ), num_pixels )
418430 elif py_prop == "CoverageImage" :
419- mask = numpy .ndarray (shape = pixels .shape , dtype = numpy . bool )
431+ mask = numpy .ndarray (shape = pixels .shape , dtype = bool )
420432 mask .fill (0 )
421433 for i in members :
422434 mask = mask | (pixels == i )
@@ -433,6 +445,8 @@ def result():
433445
434446class Lighter (Builtin ):
435447 """
448+ <url>:WMA link:https://reference.wolfram.com/language/ref/Lighter.html</url>
449+
436450 <dl>
437451 <dt>'Lighter[$c$, $f$]'
438452 <dd>is equivalent to 'Blend[{$c$, White}, $f$]'.
0 commit comments