Class: DevSystem::ColorShell

Inherits:
Shell show all
Defined in:
lib/dev_system/subsystems/command/shells/color_shell.rb

Instance Attribute Summary

Attributes inherited from Liza::Controller

#menv

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Shell

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

Methods inherited from Liza::Controller

#`, `, attr_accessor, attr_reader, attr_writer, #attrs, box, #box, call, color, division, division!, division?, inherited, menv_accessor, menv_reader, menv_writer, on_connected, panel, #panel, plural, require, requirements, sh, #sh, singular, subsystem, subsystem!, subsystem?, subsystem_token, token

Methods inherited from Liza::Unit

_erbs_for, #add, add, cl, #cl, class_methods_defined, const_added, const_missing, constants_defined, define_error, descendants_select, division, erbs_available, erbs_defined, erbs_for, errors, #fetch, fetch, get, #get, instance_methods_defined, log, #log, log?, #log?, #log_array, log_array, log_hash, #log_hash, #log_level, log_level, #log_level?, log_level?, log_levels, #log_levels, #log_render_convert, #log_render_format, #log_render_in, #log_render_out, method_added, methods_defined, namespace, part, raise_error, #raise_error, reload!, #reload!, #render, #render!, #render_stack, renderable_formats_for, renderable_names, section, sections, #set, set, #settings, settings, singleton_method_added, sleep, #sleep, stick, #stick, sticks, #sticks, subclasses_select, subunits, system, #system, system?, test_class, time_diff, #time_diff

Constructor Details

#initialize(color) ⇒ ColorShell

Returns a new instance of ColorShell.



48
49
50
# File 'lib/dev_system/subsystems/command/shells/color_shell.rb', line 48

def initialize(color)
  @rgb = self.class.parse color
end

Class Method Details

.colorsObject

Colors



16
17
18
# File 'lib/dev_system/subsystems/command/shells/color_shell.rb', line 16

def self.colors
  PalletShell.colors
end

.parse(color) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/dev_system/subsystems/command/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



22
23
24
25
26
27
28
29
30
# File 'lib/dev_system/subsystems/command/shells/color_shell.rb', line 22

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



32
33
34
35
36
37
38
# File 'lib/dev_system/subsystems/command/shells/color_shell.rb', line 32

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



40
41
42
43
44
45
46
# File 'lib/dev_system/subsystems/command/shells/color_shell.rb', line 40

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

Instance Method Details

#to_rgbObject



56
57
58
# File 'lib/dev_system/subsystems/command/shells/color_shell.rb', line 56

def to_rgb
  @rgb
end

#to_sObject



52
53
54
# File 'lib/dev_system/subsystems/command/shells/color_shell.rb', line 52

def to_s
  @string ||= self.class.rgb_to_str @rgb
end