Class: Vic::Color

Inherits:
Object
  • Object
show all
Defined in:
lib/vic/color.rb

Defined Under Namespace

Classes: ColorError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Color

Returns a new instance of Color.



5
6
7
# File 'lib/vic/color.rb', line 5

def initialize(value)
  @value = value
end

Instance Attribute Details

#valueObject

Returns the value of attribute value.



3
4
5
# File 'lib/vic/color.rb', line 3

def value
  @value
end

Instance Method Details

#cterm?TrueClass, FalseClass

Returns true if the color value is cterm compatible color and false if it’s not

Returns:

  • (TrueClass, FalseClass)

    whether or not the color value is cterm compatible



46
47
48
# File 'lib/vic/color.rb', line 46

def cterm?
  @value.kind_of?(Fixnum) and @value.between?(0, 255)
end

#gui?TrueClass, FalseClass

Returns true if the color value is a gui compatible color and false if it’s not

Returns:

  • (TrueClass, FalseClass)

    whether or not the color value is gui compatible



57
58
59
# File 'lib/vic/color.rb', line 57

def gui?
  hexadecimal?
end

#hexadecimal?TrueClass, FalseClass

Returns true if the color value is a hexadecimal color and false if it’s not

Returns:

  • (TrueClass, FalseClass)

    whether or not the color value is hexadecimal



67
68
69
70
# File 'lib/vic/color.rb', line 67

def hexadecimal?
  # Both standard and shorthand (CSS) style hexadecimal color value.
  not cterm? and /\A#?(?:[0-9a-f]{3}|[0-9a-f]{6})\z/io.match(@value.to_s)
end

#none?TrueClass, FalseClass

Returns true if the color value is either empty or set to “NONE”

Returns:

  • (TrueClass, FalseClass)

    whether or not the color value is empty or “NONE”



78
79
80
# File 'lib/vic/color.rb', line 78

def none?
  @value.to_s.empty? or /\Anone\z/io.match(@value.to_s)
end

#to_ctermFixnum

Convert the color value to a cterm compatible color

Returns:

  • (Fixnum)

    the color as either “NONE” or cterm color



31
32
33
34
35
36
37
# File 'lib/vic/color.rb', line 31

def to_cterm
  return @value if cterm?
  return Convert.hex_to_xterm(to_standard_hex) if hexadecimal?
  return :NONE if none?

  raise ColorError.new "can't convert \"#{ @value }\" to cterm"
end

#to_guiSymbol, String

Convert the color value to a hexadecimal color

Returns:

  • (Symbol, String)

    the color as either “NONE” or hexadecimal



17
18
19
20
21
22
23
# File 'lib/vic/color.rb', line 17

def to_gui
  return to_standard_hex if hexadecimal?
  return Convert.xterm_to_hex(@value) if cterm?
  return :NONE if none?

  raise ColorError.new "can't convert \"#{ @value }\" to gui"
end