colours 0.6.12

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of colours might be problematic. Click here for more details.

Files changed (147) hide show
  1. checksums.yaml +7 -0
  2. data/LICENCE.md +22 -0
  3. data/README.md +970 -0
  4. data/bin/colours +7 -0
  5. data/bin/html_colour_to_hex_value +7 -0
  6. data/bin/html_colourize +7 -0
  7. data/bin/print_rainbow_line +7 -0
  8. data/bin/rainbow_colours +7 -0
  9. data/bin/remove_ansii_escape_sequences +7 -0
  10. data/colours.gemspec +76 -0
  11. data/doc/COLOUR_CODES_CHART.md +28 -0
  12. data/doc/HOW_TO_PICK_YOUR_OWN_COLOURS.md +29 -0
  13. data/doc/README.gen +927 -0
  14. data/doc/todo/todo.md +25 -0
  15. data/lib/colours/256_colours/support_for_256_colours.rb +190 -0
  16. data/lib/colours/autoalias_e.rb +10 -0
  17. data/lib/colours/autogenerated/README.md +2 -0
  18. data/lib/colours/autogenerated/support_for_256_colours.rb +2235 -0
  19. data/lib/colours/autogenerated/support_for_html_colours.rb +1778 -0
  20. data/lib/colours/autogenerated/toplevel_basic_colour_methods.rb +6367 -0
  21. data/lib/colours/autoinclude.rb +14 -0
  22. data/lib/colours/base/base.rb +23 -0
  23. data/lib/colours/basic_colours/README.md +4 -0
  24. data/lib/colours/basic_colours/basic_colours.rb +334 -0
  25. data/lib/colours/class/class.rb +125 -0
  26. data/lib/colours/colour_methods/README.md +11 -0
  27. data/lib/colours/colour_methods/default_colour.rb +66 -0
  28. data/lib/colours/colour_methods/sargument.rb +70 -0
  29. data/lib/colours/colour_methods/scomments.rb +105 -0
  30. data/lib/colours/colour_methods/sdir.rb +66 -0
  31. data/lib/colours/colour_methods/sfancy.rb +84 -0
  32. data/lib/colours/colour_methods/sfile.rb +71 -0
  33. data/lib/colours/colour_methods/simportant.rb +92 -0
  34. data/lib/colours/colour_methods/snormal.rb +66 -0
  35. data/lib/colours/colour_methods/ssymlink.rb +102 -0
  36. data/lib/colours/colour_methods/swarn.rb +76 -0
  37. data/lib/colours/colour_table/README.md +3 -0
  38. data/lib/colours/colour_table/colour_table.rb +275 -0
  39. data/lib/colours/colours_e_autoinclude.rb +9 -0
  40. data/lib/colours/commandline/README.md +2 -0
  41. data/lib/colours/commandline/commandline.rb +44 -0
  42. data/lib/colours/commandline/menu.rb +111 -0
  43. data/lib/colours/constants/colour_constants.rb +301 -0
  44. data/lib/colours/constants/file_constants.rb +72 -0
  45. data/lib/colours/constants/hash_simple_colours.rb +146 -0
  46. data/lib/colours/constants/misc.rb +59 -0
  47. data/lib/colours/constants/newline.rb +14 -0
  48. data/lib/colours/constants/registered_colour_methods.rb +53 -0
  49. data/lib/colours/e/README.md +13 -0
  50. data/lib/colours/e/autoinclude.rb +11 -0
  51. data/lib/colours/e/e.rb +35 -0
  52. data/lib/colours/e.rb +5 -0
  53. data/lib/colours/eparse/eparse.rb +77 -0
  54. data/lib/colours/everything/autoinclude.rb +11 -0
  55. data/lib/colours/html_colours/README.md +1 -0
  56. data/lib/colours/html_colours/add_html_colours_onto_the_toplevel_namespace.rb +22 -0
  57. data/lib/colours/html_colours/hash_html_colours.rb +63 -0
  58. data/lib/colours/html_colours/html_colourize.rb +48 -0
  59. data/lib/colours/html_colours/is_this_html_colour_included.rb +64 -0
  60. data/lib/colours/html_colours/misc.rb +186 -0
  61. data/lib/colours/html_colours/show_html_colours.rb +85 -0
  62. data/lib/colours/kde_colour_palette/kde_colour_palette.rb +128 -0
  63. data/lib/colours/map_symbol_to_corresponding_colour/map_symbol_to_corresponding_colour.rb +219 -0
  64. data/lib/colours/module.rb +11 -0
  65. data/lib/colours/project/project_base_directory.rb +22 -0
  66. data/lib/colours/rainbow_colours/README.md +2 -0
  67. data/lib/colours/rainbow_colours/check_for_trollop_being_available_or_exit.rb +26 -0
  68. data/lib/colours/rainbow_colours/constants.rb +30 -0
  69. data/lib/colours/rainbow_colours/do_parse_via_rainbow_colours.rb +164 -0
  70. data/lib/colours/rainbow_colours/paint_detected_mode.rb +20 -0
  71. data/lib/colours/rainbow_colours/print_rainbow_line.rb +68 -0
  72. data/lib/colours/rainbow_colours/println_ani.rb +32 -0
  73. data/lib/colours/rainbow_colours/println_plain.rb +36 -0
  74. data/lib/colours/rainbow_colours/rainbow.rb +38 -0
  75. data/lib/colours/rainbow_colours/report_errors.rb +32 -0
  76. data/lib/colours/rainbow_colours/returnln_plain.rb +63 -0
  77. data/lib/colours/rainbow_colours/set_mode.rb +24 -0
  78. data/lib/colours/requires/require_all_colour_methods.rb +5 -0
  79. data/lib/colours/requires/require_commandline.rb +7 -0
  80. data/lib/colours/requires/require_eparse.rb +11 -0
  81. data/lib/colours/requires/require_rgb.rb +5 -0
  82. data/lib/colours/requires/require_sdir.rb +5 -0
  83. data/lib/colours/requires/require_sfile.rb +5 -0
  84. data/lib/colours/requires/require_the_256_colours_module.rb +16 -0
  85. data/lib/colours/requires/require_the_basic_colours.rb +7 -0
  86. data/lib/colours/requires/require_the_colour_methods.rb +34 -0
  87. data/lib/colours/requires/require_the_colour_table.rb +7 -0
  88. data/lib/colours/requires/require_the_colours_project.rb +52 -0
  89. data/lib/colours/requires/require_the_constants.rb +11 -0
  90. data/lib/colours/requires/require_the_html_colours.rb +11 -0
  91. data/lib/colours/requires/require_the_kde_colour_palette.rb +7 -0
  92. data/lib/colours/requires/require_the_toplevel_methods.rb +42 -0
  93. data/lib/colours/requires/require_yaml.rb +9 -0
  94. data/lib/colours/revert/revert.rb +106 -0
  95. data/lib/colours/rgb/rgb.rb +547 -0
  96. data/lib/colours/testing/README.md +2 -0
  97. data/lib/colours/testing/testing.rb +157 -0
  98. data/lib/colours/toplevel_methods/bold.rb +35 -0
  99. data/lib/colours/toplevel_methods/bold_and_italic.rb +36 -0
  100. data/lib/colours/toplevel_methods/cat.rb +39 -0
  101. data/lib/colours/toplevel_methods/clear_screen.rb +18 -0
  102. data/lib/colours/toplevel_methods/cliner.rb +19 -0
  103. data/lib/colours/toplevel_methods/col.rb +56 -0
  104. data/lib/colours/toplevel_methods/e.rb +63 -0
  105. data/lib/colours/toplevel_methods/esystem.rb +19 -0
  106. data/lib/colours/toplevel_methods/fancy_parse.rb +83 -0
  107. data/lib/colours/toplevel_methods/html_colourize.rb +64 -0
  108. data/lib/colours/toplevel_methods/is_on_roebe.rb +16 -0
  109. data/lib/colours/toplevel_methods/italic.rb +110 -0
  110. data/lib/colours/toplevel_methods/make_colour.rb +29 -0
  111. data/lib/colours/toplevel_methods/methods_related_to_html_colours.rb +350 -0
  112. data/lib/colours/toplevel_methods/misc.rb +156 -0
  113. data/lib/colours/toplevel_methods/open_this_file.rb +26 -0
  114. data/lib/colours/toplevel_methods/prefer_this_colour_schemata.rb +85 -0
  115. data/lib/colours/toplevel_methods/random_html_colour.rb +48 -0
  116. data/lib/colours/toplevel_methods/random_value.rb +37 -0
  117. data/lib/colours/toplevel_methods/remove_escape_sequence.rb +112 -0
  118. data/lib/colours/toplevel_methods/set_last_colour_used.rb +32 -0
  119. data/lib/colours/toplevel_methods/show_basic_colour_palette.rb +36 -0
  120. data/lib/colours/toplevel_methods/underline.rb +129 -0
  121. data/lib/colours/toplevel_methods/use_colours.rb +290 -0
  122. data/lib/colours/utility_scripts/README.md +2 -0
  123. data/lib/colours/utility_scripts/autogenerate.rb +291 -0
  124. data/lib/colours/version/version.rb +25 -0
  125. data/lib/colours/yaml/256_colours.yml +277 -0
  126. data/lib/colours/yaml/basic_colours.yml +23 -0
  127. data/lib/colours/yaml/html_colours.yml +835 -0
  128. data/lib/colours/yaml/kde_colour_palette.yml +183 -0
  129. data/lib/colours/yaml/prefer_this_colour_schemata.yml +15 -0
  130. data/lib/colours/yaml/use_these_values_for_the_colour_methods.yml +15 -0
  131. data/lib/colours.rb +5 -0
  132. data/test/test.html +18 -0
  133. data/test/testing_256_colours_support.rb +29 -0
  134. data/test/testing_col.rb +10 -0
  135. data/test/testing_colours.rb +95 -0
  136. data/test/testing_colours_e.rb +13 -0
  137. data/test/testing_eparse.rb +13 -0
  138. data/test/testing_kde_colour_palette.rb +30 -0
  139. data/test/testing_konsole_submodule.rb +226 -0
  140. data/test/testing_map_symbol_to_corresponding_colour.rb +14 -0
  141. data/test/testing_rgb_to_hex.rb +32 -0
  142. data/test/testing_the_basic_colours_of_the_colours_project.rb +58 -0
  143. data/test/testing_the_colour_methods.rb +36 -0
  144. data/test/testing_the_constants_of_the_colours_project.rb +9 -0
  145. data/test/testing_the_html_colours_of_the_colours_project.rb +37 -0
  146. data/test/testing_whether_colours_will_be_used.rb +7 -0
  147. metadata +229 -0
