Module: Skylab::Myterm::Color

Defined in:
lib/myterm/api.rb

Constant Summary collapse

TARGET_PLACES =

each component of an #rrggbb hexadecimal color has 2 places

2
DIVISOR =
16 ** TARGET_PLACES

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.[](obj) ⇒ Object



34
35
36
# File 'lib/myterm/api.rb', line 34

def [] obj
  obj.extend self
end

.dup(color) ⇒ Object



38
39
40
# File 'lib/myterm/api.rb', line 38

def dup color
  self[color.dup]
end

Instance Method Details

#alpha=(mixed) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/myterm/api.rb', line 8

def alpha= mixed
  if mixed.kind_of?(String)
    md = /\A(\d+(?:\.\d+)?)%?\z/.match(mixed) or
      raise ValidationError.new("invalid format for percent #{val.inspect} -- expecting e.g. \"58%\"")
    mixed = md[1].to_f
  end
  (0.0..100.0).include?(mixed) or
    raise ValidationError.new("Percent value (#{mixed}%) must be between 0 and 100 inclusive.")
  self[3] = ChannelScalarNormalized[mixed / 100.0]
end

#int_to_hex(int) ⇒ Object



27
28
29
30
# File 'lib/myterm/api.rb', line 27

def int_to_hex int
  int.respond_to?(:to_hex) and return int.to_hex
  (int.to_f / DIVISOR).round.to_s(16).rjust(TARGET_PLACES, '0')
end

#to_hexObject



19
20
21
# File 'lib/myterm/api.rb', line 19

def to_hex
  '#' + self.map{ |x| int_to_hex(x) }.join('')
end