ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* METAPOST/MetaFun withtransparency doesn't accept my pair primary argument, what am I doing wrong?
@ 2020-04-09 14:58 Gerben Wierda
  2020-04-09 16:06 ` Hans Hagen
  0 siblings, 1 reply; 3+ messages in thread
From: Gerben Wierda @ 2020-04-09 14:58 UTC (permalink / raw)
  To: mailing list for ConTeXt users


[-- Attachment #1.1: Type: text/plain, Size: 1929 bytes --]

I have refactored my code to use withtransparency and separate parameters for colors, sizes, transparency, etc. But I’m running into a problem. The fillTransparency parameter is a pair primary (as can be seen from the show command)

Here is a pretty minimum example (in reality there are multiple colors and transparencies passed on):

\starttext
\startMPpage[instance=doublefun]

vardef Foo( expr w, h, fillColor, fillTransparency) =
  show "FOO:", w, h, fillColor, fillTransparency;
  save pic; picture pic;
  save circ; path circ;
  circ := (fullcircle xyscaled (h,h));
  pic := image (
    fill (circ shifted (w-h/2,-h/2)) withcolor fillColor withtransparency fillTransparency;
  );
  pic
enddef;

pic := Foo( 150, 50, (0.686,1.000,1.000), (1,1.000));
draw pic;
\stopMPpage
\stoptext

and the result is:

metapost log    > >> "FOO:"
metapost log    > >> 150
metapost log    > >> 50
metapost log    > >> (0.68600000000000005,1,1)
metapost log    > >> (1,1)
metapost log    > ! Missing argument to withtransparency.
metapost log    > <to be read again> 
metapost log    >                    ((1,1))
metapost log    > <argument> ...0000005,1,1))withtransparency((1,1))
metapost log    >                                                   ;
metapost log    > image->...ture;currentpicture:=nullpicture;(TEXT3)
metapost log    >                                                   ;currentpicture.if.str(SUF...
metapost log    > 
metapost log    > Foo->...)withcolor(EXPR4)withtransparency(EXPR5);)
metapost log    >                                                   ;pic.endgroup
metapost log    > <*> ...o( 150, 50, (0.686,1.000,1.000), (1,1.000))
metapost log    >                                                   ; draw pic; ;

What am I doing wrong here?

Thanks,

G

PS. Given the complaint about asking both here and on tex.stackexchange, which of the two is the best place?

[-- Attachment #1.2: Type: text/html, Size: 6190 bytes --]

[-- Attachment #2: Type: text/plain, Size: 493 bytes --]

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: METAPOST/MetaFun withtransparency doesn't accept my pair primary argument, what am I doing wrong?
  2020-04-09 14:58 METAPOST/MetaFun withtransparency doesn't accept my pair primary argument, what am I doing wrong? Gerben Wierda
@ 2020-04-09 16:06 ` Hans Hagen
  2020-04-10  8:07   ` Gerben Wierda
  0 siblings, 1 reply; 3+ messages in thread
From: Hans Hagen @ 2020-04-09 16:06 UTC (permalink / raw)
  To: mailing list for ConTeXt users, Gerben Wierda

On 4/9/2020 4:58 PM, Gerben Wierda wrote:
> \starttext
> \startMPpage[instance=doublefun]
> 
> vardef Foo( expr w, h, fillColor, fillTransparency) =
>    show "FOO:", w, h, fillColor, fillTransparency;
>    save pic; picture pic;
>    save circ; path circ;
>    circ := (fullcircle xyscaled (h,h));
>    pic := image (
>      fill (circ shifted (w-h/2,-h/2)) withcolor fillColor 
> withtransparency fillTransparency;
>    );
>    pic
> enddef;
> 
> pic := Foo( 150, 50, (0.686,1.000,1.000), (1,1.000));
> draw pic;
> \stopMPpage
> \stoptext

Maybe this definition is better then but I need to test it more

\starttext
\startMPpage[instance=doublefun]

def withtransparency expr t =
     if pair t :
         withprescript "tr_alternative="  & decimal 
transparency_alternative_to_number(xpart t)
         withprescript "tr_transparency=" & decimal ypart t
     else :
         mfun_with_transparency (transparency_alternative_to_number(t))
     fi
enddef ;

def mfun_with_transparency (expr a) expr t =
     withprescript "tr_alternative="  & decimal a
     withprescript "tr_transparency=" & decimal t
enddef ;

vardef Foo ( expr w, h, fillColor, fillTransparency) =
   image (
     fill fullcircle xyscaled (h,h) shifted (w-h/2,-h/2)
         withcolor fillColor
         withtransparency fillTransparency
     ;
   )
enddef;

vardef FooX ( expr w, h, fillColor) =
   image (
     fill fullcircle xyscaled (h,h) shifted (w-h/2,-h/2)
         withcolor fillColor
         withtransparency (normaltransparent, .5)
     ;
   )
enddef;

vardef FooY ( expr w, h, fillColor) =
   image (
     fill fullcircle xyscaled (h,h) shifted (w-h/2,-h/2)
         withcolor fillColor
         withtransparency normaltransparent .5
     ;
   )
enddef;

draw Foo  ( 350, 50, (0.5,1,0), (1,.5));
draw FooX ( 250, 50, (1,0.5,1));
draw FooY ( 150, 50, (1,1,0.5));

\stopMPpage
\stoptext


-- 

-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
        tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
-----------------------------------------------------------------
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: METAPOST/MetaFun withtransparency doesn't accept my pair primary argument, what am I doing wrong?
  2020-04-09 16:06 ` Hans Hagen
