Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Passing effect as parameter to a function
Started by firerabbit.info Oct 01 2018 07:35 AM

3 replies to this topic
filter effect parameterize generalize

Best Answer roaminggamer , 01 October 2018 - 08:01 AM

Maybe do this instead:

displayViewImageFilter = function (details)

   --Apply to back image
   viewBackImage.fill.effect = details.name

   --Apply to all overviews
   for k,v in pairs( details.params ) do
      viewBackImage.fill.effect[k] = v
   end
end

Then do this:
 

local thisEffect = { 
   name = "filter.colorPolynomial",
   params = { 
      coefficients = {
          0, 0, 1, 0,  --red coefficients
          0, 0, 1, 0,  --green coefficients
          0, 1, 0, 0,  --blue coefficients
          0, 0, 0, 1   --alpha coefficients
      },
   }

displayViewImageFilter(thisEffect)

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

firerabbit.info

[GLOBAL: userInfoPane.html]
firerabbit.info
  • Contributor

  • 155 posts
  • Corona SDK

Hi all,

 

I'm trying to generalize a method so that I can apply a predefined filter across multiple display objects.

 

Based on applying an effect like this:

displayObject.fill.effect = "filter.colorPolynomial" 
displayObject.fill.effect.coefficients =
{
    0, 0, 1, 0,  --red coefficients
    0, 0, 1, 0,  --green coefficients
    0, 1, 0, 0,  --blue coefficients
    0, 0, 0, 1   --alpha coefficients
}

I tried the following:

--Applies the given effect to the view and overviews
displayViewImageFilter = function (thisEffect)

   --Apply to back image
   viewBackImage.fill.effect = thisEffect

   --Apply to all overviews
   for i, overviewImage in pairs(overviews) do
      overviewImage.fill.effect = thisEffect
   end
end

But I can't figure out how to build the thisEffect parameter so that it can be passed into the method.  Trying code below will return "Attempt to index thisEffect (a string value)":

local thisEffect = "filter.colorPolynomial"
thisEffect.coefficients = {
    0, 0, 1, 0,  --red coefficients
    0, 0, 1, 0,  --green coefficients
    0, 1, 0, 0,  --blue coefficients
    0, 0, 0, 1   --alpha coefficients
}
displayViewImageFilter(thisEffect)

 

 

I assume there's something going on behind the scenes when you set displayObject.fill.effect to a String.  Is what I'm trying to do even possible?

 

Cheers.



[TOPIC: post.html]
#2

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,363 posts
  • Corona SDK

Yeah, you can't do that.

You'll need to pass the effect name and parameters as two separate arguments and then apply them to your object.
 
This isn't really assigning a string to effect: 

displayObject.fill.effect = "filter.colorPolynomial"

I'm pretty sure this is actually doing more behind the scenes and the results is that fill is a special object afterwards.

 

You can confirm this on your own by doing this:

print(displayObject.fill.effect )


[TOPIC: post.html]
#3

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,363 posts
  • Corona SDK

  Best Answer

Maybe do this instead:

displayViewImageFilter = function (details)

   --Apply to back image
   viewBackImage.fill.effect = details.name

   --Apply to all overviews
   for k,v in pairs( details.params ) do
      viewBackImage.fill.effect[k] = v
   end
end

Then do this:
 

local thisEffect = { 
   name = "filter.colorPolynomial",
   params = { 
      coefficients = {
          0, 0, 1, 0,  --red coefficients
          0, 0, 1, 0,  --green coefficients
          0, 1, 0, 0,  --blue coefficients
          0, 0, 0, 1   --alpha coefficients
      },
   }

displayViewImageFilter(thisEffect)

Edited by roaminggamer, 01 October 2018 - 08:01 AM.

  • firerabbit.info likes this

[TOPIC: post.html]
#4

firerabbit.info

[GLOBAL: userInfoPane.html]
firerabbit.info
  • Contributor

  • 155 posts
  • Corona SDK

Worked like a charm!  Thanks, RG!!




[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: filter, effect, parameterize, generalize