Module: Colours::HtmlColours

Defined in:
lib/colours/html_colours/misc.rb,
lib/colours/html_colours/html_colourize.rb,
lib/colours/html_colours/hash_html_colours.rb,
lib/colours/html_colours/show_html_colours.rb,
lib/colours/html_colours/is_this_html_colour_included.rb

Overview

Colours::HtmlColours

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.[](which_colour_to_use, optional_text_to_display = '') ⇒ Object

#

Colours::HtmlColours[]

Usage examples:

Colours::HtmlColours['slateblue','Hello World!'] # works as Example 1
Colours::HtmlColours['slateblue']
Colours::HtmlColours['lightgreen','Hello World!']
Colours::HtmlColours[:random]
e Colours::HtmlColours[:random, 'yo there']
#

62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/colours/html_colours/misc.rb', line 62

def self.[](
    which_colour_to_use, optional_text_to_display = ''
  )
  case which_colour_to_use
  when :random
    which_colour_to_use = random_html_colour
  end
  if ::Colours.use_html_colours?
    ::Colours::SupportForHTMLColours.send(
      which_colour_to_use, optional_text_to_display
    )
  else # else return it unmodified.
    optional_text_to_display
  end
end

.available_html_colours?Boolean

#

Colours::HtmlColours.available_html_colours?

This method will return an Array with all available HTML colours.

#

Returns:

  • (Boolean)

27
28
29
# File 'lib/colours/html_colours/misc.rb', line 27

def self.available_html_colours?
  @hash_html_colours.keys
end

.colour_to_rgb(long_name = :slateblue) ⇒ Object

#

Colours::HtmlColours.colour_to_rgb('royalblue')

This method will convert the long colour name to an Array with R,G,B values.

Usage examples:

Colours::HtmlColours.colour_to_rgb('whitesmoke') # => [245, 245, 245]
Colours::HtmlColours.colour_to_rgb('slateblue')  # => [106, 90, 205]
Colours::HtmlColours.colour_to_rgb(:royalblue)   # => [65, 105, 225]
#

91
92
93
94
# File 'lib/colours/html_colours/misc.rb', line 91

def self.colour_to_rgb(long_name = :slateblue)
  pointer = @hash_html_colours[long_name.to_s] # This will be an Array.
  return [pointer[0], pointer[1], pointer[2]]
end

.does_include?(i = :slateblue) ⇒ Boolean

#

Colours::HtmlColours.does_include?

This method will return true if the given input is included in the HTML colours.

Usage examples:

Colours::HtmlColours.does_include? 'slateblue'                 # => true
Colours::HtmlColours.is_this_html_colour_included? 'royalblue' # => true
Colours::HtmlColours.is_this_html_colour_included? 'megawhite' # => false
#

Returns:

  • (Boolean)

26
27
28
29
# File 'lib/colours/html_colours/is_this_html_colour_included.rb', line 26

def self.does_include?(i = :slateblue)
  return true if @hash_html_colours.has_key?(i.to_s)
  false # else return false.
end

.e(i = '') ⇒ Object

#

Colours::HtmlColours.e

#

18
19
20
# File 'lib/colours/html_colours/misc.rb', line 18

def self.e(i = '')
  ::Colours.e(i)
end

.hash_html_colours?Boolean

#

Colours::HtmlColours.hash_html_colours?

#

Returns:

  • (Boolean)

46
47
48
# File 'lib/colours/html_colours/hash_html_colours.rb', line 46

def self.hash_html_colours?
  @hash_html_colours
end

.html_colourize(i) ⇒ Object

#

Colours::HtmlColours.html_colourize

#

17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/colours/html_colours/html_colourize.rb', line 17

def self.html_colourize(i)
  if i.is_a? Array
    i = i.join(' ')
  end
  if i
    if File.file?(i)
      i = File.read(i)
    elsif i.respond_to? :read
      i = i.read
    end
  end
  if i.is_a? String
    # ===================================================================== #
    # Find all HTML colours next in that String:
    # ===================================================================== #
    splitted = i.split("\n")
    splitted.map! {|entry|
      if ::Colours.does_this_line_include_a_html_colour?(entry)
        entry = ::Colours.replace_all_raw_html_colours_in_this_line(entry)
      end
      entry
    }
    i = splitted.join("\n")
  end
  e i
end

.random_colour?Boolean

#

Colours::HtmlColours.random_colour?

Obtain a random sample, aka a random colour, from all the registered html colours.

#

Returns:

  • (Boolean)

102
103
104
# File 'lib/colours/html_colours/misc.rb', line 102

def self.random_colour?
  available_html_colours?.sample
end

.revObject

#

Colours::HtmlColours.rev

#

19
20
21
# File 'lib/colours/html_colours/show_html_colours.rb', line 19

def self.rev
  ::Colours.rev
end

.show_html_coloursObject

#

Colours::HtmlColours.show_html_colours

This can be used to output the colours. It requires the method html_colours().

To invoke this method from the commandline, try:

colours --show_html_colours
#

34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/colours/html_colours/show_html_colours.rb', line 34

def self.show_html_colours
  lpad = 22
  show_this_standard_sentence =
    'This is a longer test-sentence in that colour.'
  print '  '; e '=' * 100
  html_colours.each {|this_html_colour|
    result = "#{::Colours.rgb_as_string(this_html_colour)} #{this_html_colour.to_s.ljust(22)}"
    # ===================================================================== #
    # Next, show that particular colour.
    # ===================================================================== #
    string = ::Colours.rgb_as_string(this_html_colour).strip
    ansi_escape_code = string.tr("\e","\\e")
    display_this_line =
      "  | #{(this_html_colour+': ').ljust(lpad)}| "\
      "#{result}#{::Colours.rev} | #{::Colours.rgb_as_string(this_html_colour)}#{show_this_standard_sentence}#{rev} |\n".dup # rev() is needed to restore the old default.
    display_this_line << '  |                       | '
    display_this_line << ''.ljust(22)
    echo_string = "  |   echo -e \""+string+"\\"+ansi_escape_code+"xyz#{rev}"+'"'
    padded_echo_string = (echo_string.rstrip).ljust(72)
    display_this_line << padded_echo_string+'|'
    e display_this_line
  }
  print '  '; e '=' * 100
end

Instance Method Details

#all_html_coloursObject Also known as: html_colours?, all_colours

#

all_html_colours

This method will feedback all html colours known.

#

44
45
46
# File 'lib/colours/html_colours/misc.rb', line 44

def all_html_colours
  ::Colours::HtmlColours.available_html_colours?
end