Class: Sh::Plasma

Inherits:
Object show all
Defined in:
lib/sh_plasma.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width, height, grid_size = 5) ⇒ Plasma

Returns a new instance of Plasma.



5
6
7
8
# File 'lib/sh_plasma.rb', line 5

def initialize width, height, grid_size = 5
  @width, @height = width.to_f, height.to_f
@grid_size = grid_size.to_f
end

Instance Attribute Details

#grid_sizeObject (readonly)

Returns the value of attribute grid_size.



3
4
5
# File 'lib/sh_plasma.rb', line 3

def grid_size
  @grid_size
end

#heightObject (readonly)

Returns the value of attribute height.



3
4
5
# File 'lib/sh_plasma.rb', line 3

def height
  @height
end

#widthObject (readonly)

Returns the value of attribute width.



3
4
5
# File 'lib/sh_plasma.rb', line 3

def width
  @width
end

Instance Method Details

#generate_mapObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/sh_plasma.rb', line 10

def generate_map
  map = [[rand, rand, nil],[rand, rand, nil], nil]
  w = @width / 2
  h = @height / 2

  while w > @grid_size or h > @grid_size
    w /= 2
    h /= 2

    new_map = []
    map.each_cons(2) do |row, next_row|
      next_row ||= map.first

      r = []
      row.each_cons(2) do |v, nv|
        nv ||= row.first
        r << v
        r << (v + nv) / 2
      end
      r << nil
      new_map << r

      r = []
      (row.size - 1).times do |x|
        r << (row[x] + next_row[x]) / 2
        midpoint = nil
        if row[x+1]
          midpoint = (row[x] + next_row[x] + row[x+1] + next_row[x+1]) / 4
        else
          midpoint = (row[x] + next_row[x] + row.first + next_row.first) / 4
        end
        midpoint += displace(w + h)
        midpoint = midpoint > 1 ? 1 : midpoint < 0 ? 0 : midpoint
        r << midpoint
      end
      r << nil
      new_map << r
    end

    map = new_map << nil
  end
  # Get rid of all the nil values introduced as part of the processing
  map = map[0..-2].collect {|r| r[0..-2]}
end

#generate_pixmap(color1 = [0, 0, 0], color2 = [65535, 65535, 65535]) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/sh_plasma.rb', line 55

def generate_pixmap color1=[0, 0, 0], color2=[65535, 65535, 65535]
  if try_require 'gtk2'
    depth = 24
    begin
      depth = Gdk::Window.default_root_window.geometry.last
    rescue
      depth = 24
    end
    pixmap = Gdk::Pixmap.new(nil, @width, @height, depth)
    gc = Gdk::GC.new(pixmap)
    map = generate_map
    h = @height / map.size
    w = @width / map.first.size
    x = y = 0
    map.each do |row|
      row.each do |v|
        r1, g1, b1 = *color1
        r2, g2, b2 = *color2
        # Simulate alpha transparency overlay of one colour over the other
        red = v*r1+(1-v)*r2
        green = v*g1+(1-v)*g2
        blue = v*b1+(1-v)*b2
        # Draw rectangle in pixmap
        color = Gdk::Color.new red, green, blue
        gc.rgb_fg_color = color
        pixmap.draw_rectangle(gc, true, x, y, @grid_size, @grid_size)
        # Increment horizontal position
        x += w
      end
      x = 0
      y += h
    end
    return pixmap
  else
    raise "GTK is required to create a pixmap!"
  end
end