@ 2020-04-10  8:07   ` Gerben Wierda
  0 siblings, 0 replies; 3+ messages in thread
From: Gerben Wierda @ 2020-04-10  8:07 UTC (permalink / raw)
  To: Hans Hagen; +Cc: mailing list for ConTeXt users


[-- Attachment #1.1: Type: text/plain, Size: 3117 bytes --]

As I only use one transparency method anyway I could also fix this by passing a numeric instead of a pair and use the standard withtransparency (I don’t like overriding a standard MetaFun method, one never knows what might break in future updates).

vardef Foo( expr w, h, fillColor, fillTransparency) =
  show "FOO:", w, h, fillColor, fillTransparency;
  save pic; picture pic;
  save circ; path circ;
  circ := (fullcircle xyscaled (h,h));
  pic := image (
    fill (circ shifted (w-h/2,-h/2)) withcolor fillColor withtransparency (1,fillTransparency);
  );
  pic
enddef;

picture pic;
pic := Foo( 150, 50, (0.686,1.000,1.000), 0.5);
draw pic;
\stopMPpage
\stoptext


> On 9 Apr 2020, at 18:06, Hans Hagen <j.hagen@xs4all.nl> wrote:
> 
> On 4/9/2020 4:58 PM, Gerben Wierda wrote:
>> \starttext
>> \startMPpage[instance=doublefun]
>> vardef Foo( expr w, h, fillColor, fillTransparency) =
>>   show "FOO:", w, h, fillColor, fillTransparency;
>>   save pic; picture pic;
>>   save circ; path circ;
>>   circ := (fullcircle xyscaled (h,h));
>>   pic := image (
>>     fill (circ shifted (w-h/2,-h/2)) withcolor fillColor withtransparency fillTransparency;
>>   );
>>   pic
>> enddef;
>> pic := Foo( 150, 50, (0.686,1.000,1.000), (1,1.000));
>> draw pic;
>> \stopMPpage
>> \stoptext
> 
> Maybe this definition is better then but I need to test it more
> 
> \starttext
> \startMPpage[instance=doublefun]
> 
> def withtransparency expr t =
>    if pair t :
>        withprescript "tr_alternative="  & decimal transparency_alternative_to_number(xpart t)
>        withprescript "tr_transparency=" & decimal ypart t
>    else :
>        mfun_with_transparency (transparency_alternative_to_number(t))
>    fi
> enddef ;
> 
> def mfun_with_transparency (expr a) expr t =
>    withprescript "tr_alternative="  & decimal a
>    withprescript "tr_transparency=" & decimal t
> enddef ;
> 
> vardef Foo ( expr w, h, fillColor, fillTransparency) =
>  image (
>    fill fullcircle xyscaled (h,h) shifted (w-h/2,-h/2)
>        withcolor fillColor
>        withtransparency fillTransparency
>    ;
>  )
> enddef;
> 
> vardef FooX ( expr w, h, fillColor) =
>  image (
>    fill fullcircle xyscaled (h,h) shifted (w-h/2,-h/2)
>        withcolor fillColor
>        withtransparency (normaltransparent, .5)
>    ;
>  )
> enddef;
> 
> vardef FooY ( expr w, h, fillColor) =
>  image (
>    fill fullcircle xyscaled (h,h) shifted (w-h/2,-h/2)
>        withcolor fillColor
>        withtransparency normaltransparent .5
>    ;
>  )
> enddef;
> 
> draw Foo  ( 350, 50, (0.5,1,0), (1,.5));
> draw FooX ( 250, 50, (1,0.5,1));
> draw FooY ( 150, 50, (1,1,0.5));
> 
> \stopMPpage
> \stoptext
> 
> 
> -- 
> 
> -----------------------------------------------------------------
>                                          Hans Hagen | PRAGMA ADE
>              Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
>       tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
> -----------------------------------------------------------------


[-- Attachment #1.2: Type: text/html, Size: 7121 bytes --]

[-- Attachment #2: Type: text/plain, Size: 493 bytes --]

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-04-10  8:07 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-09 14:58 METAPOST/MetaFun withtransparency doesn't accept my pair primary argument, what am I doing wrong? Gerben Wierda
2020-04-09 16:06 ` Hans Hagen
2020-04-10  8:07   ` Gerben Wierda

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).