Class: Term::ANSIColor::RGBTriple

Inherits:
Object
  • Object
show all
Includes:
Term::ANSIColor::RGBColorMetricsHelpers::WeightedEuclideanDistance
Defined in:
lib/term/ansicolor/rgb_triple.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Term::ANSIColor::RGBColorMetricsHelpers::WeightedEuclideanDistance

#weighted_euclidean_distance_to

Constructor Details

#initialize(red, green, blue) ⇒ RGBTriple

Returns a new instance of RGBTriple.



61
62
63
64
65
# File 'lib/term/ansicolor/rgb_triple.rb', line 61

def initialize(red, green, blue)
  @values = [ red, green, blue ].map { |v|
    [ [ Integer(v), 0 ].max, 0xff ].min
  }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



170
171
172
173
174
175
176
# File 'lib/term/ansicolor/rgb_triple.rb', line 170

def method_missing(name, *args, &block)
  if Term::ANSIColor::HSLTriple.method_defined?(name)
    to_hsl_triple.send(name, *args, &block)
  else
    super
  end
end

Class Method Details

.[](thing) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/term/ansicolor/rgb_triple.rb', line 48

def self.[](thing)
  case
  when thing.respond_to?(:to_rgb_triple) then thing.to_rgb_triple
  when thing.respond_to?(:to_ary)        then from_array(thing.to_ary)
  when thing.respond_to?(:to_str)
    thing = thing.to_str
    from_html(thing.sub(/\Aon_/, '')) || from_css(thing) ||
      Term::ANSIColor::HSLTriple.from_css(thing).full?(:to_rgb_triple)
  when thing.respond_to?(:to_hash)       then from_hash(thing.to_hash)
  else raise ArgumentError, "cannot convert #{thing.inspect} into #{self}"
  end
end

.from_array(array) ⇒ Object



44
45
46
# File 'lib/term/ansicolor/rgb_triple.rb', line 44

def self.from_array(array)
  new(*array)
end

.from_css(css) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/term/ansicolor/rgb_triple.rb', line 27

def self.from_css(css)
  case css
  when /\A\s*rgb\(\s*([^%\s]+)\s*%\s*,\s*([^%\s]+)\s*%\s*,\s*([^%\s]+)\s*%\s*\)\z/
    new(*$~.captures.map { |c| convert_value(((Float(c) / 100) * 0xff).round) })
  when /\A\s*rgb\(\s*([^,\s]+)\s*,\s*([^,\s]+)\s*,\s*([^\)\s]+)\s*\)\z/
    new(*$~.captures.map { |c| convert_value((Float(c)).round) })
  end
end

.from_hash(options) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/term/ansicolor/rgb_triple.rb', line 36

def self.from_hash(options)
  new(
    convert_value(options[:red]),
    convert_value(options[:green]),
    convert_value(options[:blue])
  )
end

.from_html(html) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/term/ansicolor/rgb_triple.rb', line 18

def self.from_html(html)
  case html
  when /\A#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})\z/i
    new(*$~.captures.map { |c| convert_value(c.to_i(16)) })
  when /\A#([0-9a-f])([0-9a-f])([0-9a-f])\z/i
    new(*$~.captures.map { |c| convert_value((c + c).to_i(16)) })
  end
end

Instance Method Details

#==(other) ⇒ Object



132
133
134
# File 'lib/term/ansicolor/rgb_triple.rb', line 132

def ==(other)
  @values == other.to_rgb_triple.values
end

#blueObject



75
76
77
# File 'lib/term/ansicolor/rgb_triple.rb', line 75

def blue
  @values[2]
end

#blue_pObject



91
92
93
# File 'lib/term/ansicolor/rgb_triple.rb', line 91

def blue_p
  percentages[2]
end

#color(string) ⇒ Object



136
137
138
# File 'lib/term/ansicolor/rgb_triple.rb', line 136

def color(string)
  Term::ANSIColor.color(self, string)
end

#css(percentage: false) ⇒ Object



109
110
111
112
113
114
115
# File 'lib/term/ansicolor/rgb_triple.rb', line 109

def css(percentage: false)
  if percentage
    "rgb(%s%%,%s%%,%s%%)" % @values.map { |v| 100.0 * v / 255 }
  else
    "rgb(%u,%u,%u)" % @values
  end
end

#distance_to(other, options = {}) ⇒ Object



140
141
142
143
# File 'lib/term/ansicolor/rgb_triple.rb', line 140

def distance_to(other, options = {})
  options[:metric] ||= RGBColorMetrics::CIELab
  options[:metric].distance(self, other)
end

#gradient_to(other, options = {}) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/term/ansicolor/rgb_triple.rb', line 151

def gradient_to(other, options = {})
  options[:steps] ||= 16
  steps = options[:steps].to_i
  steps < 2 and raise ArgumentError, 'at least 2 steps are required'
  changes = other.values.zip(@values).map { |x, y| x - y }
  current = self
  gradient = [ current.dup ]
  s = steps - 1
  while s > 1
    current = current.dup
    gradient << current
    3.times do |i|
      current.values[i] += changes[i] / (steps - 1)
    end
    s -= 1
  end
  gradient << other
end

#gray?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/term/ansicolor/rgb_triple.rb', line 99

def gray?
  red != 0 && red != 0xff && red == green && green == blue && blue == red
end

#greenObject



71
72
73
# File 'lib/term/ansicolor/rgb_triple.rb', line 71

def green
  @values[1]
end

#green_pObject



87
88
89
# File 'lib/term/ansicolor/rgb_triple.rb', line 87

def green_p
  percentages[1]
end

#htmlObject



103
104
105
106
107
# File 'lib/term/ansicolor/rgb_triple.rb', line 103

def html
  s = '#'
  @values.each { |c| s << '%02x' % c }
  s
end

#initialize_copy(other) ⇒ Object



145
146
147
148
149
# File 'lib/term/ansicolor/rgb_triple.rb', line 145

def initialize_copy(other)
  r = super
  other.instance_variable_set :@values, @values.dup
  r
end

#invertObject



95
96
97
# File 'lib/term/ansicolor/rgb_triple.rb', line 95

def invert
  self.class.new(255 - red, 255 - green, 255 - blue)
end

#percentagesObject



79
80
81
# File 'lib/term/ansicolor/rgb_triple.rb', line 79

def percentages
  @percentages ||= @values.map { |v| 100 * v / 255.0 }
end

#redObject



67
68
69
# File 'lib/term/ansicolor/rgb_triple.rb', line 67

def red
  @values[0]
end

#red_pObject



83
84
85
# File 'lib/term/ansicolor/rgb_triple.rb', line 83

def red_p
  percentages[0]
end

#to_aObject



128
129
130
# File 'lib/term/ansicolor/rgb_triple.rb', line 128

def to_a
  @values.dup
end

#to_hsl_tripleObject



121
122
123
# File 'lib/term/ansicolor/rgb_triple.rb', line 121

def to_hsl_triple
  Term::ANSIColor::HSLTriple.from_rgb_triple(self)
end

#to_rgb_tripleObject



117
118
119
# File 'lib/term/ansicolor/rgb_triple.rb', line 117

def to_rgb_triple
  self
end