Class: Hansi::Color

Inherits:
AnsiCode show all
Defined in:
lib/hansi/color.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from AnsiCode

#to_ansi_code, #to_css

Constructor Details

#initialize(red, green, blue) ⇒ Color

Returns a new instance of Color.



6
7
8
9
# File 'lib/hansi/color.rb', line 6

def initialize(red, green, blue)
  @red, @green, @blue = red, green, blue
  @distance_cache     = {}
end

Instance Attribute Details

#blueObject (readonly)

Returns the value of attribute blue.



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

def blue
  @blue
end

#greenObject (readonly)

Returns the value of attribute green.



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

def green
  @green
end

#redObject (readonly)

Returns the value of attribute red.



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

def red
  @red
end

Instance Method Details

#==(other) ⇒ Object



15
16
17
# File 'lib/hansi/color.rb', line 15

def ==(other)
  other.class == self.class and other.to_i == self.to_i
end

#closest(set) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/hansi/color.rb', line 32

def closest(set)
  if set.respond_to? :to_hash
    hash = set.to_hash
    hash.key(closest(hash.values))
  elsif set.include? self
    self
  else
    set.grep(Color).min_by { |c| distance(c) }
  end
end

#distance(other) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/hansi/color.rb', line 23

def distance(other)
  distance_cache[other.to_i] ||= other.distance_cache[to_i] || begin
    y1, u1, v1 = to_yuv
    y2, u2, v2 = other.to_yuv
    dy, du, dv = y1 - y2, u1 - u2, v1 - v2
    Math.sqrt(dy**2 + du**2 + dv**2)
  end
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/hansi/color.rb', line 19

def eql?(other)
  other.class.eql?(self.class) and other.to_i.eql?(self.to_i)
end

#hashObject



11
12
13
# File 'lib/hansi/color.rb', line 11

def hash
  to_i.hash
end

#inspectObject



56
57
58
# File 'lib/hansi/color.rb', line 56

def inspect
  "#<%p:%d,%d,%d>" % [self.class, red, green, blue]
end

#to_ansi(mode: Hansi.mode, **options) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/hansi/color.rb', line 68

def to_ansi(mode: Hansi.mode, **options)
  case Integer === mode ? mode : Integer(mode.to_s[/\d+/])
  when 256            then to_ansi_256colors(**options)
  when 24, TRUE_COLOR then to_ansi_24bit(**options)
  when 88             then to_ansi_88colors(**options)
  when 16             then to_ansi_16colors(**options)
  when 8              then to_ansi_8colors(**options)
  when 1, 0           then ""
  else raise ArgumentError, "unknown mode %p" % mode
  end
end

#to_ansi_16colors(**options) ⇒ Object



92
93
94
# File 'lib/hansi/color.rb', line 92

def to_ansi_16colors(**options)
  from_palette('xterm', **options)
end

#to_ansi_24bit(**options) ⇒ Object



80
81
82
# File 'lib/hansi/color.rb', line 80

def to_ansi_24bit(**options)
  "\e[38;2;%d;%d;%dm" % [red, green, blue]
end

#to_ansi_256colors(**options) ⇒ Object



84
85
86
# File 'lib/hansi/color.rb', line 84

def to_ansi_256colors(**options)
  from_palette('xterm-256color', 'xterm', **options)
end

#to_ansi_88colors(**options) ⇒ Object



88
89
90
# File 'lib/hansi/color.rb', line 88

def to_ansi_88colors(**options)
  from_palette('xterm-88color', 'xterm', **options)
end

#to_ansi_8colors(**options) ⇒ Object



96
97
98
# File 'lib/hansi/color.rb', line 96

def to_ansi_8colors(**options)
  from_palette('ansi', **options)
end

#to_css_ruleObject



64
65
66
# File 'lib/hansi/color.rb', line 64

def to_css_rule
  "color: #{self};"
end

#to_iObject



52
53
54
# File 'lib/hansi/color.rb', line 52

def to_i
  (red << 16) + (green << 8) + blue
end

#to_sObject



60
61
62
# File 'lib/hansi/color.rb', line 60

def to_s
  "#%06x" % to_i
end

#to_web_name(**options) ⇒ Object



100
101
102
# File 'lib/hansi/color.rb', line 100

def to_web_name(**options)
  from_palette('web', **options)
end

#to_yuvObject



43
44
45
46
47
48
49
50
# File 'lib/hansi/color.rb', line 43

def to_yuv
  @yuv ||= begin
    y =  (0.257 * red) + (0.504 * green) + (0.098 * blue) + 16
    u = -(0.148 * red) - (0.291 * green) + (0.439 * blue) + 128
    v =  (0.439 * red) - (0.368 * green) - (0.071 * blue) + 128
    [y, u, v].freeze
  end
end