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

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)


15
16
17
18
19
20
21
22
23
# File 'lib/image_voodoo/awt/shapes.rb', line 15

def add_border(options = {})
  border_width = options[: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, options)
    g.draw_image(@src, nil, border_width, border_width)
  end
  block_given? ? yield(target) : target
end

#as_color(color) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/image_voodoo/awt/shapes.rb', line 76

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



31
32
33
34
35
36
37
38
39
40
# File 'lib/image_voodoo/awt/shapes.rb', line 31

def border_style(options)
  case (options[: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



25
26
27
28
29
# File 'lib/image_voodoo/awt/shapes.rb', line 25

def paint_border(g, new_width, new_height, options)
  g.color = hex_to_color(options[:color])
  fill_method, *args = border_style(options)
  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



52
53
54
# File 'lib/image_voodoo/awt/shapes.rb', line 52

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



66
67
68
69
70
71
72
73
74
# File 'lib/image_voodoo/awt/shapes.rb', line 66

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



45
46
47
# File 'lib/image_voodoo/awt/shapes.rb', line 45

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



59
60
61
# File 'lib/image_voodoo/awt/shapes.rb', line 59

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