lab42_tag_cloud 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f436800624ff7f6586968f55090f6001d0c4e90edcc686f9657217c9b46ff7bd
4
+ data.tar.gz: f79aec9fd036b91ca147d6a6f04be3a2058b3f39627f7fae59f05a9376156149
5
+ SHA512:
6
+ metadata.gz: 5bd224ec924a387260b50fe80fad583bcc6d9c95ea296bb6f395701a2d490299a221107b0c0faf781af75c3371bc1aa5b24f9048edfe50b921e8e488d73e8c52
7
+ data.tar.gz: d549bc5db2f6c04aa7a5c9140d8972d5baea353f458cb2fed98cb9067f2d6f845e4aa5bd935afe3be03537e7cb79a8de88737e120b19bb38f87bd5ed8c961b37
data/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,138 @@
1
+ [![Issue Count](https://codeclimate.com/github/RobertDober/lab42_tag_cloud/badges/issue_count.svg)](https://codeclimate.com/github/RobertDober/lab42_tag_cloud)
2
+ [![Gem Version](http://img.shields.io/gem/v/lab42_lab42_tag_cloud.svg)](https://rubygems.org/gems/lab42_lab42_tag_cloud)
3
+ [![CI](https://github.com/robertdober/lab42_tag_cloud/workflows/CI/badge.svg)](https://github.com/robertdober/lab42_tag_cloud/actions)
4
+ [![Coverage Status](https://coveralls.io/repos/github/RobertDober/lab42_tag_cloud/badge.svg?branch=main)](https://coveralls.io/github/RobertDober/lab42_tag_cloud?branch=main)
5
+
6
+
7
+ # Lab42::TagCloud
8
+
9
+ Creating Tag Clouds with gamma correct color values and styles from a simple DSL.
10
+
11
+ This is a port of [Elixir's tag_cloud](https://github.com/RobertDober/tag_cloud) to Ruby
12
+
13
+ ## Installation:
14
+
15
+ ##### With bundler
16
+
17
+ ```ruby
18
+ gem 'lab42_tag_cloud'
19
+ ```
20
+
21
+ ##### or simply
22
+
23
+ ```sh
24
+ gem install lab42_tag_cloud
25
+ ```
26
+
27
+ ## Usage:
28
+
29
+ A very simple DSL allows you to describe the size, color and thickness of your tag and each line of this DSL will be transformed into
30
+ some CSS and the text of the tag.
31
+
32
+ ### Colors
33
+
34
+ Gamma correction for scaled colors
35
+
36
+ To create 13 different shades of a color, where 0 means transparent (#ffffff) and 12 opaque (original color value or #000000 as default) which are equally spaced for the human eye we use a gamma correction of 1/2.2 which seems to work very well on modern screens.
37
+
38
+ The result for all 13 shades for some colors can be seen [here](https://htmlpreview.github.io/?https://github.com/RobertDober/tag_cloud/blob/v0.1.0/examples/gamma_correction.html)
39
+
40
+ Right now the size of the scale and the gamma value cannot be modified but that could be easily implemented if desired.
41
+
42
+
43
+ Well let us describe the behavior of this DSL by means of [speculations](https://github.com/RobertDober/speculate_about).
44
+
45
+ ### Context: The DSL
46
+
47
+ The DSL is a very simple string with 3 white space separated words, of which the first is a color specification which we
48
+ speculate about below, the second a `font size` in CSS (defaulting to `pt` if only a number is specified) and the third
49
+ the `font weight` as an optional integer with defaults to `100`
50
+
51
+ This simply is translated to HTML style attributes as follows:
52
+
53
+ Given the following DSL strings
54
+ ```ruby
55
+ include Lab42::TagCloud
56
+
57
+ let(:blue_bold) { "blue 10 800" }
58
+ let(:dgr_shaded) { "10/darkgoldenrod 1.2em" }
59
+ let(:explicit) { "6/#2f3ab0 30px 450" }
60
+ ```
61
+
62
+ Then the `to_style` method will yield the following results
63
+ ```ruby
64
+ expect(to_style(blue_bold)).to eq("color: #0000ff; font-size: 10pt; font-weight: 800;")
65
+ expect(to_style(dgr_shaded)).to eq("color: #995061; font-size: 1.2em;")
66
+ expect(to_style(explicit)).to eq("color: #695676; font-size: 30px; font-weight: 450;")
67
+ ```
68
+
69
+ #### Context: Colors
70
+
71
+ Given we include the module:
72
+ ```ruby
73
+ include Lab42::TagCloud
74
+ ```
75
+
76
+ Then we can see some transformation for colors, e.g. shade 11 of black can be expressed in different ways
77
+ ```ruby
78
+ gray = "525252"
79
+ expect(color_value(11)).to eq(gray)
80
+ expect(color_value("11")).to eq(gray)
81
+ expect(color_value("11/black")).to eq(gray)
82
+ expect(color_value("11/#000000")).to eq(gray)
83
+ ```
84
+
85
+ And we can also use all web color names
86
+ ```ruby
87
+ expect(color_value("10/blue")).to eq("7171ff")
88
+ expect(color_value("10/lime")).to eq("71ff71")
89
+ ```
90
+
91
+ And we can add underscores for readability to the color names
92
+ ```ruby
93
+ expect(color_value("4/medium_slate_blue")).to eq("0d16e0")
94
+ ```
95
+
96
+ #### Context: Convenience Helpers
97
+
98
+ Now we have already everything we need to create nice tag clouds, e.g. with `ERB`, however
99
+ with the API described so far we would need to write code like the following:
100
+
101
+ ```eruby
102
+ <% somd_data_source.each do |datum| %>
103
+ ...
104
+ <span style="<%= Lab42::TagCloud.to_style(datum.dsl) %>"><%= datum.tag %></span>
105
+ ```
106
+
107
+ However if we had, say a helper, that would operate on objects that respond to `dsl` and `tag`, or `[:dsl]` and `[:tag]`,
108
+ then we could do very nice things like
109
+
110
+ ```eruby
111
+ <%= some_data_source.map { Lab42::TagCloud.tag_from_object(_1, tag: "span") } %>
112
+ ```
113
+
114
+ would that not be great?
115
+
116
+ Well guess what it _is_ _great_, and the helper is called `tag_from_object` indeed
117
+
118
+ Given an `OpenStruct` and a `Hash` instance of the required format
119
+ ```ruby
120
+ let(:ostruct) { OpenStruct.new(tag: "Ruby", dsl: "10/red 1.2em") }
121
+ let(:hash) { {tag: "Elixir", dsl: "blue 1.5em 800"} }
122
+ ```
123
+
124
+ Then we can obtain tags from these objects
125
+ ```ruby
126
+ expect(tag_from_object(ostruct)).to eq(%{<span style="color: #ff7171; font-size: 1.2em;">Ruby</span>})
127
+ expect(tag_from_object(hash, tag: :div, class: "some-class"))
128
+ .to eq(%{<div class="some-class" style="color: #0000ff; font-size: 1.5em; font-weight: 800;">Elixir</div>})
129
+ ```
130
+
131
+ Typically such tag clouds can than be easily constructed from external data sources like JSON or YAML
132
+
133
+ # LICENSE
134
+
135
+ Copyright 2022 Robert Dober robert.dober@gmail.com
136
+
137
+ Apache-2.0 [c.f LICENSE](LICENSE)
138
+ <!-- SPDX-License-Identifier: Apache-2.0-->
@@ -0,0 +1,161 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lab42
4
+ module TagCloud
5
+ module Colors
6
+ # rubocop:disable Metrics/ModuleLength
7
+ module Names
8
+ extend self
9
+
10
+ WEBCOLORS = {
11
+ "aliceblue" => "f0f8ff",
12
+ "antiquewhite" => "faebd7",
13
+ "aqua" => "00ffff",
14
+ "aquamarine" => "7fffd4",
15
+ "azure" => "f0ffff",
16
+ "beige" => "f5f5dc",
17
+ "bisque" => "ffe4c4",
18
+ "black" => "000000",
19
+ "blanchedalmond" => "ffebcd",
20
+ "blue" => "0000ff",
21
+ "blueviolet" => "8a2be2",
22
+ "brown" => "a52a2a",
23
+ "burlywood" => "deb887",
24
+ "cadetblue" => "5f9ea0",
25
+ "chartreuse" => "7fff00",
26
+ "chocolate" => "d2691e",
27
+ "coral" => "ff7f50",
28
+ "cornflowerblue" => "6495ed",
29
+ "cornsilk" => "fff8dc",
30
+ "crimson" => "dc143c",
31
+ "cyan" => "00ffff",
32
+ "darkblue" => "00008b",
33
+ "darkcyan" => "008b8b",
34
+ "darkgoldenrod" => "b8860b",
35
+ "darkgray" => "a9a9a9",
36
+ "darkgreen" => "006400",
37
+ "darkkhaki" => "bdb76b",
38
+ "darkmagenta" => "8b008b",
39
+ "darkolivegreen" => "556b2f",
40
+ "darkorange" => "ff8c00",
41
+ "darkorchid" => "9932cc",
42
+ "darkred" => "8b0000",
43
+ "darksalmon" => "e9967a",
44
+ "darkseagreen" => "8fbc8f",
45
+ "darkslateblue" => "483d8b",
46
+ "darkslategray" => "2f4f4f",
47
+ "darkturquoise" => "00ced1",
48
+ "darkviolet" => "9400d3",
49
+ "deeppink" => "ff1493",
50
+ "deepskyblue" => "00bfff",
51
+ "dimgray" => "696969",
52
+ "dodgerblue" => "1e90ff",
53
+ "firebrick" => "b22222",
54
+ "floralwhite" => "fffaf0",
55
+ "forestgreen" => "228b22",
56
+ "fuchsia" => "ff00ff",
57
+ "gainsboro" => "dcdcdc",
58
+ "ghostwhite" => "f8f8ff",
59
+ "gold" => "ffd700",
60
+ "goldenrod" => "daa520",
61
+ "gray" => "808080",
62
+ "green" => "008000",
63
+ "greenyellow" => "adff2f",
64
+ "honeydew" => "f0fff0",
65
+ "hotpink" => "ff69b4",
66
+ "indianred" => "cd5c5c",
67
+ "indigo" => "4b0082",
68
+ "ivory" => "fffff0",
69
+ "khaki" => "f0e68c",
70
+ "lavender" => "e6e6fa",
71
+ "lavenderblush" => "fff0f5",
72
+ "lawngreen" => "7cfc00",
73
+ "lemonchiffon" => "fffacd",
74
+ "lightblue" => "add8e6",
75
+ "lightcoral" => "f08080",
76
+ "lightcyan" => "e0ffff",
77
+ "lightgoldenrodyellow" => "fafad2",
78
+ "lightgray" => "d3d3d3",
79
+ "lightgreen" => "90ee90",
80
+ "lightpink" => "ffb6c1",
81
+ "lightsalmon" => "ffa07a",
82
+ "lightseagreen" => "20b2aa",
83
+ "lightskyblue" => "87cefa",
84
+ "lightslategray" => "778899",
85
+ "lightsteelblue" => "b0c4de",
86
+ "lightyellow" => "ffffe0",
87
+ "lime" => "00ff00",
88
+ "limegreen" => "32cd32",
89
+ "linen" => "faf0e6",
90
+ "magenta" => "ff00ff",
91
+ "maroon" => "800000",
92
+ "mediumaquamarine" => "66cdaa",
93
+ "mediumblue" => "0000cd",
94
+ "mediumorchid" => "ba55d3",
95
+ "mediumpurple" => "9370db",
96
+ "mediumseagreen" => "3cb371",
97
+ "mediumslateblue" => "7b68ee",
98
+ "mediumspringgreen" => "00fa9a",
99
+ "mediumturquoise" => "48d1cc",
100
+ "mediumvioletred" => "c71585",
101
+ "midnightblue" => "191970",
102
+ "mintcream" => "f5fffa",
103
+ "mistyrose" => "ffe4e1",
104
+ "moccasin" => "ffe4b5",
105
+ "navajowhite" => "ffdead",
106
+ "navy" => "000080",
107
+ "oldlace" => "fdf5e6",
108
+ "olive" => "808000",
109
+ "olivedrab" => "6b8e23",
110
+ "orange" => "ffa500",
111
+ "orangered" => "ff4500",
112
+ "orchid" => "da70d6",
113
+ "palegoldenrod" => "eee8aa",
114
+ "palegreen" => "98fb98",
115
+ "paleturquoise" => "afeeee",
116
+ "palevioletred" => "db7093",
117
+ "papayawhip" => "ffefd5",
118
+ "peachpuff" => "ffdab9",
119
+ "peru" => "cd853f",
120
+ "pink" => "ffc0cb",
121
+ "plum" => "dda0dd",
122
+ "powderblue" => "b0e0e6",
123
+ "purple" => "800080",
124
+ "red" => "ff0000",
125
+ "rosybrown" => "bc8f8f",
126
+ "royalblue" => "4169e1",
127
+ "saddlebrown" => "8b4513",
128
+ "salmon" => "fa8072",
129
+ "sandybrown" => "f4a460",
130
+ "seagreen" => "2e8b57",
131
+ "seashell" => "fff5ee",
132
+ "sienna" => "a0522d",
133
+ "silver" => "c0c0c0",
134
+ "skyblue" => "87ceeb",
135
+ "slateblue" => "6a5acd",
136
+ "slategray" => "708090",
137
+ "snow" => "fffafa",
138
+ "springgreen" => "00ff7f",
139
+ "steelblue" => "4682b4",
140
+ "tan" => "d2b48c",
141
+ "teal" => "008080",
142
+ "thistle" => "d8bfd8",
143
+ "tomato" => "ff6347",
144
+ "turquoise" => "40e0d0",
145
+ "violet" => "ee82ee",
146
+ "wheat" => "f5deb3",
147
+ "white" => "ffffff",
148
+ "whitesmoke" => "f5f5f5",
149
+ "yellow" => "ffff00",
150
+ "yellowgreen" => "9acd32",
151
+ }.freeze
152
+
153
+ def get_color(name)
154
+ WEBCOLORS.fetch(name.to_s.gsub(/[-_]/, ""))
155
+ end
156
+ end
157
+ # rubocop:enable Metrics/ModuleLength
158
+ end
159
+ end
160
+ end
161
+ # SPDX-License-Identifier: Apache-2.0
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "colors/names"
4
+ module Lab42
5
+ module TagCloud
6
+ module Colors
7
+ extend self
8
+
9
+ COLOR_RGX = /\A (..) (..) (..) \z/x
10
+ SCALES = 12
11
+ GAMMA = 2.2
12
+
13
+ def gamma_correct((scale, color))
14
+ COLOR_RGX
15
+ .match(color)
16
+ .captures
17
+ .map(&_gamma_corrected_octet(scale))
18
+ .join
19
+ end
20
+
21
+ HEX_COLOR_RGX = %r<\A (?: (?<scale> \d\d? ) / )? \# (?<hex> \h{6} ) \z>x
22
+ NAMED_COLOR_RGX = %r{\A (?: (?<scale> \d\d? ) / )? (?<color_name> \w+ ) \z}x
23
+ SCALE_RGX = /\A \d\d? \z/x
24
+
25
+ def parse_color(color)
26
+ case
27
+ when match = SCALE_RGX.match(color)
28
+ _gray_scale(match)
29
+ when match = HEX_COLOR_RGX.match(color)
30
+ _hex_color(match)
31
+ [match[1].to_i, match[2]]
32
+ when match = NAMED_COLOR_RGX.match(color)
33
+ _named_scale(match)
34
+ else
35
+ raise ArgumentError, "illegal color spec #{color}"
36
+ end
37
+ end
38
+
39
+ private
40
+
41
+ def _gamma_corrected_octet(scale)
42
+ ->(octet) do
43
+ inv_c = octet.to_i(16)
44
+ scaled = (255 - inv_c) * (((SCALES - scale).to_f/SCALES) ** (1.0/GAMMA))
45
+ value = (inv_c - scaled.round).abs.round
46
+ "%02x" % value
47
+ end
48
+ end
49
+
50
+ def _gray_scale(match)
51
+ [match.to_s.to_i, "000000"]
52
+ end
53
+
54
+ def _hex_color(match)
55
+ [(match[:scale] || 12).to_i, match[:hex]]
56
+ end
57
+
58
+ def _named_scale(match)
59
+ [(match[:scale] || 12).to_i, Names.get_color(match[:color_name])]
60
+ end
61
+ end
62
+ end
63
+ end
64
+ # SPDX-License-Identifier: Apache-2.0
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "colors"
4
+ module Lab42
5
+ module TagCloud
6
+ module Compiler
7
+ extend self
8
+ def color_value(color_spec)
9
+ parsed = Colors.parse_color(color_spec)
10
+ Colors.gamma_correct(parsed)
11
+ end
12
+
13
+ def make_css_attributes(color, size, weight = nil)
14
+ color = "##{color_value(color)}"
15
+ font_size = _normalize_size(size)
16
+ font_weight = _maybe_weight(weight)
17
+ attributes = { color:, font_size: }.merge(font_weight)
18
+ _format_css_attributes(**attributes)
19
+ end
20
+
21
+ def tag_from_object(object, tag: :span, **atts)
22
+ dsl = object[:dsl] || object.dsl
23
+ tag_text = object[:tag] || object.tag
24
+ "<#{tag} #{_make_html_attributes(dsl, **atts)}>#{tag_text}</#{tag}>"
25
+ end
26
+
27
+ private
28
+
29
+ def _format_css_attribute(k, v)
30
+ "#{k}: #{v}".gsub("_", "-")
31
+ end
32
+
33
+ def _format_css_attributes(**atts)
34
+ atts
35
+ .inject([]) { |r, (k, v)| r << _format_css_attribute(k, v) }
36
+ .join("; ") << ";"
37
+ end
38
+
39
+ def _format_html_attribute(k, v)
40
+ %(#{k}="#{v}")
41
+ end
42
+
43
+ def _format_html_attributes(atts)
44
+ atts
45
+ .inject([]) { |r, (k, v)| r << _format_html_attribute(k, v) }
46
+ .join(" ")
47
+ end
48
+
49
+ def _make_html_attributes(dsl, **atts)
50
+ _format_html_attributes(
51
+ atts
52
+ .merge(style: make_css_attributes(*dsl.split))
53
+ )
54
+ end
55
+
56
+ def _maybe_weight(weight)
57
+ return {} unless weight
58
+
59
+ { font_weight: weight }
60
+ end
61
+
62
+ DEFAULT_SIZE_RGX = / \A \d+ \z /x
63
+ def _normalize_size(size)
64
+ case size
65
+ when DEFAULT_SIZE_RGX
66
+ "#{size}pt"
67
+ else
68
+ size
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
74
+ # SPDX-License-Identifier: Apache-2.0
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lab42
4
+ module TagCloud
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
8
+ # SPDX-License-Identifier: Apache-2.0
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "tag_cloud/compiler"
4
+ module Lab42
5
+ module TagCloud
6
+ extend self
7
+ def color_value(color_spec)
8
+ Compiler.color_value(color_spec.to_s)
9
+ end
10
+
11
+ def tag_from_object(object, **atts)
12
+ Compiler.tag_from_object(object, **atts)
13
+ end
14
+
15
+ def to_style(dsl_string)
16
+ Compiler.make_css_attributes(*dsl_string.split)
17
+ end
18
+ end
19
+ end
20
+ # SPDX-License-Identifier: Apache-2.0
metadata ADDED
@@ -0,0 +1,52 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lab42_tag_cloud
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Robert Dober
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-02-03 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: |
14
+ Creating Tag Clouds with gamma correct color values and styles from a simple DSL.
15
+
16
+ This is a port of Elixir's tag_cloud https://github.com/RobertDober/tag_cloud to Ruby
17
+ email: robert.dober@gmail.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - LICENSE
23
+ - README.md
24
+ - lib/lab42/tag_cloud.rb
25
+ - lib/lab42/tag_cloud/colors.rb
26
+ - lib/lab42/tag_cloud/colors/names.rb
27
+ - lib/lab42/tag_cloud/compiler.rb
28
+ - lib/lab42/tag_cloud/version.rb
29
+ homepage: https://github.com/robertdober/lab42_tag_cloud
30
+ licenses:
31
+ - Apache-2.0
32
+ metadata: {}
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: 3.1.0
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ requirements: []
48
+ rubygems_version: 3.3.3
49
+ signing_key:
50
+ specification_version: 4
51
+ summary: Simple DSL for nicely styled, gamma corrected Tag Clouds
52
+ test_files: []