Class: Hansi::Color
Instance Attribute Summary collapse
-
#blue ⇒ Object
readonly
Returns the value of attribute blue.
-
#green ⇒ Object
readonly
Returns the value of attribute green.
-
#red ⇒ Object
readonly
Returns the value of attribute red.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #closest(set) ⇒ Object
- #distance(other) ⇒ Object
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
-
#initialize(red, green, blue) ⇒ Color
constructor
A new instance of Color.
- #inspect ⇒ Object
- #to_ansi(mode: Hansi.mode, **options) ⇒ Object
- #to_ansi_16colors(**options) ⇒ Object
- #to_ansi_24bit(**options) ⇒ Object
- #to_ansi_256colors(**options) ⇒ Object
- #to_ansi_88colors(**options) ⇒ Object
- #to_ansi_8colors(**options) ⇒ Object
- #to_css_rule ⇒ Object
- #to_i ⇒ Object
- #to_s ⇒ Object
- #to_web_name(**options) ⇒ Object
- #to_yuv ⇒ Object
Methods inherited from AnsiCode
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
#blue ⇒ Object (readonly)
Returns the value of attribute blue.
3 4 5 |
# File 'lib/hansi/color.rb', line 3 def blue @blue end |
#green ⇒ Object (readonly)
Returns the value of attribute green.
3 4 5 |
# File 'lib/hansi/color.rb', line 3 def green @green end |
#red ⇒ Object (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
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 |
#hash ⇒ Object
11 12 13 |
# File 'lib/hansi/color.rb', line 11 def hash to_i.hash end |
#inspect ⇒ Object
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, **) case Integer === mode ? mode : Integer(mode.to_s[/\d+/]) when 256 then to_ansi_256colors(**) when 24, TRUE_COLOR then to_ansi_24bit(**) when 88 then to_ansi_88colors(**) when 16 then to_ansi_16colors(**) when 8 then to_ansi_8colors(**) 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(**) from_palette('xterm', **) end |
#to_ansi_24bit(**options) ⇒ Object
80 81 82 |
# File 'lib/hansi/color.rb', line 80 def to_ansi_24bit(**) "\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(**) from_palette('xterm-256color', 'xterm', **) end |
#to_ansi_88colors(**options) ⇒ Object
88 89 90 |
# File 'lib/hansi/color.rb', line 88 def to_ansi_88colors(**) from_palette('xterm-88color', 'xterm', **) end |
#to_ansi_8colors(**options) ⇒ Object
96 97 98 |
# File 'lib/hansi/color.rb', line 96 def to_ansi_8colors(**) from_palette('ansi', **) end |
#to_css_rule ⇒ Object
64 65 66 |
# File 'lib/hansi/color.rb', line 64 def to_css_rule "color: #{self};" end |
#to_i ⇒ Object
52 53 54 |
# File 'lib/hansi/color.rb', line 52 def to_i (red << 16) + (green << 8) + blue end |
#to_s ⇒ Object
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(**) from_palette('web', **) end |
#to_yuv ⇒ Object
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 |