6
$\begingroup$

For example, I have the following

hexToRGB = RGBColor @@ (IntegerDigits[#~StringDrop~1~FromDigits~16, 256, 3]/255.) &;

Image@hexToRGB["#99c361"] // DominantColors["HexRGBColor"] (* why not? *)

To be clear, I am well aware that I can write

DominantColors[#, 1, "HexRGBColor"] &@Image@hexToRGB["#99c361"]

But there doesn't seem to be a postfix form for pure functions, or is there?

$\endgroup$

2 Answers 2

7
$\begingroup$

Because currying is not performed automatically. But you can do the following:

Image@hexToRGB["#99c361"] // DominantColors[#, 1, "HexRGBColor"] & 
$\endgroup$
7
  • $\begingroup$ Awesome thanks, is there a way to combine this w/ the prefix form e.g. First[#]&@Image@hexToRGB["#99c361"] // DominantColors[#, 1, "HexRGBColor"] & ? $\endgroup$
    – skyfire
    Commented Apr 2, 2020 at 13:13
  • $\begingroup$ ah nvm, just need to add parentheses $\endgroup$
    – skyfire
    Commented Apr 2, 2020 at 13:16
  • 1
    $\begingroup$ @skyfire Syntax precedence determines the order @ and // are applied. My guess is that you want Image@hexToRGB["#99c361"] // First@ DominantColors[#, 1, "HexRGBColor"] & $\endgroup$
    – Michael E2
    Commented Apr 2, 2020 at 13:16
  • 4
    $\begingroup$ @skyfire The precedence of // is very low, lower than @; that of & is even lower. $\endgroup$
    – Michael E2
    Commented Apr 2, 2020 at 13:22
  • 3
    $\begingroup$ The link in my comment is to an answer discussing precedence, which also includes a link to a tutorial in the docs. $\endgroup$
    – Michael E2
    Commented Apr 2, 2020 at 13:47
5
$\begingroup$

You can define an operator form for DominantColors by modifying its definition:

Unprotect[DominantColors]
DominantColors[s_String] := DominantColors[#, 1, s] &; 
Protect[DominantColors];

A better/safer alternative is to define your own function with its operator form that works like the function DominantColors:

ClearAll[dominantColors]
dominantColors[a__, o : OptionsPattern[DominantColors]] := DominantColors[a, o]
dominantColors[s_String] := dominantColors[#, 1, s] &

"#99c361" // hexToRGB // Image // dominantColors["HexRGBColor"] // First
"#99c361"
First @ dominantColors["HexRGBColor"] @ Image @ hexToRGB @ "#99c361"
 "#99c361"
$\endgroup$
3
  • $\begingroup$ nice, was not aware of OptionsPattern[] $\endgroup$
    – skyfire
    Commented Apr 2, 2020 at 13:15
  • 3
    $\begingroup$ @skyfire You can also use OptionsPattern[DominantColors] to restrict to the options of DominantColors. $\endgroup$
    – Michael E2
    Commented Apr 2, 2020 at 13:20
  • $\begingroup$ Thank you @MichaelE2. I added the argument to OptionsPattern. $\endgroup$
    – kglr
    Commented Apr 2, 2020 at 13:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.