Module: Colours::HtmlColours

Defined in:
lib/colours/html_colours/html_colours.rb,
lib/colours/html_colours/hash_html_colours.rb,
lib/colours/html_colours/random_html_colour.rb

Overview

Colours::HtmlColours

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.[](use_this_colour = :slateblue, display_this_text = '') ⇒ Object

#

Colours::HtmlColours[]

Easier toplevel-access method, through [].

The second message is the text that you wish to display.

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']
e Colours::HtmlColours[:steelblue, 'yo there']
#


265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/colours/html_colours/html_colours.rb', line 265

def self.[](
    use_this_colour   = :slateblue,
    display_this_text = ''
  )
  case use_this_colour
  # ======================================================================= #
  # === :random
  # ======================================================================= #
  when :random
    use_this_colour = random_html_colour
  end
  ::Colours::HtmlColoursMethods.send(
    use_this_colour,
    display_this_text
  )
end

.autogenerate_html_colours_methods(into = HOME_DIRECTORY_OF_USER_X+ 'programming/ruby/src/'\ 'colours/lib/colours/autogenerated/'\ 'html_colours_methods.rb') ⇒ Object

#

Colours::HtmlColours.autogenerate_html_colours_methods (autogenerate tag)

This method will autogenerated all HTML colours, onto the toplevel namespace called “Colours”.

Commandline invocation example:

colours --autogenerate_html_colours_methods
#


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/colours/html_colours/html_colours.rb', line 65

def self.autogenerate_html_colours_methods(
    into = HOME_DIRECTORY_OF_USER_X+
           'programming/ruby/src/'\
           'colours/lib/colours/autogenerated/'\
           'html_colours_methods.rb'
  )
  require 'colours/requires/require_save_file.rb'
  dataset_from_the_yaml_file = YAML.load_file(::Colours.file_html_colours)
  _ = "#{RUBY_HEADER}\n".dup
  _ << "# require 'colours/autogenerated/#{File.basename(into)}'\n"
  _ << "# =========================================================================== #\n"
  _ << "# include ::Colours::HtmlColoursMethods\n"
  _ << "# =========================================================================== #\n"
  _ << "module Colours\n\n"
  _ << "module HtmlColoursMethods # === ::Colours::HtmlColoursMethods\n\n"

  ::Colours::HtmlColours.all_html_colours?.each {|this_html_colour|
    # ===================================================================== #
    # We must obtain the proper entry from the .yml file next:
    # ===================================================================== #
    pointer = dataset_from_the_yaml_file[this_html_colour.to_s]
    r = pointer[0]
    g = pointer[1]
    b = pointer[2]
    _ << "  # ========================================================================= #\n"
    _ << "  # === Colours::HtmlColoursMethods.#{this_html_colour}\n"
    _ << "  # ========================================================================= #\n"
    _ << "  def self.#{this_html_colour}(i = '', &block)\n"
    # ===================================================================== #
    # The colour code goes like this:
    #
    #   \033[38;2;<R>;<G>;<B>m
    #
    # ===================================================================== #
    _ << '    return "'+
         '\e[38;2;'+
         "#{r};#{g};#{b}m"+ # ← Here the r,g,b values. 'm' terminates the escape sequence.
         "#\{i\}\""+
         " if block_given? and (yield == :omit_end)"+
         "\n"
    _ << '    return "'+
         '\e[38;2;'+
         "#{r};#{g};#{b}m"+ # ← Here the r,g,b values. 'm' terminates the escape sequence.
         "#\{i\}"+
         "\\e[0m"+
         "\"\n"
    _ << "  end\n\n"
  }
  _ << "end\n\n"
  _ << "end\n"
  e "Storing into the file `#{into}` next."
  SaveFile.write_what_into(_, into)
  autogenerate_html_colours_methods_for_instance_methods
end

.autogenerate_html_colours_methods_for_instance_methods(into = HOME_DIRECTORY_OF_USER_X+ 'programming/ruby/src/'\ 'colours/lib/colours/autogenerated/'\ 'html_colours_instance_methods.rb') ⇒ Object

#

Colours::HtmlColours.autogenerate_html_colours_methods_for_instance_methods

On my home system I can use this command to trigger this method:

autogenerate_html_colours_methods
#


128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/colours/html_colours/html_colours.rb', line 128

