Class: Ranicoma::Design::Base

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/ranicoma/design/base.rb

Direct Known Subclasses

Geji, PolyPoly, RotObj, SpBox

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

#element

Constructor Details

#initialize(rng) ⇒ Base

Returns a new instance of Base.



33
34
35
# File 'lib/ranicoma/design/base.rb', line 33

def initialize(rng)
  @rng=rng
end

Instance Attribute Details

#rngObject (readonly)

Returns the value of attribute rng.



45
46
47
# File 'lib/ranicoma/design/base.rb', line 45

def rng
  @rng
end

Class Method Details

.add_subclass(cls) ⇒ Object



10
11
12
13
# File 'lib/ranicoma/design/base.rb', line 10

def self.add_subclass(cls)
  @subclasses ||= []
  @subclasses.push(cls)
end

.inherited(subclass) ⇒ Object



83
84
85
# File 'lib/ranicoma/design/base.rb', line 83

def Base.inherited(subclass)
  Base.add_subclass(subclass)
end

.subclassesObject



29
30
31
# File 'lib/ranicoma/design/base.rb', line 29

def Base.subclasses
  @subclasses
end

Instance Method Details

#fill(col) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/ranicoma/design/base.rb', line 60

def fill(col)
  case col
  when Array
    { style:"fill:rgb(#{col.join(",")})" }
  when /^\d+\,\d+\,\d+$/
    { style:"fill:rgb(#{col})" }
  else
    { style:"fill:#{col}" }
  end
end

#points_str(pts) ⇒ Object



37
38
39
# File 'lib/ranicoma/design/base.rb', line 37

def points_str(pts)
  pts.map{ |e| e.join(",") }.join(" ")
end

#rainbow(t, mx = ->(v){v}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ranicoma/design/base.rb', line 15

def rainbow(t,mx=->(v){v})
  f = lambda{ |t0|
    v = lambda{ |x|
      case x
      when 0..1 then x
      when 1..2 then 2-x
      else 0
      end
    }[t0 % 3]
    (mx[v]*255).round
  }
  [f[t],f[t+1],f[t+2]]
end

#rand(*args) ⇒ Object



47
48
49
# File 'lib/ranicoma/design/base.rb', line 47

def rand(*args)
  rng.rand(*args)
end

#rand_rotate(ary) ⇒ Object



55
56
57
58
# File 'lib/ranicoma/design/base.rb', line 55

def rand_rotate(ary)
  ix=rng.rand(ary.size)
  ary[ix,ary.size-ix] + ary[0,ix]
end

#rect_element(rc, col) ⇒ Object



41
42
43
# File 'lib/ranicoma/design/base.rb', line 41

def rect_element(rc, col)
  element("rect", **rectpos(rc), **fill(col))
end

#rectpos(rc) ⇒ Object



51
52
53
# File 'lib/ranicoma/design/base.rb', line 51

def rectpos(rc)
  { x:rc.x, y:rc.y, width:rc.w, height:rc.h }
end

#stroke(col, w) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ranicoma/design/base.rb', line 71

def stroke(col, w)
  c=case col
  when Array
    "rgb(#{col.join(",")})"
  when /^\d+\,\d+\,\d+$/
    "rgb(#{col})"
  else
    col
  end
  { stroke:c, "stroke-width":w }
end