class DYI::Drawing::ColorEffect::LinearGradient

Constants

SPREAD_METHODS

Attributes

spread_method[R]
start_point[R]
stop_point[R]

Public Class Methods

new(start_point=[0,0], stop_point=[1,0], spread_method=nil) click to toggle source
# File lib/dyi/drawing/color_effect.rb, line 32
def initialize(start_point=[0,0], stop_point=[1,0], spread_method=nil)
  @start_point = start_point
  @stop_point = stop_point
  self.spread_method = spread_method
  @child_elements = []
end
simple_gradation(derection, *colors) click to toggle source
# File lib/dyi/drawing/color_effect.rb, line 72
def simple_gradation(derection, *colors)
  case count = colors.size
  when 0
    nil
  when 1
    Color.new(colors.first)
  else
    case deraction
      when :vertical then obj = new([0,0], [0,1])
      when :horizontal then obj = new([0,0], [1,0])
      when :lowerright then obj = new([0,0], [1,1])
      when :upperright then obj = new([0,1], [1,0])
      else raise ArgumentError, "unknown derection: `#{derection}'"
    end
    colors.each_with_index do |color, i|
      obj.add_color(i.quo(count - 1), color)
    end
    obj
  end
end

Public Instance Methods

add_color(offset, color) click to toggle source
# File lib/dyi/drawing/color_effect.rb, line 39
def add_color(offset, color)
  @child_elements.push(GradientStop.new(offset, :color => color))
end
add_color_opacity(offset, color, opacity) click to toggle source
# File lib/dyi/drawing/color_effect.rb, line 47
def add_color_opacity(offset, color, opacity)
  @child_elements.push(GradientStop.new(offset, :color => color, :opacity => opacity))
end
add_opacity(offset, opacity) click to toggle source
# File lib/dyi/drawing/color_effect.rb, line 43
def add_opacity(offset, opacity)
  @child_elements.push(GradientStop.new(offset, :opacity => opacity))
end
child_elements() click to toggle source
# File lib/dyi/drawing/color_effect.rb, line 56
def child_elements
  @child_elements.clone
end
cls_wrap_mode() click to toggle source
# File lib/ironruby.rb, line 218
def cls_wrap_mode
  case spread_method
    when 'pad' then System::Drawing::Drawing2D::WrapMode.tile_flip_xy
    when 'reflect' then System::Drawing::Drawing2D::WrapMode.tile_flip_xy
    when 'repeat' then System::Drawing::Drawing2D::WrapMode.tile
    else System::Drawing::Drawing2D::WrapMode.tile_flip_xy
  end
end
color?() click to toggle source
# File lib/dyi/drawing/color_effect.rb, line 60
def color?
  true
end
create_cls_brush(opacity=nil, shape=nil) click to toggle source
# File lib/ironruby.rb, line 175
def create_cls_brush(opacity=nil, shape=nil)
  brush = System::Drawing::Drawing2D::LinearGradientBrush.new(
      System::Drawing::PointF.new(
          shape.left.to_f * (1.0 - start_point[0]) + shape.right.to_f * start_point[0],
          shape.top.to_f * (1.0 - start_point[1]) + shape.bottom.to_f * start_point[1]),
      System::Drawing::PointF.new(
          shape.left.to_f * (1.0 - stop_point[0]) + shape.right.to_f * stop_point[0],
          shape.top.to_f * (1.0 - stop_point[1]) + shape.bottom.to_f * stop_point[1]),
      System::Drawing::Color.empty,
      System::Drawing::Color.empty)
  start_pad = end_pad = false
  if !spread_method || spread_method == 'pad'
    start_pad = (0.001 < @child_elements.first.offset && @child_elements.first.offset < 0.999)
    end_pad = (0.001 < @child_elements.last.offset && @child_elements.last.offset < 0.999)
  end
  color_count = @child_elements.size
  color_count += 1 if start_pad
  color_count += 1 if end_pad
  color_blend = System::Drawing::Drawing2D::ColorBlend.new(color_count)
  cls_colors = System::Array[System::Drawing::Color].new(color_count)
  positions = System::Array[System::Single].new(color_count)
  if start_pad
    gradient_stop = @child_elements.first
    cls_colors[0] = gradient_stop.color.to_cls_color(gradient_stop.opacity)
    positions[0] = 0.0
  end
  @child_elements.each_with_index do |gradient_stop, i|
    idx = start_pad ? i + 1 : i
    cls_colors[idx] = gradient_stop.color.to_cls_color(gradient_stop.opacity)
    positions[idx] = gradient_stop.offset.to_f
  end
  if end_pad
    gradient_stop = @child_elements.last
    cls_colors[color_count - 1] = gradient_stop.color.to_cls_color(gradient_stop.opacity)
    positions[color_count - 1] = 1.0
  end
  color_blend.colors = cls_colors
  color_blend.positions = positions
  brush.interpolation_colors = color_blend
  brush.wrap_mode = cls_wrap_mode
  brush
end
spread_method=(value) click to toggle source
# File lib/dyi/drawing/color_effect.rb, line 51
def spread_method=(value)
  raise ArgumentError, "\"#{value}\" is invalid spreadMethod" if value && !SPREAD_METHODS.include?(value)
  @spread_method = value
end
write_as(formatter, io=$>) click to toggle source
# File lib/dyi/drawing/color_effect.rb, line 64
def write_as(formatter, io=$>)
  formatter.write_linear_gradient(self, io)
end