Class: PryTheme::RGB Private

Inherits:
Object
  • Object
show all
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).

Examples:

Conversion to HEX

RGB.new([0, 0, 0]).to_hex #=> (HEX: #000000)

Conversion to TERM

RGB.new([0, 0, 0]).to_term(8) #=> (TERM-8: 0)

# Approximation.
RGB.new([254, 244, 231]).to_term(8) #=> (TERM-8: 7)

Since:

  • 0.2.0

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.

Since:

  • 0.2.0

[
  [  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.

Since:

  • 0.2.0

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.

Since:

  • 0.2.0

[
  [  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.

Since:

  • 0.2.0

(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.

Since:

  • 0.2.0

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.

Since:

  • 0.2.0

[0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff]

Instance Method Summary collapse

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.

Parameters:

  • value (Array<Integer>, String)

    a String will be converted to Array

Raises:

  • (TypeError)

    if the value is neither Array or String

Since:

  • 0.2.0



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

#inspectString

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:

  • (String)

Since:

  • 0.2.0



118
119
120
# File 'lib/pry-theme/rgb.rb', line 118

def inspect
  "(RGB: #{ to_s })"
end

#to_aArray<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.

Examples:

RGB.new([0, 12, 255]).to_s #=> [0, 12, 255]

Returns:

  • (Array<Integer>)

Since:

  • 0.2.0



156
157
158
# File 'lib/pry-theme/rgb.rb', line 156

def to_a
  @value
end

#to_cssString

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.

Examples:

RGB.new([0, 0, 0]).to_css #=> 'rgb(0, 0, 0)'

Returns:

  • (String)

Since:

  • 0.2.0



163
164
165
# File 'lib/pry-theme/rgb.rb', line 163

def to_css
  "rgb(#{ to_s })"
end

#to_hexHEX

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.

Returns:

  • (HEX)

    a HEX representation of the RGB

Since:

  • 0.2.0



142
143
144
# File 'lib/pry-theme/rgb.rb', line 142

def to_hex
  PryTheme::HEX.new("#%02x%02x%02x" % @value)
end

#to_sString

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.

Examples:

RGB.new([0, 12, 255]).to_s #=> "0, 12, 255"

Returns:

  • (String)

Since:

  • 0.2.0



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.

Note:

Accepts the following numbers: 256, 16, 8.

Converts the RGB to a terminal colour equivalent.

Parameters:

  • color_model (Integer) (defaults to: 256)

Returns:

  • (TERM)

    a TERM representation of the RGB

Raises:

  • (ArgumentError)

    if color_model parameter is incorrect

Since:

  • 0.2.0



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