Class: Colors::Xterm256

Inherits:
AbstractColor show all
Includes:
Helper
Defined in:
lib/colors/xterm256.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractColor

#desaturate, #inspect

Constructor Details

#initialize(code) ⇒ Xterm256

Returns a new instance of Xterm256.



5
6
7
8
9
10
# File 'lib/colors/xterm256.rb', line 5

def initialize(code)
  unless 16 <= code && code <= 255
    raise ArgumentError, "code should be in 16..255, but #{code} is given"
  end
  @code = code
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



12
13
14
# File 'lib/colors/xterm256.rb', line 12

def code
  @code
end

Instance Method Details

#==(other) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/colors/xterm256.rb', line 14

def ==(other)
  case other
  when Xterm256
    code == other.code
  else
    super
  end
end

#to_grey_levelObject



42
43
44
45
46
47
48
49
50
51
# File 'lib/colors/xterm256.rb', line 42

def to_grey_level
  if code < 232
    r, g, b = to_rgb_components
    y = Convert.rgb_to_greyscale(r, g, b)
    canonicalize_component_to_rational(y, :grey)
  else
    grey = 10*(code - 232) + 8
    canonicalize_component_from_integer(grey, :grey)
  end
end

#to_rgbObject



53
54
55
# File 'lib/colors/xterm256.rb', line 53

def to_rgb
  RGB.new(*to_rgb_components)
end

#to_rgb_componentsObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/colors/xterm256.rb', line 23

def to_rgb_components
  if code < 232
    x = code - 16
    x, b = x.divmod(6)
    r, g = x.divmod(6)
    r = 40*r + 55 if r > 0
    g = 40*g + 55 if g > 0
    b = 40*b + 55 if b > 0
    [
      canonicalize_component_from_integer(r, :r),
      canonicalize_component_from_integer(g, :r),
      canonicalize_component_from_integer(b, :r)
    ]
  else
    grey = to_grey_level
    [grey, grey, grey]
  end
end