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
-
#assert_one_of(name, arg, *types) ⇒ Object
Checks if
arg
is a sub-class of any of the supportedtypes
and raises a NotSupported exception otherwise. -
#assert_type(name, arg, type) ⇒ Object
Checks if
arg
is of the expectedtype
and raises a TypeMismatch exception otherwise. -
#to_canvas(type, width, height) ⇒ Canvas
Converts a fill type (solid color or gradient) to a Canvas object.
-
#to_chunky_color(color) ⇒ ChunkyPNG::Color
Converts a Sass::Script::Color to ChunkyPNG::Color object.
-
#value_of(number, max, default = nil) ⇒ Float
Converts the Sass::Script::Number to a fixed value.
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.
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.
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.
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.
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.
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 |