Module: ImageVoodoo::Shapes
- Included in:
- ImageVoodoo
- Defined in:
- lib/image_voodoo/awt/shapes.rb
Overview
(Experimental) An attempt at some primitive drawing in images.
Instance Method Summary collapse
-
#add_border(options = {}) ⇒ Object
FIXME: if image has alpha values the border shows through since it is a solid fill.
- #as_color(color) ⇒ Object
- #border_style(options) ⇒ Object
- #paint_border(g, new_width, new_height, options) ⇒ Object
-
#rect(x, y, width, height, rgb, fill = true) ⇒ Object
AWT Draw a rectangle.
-
#rect_rounded(x, y, width, height, rgb, arc_width = 0, arc_height = 0, fill = true) ⇒ Object
AWT Draw a rounded rectangle.
-
#square(x, y, dim, rgb, fill = true) ⇒ Object
AWT Draw a square.
-
#square_rounded(x, y, dim, rgb, arc_width = 0, fill = true) ⇒ Object
AWT Draw a rounded square.
Instance Method Details
#add_border(options = {}) ⇒ Object
FIXME: if image has alpha values the border shows through since it is
a solid fill.
AWT (experimental) Add a border to the image and yield/return a new image. The following options are supported:
- width: How thick is the border (default: 3)
- color: Which color is the border (in rrggbb hex value)
- style: etched, raised, plain (default: plain)
13 14 15 16 17 18 19 20 21 |
# File 'lib/image_voodoo/awt/shapes.rb', line 13 def add_border( = {}) border_width = [:width].to_i || 2 new_width, new_height = width + 2*border_width, height + 2*border_width target = paint(BufferedImage.new(new_width, new_height, color_type)) do |g| paint_border(g, new_width, new_height, [:color]) g.draw_image(@src, nil, border_width, border_width) end block_given? ? yield(target) : target end |
#as_color(color) ⇒ Object
74 75 76 77 78 79 80 81 |
# File 'lib/image_voodoo/awt/shapes.rb', line 74 def as_color(color) paint do |g| old_color = g.color g.color = color yield g g.color = old_color end end |
#border_style(options) ⇒ Object
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/image_voodoo/awt/shapes.rb', line 29 def border_style() case ([:style] || "").to_s when "raised" then [:fill3DRect, true] when "etched" then [:fill3DRect, false] else [:fill_rect] end end |
#paint_border(g, new_width, new_height, options) ⇒ Object
23 24 25 26 27 |
# File 'lib/image_voodoo/awt/shapes.rb', line 23 def paint_border(g, new_width, new_height, ) g.color = hex_to_color([:color]) fill_method, *args = border_style() g.send fill_method, 0, 0, new_width, new_height, *args end |
#rect(x, y, width, height, rgb, fill = true) ⇒ Object
AWT Draw a rectangle
50 51 52 |
# File 'lib/image_voodoo/awt/shapes.rb', line 50 def rect(x, y, width, height, rgb, fill=true) rect_rounded(x, y, width, height, rgb, 0, 0, fill) end |
#rect_rounded(x, y, width, height, rgb, arc_width = 0, arc_height = 0, fill = true) ⇒ Object
AWT Draw a rounded rectangle
64 65 66 67 68 69 70 71 72 |
# File 'lib/image_voodoo/awt/shapes.rb', line 64 def rect_rounded(x, y, width, height, rgb, arc_width=0, arc_height=0, fill=true) as_color(hex_to_color(rgb)) do |g| if fill g.fill_round_rect x, y, width, height, arc_width, arc_height else g.draw_round_rect x, y, width, height, arc_width, arc_height end end end |
#square(x, y, dim, rgb, fill = true) ⇒ Object
AWT Draw a square
43 44 45 |
# File 'lib/image_voodoo/awt/shapes.rb', line 43 def square(x, y, dim, rgb, fill=true) square_rounded(x, y, dim, rgb, 0, fill) end |
#square_rounded(x, y, dim, rgb, arc_width = 0, fill = true) ⇒ Object
AWT Draw a rounded square
57 58 59 |
# File 'lib/image_voodoo/awt/shapes.rb', line 57 def square_rounded(x, y, dim, rgb, arc_width=0, fill=true) rect_rounded(x, y, dim, dim, rgb, arc_width, arc_width, fill) end |