Class: DYI::Drawing::Brush

Inherits:
PenBase
  • Object
show all
Defined in:
lib/dyi/drawing/pen.rb

Overview

Brush object holds a Painting object and a Font object. Using these object, Brush object creates instances of concrete subclass of Shape::Base; a created instance has a painting attribute and a font attribute that Brush object holds.

Brush class has been optimized to fill a shape with a color and so on. Synonym methods of attributes fill_xxx has been defined in this class: color(synonym of fill), rule(synonym of fill_rule).

This class has shortcut contractors: color_name_brush, which a fill color is specified in.

Examples:

brush = DYI::Drawing::Brush.red_brush
# the followings is the same processing
# brush = DYI::Drawing::Brush.new(:color => 'red')
# brush = DYI::Drawing::Brush.new(:fill => 'red')

canvas = DYI::Canvas.new(100, 50)
brush.draw_ellipse(canvas, [50, 25], 40, 15)

Since:

  • 0.0.0

Direct Known Subclasses

ColumnBrush, CylinderBrush

Constant Summary collapse

ALIAS_ATTRIBUTES =

Since:

  • 0.0.0

Painting::IMPLEMENT_ATTRIBUTES.inject({}) do |hash, key|
  hash[$'.empty? ? :color : $'.to_sym] = key if key.to_s =~ /^(fill_|fill$)/ && key != :fill_opacity
  hash
end

Constants inherited from PenBase

PenBase::DROP_SHADOW_OPTIONS

Instance Attribute Summary collapse

Attributes inherited from PenBase

#display, #drop_shadow, #fill, #fill_opacity, #fill_rule, #opacity, #stroke, #stroke_dasharray, #stroke_dashoffset, #stroke_linecap, #stroke_linejoin, #stroke_miterlimit, #stroke_opacity, #stroke_width, #visibility

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PenBase

#draw_circle, #draw_closed_path, #draw_ellipse, #draw_image, #draw_line, #draw_line_on_direction, #draw_path, #draw_polygon, #draw_polyline, #draw_rectangle, #draw_rectangle_on_corner, #draw_sector, #draw_text, #draw_toroid, #import_image

Constructor Details

#initialize(options = {}) ⇒ Brush

Returns a new instance of Brush.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :font (Font, Hash)

    the value of attribute font

  • :color (Color, #write_as)

    the value of attribute color

  • :rule (String)

    the value of attribute rule

  • :display (String)

    the value of attribute display; one of following values: "block", "none"

  • :fill (Color, #write_as)

    the value of attribute fill

  • :fill_opacity (#to_f)

    the value of attribute fill_opacity

  • :fill_rule (String)

    the value of attribute fill_rule; one of following values: "nonzero", "evenodd"

  • :opacity (#to_f)

    the value of attribute opacity

  • :stroke (Color, #write_as)

    the value of attribute stroke

  • :stroke_dasharray (Array<Length>, String)

    the value of attribute stroke_dasharray

  • :stroke_dashoffset (Length)

    the value of attribute stroke_dashoffset

  • :stroke_linecap (String)

    the value of attribute stroke_linecap; one of following values: "butt", "round", "square"

  • :stroke_linejoin (String)

    the value of attribute stroke_linejoin; one of following values: "miter", "round", "bevel"

  • :stroke_miterlimit (#to_f)

    the value of attribute stroke_miterlimit

  • :stroke_opacity (#to_f)

    the value of attribute stroke_opacity

  • :stroke_width (Length)

    the value of attribute stroke_width

  • :visible (String)

    the value of attribute visibility; one of following values: "visible", "hidden"

Since:

  • 0.0.0



690
691
692
693
694
695
696
697
698
# File 'lib/dyi/drawing/pen.rb', line 690

def initialize(options={})
  options = options.clone
  ALIAS_ATTRIBUTES.each do |key, value|
    options[value] = options.delete(key) if options.key?(key) && !options.key?(value)
  end
  options[:stroke_width] = 0 unless options.key?(:stroke_width)
  options[:fill] = 'black' unless options.key?(:fill)
  super
end

Instance Attribute Details

#colorColor, ...

Synonym of attribute fill. +++ Synonym of attribute fill_rule.

Returns:

  • (Color, #write_as)

    the value of attribute fill

  • (String)

    the value of attribute fill_rule



707
708
709
710
# File 'lib/dyi/drawing/pen.rb', line 707

ALIAS_ATTRIBUTES.each do |key, value|
  alias_method key, value
  alias_method "#{key}=", "#{value}="
end

#ruleColor, ...

Synonym of attribute fill. +++ Synonym of attribute fill_rule.

Returns:

  • (Color, #write_as)

    the value of attribute fill

  • (String)

    the value of attribute fill_rule



707
708
709
710
# File 'lib/dyi/drawing/pen.rb', line 707

ALIAS_ATTRIBUTES.each do |key, value|
  alias_method key, value
  alias_method "#{key}=", "#{value}="
end

Class Method Details

.method_missing(method_name, *args, &block) ⇒ Object

Since:

  • 0.0.0



713
714
715
716
717
718
719
720
721
722
723
# File 'lib/dyi/drawing/pen.rb', line 713

def method_missing(method_name, *args, &block)
  if method_name.to_s =~ /([a-z]+)_brush/
    if options = args.first
      self.new(options.merge(:fill => $1))
    else
      self.new(:fill => $1)
    end
  else
    super
  end
end