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
data/README.md ADDED
@@ -0,0 +1,970 @@
1
+ [![forthebadge](https://forthebadge.com/images/badges/built-with-love.svg)](https://www.gobolinux.org/)
2
+ [![forthebadge](https://forthebadge.com/images/badges/made-with-ruby.svg)](https://www.ruby-lang.org/en/)
3
+ [![Gem Version](https://badge.fury.io/rb/colours.svg)](https://badge.fury.io/rb/colours)
4
+
5
+ This gem was <b>last updated</b> on the <span style="color: darkblue; font-weight: bold">15.09.2022</span> (dd.mm.yyyy notation), at <span style="color: steelblue; font-weight: bold">18:08:12</span> o'clock.
6
+
7
+ # The Colours project
8
+
9
+ ## Goals, Scope and the History of the colours project
10
+
11
+ The Colours project originated from a requirement to have to support **colours**
12
+ on the commandline. Colours can be immensely helpful, so it makes sense
13
+ to make use of them on the commandline. The modern www also makes use of
14
+ colours - just look at any random webpage; you may find lots of
15
+ different colours in use there.
16
+
17
+ If you do happen to take a look on the projects offered on rubygems.org then
18
+ you may be able to find **lots** of different colour-related projects, written
19
+ in ruby. The primary reason as to why I created a new colour-centric project
20
+ was because I needed certain functionality that was not provided by any of
21
+ the other projects; at the least not in a way as I thought it should be
22
+ available (and used in downstream projects). For example, some terminals
23
+ support **RGB values** and other terminals do not. I did not want to have
24
+ to spend time thinking about this much at all, so I wanted to have a
25
+ colours-related project that could abstract this away for me.
26
+
27
+ ![alt text][screenshot1]
28
+ [screenshot1]: https://i.imgur.com/F6kac8W.png
29
+
30
+ The **main goal** of the **colours project** is to collect colour-related
31
+ code and make this code available to other projects, so that these projects
32
+ can benefit from colour support.
33
+
34
+ The primary goal herein is for **commandline applications**, but there
35
+ are some HTML components as part of this project that could be used,
36
+ such as for when you wish to make use of **HTML colours** (slateblue,
37
+ royalblue, teal, tomato, steelblue and names such as these). The
38
+ partial screenshot above indicates this, on a black **KDE konsole**
39
+ background. (I tend to prefer dark backgrounds for my terminals.)
40
+
41
+ Note that many **terminals** support the **display of HTML colours**, via
42
+ their **corresponding RGB values**. Since I wanted to use good terminals,
43
+ such as the **KDE konsole**, the colours project also had to support
44
+ these names (such as **slateblue** or **royalblue**) directly. This is
45
+ why method calls such as **Colours.royalblue()** will also work - see
46
+ for a later subsection how to customize (and control) this.
47
+
48
+ The **Colours gem** has other, older projects, such as AnsiColours,
49
+ ColourE, AliasE and several other smaller sub-projects that I have
50
+ used over the years, integrated. It is thus a **bundled project**. This
51
+ is specifically mentioned in the event that you may wish to look at
52
+ the code, and wonder a little why it is structured the way it is.
53
+
54
+ ## Requiring the colours project
55
+
56
+ To require the colours project, do:
57
+
58
+ require 'colours'
59
+
60
+ You can also **autoinclude** this module into your project, at
61
+ <b>require-time</b>, via:
62
+
63
+ require 'colours/autoinclude'
64
+
65
+ This will make the **Colours namespace** and the
66
+ **Colours::HtmlColours namespace** available, via
67
+ **include Colours** ultimately.
68
+
69
+ If you need more control over the include-action then you should
70
+ just use the first variant, require 'colours', and then do the
71
+ include action specifically onto whatever class/module you need
72
+ that functionality.
73
+
74
+ ## Introduction and Overview
75
+
76
+ The toplevel module name is **Colours** and you can include
77
+ this module in any of your classes, via:
78
+
79
+ require 'colours'
80
+
81
+ include Colours
82
+
83
+ If you include Colours into main (toplevel), then you can simply
84
+ use the colour-constants directly:
85
+
86
+ puts RED+'This is a red text.'
87
+ puts BLUE+'This will be in blue.'
88
+
89
+ Keep in mind when you use something like the above, with the
90
+ leading escape-code for RED or BLUE, then this is missing the
91
+ proper escape-code for **end** (**revert**). This value is
92
+ usually **\e[0;37m**, or simpler, use **Colours.rev()** such
93
+ as in:
94
+
95
+ puts RED+'This is a red text.'+Colours.rev
96
+ puts BLUE+'This will be in blue.'+Colours.rev
97
+
98
+ The corresponding colour-methods can also be used:
99
+
100
+ red 'This is a red text.'
101
+ blue 'This is a blue text.'
102
+
103
+ This has the advantage that you do not have to use
104
+ Colours.rev anymore. So in some ways the colour-named
105
+ methods are better. (Of course it is a bit complicated
106
+ if you want to use RGB colours, so always make sure to
107
+ specifically **include** what you really need.)
108
+
109
+ Colour-related constants are simply **hardcoded**, such as
110
+ in this way:
111
+
112
+ GREEN = "\e[0;32m"
113
+
114
+ To test all colours, after requiring the project such as
115
+ described above, do:
116
+
117
+ Colours.test
118
+
119
+ Several methods exist as well, such as **sfancy()**, **swarn()**,
120
+ **simp()**, **sdir()** and **sfile()**, among others.
121
+
122
+ These methods can be used to colourize certain Strings,
123
+ such as:
124
+
125
+ a file → use sfile()
126
+ a directory → use sdir()
127
+ a warning → use swarn()
128
+
129
+ The methods **sfancy()** and **simp()** are there to denote
130
+ more important output, usually in the middle of a String.
131
+ Think of this as a way to **emphasize** what you wish to
132
+ display to the user at hand. (The important parts of
133
+ the sentence should be colourized and emphasized. This is
134
+ the whole point for simp() to exist; simp() is a shortcut
135
+ for **string_important**, by the way.)
136
+
137
+ To make use of the **html-colours component**, such as converting
138
+ a "HTML Colour" to its RGB values, you can do this:
139
+
140
+ puts Colours::HtmlColours.colour_to_rgb(:sienna) # ← This variant no longer works.
141
+ puts Colours.html_colours.colour_to_rgb(:sienna) # ← This variant no longer works.
142
+ pp Colours.colour_to_rgb(:sienna) # ← This variant actually works, and will return an Array such as: [160, 82, 45]
143
+
144
+ If you want to return a random HTML colour, you can use this method
145
+ call:
146
+
147
+ Colours::HtmlColours.random
148
+
149
+ or
150
+
151
+ Colours.html_colours.sample # => "darksalmon"
152
+ Colours.html_colours.sample # => "turquoise"
153
+ Colours.html_colours.sample # => "lightblue"
154
+
155
+ Or just:
156
+
157
+ Colours.sample # => "khaki"
158
+ Colours.random_html_colour # => "slateblue"
159
+
160
+ Use whichever variant you prefer. The shorter names are more elegant in
161
+ my opinion.
162
+
163
+ In general, the html component can be used to convert the trivial
164
+ <b>html colours</b> into <b>corresponding R,G,B values</b>.
165
+
166
+ ## Linux terminals and colour support
167
+
168
+ The general syntax rules for colours is in the form of **fg_bg** values,
169
+ where a value of 38 stands for the foreground, and 48 stands for
170
+ the background.
171
+
172
+ The ANSI colour for red is 196 and the ANSI colour for black is
173
+ 0.
174
+
175
+ To use the colour red, you could issue this command:
176
+
177
+ printf "\e[38;5;196m Hello world in red\n"
178
+
179
+ To use the colour black as background, you could issue this command:
180
+
181
+ printf "\e[48;5;0m Hello world in black\n"
182
+
183
+ Do note that the same can be accomplished via RGB values rather than
184
+ ANSI color codes, as long as the terminal supports this (KDE Konsole
185
+ does).
186
+
187
+ Depending on whether you want to apply the color to the foreground or
188
+ to the background, use an **fg_bg** value of 38 or 48 (respectively).
189
+
190
+ Example:
191
+
192
+ printf "\e[<fg_bg>;2;<R>;<G>;<B>m"
193
+ printf "\e[38;2;255;0;0m Foreground color: red\n"
194
+ printf "\e[48;2;0;0;0m Background color: black\n"
195
+
196
+ This may be the better variant altogether, as it is quite easy to convert
197
+ into (and from) **RGB values**, but your mileage may vary.
198
+
199
+ Of course you can use this in plain ruby just as well - let's show this
200
+ via puts:
201
+
202
+ puts "\e[38;2;#{222};#{131};#{141}m Hello world!"
203
+ puts "\e[38;2;#{122};#{56};#{141}m Hello world!"
204
+ puts "\e[38;2;122;156;141m Hello world!"
205
+ puts "\x1b[3mHello world!\x1b[0m"
206
+ puts "\e[38;3mHello world!\x1b[0m"
207
+
208
+ In bash the ESC code can be either of the following:
209
+
210
+ \e
211
+ \033 (octal)
212
+ \x1B (hexadecimal)
213
+
214
+ The "\e[0m" sequence removes all attributes, including formatting and colors.
215
+ It may be useful to add it to the end of each colour text - and this is
216
+ what the **Colours** project is essentially doing.
217
+
218
+ To see which colours are supported/supportable, for each terminal,
219
+ have a look at the following **link**:
220
+
221
+ https://misc.flogisoft.com/bash/tip_colors_and_formatting#terminals_compatibility
222
+
223
+ To set both the foreground and background colours at once, you can use:
224
+
225
+ printf "\e[S;FG;BGm"
226
+ echo -e "\e[S;FG;BGm"
227
+
228
+ For example, bold white foreground on a red background:
229
+
230
+ printf "\e[1;97;41mHello world!"
231
+ printf "\e[1;97;41mHello world!\n"
232
+
233
+ Thus, if you would like to use red colour on black background,
234
+ you could do this:
235
+
236
+ printf '\e[38;5;196m;\e[48;5;0m Hello world!\n'
237
+
238
+ Specifically, the background colours are:
239
+
240
+ 40 black
241
+ 41 red
242
+ 42 green
243
+ 43 yellow
244
+ 44 blue
245
+ 45 magenta
246
+ 46 cyan
247
+ 47 white
248
+
249
+ The following command will use red background:
250
+
251
+ echo -e '\e[0;41m'
252
+ echo -e '\e[0;41m hello world\n\n ok\e[0;m'
253
+
254
+ ## Obtain all available HTML colours
255
+
256
+ To obtain all available html-colours, do this:
257
+
258
+ Colours::HtmlColours.all_colours?
259
+
260
+ Or in a simpler way, without the ::HtmlColours part:
261
+
262
+ Colours.return_all_html_colours
263
+
264
+ There are presently 142 registered HTML colours available:
265
+
266
+ Colours.return_all_html_colours.size # => 142
267
+
268
+ If you need to find out whether a given String (a **word**)
269
+ is registered as part of the HTML-Colours within **module
270
+ Colours**, then you could use the following toplevel-method:
271
+
272
+ Colours.is_this_html_colour_included?
273
+ Colours.is_this_html_colour_included? :slateblue # => true
274
+ Colours.is_this_html_colour_included? 'royalblue' # => true
275
+
276
+ ## eparse()
277
+
278
+ The eparse() method is a convenience method to apply on
279
+ Strings such as 'Foo: bar'. Note the ':' character
280
+ there. That input will be split, and then displayed
281
+ via two different colours.
282
+
283
+ ## Underline / Underlining
284
+
285
+ You can **underline** text, and print it onto the terminal,
286
+ by issuing a command such as the following:
287
+
288
+ txt = 'Hello world!'
289
+
290
+ Colours.underline(txt)
291
+
292
+ You can also add colours to this, via {}:
293
+
294
+ Colours.underline(txt)
295
+ Colours.underline(txt) { :palegreen }
296
+ Colours.underline(txt) { :slateblue }
297
+ Colours.underline(txt) { :orange }
298
+ Colours.underline(txt) { :crimson }
299
+
300
+ Within the {} block you can use HTML colours, as symbol, such as
301
+ :slateblue or :orange and so forth. If you would rather not like
302
+ to use these colours then simply do not pass them into the
303
+ method, as the first variant shows. :)
304
+
305
+ If you only want to get the colour code for that string, without
306
+ displaying it on the terminal it, then you can use .string_underline()
307
+ or .return_underline() method:
308
+
309
+ Colours.string_underline(txt) { :palegreen }
310
+ Colours.string_underline(txt) { :slateblue }
311
+ Colours.string_underline(txt) { :orange }
312
+ Colours.string_underline(txt) { :crimson }
313
+ Colours.return_underline(txt) { :royalblue }
314
+
315
+ ## include Colours::Methods
316
+
317
+ Since as of February 2019 there is a module called **Methods**
318
+ part of the colours gem. This module allows us to include
319
+ the konsole-related colour methods into a subclass.
320
+
321
+ Example:
322
+
323
+ class Foo
324
+ include Colours::Methods
325
+ end
326
+
327
+ e Foo.new.royalblue('hey there')
328
+
329
+ As you can see, this class will have the HTML colours available,
330
+ such as .royalblue() or .slateblue() and so forth.
331
+
332
+ I needed this in some of my other code, so it was added. I like
333
+ full colour support on terminals such as **KDE konsole**.
334
+
335
+ Note that this has to be specifically included, as I am not sure
336
+ everyone wants to have that the moment **include Colours** is
337
+ done. The toplevel Colours module will stay a bit simpler by
338
+ default; for customization, you will have to go the extra
339
+ line through **include Colours::Methods**, which appears to be
340
+ an acceptable trade-off.
341
+
342
+ Note that you can also subclass from a "dummy" class with colour
343
+ support, such as royalblue() or slateblue.
344
+
345
+ Use code similar to the following variant for this:
346
+
347
+ require 'colours/base/base.rb'
348
+
349
+ class Foobar < Colours::Base # Or whatever the name of your class is
350
+ end
351
+
352
+ ## KDE Konsole support
353
+
354
+ The **Colours gem** used to have a submodule called **Konsole**,
355
+ in particular the <b>KDE Konsole</b>. In May 2019 this submodule
356
+ was removed; the functionality is now available in the form of
357
+ an autogenerated .rb file instead.
358
+
359
+ You can **use RGB colours** in the KDE konsole (but also in
360
+ other terminal-types such as vte-based ones, like
361
+ **mate-terminal**).
362
+
363
+ For an example, have a look at the file **bin/colours**
364
+ that is distributed with this gem here (the colours gem). That file
365
+ will output all the HTML colour variants (via their RGB values).
366
+ Best shown on a black background in your terminal.
367
+
368
+ To **view all RGB colours** based on their HTML names, such
369
+ as <b>palegreen</b> or <b>slateblue</b>, do this:
370
+
371
+ colours
372
+
373
+ Also note that since as of **May 2018**, you can invoke the
374
+ HTML colours on the Konsole namespace directly, including
375
+ text-output, via code like this:
376
+
377
+ Colours.edarkgreen 'yo there'
378
+ Colours.eslateblue 'yo there'
379
+ Colours.eroyalblue 'yo there'
380
+ Colours.edarkgreen 'Hello world!'
381
+
382
+ The leading 'e' of these methods stands for "echo", aka
383
+ puts-related output. In other words, to print the text
384
+ that comes afterwards.
385
+
386
+ To print something in bold, you can use **Colours.bold()**
387
+ like in this way:
388
+
389
+ Colours.bold
390
+
391
+ ## Showing the colour palette on the commandline
392
+
393
+ You can show the "classical" ASCII colours on the commandline by
394
+ invoking this method:
395
+
396
+ Colours.show_palette
397
+
398
+ This also works, or should work, from the commandline, like so:
399
+
400
+ colours --show-palette
401
+ colours --palette
402
+
403
+ ## The KDE colour palette
404
+
405
+ The **KDE project** makes use of a special, **named colour palette**.
406
+
407
+ This palette includes the following **20 different colours**, via
408
+ a trivial name:
409
+
410
+ Abyss Blue
411
+ Alternate Grey
412
+ Beware Orange
413
+ Burnt Charcoal
414
+ Cardboard Grey
415
+ Charcoal Grey
416
+ Coastal Fog
417
+ Deco Blue
418
+ Hover Blue
419
+ Hyper Blue
420
+ Icon Blue
421
+ Icon Green
422
+ Icon Grey
423
+ Icon Red
424
+ Icon Yellow
425
+ Lazy Grey
426
+ Noble Fir
427
+ Paper White
428
+ Pimpinella
429
+ Plasma Blue
430
+
431
+ You can find these entries, including their hex-values and their
432
+ RGB values, on websites such as this one here:
433
+
434
+ https://community.kde.org/KDE_Visual_Design_Group/HIG/Color
435
+
436
+ Note that these are also called the "Breeze" colours, which I
437
+ assume is the name of the theme.
438
+
439
+ Since as of July 2018, the colours project also includes these
440
+ colours, via the file <b>colours/constants/kde_colour_palette.rb</b>.
441
+
442
+ The entries are stored in a **.yml file**, so if anyone wants to re-use
443
+ these from a yaml file, feel free to just copy/paste it from there.
444
+ That file is at <b>colours/yaml/kde_colour_palette.yml</b>.
445
+
446
+ Internally, the values are made available via the constant:
447
+
448
+ Colours::KDE_COLOUR_PALETTE
449
+
450
+ Which is a hash. There are also a few methods that may be useful to
451
+ use. For example, if you want to use a random colour, and output
452
+ <b>Hello world!</b>, then you could use the following method:
453
+
454
+ Colours.write_this_via_kde_colour_palette 'Hello world!', :random
455
+
456
+ While random colours may be nice, perhaps you may want to use a
457
+ definite colour from the above list. Say that you may want to
458
+ write via <b>Plasma Blue</b>. In this case, you could use:
459
+
460
+ Colours.write_this_via_kde_colour_palette 'Hello world!', :plasma_blue
461
+
462
+ So using a symbol works too.
463
+
464
+ If you tend to use this regularly, then an even simpler way may exist,
465
+ by simply calling a method that already has that as part of its name.
466
+
467
+ Examples with **Hello World!**:
468
+
469
+ Colours.kde_colour_palette_abyss_blue 'Hello world!'
470
+ Colours.kde_colour_palette_alternate_grey 'Hello world!'
471
+ Colours.kde_colour_palette_beware_orange 'Hello world!'
472
+ Colours.kde_colour_palette_burnt_charcoal 'Hello world!'
473
+ Colours.kde_colour_palette_cardboard_grey 'Hello world!'
474
+ Colours.kde_colour_palette_charcoal_grey 'Hello world!'
475
+ Colours.kde_colour_palette_coastal_fog 'Hello world!'
476
+ Colours.kde_colour_palette_deco_blue 'Hello world!'
477
+ Colours.kde_colour_palette_hover_blue 'Hello world!'
478
+ Colours.kde_colour_palette_hyper_blue 'Hello world!'
479
+ Colours.kde_colour_palette_icon_blue 'Hello world!'
480
+ Colours.kde_colour_palette_icon_green 'Hello world!'
481
+ Colours.kde_colour_palette_icon_grey 'Hello world!'
482
+ Colours.kde_colour_palette_icon_red 'Hello world!'
483
+ Colours.kde_colour_palette_icon_yellow 'Hello world!'
484
+ Colours.kde_colour_palette_lazy_grey 'Hello world!'
485
+ Colours.kde_colour_palette_noble_fir 'Hello world!'
486
+ Colours.kde_colour_palette_paper_white 'Hello world!'
487
+ Colours.kde_colour_palette_pimpinella 'Hello world!'
488
+ Colours.kde_colour_palette_plasma_blue 'Hello world!'
489
+
490
+ The reason as to why this is so long is so that we can avoid any name clashes -
491
+ but in principle, we could also enable a **shorter name**, such as:
492
+
493
+ Colours.pimpinella # much shorter than Colours.kde_colour_palette_pimpinella
494
+
495
+ You can also use the "e" method, **e** which stands for **echo**, such as in:
496
+
497
+ Colours.epimpinella 'Hello cats!'
498
+ Colours.eplasma_blue 'Hello cats!'
499
+
500
+ This functionality is available for the Colours project since as of July 2018 -
501
+ but be careful, since this may change one day, in the event that a conflict
502
+ may exist with an already defined name (such as the names in the HTML colour
503
+ charts, e. g. "slateblue", "royalblue" and so forth).
504
+
505
+ Do note that the behaviour may change, too; e. g. **Colours.pimpinella()** may in
506
+ the future only return a String, and a new method called **Colours.epimpinella()**
507
+ would be tasked with outputting the text - but for the time being, things stay
508
+ as described above (in July 2018).
509
+
510
+ ## Generating a shell file with all HTML colours
511
+
512
+ You can generate a shell file that can be sourced, in **bash**, **fish**
513
+ and possibly **zsh**, in order to make use of the HTML colours on the
514
+ commandline.
515
+
516
+ The method that does so is:
517
+
518
+ Colours.generate_shell_file_containing_the_html_colours()
519
+
520
+ This will store in the current working directory; or to another
521
+ directory if you pass an argument to it.
522
+
523
+ The file will have entries such as:
524
+
525
+ export CHARTREUSE="\e[38;2;127;255;0m"
526
+
527
+ This is the RGB variant for the colour at hand. The closing tag
528
+ is missing there, so you may have to use it if you wish to
529
+ output text that is coloured.
530
+
531
+ You can source this .sh file and re-use it in your own scripts.
532
+
533
+ You can also generate this **shell file** from the commandline,
534
+ through **bin/colours**.
535
+
536
+ Issue a command like any of the following variants:
537
+
538
+ colours --generate-shell-file-containing-the-html-colours
539
+ colours --generate_shell_file_containing_the_html_colours
540
+ colours --generateshellfilecontainingthehtmlcolours
541
+ colours --create-shell-file
542
+
543
+ == Using the Konsole submodule
544
+
545
+ In the past there was a Konsole submodule, but in May 2019 during
546
+ a large rewrite, this submodule has been removed.
547
+
548
+ The functionality has been integrated into an autogenerated .rb
549
+ file though. That module can be found in the file called
550
+ **toplevel_basic_colour_methods.rb**.
551
+
552
+ Old invocation examples such as:
553
+
554
+ Colours[:slateblue]
555
+
556
+ Should be possible still.
557
+
558
+ You can also include this new module:
559
+
560
+ include Colours::AllColourMethods
561
+
562
+ Then you can simply call the respective colour output:
563
+
564
+ slateblue('Hello World!')
565
+
566
+ eslateblue()
567
+ eslateblue('Hello World!')
568
+
569
+ eslateblue() works like slateblue() but outputs the
570
+ result.
571
+
572
+ konsole_colour_slateblue('Hello World!')
573
+ ekonsole_colour_slateblue('Hello world!')
574
+
575
+ The above two variants are probably too long, but they
576
+ also exist if you wish to be more specific.
577
+
578
+ Automatic inclusion can be done like so:
579
+
580
+ require 'colours/konsole/autoinclude'
581
+
582
+ Konsole['slateblue']+'Hello World'
583
+ konsole :green, 'hello world'
584
+
585
+ Of course you can also manually include it by yourself:
586
+
587
+ require 'colours'
588
+
589
+ Note that when you include that module, you will have access
590
+ to methods such as e. g. slateblue() or sandybrown().
591
+
592
+ konsole_colours :slateblue, 'hello world!'
593
+
594
+ ## 256 colour support
595
+
596
+ Some terminals allow **support for 256 colours**.
597
+
598
+ The colours gem allows you to test this, via this toplevel-method:
599
+
600
+ Colours.show_all_256_colours
601
+
602
+ If you need to specifically use one of these colours, have a look
603
+ at the following two methods:
604
+
605
+ Colours.return_this_256_colour()
606
+ Colours.display_this_256_colour()
607
+
608
+ The first input argument should be the number, from 0-255, and
609
+ the second argument is the text that is to be displayed (append
610
+ a newline to this if you need one).
611
+
612
+ The first input argument is called <b>id</b>, for the purpose of
613
+ this document here.
614
+
615
+ Let's provide specific examples how to use the latter method.
616
+
617
+ For example, to ouput, in **red**, the sentence "<b>Ruby is awesome!</b>",
618
+ you could use either of the following methods:
619
+
620
+ Colours.display_this_256_colour(88, "Ruby is awesome!\n")
621
+ Colours.display_this_in_256_colour(88, "Ruby is awesome!\n")
622
+
623
+ Note that you can also use several colours, based on the id input,
624
+ via a pseudo-range. A **pseudo-range** is input that is a String and
625
+ includes one '-' character. For example, **33-44** is a pseudo-range
626
+ and so is **0-255**.
627
+
628
+ In ruby code, this could work like so - give it a try:
629
+
630
+ require 'colours'
631
+
632
+ Colours.display_this_256_colour('0-255',"Hello world, in a batch!\n")
633
+
634
+ If you wish to make use of these colour-methods in one of your classes,
635
+ then you can require the module, and include it into your class.
636
+
637
+ Example for this:
638
+
639
+ require 'new_colours/autogenerated/support_for_256_colours.rb'
640
+
641
+ class Foobar
642
+
643
+ include NewColours::SupportFor256Colours
644
+
645
+ def initialize
646
+ puts darkturquoise('HELLO ')+
647
+ maroon('WORLD ')+
648
+ 'This is ok again'
649
+ end
650
+
651
+ end
652
+
653
+ Foobar.new
654
+
655
+ ## revert
656
+
657
+ The toplevel instance variable called **@revert** designates which
658
+ escape code is used for reverting the colours again.
659
+
660
+ By default, this is **\e0m**. However had, for some strange reason
661
+ this does not appear to work very well on the default terminal
662
+ style that I use (KDE konsole, white font on black background).
663
+ It seems to default to white bold text, but I would rather want
664
+ light white text, aka **\e[0;37m**. This is why revert defaults
665
+ to **\e[0;37m**.
666
+
667
+ If you wish to use \e0m instead, then you can do so via:
668
+
669
+ Colours.set_revert('\e0m')
670
+
671
+ Or, via symbol, to the same value:
672
+
673
+ Colours.set_revert(:default)
674
+
675
+ ## Legacy versions of the Colours gem
676
+
677
+ In May 2019, the old **Konsole** submodule has been removed; the
678
+ functionality itself has been retained, though. Still, as the API
679
+ changed this means that not everyone may be able to use the new
680
+ colours gem release.
681
+
682
+ This is the reason why the old version at **0.3.40** will continue
683
+ to be available here. This one still has the old **Colours::Konsole**
684
+ submodule defined, so if you need it in a project, feel free to
685
+ use that older version.
686
+
687
+ Otherwise I recommend to **upgrade** to the latest version of the
688
+ colours gem - the code is, in my opinion, better too.
689
+
690
+ In 2021 the old gem was released; only version 0.5.55 is
691
+ still available. Consider changing to the new code base if
692
+ possible - it should have a higher quality as well.
693
+
694
+ ## Available colour methods
695
+
696
+ If you wish to find out which colour methods will be available by
697
+ default, onto the main Colours namespace, you can use the following
698
+ method to find out:
699
+
700
+ Colours.all_available_colour_methods?
701
+
702
+ This will return an Array containing the names of all these
703
+ toplevel methods. In May 2019 we can find 307 available
704
+ colour methods e. g. such as **Colours.slateblue** or
705
+ **Colours.lightblue** and so forth.
706
+
707
+ ## Remove escaping sequences
708
+
709
+ If you wish to remove all escaping sequences from a given String,
710
+ you can use the following API for this:
711
+
712
+ Colours.remove_escape_sequences()
713
+ Colours.remove_escape_sequences "\e[38;2;41;128;18mHello world!\e[0;37m" # => "Hello world!"
714
+ pp Colours.remove_escape_sequences(Colours.slateblue('Hello world!')) # => "Hello world!"
715
+
716
+ The latter example shows that the escape-sequences are properly removed.
717
+
718
+ If you still find an example where the escape sequences are not
719
+ working properly, e. g. because they are retained, then consider
720
+ this to be a bug; once reported, a test case can be added to allow
721
+ for removing this escape sequence as well.
722
+
723
+ **Sometimes** you may **only wish to remove the trailing escape
724
+ sequence**, aka "\e[0;37m". In this case the following method may
725
+ be useful:
726
+
727
+ Colours.remove_trailing_end_from()
728
+ Colours.remove_trailing_ansii_escape_code()
729
+
730
+ Usage example:
731
+
732
+ x = Colours.remove_trailing_end_from("\e[38;2;70;130;180m\e[0;37m") # => "\e[38;2;70;130;180m"
733
+
734
+ ## Colours.does_this_line_include_a_html_colour?
735
+
736
+ If you need to determine whether a line (a string) includes a valid
737
+ HTML colour, such as slateblue>, then you can use the following method:
738
+
739
+ Colours.does_this_line_include_a_html_colour?
740
+ Colours.does_this_line_include_a_html_colour? "<green>yo there</green> <orange>getline() function</orange>" # => true
741
+ Colours.does_this_line_include_a_html_colour? "foo bar" # => false
742
+
743
+ ## Colours.replace_all_html_colours_in_this_line
744
+
745
+ If you wish to replace all HTML colours in a given line/string,
746
+ then the following **toplevel method** can be used:
747
+
748
+ Colours.replace_all_html_colours_in_this_line
749
+ puts Colours.replace_all_html_colours_in_this_line '<one>hey</one>' # ← This variant works as well.
750
+
751
+ This has been specifically added for commandline-use. It allows us
752
+ to replace HTML colour "tags" with the corresponding RGB value,
753
+ so that a terminal emulator such as the KDE konsole can display
754
+ this.
755
+
756
+ ## Rainbow colours
757
+
758
+ You must install the paint gem first:
759
+
760
+ gem install paint
761
+
762
+ Then you can do the following:
763
+
764
+ Colours::RainbowColours.print_rainbow_line("Hello world \n" * 40)
765
+
766
+ To print a line directly you can also use printl_plain():
767
+
768
+ Colours::RainbowColours.println_plain "one two three four five six seven eight nine ten\n\n\n"
769
+
770
+ ## Colours.fancy_parse
771
+
772
+ The toplevel method **Colours.fancy_parse()** can be used to
773
+ parse a more complicated text/string.
774
+
775
+ For example, say that you have a HTML string with embedded i
776
+ tag and HTML colours.
777
+
778
+ You can display this on the commandline.
779
+
780
+ Example:
781
+
782
+ puts Colours.fancy_parse "<lightgreen><i>hey</i></lightgreen> <teal>there</teal>"
783
+ puts Colours.fancy_parse "<tomato>hey</tomato> <teal>there</teal>"
784
+ puts Colours.fancy_parse "<tomato><i>hey</i></tomato> <teal>there</teal>"
785
+ puts Colours.fancy_parse "<tomato><b>Hello world.</b></tomato>"
786
+ puts Colours.fancy_parse "<tomato>Hello world.</tomato>"
787
+
788
+ I recommend the KDE Konsole for this, but it should work on gnome-terminal as
789
+ well. Currently (September 2019) only HTML colours, such as tomato, steelblue,
790
+ and so forth, are supported, as well as i (italic). This may be extended at
791
+ a later time including bold.
792
+
793
+ Note that this is not working perfectly correctly for longer strings with
794
+ lots of tags. At a later point this will have to be improved, but for now,
795
+ it simply has to suffice. Patches are welcome, though. :)
796
+
797
+ ## Support for italic text
798
+
799
+ In KDE konsole, the escape sequences **\e[3m** and **\e[23m** can be used
800
+ to turn **italics** on and off, respectively. See this commit:
801
+
802
+ https://invent.kde.org/utilities/konsole/commit/68a98ed77063e622985d422b625d7dc5895f10c3
803
+
804
+ Let's have a look at an example for this in ruby (and KDE konsole,
805
+ but it may work in other terminals as well, such as the mate-terminal,
806
+ which in turn is based on **VTE**):
807
+
808
+ puts "\e[3mHello world!\e[23m"
809
+
810
+ I tested this in **July 2020** and it works fine.
811
+
812
+ You can use regular text afterwards and that works as well.
813
+
814
+ Example:
815
+
816
+ puts "\e[3mHello world!\e[23m abc"
817
+
818
+ See the following partial image:
819
+
820
+ <img src="https://i.imgur.com/gZ183qA.png" style="margin: 1em; margin-left: 3em">
821
+
822
+ Of course it can also work on the commandline, e. g. via bash/zsh or
823
+ a similar shell, as the following example shows:
824
+
825
+ echo -e "\e[3mHello World\e[0m"
826
+
827
+ Notice the **3** there - this is the code for <i>italic</i>.
828
+
829
+ If you use the colours gem and you need a String in italic,
830
+ consider using the following API:
831
+
832
+ puts Colours.string_italic("Hello World!")+' OK' # => Hello World! OK
833
+
834
+ ## Removing html-colours and other "tags" from a String
835
+
836
+ If you have entries such as <one> or <steelblue> (aka one, and steelblue),
837
+ and wish to replace them with the RGB values, for commandline use,
838
+ you could try to use this method:
839
+
840
+ Colours.eliminate_html(your_string_here)
841
+ Colours.away_with_html_colours_and_special_numbers(your_string_here)
842
+ Colours.away_with_html_colours_and_special_numbers "<royalblue>+</royalblue>" # => "\e[38;2;128;128;128m\e[38;2;65;105;225m+\e[38;2;128;128;128m"
843
+
844
+ This was needed so that other projects can **turn strings into colourized
845
+ strings** - on the commandline. This explains the result, as the \e is
846
+ typically used to specify an escape sequence.
847
+
848
+ ## Converting hex-value to R,G,B value
849
+
850
+ If you need the R,G,B value from a hexadecimal value then you can
851
+ use the following method:
852
+
853
+ Colours.convert_hex_to_rgb('#baf185') # => [186, 241, 133]
854
+ Colours.hex_to_rgb('#baf185') # => [186, 241, 133]
855
+
856
+ Note that this will return an Array, with R,G,B values. If you need
857
+ a string, use **.join()** on that result.
858
+
859
+ ## class Colours::Colours
860
+
861
+ This oddly named class (<b>Colours::Colours</b>) has been
862
+ <b>inspired by another gem</b>, called **rainbow**.
863
+
864
+ That gem allows you to do the following:
865
+
866
+ require 'rainbow'
867
+ x = Rainbow("hola!").blue.bright.underline
868
+ puts x
869
+
870
+ I found this neat, in particular the <b>Rainbow(STRING_GOES_HERE)</b>
871
+ part. Then I realised that the **colours** gem does not allow for a
872
+ toplevel-method call such as **Colours()**. So, in <b>December 2021</b>,
873
+ I decided to change this, and added the file **colours/class/class.rb**.
874
+
875
+ This file defines **Colours()**, as method. In the long run I
876
+ intend to make this fully compatible with what **Rainbow()**
877
+ offers, but for now this is a semi-minimal implementation.
878
+
879
+ Have a look at that .rb file to see what it is currently capable
880
+ of doing.
881
+
882
+ ## Converting html-colours to their HEX value
883
+
884
+ If you want to convert a html-colour into the corresponding RGB value
885
+ then try **bin/html_colour_to_hex_value** like in this way:
886
+
887
+ html_colour_to_hex_value slateblue # Output would be '#6A5ACD', without '' quotes.
888
+
889
+ (You may have to add the bin/ path of that gem to your $PATH.)
890
+
891
+ Internally the code that allows this is the following:
892
+
893
+ require 'colours/toplevel_methods/methods_related_to_html_colours.rb'
894
+ puts Colours.html_colour_to_hex_value(ARGV)
895
+
896
+ If you need the alpha value then you could use this method:
897
+
898
+ Colours.convert_hex_code_to_RGBA_array('#baf185') # => [186, 241, 133, 1.0]
899
+
900
+ This defaults to alpha being 1.0. In a future release of the
901
+ colours gem this may be made more sophisticated, but for
902
+ now this has to suffice.
903
+
904
+ ## Licence
905
+
906
+ The project used to be under the **GPL-2.0 licence** (no later clause),
907
+ until **August 2019** (**26.08.2019**, in dd.mm.yyyy notation).
908
+
909
+ However had, I have changed my mind for various reasons (including the
910
+ situation that different projects, with different licenses, may make
911
+ use of the **colours gem**) and thus re-published the colours
912
+ project under the less stringent **MIT licence**. Both licences are
913
+ fine licences, but I feel that for the basic building blocks, such
914
+ as the colours gem, a less stringent licence makes a lot more
915
+ sense.
916
+
917
+ See the file **LICENCE.md** for this licence, or just have look at
918
+ the following URL here:
919
+
920
+ https://opensource.org/licenses/MIT
921
+
922
+ Replace the effective year simply with whatever was the last release
923
+ on the rubygems.org homepage of this gem here.
924
+
925
+ The most important requirement for the MIT licence is the no-warranty
926
+ clause. The rest is, IMO, not so relevant (I don't even care about
927
+ author citation either, but mentioning a no-warranty clause makes
928
+ sense, in my opinion).
929
+
930
+
931
+ ## Contact information and mandatory 2FA coming up in 2022
932
+
933
+ If your creative mind has ideas and specific suggestions to make this gem
934
+ more useful in general, feel free to drop me an email at any time, via:
935
+
936
+ shevy@inbox.lt
937
+
938
+ Before that email I used an email account at Google gmail, but in **2021** I
939
+ decided to slowly abandon gmail for various reasons. In order to limit this
940
+ explanation here, allow me to just briefly state that I do not feel as if I
941
+ want to promote any Google service anymore, for various reasons.
942
+
943
+ Do keep in mind that responding to emails may take some time, depending on
944
+ the amount of work I may have at that moment.
945
+
946
+ In 2022 rubygems.org decided to make 2FA mandatory for every gem owner:
947
+ see https://blog.rubygems.org/2022/06/13/making-packages-more-secure.html
948
+
949
+ As I can not use 2FA, for reasons I will skip explaining here (see
950
+ various github issue discussions in the past), this effectively means that
951
+ Betty Li and others who run the show at rubygems.org will perma-ban me
952
+ from using rubygems as a developer.
953
+
954
+ As I disagree with that decision completely, this will mean that all my
955
+ gems will be removed from rubygems.org prior to that sunset date, e. g.
956
+ before they permanently lock me out from the code that I used
957
+ to maintain. It is pointless to want to discuss this with them anymore -
958
+ they have made up their minds and decided that you can only use
959
+ the code if 2FA is in place, even though the code itself works just
960
+ fine. If you don't use 2FA you are effectively locked out from your
961
+ own code; I consider this a malicious attack. See also how they limited
962
+ discussions to people with mandatory 2FA on the ruby-bugtracker, thus
963
+ banning everyone permanently without 2FA:
964
+
965
+ https://bugs.ruby-lang.org/issues/18800
966
+
967
+ Guess it may indeed be time to finally abandon ruby - not because
968
+ ruby is a bad language, but there are people now in charge who
969
+ really should not be part of ruby in the first place.
970
+