Class: DevSystem::ColorShell

Inherits:
Shell show all
Defined in:
lib/dev_system/sub/shell/shells/color_shell.rb

Constant Summary collapse

@@colors =

Colors

{}

Class Method Summary collapse

Methods inherited from Shell

cruby?, engine, jruby?, linux?, mac?, os, ruby_version, unix?, windows?

Methods inherited from Liza::Controller

color, inherited, on_connected

Methods inherited from Liza::Unit

const_missing, division, part, system, #system, test_class

Class Method Details

.add(k, v) ⇒ Object



22
23
24
25
26
27
# File 'lib/dev_system/sub/shell/shells/color_shell.rb', line 22

def self.add k, v
  v = @@colors[v] if v.is_a? Symbol
  v = rgb_from_int v if v.is_a? Integer
  v = rgb_from_str v if v.is_a? String
  @@colors[k] = v
end

.colorsObject



18
19
20
# File 'lib/dev_system/sub/shell/shells/color_shell.rb', line 18

def self.colors
  @@colors
end

.parse(color) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/dev_system/sub/shell/shells/color_shell.rb', line 5

def self.parse color
  return colors[color] if colors.has_key? color

  return rgb_from_int(color) if color.is_a? Integer
  return rgb_from_str(color) if color.is_a? String
  
  color
end

.rgb_from_int(hex) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/dev_system/sub/shell/shells/color_shell.rb', line 31

def self.rgb_from_int hex
  raise "Invalid type: #{hex.class}" unless hex.is_a? Integer
  raise "Invalid hex color: #{hex}" unless 0 <= hex && hex <= 0xffffff
  
  r = (hex >> 16) & 0xff
  g = (hex >> 8) & 0xff
  b = hex & 0xff
  [r, g, b]
end

.rgb_from_str(hex) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/dev_system/sub/shell/shells/color_shell.rb', line 41

def self.rgb_from_str hex
  raise "Invalid type: #{hex.class}" unless hex.is_a? String
  raise "Invalid hex color: #{hex}" unless hex =~ /^#?[0-9a-fA-F]{6}$/

  hex = hex[1..-1] if hex[0] == "#"
  rgb_from_int hex.to_i(16)
end

.rgb_to_str(rgb) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/dev_system/sub/shell/shells/color_shell.rb', line 49

def self.rgb_to_str rgb
  raise "Invalid type: #{rgb.class}" unless rgb.is_a? Array
  raise "Invalid rgb color: #{rgb}" unless rgb.length == 3 && rgb.all? {|x| 0 <= x && x <= 0xff}

  r, g, b = rgb
  "##{"%02x" % r}#{"%02x" % g}#{"%02x" % b}"
end