Class: WindowBlessing::Color

Inherits:
Struct
  • Object
show all
Includes:
Tools
Defined in:
lib/window_blessing/color.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Tools

#buffer, #clone_value, #color, #fill_line, #gen_array2d, #gray_screen_color, #log, #overlapping_span, #overlay2d, #overlay_span, #range_length, #resize2d, #rgb_screen_color, #subarray2d, #window

Constructor Details

#initialize(r = 0.0, g = r, b = r) ⇒ Color

Returns a new instance of Color.



19
20
21
22
23
24
# File 'lib/window_blessing/color.rb', line 19

def initialize(r=0.0, g=r, b=r)
  case r
  when String then self.hex=r
  else super r, g, b
  end
end

Instance Attribute Details

#bObject

Returns the value of attribute b

Returns:

  • (Object)

    the current value of b



2
3
4
# File 'lib/window_blessing/color.rb', line 2

def b
  @b
end

#gObject

Returns the value of attribute g

Returns:

  • (Object)

    the current value of g



2
3
4
# File 'lib/window_blessing/color.rb', line 2

def g
  @g
end

#rObject

Returns the value of attribute r

Returns:

  • (Object)

    the current value of r



2
3
4
# File 'lib/window_blessing/color.rb', line 2

def r
  @r
end

Class Method Details

.blackObject



6
# File 'lib/window_blessing/color.rb', line 6

def black;  Color.new(0) end

.blueObject



12
# File 'lib/window_blessing/color.rb', line 12

def blue;   Color.new(0,0,1) end

.cyanObject



15
# File 'lib/window_blessing/color.rb', line 15

def cyan;   Color.new(0,1,1) end

.grayObject



8
# File 'lib/window_blessing/color.rb', line 8

def gray;   Color.new(0.5) end

.greenObject



11
# File 'lib/window_blessing/color.rb', line 11

def green;  Color.new(0,1,0) end

.magentaObject



16
# File 'lib/window_blessing/color.rb', line 16

def magenta;Color.new(1,0,1) end

.redObject



10
# File 'lib/window_blessing/color.rb', line 10

def red;    Color.new(1,0,0) end

.whiteObject



7
# File 'lib/window_blessing/color.rb', line 7

def white;  Color.new(1) end

.yellowObject



14
# File 'lib/window_blessing/color.rb', line 14

def yellow; Color.new(1,1,0) end

Instance Method Details

#*(v) ⇒ Object



68
# File 'lib/window_blessing/color.rb', line 68

def *(v) v.kind_of?(Color) ? Color.new(r * v.r, g * v.g, b * v.b) : Color.new(r * v, g * v, b * v) end

#+(v) ⇒ Object



66
# File 'lib/window_blessing/color.rb', line 66

def +(v) v.kind_of?(Color) ? Color.new(r + v.r, g + v.g, b + v.b) : Color.new(r + v, g + v, b + v) end

#-(v) ⇒ Object



67
# File 'lib/window_blessing/color.rb', line 67

def -(v) v.kind_of?(Color) ? Color.new(r - v.r, g - v.g, b - v.b) : Color.new(r - v, g - v, b - v) end

#/(v) ⇒ Object



69
# File 'lib/window_blessing/color.rb', line 69

def /(v) v.kind_of?(Color) ? Color.new(r / v.r, g / v.g, b / v.b) : Color.new(r / v, g / v, b / v) end

#[](i) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/window_blessing/color.rb', line 46

def [](i)
  case i
  when 0, :r then r
  when 1, :g then g
  when 2, :b then b
  end
end

#[]=(key, v) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/window_blessing/color.rb', line 54

def []=(key,v)
  case key
  when 0, :r then self.r = v
  when 1, :g then self.g = v
  when 2, :b then self.b = v
  end
end

#b256Object



64
# File 'lib/window_blessing/color.rb', line 64

def b256; (b*255).to_i end

#brObject



26
27
28
# File 'lib/window_blessing/color.rb', line 26

def br
  (r + g + b) / 3.0
end

#g256Object



63
# File 'lib/window_blessing/color.rb', line 63

def g256; (g*255).to_i end

#hex=(hex) ⇒ Object



30
31
32
33
34
35
# File 'lib/window_blessing/color.rb', line 30

def hex=(hex)
  raise "invalid hex color #{hex.inspect}" unless hex[/#?(((..)(..)(..))|((.)(.)(.)))/]
  self.r = ($3 || ($7*2)).hex/255.0
  self.g = ($4 || ($8*2)).hex/255.0
  self.b = ($5 || ($9*2)).hex/255.0
end

#inspectObject



37
# File 'lib/window_blessing/color.rb', line 37

def inspect; "color#{self}" end

#r256Object



62
# File 'lib/window_blessing/color.rb', line 62

def r256; (r*255).to_i end

#to_aObject



41
# File 'lib/window_blessing/color.rb', line 41

def to_a; [r,g,b] end

#to_a256Object



40
# File 'lib/window_blessing/color.rb', line 40

def to_a256; [r256,g256,b256] end

#to_hexObject



39
# File 'lib/window_blessing/color.rb', line 39

def to_hex; "#%02x%02x%02x"%to_a256 end

#to_sObject



38
# File 'lib/window_blessing/color.rb', line 38

def to_s; "(#{r},#{g},#{b})" end

#to_screen_colorObject

to xterm pallette color



44
# File 'lib/window_blessing/color.rb', line 44

def to_screen_color; rgb_screen_color(r,g,b) end