def self.autogenerate_html_colours_methods_for_instance_methods(
    into = HOME_DIRECTORY_OF_USER_X+
           'programming/ruby/src/'\
           'colours/lib/colours/autogenerated/'\
           'html_colours_instance_methods.rb'
  )
  require 'colours/requires/require_save_file.rb'
  _ = "#{RUBY_HEADER}\n".dup
  _ << "# require 'colours/autogenerated/#{File.basename(into)}'\n"
  _ << "# =========================================================================== #\n"
  _ << "# include ::Colours::HtmlColoursMethods\n"
  _ << "# =========================================================================== #\n"
  _ << "module Colours\n\n"
  _ << "module HtmlColoursMethods # === ::Colours::HtmlColoursMethods\n\n"

  ::Colours::HtmlColours.all_html_colours?.each {|this_html_colour|
    _ << "  # ========================================================================= #\n"
    _ << "  # === #{this_html_colour}\n"
    _ << "  # ========================================================================= #\n"
    _ << "  def #{this_html_colour}(i = '', &block)\n"
    _ << "    return ::Colours::HtmlColoursMethods.#{this_html_colour}(i, &block)\n"
    _ << "  end; alias konsole_colour_#{this_html_colour} #{this_html_colour}\n"
    _ << "       alias konsole_#{this_html_colour} #{this_html_colour}\n\n"
  }
  _ << "end\n\n"
  _ << "end\n"
  e "Storing into the file `#{into}` next."
  SaveFile.write_what_into(_, into)
end

.available_html_colours?Boolean

#

Colours::HtmlColours.available_html_colours?

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

#

Returns:

  • (Boolean)


99
100
101
# File 'lib/colours/html_colours/hash_html_colours.rb', line 99

def self.available_html_colours?
  @dataset.keys
end

.colour_to_rgb(long_name = :slateblue) ⇒ Object

#

Colours::HtmlColours.colour_to_rgb

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]
#


241
242
243
244
245
246
# File 'lib/colours/html_colours/html_colours.rb', line 241

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

.dataset?Boolean

#

Colours::HtmlColours.dataset?

#

Returns:

  • (Boolean)


25
26
27
# File 'lib/colours/html_colours/hash_html_colours.rb', line 25

def self.dataset?
  @dataset
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)


77
78
79
80
81
82
# File 'lib/colours/html_colours/hash_html_colours.rb', line 77

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

.e(i = '') ⇒ Object

#

Colours::HtmlColours.e

#


39
40
41
# File 'lib/colours/html_colours/html_colours.rb', line 39

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

.html_colourize(i) ⇒ Object

#

Colours::HtmlColours.html_colourize

#


201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/colours/html_colours/html_colours.rb', line 201

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

.load_the_dataset_containing_the_html_colours(use_this_file = ::Colours.file_html_colours) ⇒ Object

#

Colours::HtmlColours.load_the_dataset_containing_the_html_colours (load tag)

This method can be used to load all registered HTML-colours, from a yaml file.

The colours in the yaml file are stored in a

name: hexvalue

format.

#


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/colours/html_colours/hash_html_colours.rb', line 42

def self.load_the_dataset_containing_the_html_colours(
    use_this_file = ::Colours.file_html_colours
  )
  if File.exist? use_this_file
    @dataset = YAML.load_file(use_this_file)
  else
    # ===================================================================== #
    # The second check is not ideal but I think that I only need this
    # on my home system anyway.
    # ===================================================================== #
    if ("#{ENV['IS_ROEBE']}" == '1')
      puts "Colours::HtmlColours: We could not find any "\
           "file at `#{_}`."
    end
  end
end

.random_colour?Boolean

#

Colours::HtmlColours.random_colour?

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

#

Returns:

  • (Boolean)


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

def self.random_colour?
  available_html_colours?.sample
end

.revObject

#

Colours::HtmlColours.rev

#


32
33
34
# File 'lib/colours/html_colours/html_colours.rb', line 32

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() as well as support for RGB values.

To invoke this method from the commandline, try:

colours --show_html_colours
#


169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/colours/html_colours/html_colours.rb', line 169

def self.show_html_colours
  require 'colours/rgb/rgb.rb'
  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: all_html_colours?, html_colours?, all_colours

#

all_html_colours

This method will feedback all html colours known.

#


48
49
50
# File 'lib/colours/html_colours/html_colours.rb', line 48

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