Module: Compass::Magick::Utils

Extended by:
Utils
Included in:
Canvas, Command, Types::Gradients::ColorStop, Types::Gradients::Linear, Types::Solid, Utils
Defined in:
lib/magick/utils.rb

Overview

Utilities methods used throughout Compass Magick.

Defined Under Namespace

Classes: Point

Instance Method Summary collapse

Instance Method Details

#assert_one_of(name, arg, *types) ⇒ Object

Checks if arg is a sub-class of any of the supported types and raises a NotSupported exception otherwise.

Parameters:

  • name (String)

    The argument name or method signature (used in the exception message).

  • arg (Object)

    The argument to validate.

  • types (Array<Object>)

    The list of supported arg types.

Raises:



26
27
28
29
30
31
# File 'lib/magick/utils.rb', line 26

def assert_one_of(name, arg, *types)
  for type in types do
    return if arg.kind_of?(type)
  end
  raise NotSupported.new("#{name} expected argument of type [#{types.join ', '}] got #{arg.class}(#{arg.inspect}) instead")
end

#assert_type(name, arg, type) ⇒ Object

Checks if arg is of the expected type and raises a TypeMismatch exception otherwise.

Parameters:

  • name (String)

    The argument name (used in the exception message).

  • arg (Object)

    The argument to validate.

  • type (Object)

    The expected arg type.

Raises:



12
13
14
15
16
17
# File 'lib/magick/utils.rb', line 12

def assert_type(name, arg, type)
  raise TypeMismatch.new("(#{self.class}) Type mismatch for argument '#{name}'; " +
      "expected #{type} got #{arg.class}(#{arg.inspect}) instead") unless
    arg.nil? ||
    arg.kind_of?(type)
end

#to_canvas(type, width, height) ⇒ Canvas

Converts a fill type (solid color or gradient) to a Canvas object.

Parameters:

  • type (Object)

    The type of fill type to convert. Supported:

  • width (Sass::Script::Number)

    The width of the generated Canvas.

  • height (Sass::Script::Number)

    The height of the generated Canvas.

Returns:

  • (Canvas)

    The canvas in the dimensions given with the fill type applied.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/magick/utils.rb', line 54

def to_canvas(type, width, height)
  Compass::Magick::Utils.assert_one_of 'to_canvas(..)', type, Sass::Script::Color, Sass::Script::String, Compass::Magick::Type, ChunkyPNG::Canvas
  if type.kind_of?(Sass::Script::Color)
    Compass::Magick::Types::Solid.new(type).to_canvas(width, height)
  elsif type.kind_of?(Sass::Script::String)
    if type.value == 'transparent'
      ChunkyPNG::Canvas.new(width.value, height.value, ChunkyPNG::Color.rgba(0, 0, 0, 0))
    else
      raise NotSupported.new("to_canvas(..) supports String argument of values ['transparent'] got '#{type}' instead")
    end
  elsif type.kind_of?(ChunkyPNG::Canvas)
    type.tile(width.value, height.value)
  elsif type.kind_of?(Compass::Magick::Types::Solid) || type.kind_of?(Compass::Magick::Types::Gradients::Linear)
    type.to_canvas(width, height)
  end
end

#to_chunky_color(color) ⇒ ChunkyPNG::Color

Converts a Sass::Script::Color to ChunkyPNG::Color object.

Parameters:

  • color (Sass::Script::Color)

    The source color in Sass’ format.

Returns:

  • (ChunkyPNG::Color)

    The source color in ChunkyPNG’s format.



37
38
39
# File 'lib/magick/utils.rb', line 37

def to_chunky_color(color)
  ChunkyPNG::Color.rgba(color.red, color.green, color.blue, (color.alpha * 255).to_i)
end

#value_of(number, max, default = nil) ⇒ Float

Converts the Sass::Script::Number to a fixed value.

Parameters:

  • number (Sass::Script::Number)

    The number to convert.

  • max (Float)

    The maximum allowed value for this number.

  • default (Float) (defaults to: nil)

    The default value for this number.

Returns:

  • (Float)

    If number is nil, true or false, the default is returned. If number‘s units are ’%‘, it is calculated as a percentage of max. If the value is negative, it is calculated as an offset against max. Otherwise, the value is returned as-is.



81
82
83
84
85
86
87
# File 'lib/magick/utils.rb', line 81

def value_of(number, max, default = nil)
  return default if number.nil? || (number.kind_of?(Sass::Script::Bool) && ! number.value)
  assert_type 'number', number, Sass::Script::Number
  return max * (number.value.to_f / 100) if number.unit_str == '%'
  return max + number.value if number.value < 0
  number.value
end