Class: DYI::Drawing::ColorEffect::LinearGradient
- Inherits:
-
Object
- Object
- DYI::Drawing::ColorEffect::LinearGradient
- Defined in:
- lib/ironruby.rb,
lib/dyi/drawing/color_effect.rb
Overview
Constant Summary collapse
- SPREAD_METHODS =
['pad', 'reflect', 'repeat']
Instance Attribute Summary collapse
- #spread_method ⇒ Object
- #start_point ⇒ Object readonly
- #stop_point ⇒ Object readonly
Class Method Summary collapse
Instance Method Summary collapse
- #add_color(offset, color) ⇒ Object
- #add_color_opacity(offset, color, opacity) ⇒ Object
- #add_opacity(offset, opacity) ⇒ Object
- #child_elements ⇒ Object
- #cls_wrap_mode ⇒ Object
- #color? ⇒ Boolean
- #create_cls_brush(opacity = nil, shape = nil) ⇒ Object
-
#initialize(start_point = [0,0], stop_point = [1,0], spread_method = nil) ⇒ LinearGradient
constructor
A new instance of LinearGradient.
- #write_as(formatter, io = $>) ⇒ Object
Constructor Details
#initialize(start_point = [0,0], stop_point = [1,0], spread_method = nil) ⇒ LinearGradient
Returns a new instance of LinearGradient.
33 34 35 36 37 38 |
# File 'lib/dyi/drawing/color_effect.rb', line 33 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 |
Instance Attribute Details
#spread_method ⇒ Object
31 32 33 |
# File 'lib/dyi/drawing/color_effect.rb', line 31 def spread_method @spread_method end |
#start_point ⇒ Object (readonly)
31 32 33 |
# File 'lib/dyi/drawing/color_effect.rb', line 31 def start_point @start_point end |
#stop_point ⇒ Object (readonly)
31 32 33 |
# File 'lib/dyi/drawing/color_effect.rb', line 31 def stop_point @stop_point end |
Class Method Details
.simple_gradation(derection, *colors) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/dyi/drawing/color_effect.rb', line 73 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 |
Instance Method Details
#add_color(offset, color) ⇒ Object
40 41 42 |
# File 'lib/dyi/drawing/color_effect.rb', line 40 def add_color(offset, color) @child_elements.push(GradientStop.new(offset, :color => color)) end |
#add_color_opacity(offset, color, opacity) ⇒ Object
48 49 50 |
# File 'lib/dyi/drawing/color_effect.rb', line 48 def add_color_opacity(offset, color, opacity) @child_elements.push(GradientStop.new(offset, :color => color, :opacity => opacity)) end |
#add_opacity(offset, opacity) ⇒ Object
44 45 46 |
# File 'lib/dyi/drawing/color_effect.rb', line 44 def add_opacity(offset, opacity) @child_elements.push(GradientStop.new(offset, :opacity => opacity)) end |
#child_elements ⇒ Object
57 58 59 |
# File 'lib/dyi/drawing/color_effect.rb', line 57 def child_elements @child_elements.clone end |
#cls_wrap_mode ⇒ Object
219 220 221 222 223 224 225 226 |
# File 'lib/ironruby.rb', line 219 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? ⇒ Boolean
61 62 63 |
# File 'lib/dyi/drawing/color_effect.rb', line 61 def color? true end |
#create_cls_brush(opacity = nil, shape = nil) ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/ironruby.rb', line 176 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 |
#write_as(formatter, io = $>) ⇒ Object
65 66 67 |
# File 'lib/dyi/drawing/color_effect.rb', line 65 def write_as(formatter, io=$>) formatter.write_linear_gradient(self, io) end |