Method: CIFilter._create_filter

Defined in:
lib/sugarcube-image/cifilter.rb

._create_filter(filter_name, args, set_args = [], names = {}) ⇒ Object

This helper instantiates the filter (raising an exception if it was unsuccessful) and applies all the arguments from options (naming the args according to set_args), translating names from names



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
# File 'lib/sugarcube-image/cifilter.rb', line 83

def _create_filter(filter_name, args, set_args=[], names={})
  filter = CIFilter.filterWithName(filter_name)
  raise "Unsupported filter #{filter_name.inspect}" unless filter

  # turn a list of arguments into a hash
  if args.length == 1 && Hash === args[0]
    options = args[0]
  else
    options = {}
    args.each_with_index do |arg, index|
      setter = set_args[index]
      raise "Cannot set option ##{index}" if setter.nil?
      options[setter] = arg
    end
  end

  options.each do |key, value|
    # translate the keys, but if there is no translation key then do nothing
    key = names[key] || key.to_s

    # translate the value if a block was given for that. `key` is always going
    # to be the CIImage key (not the alias, e.g. :radius => 'inputRadius')
    value = yield(key, value) if block_given?
    filter.setValue(value, forKey:key)
  end
  return filter
end