Class: Colorable::Colorset

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/colorable/colorset.rb

Instance Method Summary collapse

Constructor Details

#initialize(opt = {}) ⇒ Colorset

Create a X11 colorset object.

options::keys should be:

+order+:: :NAME(default), :RGB, :HSB or :HEX can be specified.
+dir+:: set :+(default) to increment order, :- to decrement order.
+colorset+:: set original colorset other than X11 if any.


14
15
16
17
18
# File 'lib/colorable/colorset.rb', line 14

def initialize(opt={})
  opt = { order: :name, dir: :+ }.merge(opt)
  @pos = 0
  @colorset = opt[:colorset] ? opt[:colorset] : build_colorset(opt)
end

Instance Method Details

#at(pos = 0) ⇒ Object

Returns a top color object in colorset



27
28
29
# File 'lib/colorable/colorset.rb', line 27

def at(pos=0)
  @colorset[(@pos+pos)%size]
end

#each(&blk) ⇒ Object



22
23
24
# File 'lib/colorable/colorset.rb', line 22

def each(&blk)
  @colorset.each(&blk)
end

#find_index(color) ⇒ Object



49
50
51
52
# File 'lib/colorable/colorset.rb', line 49

def find_index(color)
  idx = @colorset.find_index { |c| c == color }
  (@pos+idx)%size if idx
end

#next(n = 1) ⇒ Object

Returns a next color object in colorset



32
33
34
35
# File 'lib/colorable/colorset.rb', line 32

def next(n=1)
  @pos = (@pos+n)%size
  at
end

#prev(n = 1) ⇒ Object

Returns a previous color object in colorset



38
39
40
41
# File 'lib/colorable/colorset.rb', line 38

def prev(n=1)
  @pos = (@pos-n)%size
  at
end

#reverseObject

Returns a reversed colorset



60
61
62
# File 'lib/colorable/colorset.rb', line 60

def reverse
  self.class.new colorset: @colorset.reverse
end

#rewindObject

Rewind a cursor to the top



44
45
46
47
# File 'lib/colorable/colorset.rb', line 44

def rewind
  @pos = 0
  at
end

#sort_by(&blk) ⇒ Object

Returns a sorted colorset defined by passed block



55
56
57
# File 'lib/colorable/colorset.rb', line 55

def sort_by(&blk)
  self.class.new colorset: @colorset.sort_by(&blk)
end

#to_sObject Also known as: inspect



64
65
66
# File 'lib/colorable/colorset.rb', line 64

def to_s
  "#<%s %d/%d pos='%s<%s/%s>'>" % [self.class, @pos, size, at.name, at._rgb, at._hsb]
end