Class: PryTheme::HEX Private

Inherits:
Object
  • Object
show all
Defined in:
lib/pry-theme/hex.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.

Note:

Conversion to TERM relies on RGB#to_term, as a HEX instance converts itself to RGB first, and only then to TERM.

Represents a HEX colour. It’s possible to convert a HEX instance into TERM or RGB colours. However, this conversion is half-duplex (see RGB). This class validates its input (you won’t see malformed or nonexistent HEX colours).

Examples:

Conversion to RGB

HEX.new('#ffffff').to_rgb #=> (RGB: 255, 255, 255)

Conversion to TERM

HEX.new('#ffffff').to_term(16) #=> (TERM-16: 15)

# Approximation.
HEX.new('#fc33ea').to_term #=> (TERM-256: 207)

Since:

  • 0.2.0

Constant Summary collapse

BYTE =

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.

Represents a single HEX “digit”.

Since:

  • 0.2.0

/[A-F\d]{2}/i
PATTERN =

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.

A hex String must be prefixed with an octothorp. Use any letter case.

Since:

  • 0.2.0

/\A#(#{ BYTE }){3}\z/i

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ 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.

Returns a new instance of HEX.

Parameters:

  • value (String)

    must be a valid hex number

Since:

  • 0.2.0



28
29
30
31
# File 'lib/pry-theme/hex.rb', line 28

def initialize(value)
  validate_value(value)
  @value = value
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



34
35
36
# File 'lib/pry-theme/hex.rb', line 34

def inspect
  "(HEX: #{ @value })"
end

#to_rgbRGB

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 ‘self` into RGB.

Returns:

Since:

  • 0.2.0



47
48
49
# File 'lib/pry-theme/hex.rb', line 47

def to_rgb
  RGB.new(@value[1..-1].scan(BYTE).map! { |b| b.to_i(16) })
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:

HEX.new('#33aabb').to_s #=> "#33aabb"

Returns:

  • (String)

Since:

  • 0.2.0



41
42
43
# File 'lib/pry-theme/hex.rb', line 41

def to_s
  @value
end

#to_term(color_model = 256) ⇒ 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.

Converts ‘self` into TERM.

Returns:

Since:

  • 0.2.0



53
54
55
# File 'lib/pry-theme/hex.rb', line 53

def to_term(color_model = 256)
  to_rgb.to_term(color_model)
end