Class: PryTheme::RGB Private
- Inherits:
-
Object
- Object
- PryTheme::RGB
- Defined in:
- lib/pry-theme/rgb.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Represents an RGB colour. It’s possible to convert an RGB instance into HEX or TERM colours. However, this conversion is half-duplex. If an RGB instance gets converted to TERM format, there is a high chance that it will be approximated to fit in range of colour model (colour model can be set via an argument of the conversion method). This class validates its input (you won’t see malformed of nonexistent RGB colours).
Constant Summary collapse
- LINUX =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
8 colours. For the standard GNU/Linux terminal emulator.
[ [ 0, 0, 0], [128, 0, 0], [ 0, 128, 0], [128, 128, 0], [ 0, 0, 128], [128, 0, 128], [ 0, 128, 128], [192, 192, 192] ]
- SYSTEM =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
16 colours. For cmd.exe on Windows and other miserable terminals.
LINUX + [ [128, 128, 128], [255, 0, 0], [ 0, 255, 0], [255, 255, 0], [ 0, 0, 255], [255, 0, 255], [ 0, 255, 255], [255, 255, 255] ]
- COLORS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
The next 216 colours. For men.
[ [ 0, 0, 0], [ 0, 0, 95], [ 0, 0, 135], [ 0, 0, 175], [ 0, 0, 215], [ 0, 0, 255], [ 0, 95, 0], [ 0, 95, 95], [ 0, 95, 135], [ 0, 95, 175], [ 0, 95, 215], [ 0, 95, 255], [ 0, 135, 0], [ 0, 135, 95], [ 0, 135, 135], [ 0, 135, 175], [ 0, 135, 215], [ 0, 135, 255], [ 0, 175, 0], [ 0, 175, 95], [ 0, 175, 135], [ 0, 175, 175], [ 0, 175, 215], [ 0, 175, 255], [ 0, 215, 0], [ 0, 215, 95], [ 0, 215, 135], [ 0, 215, 175], [ 0, 215, 215], [ 0, 215, 255], [ 0, 255, 0], [ 0, 255, 95], [ 0, 255, 135], [ 0, 255, 175], [ 0, 255, 215], [ 0, 255, 255], [ 95, 0, 0], [ 95, 0, 95], [ 95, 0, 135], [ 95, 0, 175], [ 95, 0, 215], [ 95, 0, 255], [ 95, 95, 0], [ 95, 95, 95], [ 95, 95, 135], [ 95, 95, 175], [ 95, 95, 215], [ 95, 95, 255], [ 95, 135, 0], [ 95, 135, 95], [ 95, 135, 135], [ 95, 135, 175], [ 95, 135, 215], [ 95, 135, 255], [ 95, 175, 0], [ 95, 175, 95], [ 95, 175, 135], [ 95, 175, 175], [ 95, 175, 215], [ 95, 175, 255], [ 95, 215, 0], [ 95, 215, 95], [ 95, 215, 135], [ 95, 215, 175], [ 95, 215, 215], [ 95, 215, 255], [ 95, 255, 0], [ 95, 255, 95], [ 95, 255, 135], [ 95, 255, 175], [ 95, 255, 215], [ 95, 255, 255], [135, 0, 0], [135, 0, 95], [135, 0, 135], [135, 0, 175], [135, 0, 215], [135, 0, 255], [135, 95, 0], [135, 95, 95], [135, 95, 135], [135, 95, 175], [135, 95, 215], [135, 95, 255], [135, 135, 0], [135, 135, 95], [135, 135, 135], [135, 135, 175], [135, 135, 215], [135, 135, 255], [135, 175, 0], [135, 175, 95], [135, 175, 135], [135, 175, 175], [135, 175, 215], [135, 175, 255], [135, 215, 0], [135, 215, 95], [135, 215, 135], [135, 215, 175], [135, 215, 215], [135, 215, 255], [135, 255, 0], [135, 255, 95], [135, 255, 135], [135, 255, 175], [135, 255, 215], [135, 255, 255], [175, 0, 0], [175, 0, 95], [175, 0, 135], [175, 0, 175], [175, 0, 215], [175, 0, 255], [175, 95, 0], [175, 95, 95], [175, 95, 135], [175, 95, 175], [175, 95, 215], [175, 95, 255], [175, 135, 0], [175, 135, 95], [175, 135, 135], [175, 135, 175], [175, 135, 215], [175, 135, 255], [175, 175, 0], [175, 175, 95], [175, 175, 135], [175, 175, 175], [175, 175, 215], [175, 175, 255], [175, 215, 0], [175, 215, 95], [175, 215, 135], [175, 215, 175], [175, 215, 215], [175, 215, 255], [175, 255, 0], [175, 255, 95], [175, 255, 135], [175, 255, 175], [175, 255, 215], [175, 255, 255], [215, 0, 0], [215, 0, 95], [215, 0, 135], [215, 0, 175], [215, 0, 215], [215, 0, 255], [215, 95, 0], [215, 95, 95], [215, 95, 135], [215, 95, 175], [215, 95, 215], [215, 95, 255], [215, 135, 0], [215, 135, 95], [215, 135, 135], [215, 135, 175], [215, 135, 215], [215, 135, 255], [215, 175, 0], [215, 175, 95], [215, 175, 135], [215, 175, 175], [215, 175, 175], [215, 175, 215], [215, 175, 255], [215, 215, 0], [215, 215, 95], [215, 215, 135], [215, 215, 175], [215, 215, 215], [215, 215, 255], [215, 255, 0], [215, 255, 95], [215, 255, 135], [215, 255, 175], [215, 255, 215], [215, 255, 255], [255, 0, 0], [255, 0, 95], [255, 0, 135], [255, 0, 175], [255, 0, 215], [255, 0, 255], [255, 95, 0], [255, 95, 95], [255, 95, 135], [255, 95, 175], [255, 95, 215], [255, 95, 255], [255, 135, 0], [255, 135, 95], [255, 135, 135], [255, 135, 175], [255, 135, 215], [255, 135, 255], [255, 175, 0], [255, 175, 95], [255, 175, 135], [255, 175, 175], [255, 175, 215], [255, 175, 255], [255, 215, 0], [255, 215, 95], [255, 215, 135], [255, 215, 175], [255, 215, 215], [255, 215, 255], [255, 255, 0], [255, 255, 95], [255, 255, 135], [255, 255, 175], [255, 255, 215] ]
- GREYSCALE =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
The next 16 colours. For zen.
(0x08..0xEE).step(0x0A).map { |v| [v] * 3 }
- TABLE =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Combine everything into a full featured 256 colour RGB model.
SYSTEM + COLORS + GREYSCALE
- BYTEPOINTS_256 =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
The key points that are used to calculate the nearest match of an RGB.
[0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff]
Instance Method Summary collapse
-
#initialize(value) ⇒ RGB
constructor
private
A new instance of RGB.
- #inspect ⇒ String private
- #to_a ⇒ Array<Integer> private
- #to_css ⇒ String private
-
#to_hex ⇒ HEX
private
Converts the RGB to a HEX colour equivalent.
- #to_s ⇒ String private
-
#to_term(color_model = 256) ⇒ TERM
private
Converts the RGB to a terminal colour equivalent.
Constructor Details
#initialize(value) ⇒ RGB
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of RGB.
103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/pry-theme/rgb.rb', line 103 def initialize(value) @value = case value when Array validate_array(value) value when String validate_array(value = value.scan(/\d+/).map!(&:to_i)) value else raise TypeError, "can't convert #{ value.class } into PryTheme::RGB" end end |
Instance Method Details
#inspect ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
118 119 120 |
# File 'lib/pry-theme/rgb.rb', line 118 def inspect "(RGB: #{ to_s })" end |
#to_a ⇒ Array<Integer>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
156 157 158 |
# File 'lib/pry-theme/rgb.rb', line 156 def to_a @value end |
#to_css ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
163 164 165 |
# File 'lib/pry-theme/rgb.rb', line 163 def to_css "rgb(#{ to_s })" end |
#to_hex ⇒ HEX
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Converts the RGB to a HEX colour equivalent.
142 143 144 |
# File 'lib/pry-theme/rgb.rb', line 142 def to_hex PryTheme::HEX.new("#%02x%02x%02x" % @value) end |
#to_s ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
149 150 151 |
# File 'lib/pry-theme/rgb.rb', line 149 def to_s @value.join(', ') end |
#to_term(color_model = 256) ⇒ TERM
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Accepts the following numbers: 256, 16, 8.
Converts the RGB to a terminal colour equivalent.
128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/pry-theme/rgb.rb', line 128 def to_term(color_model = 256) term = case color_model when 256 then PryTheme::RGB::TABLE.index(@value) when 16 then PryTheme::RGB::SYSTEM.index(@value) when 8 then PryTheme::RGB::LINUX.index(@value) else raise ArgumentError, "invalid value for PryTheme::HEX#to_term(): #{ @value }" end term = find_among_term_colors(term, color_model) if term.nil? PryTheme::TERM.new(term, color_model) end |