@@ -0,0 +1,110 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ # require 'colours/toplevel_methods/italic.rb'
6
+ # =========================================================================== #
7
+ module Colours
8
+
9
+ require 'colours/revert/revert.rb'
10
+ require 'colours/toplevel_methods/e.rb'
11
+ require 'colours/rgb/rgb.rb'
12
+
13
+ # ========================================================================= #
14
+ # === ITALIC_CODE
15
+ #
16
+ # If you wish to test italic output quickly on the terminal, then you
17
+ # can use something like this:
18
+ #
19
+ # echo -e "\e[3mone two three\e[23m"
20
+ #
21
+ # ========================================================================= #
22
+ ITALIC_CODE = '3'
23
+
24
+ # ========================================================================= #
25
+ # === Colours.return_italic
26
+ #
27
+ # To test this method, try:
28
+ #
29
+ # Colours.italic('Hello world!')
30
+ #
31
+ # ========================================================================= #
32
+ def self.return_italic(
33
+ show_this_text = 'This must be italic.',
34
+ make_newline = false
35
+ )
36
+ result = "\x1b".dup
37
+ result << '['
38
+ result << ITALIC_CODE
39
+ if block_given?
40
+ # ===================================================================== #
41
+ # Right now we assume that this must be a colour.
42
+ # ===================================================================== #
43
+ result << ";38;2;"
44
+ result << html_colour_to_stringified_rgb_values(yield)
45
+ end
46
+ result << 'm'
47
+ if make_newline
48
+ show_this_text = "#{show_this_text}\n"
49
+ end
50
+ result << show_this_text
51
+ result << REVERT
52
+ result
53
+ end; self.instance_eval { alias string_italic return_italic } # === Colours.string_italic
54
+
55
+ # ========================================================================= #
56
+ # === Colours.italic
57
+ #
58
+ # This method will "puts" the result of applying italic to a string,
59
+ # on the console/terminal. If you wish to do the output on your own
60
+ # then you have to use the method Colours.return_italic or its
61
+ # alias called Colours.string_italic.
62
+ #
63
+ # To test this, try:
64
+ # Colours.italic('Hello world!')
65
+ # ========================================================================= #
66
+ def self.italic(
67
+ i = '',
68
+ make_newline = false,
69
+ &block
70
+ )
71
+ e return_italic(i, make_newline, &block)
72
+ end
73
+
74
+ # ========================================================================= #
75
+ # === italic
76
+ # ========================================================================= #
77
+ def italic(
78
+ i = '',
79
+ make_newline = false
80
+ )
81
+ ::Colours.italic(i, make_newline)
82
+ end
83
+
84
+ end
85
+
86
+ if __FILE__ == $PROGRAM_NAME
87
+ alias e puts
88
+ alias ee print
89
+ txt = 'Hello world!'
90
+ if ARGV.empty?
91
+ require 'colours/requires/require_all_colour_methods.rb'
92
+ COLOURS = Colours::AllColourMethods
93
+ system 'clear'
94
+ Colours.italic(txt)
95
+ e COLOURS.palegreen('italic in palegreen:')
96
+ ee ' '; e Colours.string_italic(txt) { :palegreen }
97
+ e COLOURS.slateblue('italic in slateblue:')
98
+ ee ' '; e Colours.string_italic(txt) { :slateblue }
99
+ e COLOURS.orange('italic in orange:')
100
+ ee ' '; e Colours.string_italic(txt) { :orange }
101
+ e COLOURS.crimson('italic in crimson:')
102
+ ee ' '; e Colours.string_italic(txt) { :crimson }
103
+ e 'Italic without colours:'
104
+ ee ' '; e 'Die '+Colours.string_italic('R-Gruppe bei')+
105
+ ' Serin (S) besteht aus ...'
106
+ else
107
+ ee ' '; e Colours.string_italic(ARGV.join(' ').strip)
108
+ ee ' '; e Colours.string_italic(ARGV.join(' ').strip) { :crimson }
109
+ end
110
+ end # italic
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ # require 'colours/toplevel_methods/make_colour.rb'
6
+ # =========================================================================== #
7
+ require 'colours/constants/colour_constants.rb'
8
+ require 'colours/constants/misc.rb'
9
+ require 'colours/toplevel_methods/e.rb'
10
+
11
+ module Colours
12
+
13
+ # ========================================================================= #
14
+ # === Colours.make_colour
15
+ #
16
+ # This is used primarily for testing.
17
+ # ========================================================================= #
18
+ def self.make_colour(
19
+ what_colour_to_use
20
+ )
21
+ i = what_colour_to_use # Copy.
22
+ constant = i.to_s.upcase
23
+ name_of_colour = const_get(constant)
24
+ string = '%-34s' % ("#{name_of_colour} Test with #{constant}.")
25
+ string << IS_A_TEST+'('+name_of_colour.inspect.delete('"')+')'
26
+ e(string)
27
+ end
28
+
29
+ end
@@ -0,0 +1,350 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ # This file will hold code that relates to the html-colours, such as
6
+ # "steelblue" or "royalblue" and so forth.
7
+ # =========================================================================== #
8
+ # require 'colours/toplevel_methods/methods_related_to_html_colours.rb'
9
+ # Colours.html_colour_to_hex_value(ARGV)
10
+ # =========================================================================== #
11
+ module Colours
12
+
13
+ require 'yaml'
14
+ require 'colours/constants/file_constants.rb'
15
+ require 'colours/html_colours/is_this_html_colour_included.rb'
16
+ require 'colours/autogenerated/toplevel_basic_colour_methods.rb'
17
+
18
+ # ========================================================================= #
19
+ # === USE_THIS_COLOUR_FOR_THE_DEFAULT_COLOUR
20
+ #
21
+ # Here we specify the default colour to be used, in order to revert
22
+ # to the prior colour in use. This can be modified by the user,
23
+ # in order to conform to other colours if said user wishes to
24
+ # do so.
25
+ # ========================================================================= #
26
+ USE_THIS_COLOUR_FOR_THE_DEFAULT_COLOUR = :grey
27
+
28
+ # ========================================================================= #
29
+ # === REGEX_FOR_HTML_COLOURS
30
+ #
31
+ # This regex supports the use case where we can match against HTML
32
+ # colours. (The name is a bit of a misnomer, though.)
33
+ #
34
+ # For the following regex, see this entry at rubular:
35
+ #
36
+ # https://rubular.com/r/o5r4DneMTGOsbX
37
+ #
38
+ # ========================================================================= #
39
+ REGEX_FOR_HTML_COLOURS =
40
+ /<([()a-zA-Z0-9²→\n]+?)>/
41
+
42
+ # ========================================================================= #
43
+ # === Colours.replace_html_colours_in_this_string
44
+ #
45
+ # This method will replace all HTML colours in a given string, such as
46
+ # "<slateblue>test</slateblue>", with the corresponding RGB colour
47
+ # variant for the commandline.
48
+ #
49
+ # Typically this refers to a terminal such as the KDE Konsole, and a
50
+ # shell such as bash (although other shells are fine too, and many
51
+ # other terminals, such as the gnome-terminal, most likely will work
52
+ # fine as well - but it is optimized for the KDE Konsole).
53
+ #
54
+ # This method should only be called after a prior check was done,
55
+ # to determine whether the given input String at hand does indeed
56
+ # include a valid HTML colour; otherwise it would be a bit pointless
57
+ # to invoke the method, if it is already known that the String at
58
+ # hand does not contain any HTML colour at all. In order to determine
59
+ # whether a String may include a valid HTML colour, the method called
60
+ # line_contains_a_valid_html_colour?() can be used.
61
+ #
62
+ # Usage example:
63
+ #
64
+ # Colours.replace_html_colours_in_this_string
65
+ # Colours.replace_html_colours_in_this_string('- The <one>UID</one> of <royalblue>the</royalblue> user called <two>root</two> is ... ? <one>0</one>.', :lightgreen)
66
+ #
67
+ # ========================================================================= #
68
+ def self.replace_html_colours_in_this_string(
69
+ i,
70
+ use_this_colour_for_the_default_colour = :default, # ← This specifies the default colour.
71
+ shall_we_revert_at_the_end_of_the_line = false
72
+ )
73
+ i = i.dup # We want to work on a copy.
74
+ result = ''.dup # Our result-string.
75
+ case use_this_colour_for_the_default_colour
76
+ # ======================================================================= #
77
+ # === :default
78
+ # ======================================================================= #
79
+ when :default,
80
+ :default_colour
81
+ use_this_colour_for_the_default_colour = USE_THIS_COLOUR_FOR_THE_DEFAULT_COLOUR
82
+ end
83
+ case shall_we_revert_at_the_end_of_the_line
84
+ # ======================================================================= #
85
+ # === :revert
86
+ # ======================================================================= #
87
+ when :revert
88
+ shall_we_revert_at_the_end_of_the_line = true
89
+ end
90
+ result << ::Colours::AllColourMethods.send(use_this_colour_for_the_default_colour) { :omit_end }
91
+ result = result.dup
92
+ # ======================================================================= #
93
+ # This method will make use of two different regexes.
94
+ # ======================================================================= #
95
+ scanned_results = i.scan(
96
+ REGEX_FOR_HTML_COLOURS
97
+ ).flatten
98
+ add_on_string = i.dup
99
+ scanned_results.uniq.each {|this_colour|
100
+ if is_this_a_valid_html_colour?(this_colour)
101
+ # =================================================================== #
102
+ # === Defining the Regex in use
103
+ #
104
+ # Note that the regex at rubular is not always updated. I only
105
+ # needed it to check for the basic parts.
106
+ #
107
+ # Regex see:
108
+ #
109
+ # https://rubular.com/r/0WJXdVznVjKRWH
110
+ #
111
+ # =================================================================== #
112
+ use_this_regex =
113
+ /<#{this_colour}>([\[\]\\{}\(\)\/!\-#*~:;≡=+a-zA-Z0-9←→₁₂₃₄₅₆₇₈₉⁰¹²³⁴⁵⁶⁷⁸⁹ÅäöüÄÖÜαβß&%°"',_\|\n\. ]+)<\/#{this_colour}>/
114
+ # =================================================================== #
115
+ # We must use .gsub!() because the colour-string may occur more
116
+ # than once. Unfortunately for longer Strings this becomes a bit
117
+ # fragile.
118
+ # =================================================================== #
119
+ part1 = ::Colours::AllColourMethods.send(this_colour.to_sym, "\\1") { :omit_end }
120
+ part2 = ::Colours::AllColourMethods.send(use_this_colour_for_the_default_colour) { :omit_end }
121
+ add_on_string.gsub!(use_this_regex, part1+part2).dup
122
+ end
123
+ }
124
+ result << add_on_string
125
+ if shall_we_revert_at_the_end_of_the_line
126
+ result << ::Colours.revert
127
+ end
128
+ return result
129
+ end; self.instance_eval { alias replace_html_colours replace_html_colours_in_this_string } # === Colours.replace_html_colours
130
+ self.instance_eval { alias replace_all_raw_html_colours_in_this_line replace_html_colours_in_this_string } # === Colours.replace_all_raw_html_colours_in_this_line
131
+ self.instance_eval { alias replace_all_html_colours_in_this_line replace_html_colours_in_this_string } # === Colours.replace_all_html_colours_in_this_line
132
+
133
+ # ========================================================================= #
134
+ # === Colours.does_this_string_include_a_html_colour?
135
+ # ========================================================================= #
136
+ def self.does_this_string_include_a_html_colour?(i)
137
+ result = (i =~ REGEX_FOR_HTML_COLOURS)
138
+ result = false if result.nil?
139
+ result
140
+ end
141
+
142
+ # ========================================================================= #
143
+ # === Colours.does_this_string_include_a_html_number?
144
+ #
145
+ # This method will return true if the string includes tags such as
146
+ # <one> or <two> and so forth.
147
+ # ========================================================================= #
148
+ def self.does_this_string_include_a_html_number?(i)
149
+ i.include?('<one>') or
150
+ i.include?('<two>') or
151
+ i.include?('<three>') or
152
+ i.include?('<four>') or
153
+ i.include?('<five>')
154
+ end
155
+
156
+ # ========================================================================= #
157
+ # === Colours.html_colour_to_hex_value
158
+ # ========================================================================= #
159
+ def self.html_colour_to_hex_value(
160
+ this_html_colour,
161
+ return_with_hash_key = true
162
+ )
163
+ if this_html_colour.is_a? Array
164
+ this_html_colour = this_html_colour.first
165
+ end
166
+ _ = file_html_colours_to_rgb?
167
+ if File.exist? _
168
+ dataset = YAML.load_file(_)
169
+ if dataset.has_key? this_html_colour
170
+ _ = dataset[this_html_colour].last
171
+ if return_with_hash_key
172
+ _ = _.dup if _.frozen?
173
+ _ = _.to_s unless _.is_a? String
174
+ _[0,0] = '#' if _.respond_to? :[]=
175
+ end
176
+ return _
177
+ end
178
+ end
179
+ return this_html_colour
180
+ end
181
+
182
+ # ========================================================================= #
183
+ # === Colours.eliminate_html
184
+ #
185
+ # This method simply combines two other methods, without any further
186
+ # checks inside of this method. The method will thus remove entries
187
+ # such as <one> or <steelblue>.
188
+ # ========================================================================= #
189
+ def self.eliminate_html(
190
+ i,
191
+ use_this_colour_for_the_default_colour = :default,
192
+ use_this_as_replacement_hash = :default_hash
193
+ )
194
+ i = i.dup
195
+ i = Colours.replace_number_words_with_the_corresponding_html_colour(
196
+ i,
197
+ use_this_colour_for_the_default_colour,
198
+ use_this_as_replacement_hash
199
+ )
200
+ i = i.dup
201
+ i = Colours.replace_html_colours_in_this_string(
202
+ i,
203
+ use_this_colour_for_the_default_colour
204
+ )
205
+ return i.dup
206
+ end; self.instance_eval { alias away_with_html_colours_and_special_numbers eliminate_html } # === Colours.away_with_html_colours_and_special_numbers
207
+ self.instance_eval { alias remove_html_tags_and_special_words eliminate_html } # === Colours.remove_html_tags_and_special_words
208
+ self.instance_eval { alias remove_crap eliminate_html } # === Colours.remove_crap
209
+
210
+ # ========================================================================= #
211
+ # === Colours.replace_number_words_with_the_corresponding_html_colour
212
+ #
213
+ # The input to this method should be the String that contains HTML
214
+ # numbers such as <one> or <two>.
215
+ #
216
+ # This method can be customized a bit. One way to customize it is
217
+ # to pass the default colour to this method, as second argument.
218
+ # The default colour is (currently, in Oct 2020) grey.
219
+ #
220
+ # The third argument is a Hash. This Hash can be used to denote the
221
+ # default colours for <one>, <two> and so forth. So if you want
222
+ # to change the colours, pass in your own Hash into this method.
223
+ #
224
+ # Usage examples:
225
+ #
226
+ # x = Colours.replace_number_words_with_the_corresponding_html_colour("- <teal>Asbestbedingte Erkrankungen</teal> haben eine <two>Latenzzeit</two> von etwa n Jahren? Etwa <steelblue>30 Jahren</steelblue>.")
227
+ # x = "<one>Methämoglobin</one> ist <two>ungeeignet</two> für <three>den</three> Sauerstofftransport; die <two>roten Blutkörperchen</two>. Welches Enzymsystem ist dies?"
228
+ # y = Colours.replace_number_words_with_the_corresponding_html_colour(x)
229
+ #
230
+ # See also:
231
+ #
232
+ # https://rubular.com/r/XUPQJFKlDs2OYP
233
+ #
234
+ # ========================================================================= #
235
+ def self.replace_number_words_with_the_corresponding_html_colour(
236
+ i,
237
+ use_this_as_replacement_hash = :default
238
+ )
239
+ # ======================================================================= #
240
+ # Specify our "replacement-hash", that is colours used for e. g.
241
+ # <one>, <two> or <three>.
242
+ # ======================================================================= #
243
+ case use_this_as_replacement_hash
244
+ # ======================================================================= #
245
+ # === :default
246
+ # ======================================================================= #
247
+ when :default,
248
+ :default_hash
249
+ # ===================================================================== #
250
+ # The following hash can be used as a default "replacement" Hash.
251
+ # ===================================================================== #
252
+ use_this_as_replacement_hash = {
253
+ one: :teal,
254
+ two: :lightseagreen,
255
+ three: :mediumseagreen, # ← Used to be 'peru' up until 23.11.2019.
256
+ four: :olivedrab,
257
+ five: :lightgreen,
258
+ default_colour: USE_THIS_COLOUR_FOR_THE_DEFAULT_COLOUR # Defined on top.
259
+ }
260
+ end
261
+ # ======================================================================= #
262
+ # === :default_colour
263
+ # ======================================================================= #
264
+ if use_this_as_replacement_hash.has_key? :default_colour
265
+ use_this_colour_for_the_default_colour = use_this_as_replacement_hash[:default_colour]
266
+ end
267
+ # ======================================================================= #
268
+ # === Enable support for <one>, <two>, <three>, <four> and <five> next.
269
+ # ======================================================================= #
270
+ if i.include?('<one>') or
271
+ i.include?('<two>') or
272
+ i.include?('<three>') or
273
+ i.include?('<four>') or
274
+ i.include?('<five>')
275
+ scanned_results = i.scan(
276
+ REGEX_FOR_HTML_COLOURS
277
+ ).flatten.uniq
278
+ scanned_results.each {|this_numbered_word|
279
+ # =================================================================== #
280
+ # Since as of December 2021 we only accept Symbols here.
281
+ # =================================================================== #
282
+ this_numbered_word = this_numbered_word.to_sym
283
+ if use_this_as_replacement_hash.has_key? this_numbered_word
284
+ i = i.dup if i.frozen?
285
+ replacement_colour = use_this_as_replacement_hash[this_numbered_word]
286
+ use_this_regex =
287
+ # =============================================================== #
288
+ # See:
289
+ #
290
+ # https://rubular.com/r/sDNQd81MtciMJL
291
+ #
292
+ # =============================================================== #
293
+ /<#{this_numbered_word}>([-#~≥!\e=><;,→⁻⁺@^„“γαµ_₀₁₂₃₄₅₆₇₈₉²³⁴⁵⁶⁷⁸⁹äöüÄÖÜβß%&:≡°$A-Za-z0-9\n \?\\'\|\(\)\{\}\[\]\"\*\.\+]+)<\/#{this_numbered_word}>/
294
+ # ================================================================= #
295
+ # We must use .gsub!() because the colour-string may occur more
296
+ # than once.
297
+ # ================================================================= #
298
+ i.gsub!(
299
+ use_this_regex,
300
+ ::Colours::AllColourMethods.send(replacement_colour.to_sym, "\\1") { :omit_end }+
301
+ ::Colours::AllColourMethods.send(use_this_colour_for_the_default_colour) { :omit_end }
302
+ )
303
+ # i << ::Colours.revert
304
+ end
305
+ }
306
+ end
307
+ return i
308
+ end
309
+
310
+ end
311
+
312
+ if __FILE__ == $PROGRAM_NAME
313
+ alias e puts
314
+ input = ARGV
315
+ if input.empty?
316
+ input << 'steelblue'
317
+ end
318
+ e 'Testing: Colours.html_colour_to_hex_value(ARGV) next'
319
+ e Colours.html_colour_to_hex_value(input)
320
+ e "Testing: Colours.does_this_string_include_a_html_number?('abc <one>def</one>') next"
321
+ puts Colours.does_this_string_include_a_html_number?('abc <one>def</one>')
322
+ # ========================================================================= #
323
+ # Test the behaviour of the method next:
324
+ # ========================================================================= #
325
+ this_line = "- <lightseagreen>hey</lightseagreen> yo <green>there</green>"
326
+ puts Colours.replace_html_colours(
327
+ this_line
328
+ )
329
+ puts Colours.does_this_string_include_a_html_colour?('<steelblue>')
330
+ e
331
+ e Colours.replace_number_words_with_the_corresponding_html_colour(
332
+ "- <teal>Asbestbedingte Erkrankungen</teal> haben eine <two>Latenzzeit</two> von etwa n Jahren? Etwa <steelblue>30 Jahren</steelblue>."
333
+ )
334
+ x = "<one>Methämoglobin</one> ist <two>ungeeignet</two> für <three>den</three> Sauerstofftransport; die <two>roten Blutkörperchen</two>. Welches Enzymsystem ist dies?"
335
+ e Colours.replace_number_words_with_the_corresponding_html_colour(x)
336
+ e Colours.replace_html_colours_in_this_string("<one>Methämoglobin</one> ist <steelblue>ungeeignet</steelblue>.")
337
+ e
338
+ string = "- <teal>Asbestbedingte Erkrankungen</teal> haben eine <two>Latenzzeit</two> von etwa n Jahren? Etwa <steelblue>30 Jahren</steelblue>."
339
+ e string
340
+ e Colours.replace_html_colours_in_this_string(
341
+ string
342
+ )
343
+ e
344
+ result = Colours.replace_all_html_colours_in_this_line(' |<steelblue>E</steelblue>]', :default, :revert)
345
+ e result
346
+ e
347
+ e 'And it looks like this via pp:'
348
+ e
349
+ pp result
350
+ end # html_colour_to_hex_value slateblue
@@ -0,0 +1,156 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ # require 'colours/toplevel_methods/misc.rb'
6
+ # =========================================================================== #
7
+ module Colours
8
+
9
+ require 'colours/toplevel_methods/esystem.rb'
10
+ require 'colours/toplevel_methods/open_this_file.rb'
11
+ require 'colours/html_colours/misc.rb'
12
+
13
+ # ========================================================================= #
14
+ # === Colours.sanitize_line
15
+ #
16
+ # This method will replace one line with the proper R,G,B valid entries.
17
+ # ========================================================================= #
18
+ def self.sanitize_line(entry)
19
+ all_potential_matches = entry.scan(/<(\w+)>/).flatten
20
+ all_potential_matches.each {|substring|
21
+ entry.gsub!(/<\/>/, rev)
22
+ entry.gsub!(/<#{substring}>/, rgb_format(substring))
23
+ }
24
+ begin
25
+ require 'roebe/modules/remove_html.rb'
26
+ rescue LoadError; end
27
+ if Object.const_defined?(:Roebe) and
28
+ Roebe.respond_to?(:remove_html)
29
+ entry = Roebe.remove_html[entry]
30
+ end
31
+ return entry
32
+ end
33
+
34
+ # ========================================================================= #
35
+ # === Colours.read_and_display_this_file
36
+ #
37
+ # This method accepts a file path - in other words, the input should be
38
+ # the location of a specific file on your filesystem - and then simply
39
+ # reads in the content of said file via File.readlines().
40
+ #
41
+ # Content such as <slateblue> found in this file will be replaced
42
+ # with the respective R,G,B substring value.
43
+ #
44
+ # In the test/ subdirectory there is an example file for this - have
45
+ # a look there for more information.
46
+ # ========================================================================= #
47
+ def self.read_and_display_this_file(
48
+ i = TEST_FILE
49
+ )
50
+ i = i.to_s
51
+ if File.exist? i
52
+ new_array = []
53
+ data = File.readlines(i)
54
+ data.each {|entry|
55
+ # ======================================================================= #
56
+ # Check whether the entry has a <> tag:
57
+ # ======================================================================= #
58
+ if entry.include?('<') and entry.include?('>') and
59
+ entry.include?('</>')
60
+ # ================================================================= #
61
+ # Ok, we may assume that something like <slateblue> is there.
62
+ # ================================================================= #
63
+ entry = sanitize_line(entry)
64
+ end
65
+ new_array << entry
66
+ }
67
+ e new_array # For now we will simply output that modified Array.
68
+ else
69
+ e "Notice: The file at `#{sfile(i)}` does not exist."
70
+ end
71
+ end; self.instance_eval { alias read_file read_and_display_this_file } # === Colours.read_file
72
+
73
+ # ========================================================================= #
74
+ # === Colours.does_this_line_include_a_html_colour?
75
+ #
76
+ # This method can be used to determine whether the given input-string
77
+ # contains a valid HTML colour or whether it does not.
78
+ #
79
+ # Returns: a boolean.
80
+ #
81
+ # Invocation examples:
82
+ #
83
+ # Colours.does_this_line_include_a_html_colour? "<green>yo there</green> <orange>getline() function</orange>" # => true
84
+ # Colours.does_this_line_include_a_html_colour? "foo bar" # => false
85
+ #
86
+ # ========================================================================= #
87
+ def self.does_this_line_include_a_html_colour?(
88
+ line
89
+ )
90
+ Colours.html_colours?.any? {|entry|
91
+ line.include? entry
92
+ }
93
+ end
94
+
95
+ # ========================================================================= #
96
+ # === Colours.convert_hex_to_rgb
97
+ #
98
+ # This method will convert e. g. #baf185 to [186, 241, 133]. Thus it
99
+ # will return an Array, denoting the R, G, B values.
100
+ #
101
+ # How to do this conversion on your own?
102
+ #
103
+ # (1) Get the 2 left digits of the hex color code and convert
104
+ # to decimal value to get the red color level.
105
+ # (2) Get the 2 middle digits of the hex color code and
106
+ # convert to decimal value to get the green color level.
107
+ # (3) Get the 2 right digits of the hex color code and
108
+ # convert to decimal value to get the blue color level.
109
+ #
110
+ # Usage example:
111
+ #
112
+ # Colours.convert_hex_to_rgb('#baf185') # => [186, 241, 133]
113
+ #
114
+ # ========================================================================= #
115
+ def self.convert_hex_to_rgb(hex)
116
+ if hex.is_a? Array
117
+ hex = hex.first
118
+ end
119
+ hex = hex.to_s.dup
120
+ hex.delete!('#') if hex.include? '#'
121
+ array = [] # We will return this Array.
122
+ r = hex[0,2].to_i(16)
123
+ g = hex[2,2].to_i(16)
124
+ b = hex[4,2].to_i(16)
125
+ array << r << g << b
126
+ return array
127
+ end; self.instance_eval { alias hex_to_rgb convert_hex_to_rgb } # === Colours.hex_to_rgb
128
+
129
+ # ========================================================================= #
130
+ # === Colours.convert_hex_code_to_RGBA_array
131
+ #
132
+ # RGBA stands for "red, green, blue, alpha". Alpha indicates how
133
+ # opaque each pixel is.
134
+ #
135
+ # The usual values for the alpha parameter, aka the last parameter,
136
+ # is a number between 0.0 (which means "fully transparent") and
137
+ # the number 1.0 (which means "not transparent at all").
138
+ #
139
+ # Note that this method is similar to Colours.convert_hex_to_rgb(hex),
140
+ # but it has a fourth argument, aka A (for Alpha), on top of
141
+ # the RGB values.
142
+ #
143
+ # Usage example:
144
+ #
145
+ # Colours.convert_hex_code_to_RGBA_array('#baf185') # => [186, 241, 133]
146
+ #
147
+ # ========================================================================= #
148
+ def self.convert_hex_code_to_RGBA_array(
149
+ i, default_alpha_value = 1.0
150
+ )
151
+ rgba_array = convert_hex_to_rgb(i)
152
+ rgba_array << default_alpha_value
153
+ return rgba_array
154
+ end
155
+
156
+ end
@@ -0,0 +1,26 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ # require 'colours/toplevel_methods/open_this_file.rb'
6
+ # =========================================================================== #
7
+ require 'colours/toplevel_methods/esystem.rb'
8
+
9
+ module Colours
10
+
11
+ # ========================================================================= #
12
+ # === self.open_this_file
13
+ # ========================================================================= #
14
+ def open_this_file(
15
+ shall_we_exit = false
16
+ )
17
+ case shall_we_exit
18
+ when :then_exit
19
+ shall_we_exit = true
20
+ end
21
+ _ = "bluefish #{__FILE__}"
22
+ esystem(_)
23
+ exit if shall_we_exit
24
+ end
25
+
26
+ end