color_hash 0.1.0 → 1.0.1
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 +4 -4
- data/CHANGELOG.md +24 -0
- data/README.md +7 -5
- data/lib/color_hash/generator.rb +24 -39
- data/lib/color_hash/version.rb +1 -1
- metadata +6 -14
- data/.rspec +0 -3
- data/.rubocop.yml +0 -13
- data/Gemfile +0 -12
- data/Gemfile.lock +0 -57
- data/Rakefile +0 -12
- data/color_hash.gemspec +0 -32
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 5cf5a999840fff027b954343a856b43276c6d2f021100ddc1811b63b03ac1636
|
|
4
|
+
data.tar.gz: c83fb8b5573feb4508c8ea64d4829f3db1ddc9390d7e6bfa82fda5a7f5a97df4
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 053a0d6b28512402726a965fc1270e195b6f132cd0f67b59521f2cfa2cc74c93905d5df0173f567abc166a25ed259ca93d8ecda2c48e068507c461f066da1b82
|
|
7
|
+
data.tar.gz: 462aea99f9e3d14e921d20fed8ed7c84c3672599fb83efb4bf588f8ce7018a6d11fc6954527260d2ba6043023ea2b47caea5a2702076795bfdde12e46883b2fb
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,29 @@
|
|
|
1
1
|
## [Unreleased]
|
|
2
2
|
|
|
3
|
+
## [1.0.1] - 2026-11-04
|
|
4
|
+
|
|
5
|
+
- Fix `NameError` when using the `:sha256` hash option
|
|
6
|
+
- Replace RSpec with Minitest
|
|
7
|
+
- Ship only `lib`, `sig`, the README and the CHANGELOG in the published gem
|
|
8
|
+
- Replace `make deploy` with `make release`, which runs the tests and security scans first
|
|
9
|
+
- Add `make security` (Trivy and bundle-audit) and `make test-rubies` targets
|
|
10
|
+
- Add `.ruby-version` and `trivy.yaml`
|
|
11
|
+
- Update dependencies
|
|
12
|
+
|
|
13
|
+
## [1.0.0] - 2026-07-15
|
|
14
|
+
|
|
15
|
+
- Update dependencies and bundler
|
|
16
|
+
- Switch to kuyio-rubocop for shared lint configuration
|
|
17
|
+
- Bump required Ruby version to >= 3.4.0
|
|
18
|
+
- Add rubygems_mfa_required metadata to gemspec
|
|
19
|
+
- Fix rubocop offenses (ASCII comments, ambiguous precedence, stale cop directives)
|
|
20
|
+
- Add Makefile with build, test, clean, and deploy targets
|
|
21
|
+
- Optimize Generator: resolve hash function once via Method#call instead of send
|
|
22
|
+
- Simplify rgb2hex using format string
|
|
23
|
+
- Avoid unnecessary array allocation in hash_bkdr
|
|
24
|
+
- Extract repeated float constants (ONE_THIRD, ONE_SIXTH, TWO_THIRDS)
|
|
25
|
+
- Clean up dead code and misleading float modulo
|
|
26
|
+
|
|
3
27
|
## [0.1.0] - 2022-12-15
|
|
4
28
|
|
|
5
29
|
- Initial release
|
data/README.md
CHANGED
|
@@ -34,9 +34,9 @@ rgb = color_hash.rgb("Hello World!")
|
|
|
34
34
|
The resulting values are going to be:
|
|
35
35
|
|
|
36
36
|
```ruby
|
|
37
|
-
hex = "#
|
|
38
|
-
hsl = [
|
|
39
|
-
rgb = [
|
|
37
|
+
hex = "#79d2b3"
|
|
38
|
+
hsl = [159, 0.5, 0.65]
|
|
39
|
+
rgb = [121, 210, 179]
|
|
40
40
|
```
|
|
41
41
|
|
|
42
42
|
You can customize the desired `hue` range(s), `saturation` and `lightness` values by passing the corresponding arguments to the constructor. For example, to generate colors in the blue spectrum:
|
|
@@ -54,9 +54,11 @@ color = "#79acd2"
|
|
|
54
54
|
|
|
55
55
|
## Development
|
|
56
56
|
|
|
57
|
-
After checking out the repo, run `bin/setup` to install dependencies. Then, run `
|
|
57
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `make test` to run the linter and the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
58
58
|
|
|
59
|
-
|
|
59
|
+
Run `make security` to scan for vulnerabilities. It requires [Trivy](https://trivy.dev) to be installed, and network access to update the advisory databases.
|
|
60
|
+
|
|
61
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, run `make test security`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
60
62
|
|
|
61
63
|
## Contributing
|
|
62
64
|
|
data/lib/color_hash/generator.rb
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "digest"
|
|
4
|
+
|
|
3
5
|
module ColorHash
|
|
4
6
|
# A ColorHash::Generator can be used to calculate the color value for a given String.
|
|
5
7
|
# You can specify the desired hue, lightness and saturation (HSL) ranges
|
|
@@ -36,65 +38,59 @@ module ColorHash
|
|
|
36
38
|
@lightness = lightness
|
|
37
39
|
@saturation = saturation
|
|
38
40
|
@hue = hue
|
|
39
|
-
@
|
|
41
|
+
@hash_method = method(:"hash_#{hash}")
|
|
40
42
|
end
|
|
41
43
|
|
|
42
|
-
# Magic
|
|
43
44
|
HUE_RESOLUTION = 727
|
|
45
|
+
ONE_THIRD = 1.0 / 3.0
|
|
46
|
+
ONE_SIXTH = 1.0 / 6.0
|
|
47
|
+
TWO_THIRDS = 2.0 / 3.0
|
|
44
48
|
|
|
45
|
-
# rubocop:disable Metrics/AbcSize
|
|
46
49
|
# Calculates an HSL color value returned as an Array of the form [H, S, L]
|
|
47
50
|
# from a given string.
|
|
48
51
|
#
|
|
49
|
-
# H Hue
|
|
50
|
-
# S Saturation
|
|
51
|
-
# L Lightness
|
|
52
|
+
# H Hue in [0, 360)
|
|
53
|
+
# S Saturation in [0, 1]
|
|
54
|
+
# L Lightness in [0, 1]
|
|
52
55
|
def hsl(str)
|
|
53
|
-
hash =
|
|
56
|
+
hash = @hash_method.call(str)
|
|
54
57
|
|
|
55
58
|
hr = @hue[hash % @hue.count]
|
|
56
|
-
h = ((hash / @hue.count) % HUE_RESOLUTION) * (hr[:max] - hr[:min]) / HUE_RESOLUTION + hr[:min]
|
|
57
|
-
|
|
58
|
-
# h = hash % 359
|
|
59
|
+
h = (((hash / @hue.count) % HUE_RESOLUTION) * (hr[:max] - hr[:min]) / HUE_RESOLUTION) + hr[:min]
|
|
59
60
|
|
|
60
61
|
hash = (hash / 360.0).ceil
|
|
61
|
-
s = @saturation[hash % @saturation.count
|
|
62
|
+
s = @saturation[hash % @saturation.count]
|
|
62
63
|
|
|
63
64
|
hash = (hash / @saturation.count.to_f).ceil
|
|
64
65
|
l = @lightness[hash % @lightness.count]
|
|
65
66
|
|
|
66
67
|
[h, s, l]
|
|
67
68
|
end
|
|
68
|
-
# rubocop:enable Metrics/AbcSize
|
|
69
69
|
|
|
70
70
|
# Calculates an RGB color value returned as an Array of the form [R, G, B]
|
|
71
71
|
# from a given string.
|
|
72
72
|
#
|
|
73
|
-
# R, G, B
|
|
73
|
+
# R, G, B in [0, 255]
|
|
74
74
|
def rgb(str)
|
|
75
75
|
hsl2rgb(*hsl(str))
|
|
76
76
|
end
|
|
77
77
|
|
|
78
|
-
# rubocop:disable Metrics/AbcSize
|
|
79
|
-
# rubocop:disable Metrics/CyclomaticComplexity
|
|
80
|
-
# rubocop:disable Metrics/PerceivedComplexity
|
|
81
|
-
# rubocop:disable Metrics/MethodLength
|
|
82
78
|
# Converts a color specified by its hue, saturation and lightness to an RGB value
|
|
83
79
|
def hsl2rgb(hue, saturation, lightness)
|
|
84
80
|
hue /= 360.0
|
|
85
81
|
q_factor = lightness < 0.5 ? lightness * (1.0 + saturation) : (lightness + saturation) - (lightness * saturation)
|
|
86
|
-
p_factor = 2.0 * lightness - q_factor
|
|
82
|
+
p_factor = (2.0 * lightness) - q_factor
|
|
87
83
|
|
|
88
|
-
[hue +
|
|
89
|
-
color += 1.0 if
|
|
90
|
-
color -= 1.0 if color > 1
|
|
84
|
+
[hue + ONE_THIRD, hue, hue - ONE_THIRD].map do |color|
|
|
85
|
+
color += 1.0 if color < 0.0
|
|
86
|
+
color -= 1.0 if color > 1.0
|
|
91
87
|
|
|
92
|
-
color = if color <
|
|
93
|
-
p_factor + (q_factor - p_factor) * 6.0 * color
|
|
88
|
+
color = if color < ONE_SIXTH
|
|
89
|
+
p_factor + ((q_factor - p_factor) * 6.0 * color)
|
|
94
90
|
elsif color < 0.5
|
|
95
91
|
q_factor
|
|
96
|
-
elsif color <
|
|
97
|
-
p_factor + (q_factor - p_factor) * 6.0 * (
|
|
92
|
+
elsif color < TWO_THIRDS
|
|
93
|
+
p_factor + ((q_factor - p_factor) * 6.0 * (TWO_THIRDS - color))
|
|
98
94
|
else
|
|
99
95
|
p_factor
|
|
100
96
|
end
|
|
@@ -102,10 +98,6 @@ module ColorHash
|
|
|
102
98
|
(color * 255.0).round
|
|
103
99
|
end
|
|
104
100
|
end
|
|
105
|
-
# rubocop:enable Metrics/AbcSize
|
|
106
|
-
# rubocop:enable Metrics/CyclomaticComplexity
|
|
107
|
-
# rubocop:enable Metrics/PerceivedComplexity
|
|
108
|
-
# rubocop:enable Metrics/MethodLength
|
|
109
101
|
|
|
110
102
|
# Calculates an Hexadecimal color value from a given string.
|
|
111
103
|
# Returns a 6 digits hex string starting with '#'.
|
|
@@ -113,15 +105,8 @@ module ColorHash
|
|
|
113
105
|
rgb2hex(rgb(str))
|
|
114
106
|
end
|
|
115
107
|
|
|
116
|
-
# Convers a color specified by its RGB value to a hexadecimal color value.
|
|
117
108
|
def rgb2hex(rgb)
|
|
118
|
-
|
|
119
|
-
rgb.each do |c|
|
|
120
|
-
hex += "0" if c < 16
|
|
121
|
-
hex += c.to_s(16)
|
|
122
|
-
end
|
|
123
|
-
|
|
124
|
-
hex
|
|
109
|
+
format("#%02x%02x%02x", *rgb)
|
|
125
110
|
end
|
|
126
111
|
|
|
127
112
|
# Calculate a SHA2-256 based hash of the given string
|
|
@@ -142,9 +127,9 @@ module ColorHash
|
|
|
142
127
|
str += "x"
|
|
143
128
|
# Number.MAX_SAFE_INTEGER equals 9007199254740991
|
|
144
129
|
max_safe = 9_007_199_254_740_991 / seed2
|
|
145
|
-
str.each_codepoint
|
|
130
|
+
str.each_codepoint do |c|
|
|
146
131
|
hash = (hash / seed2).floor if hash > max_safe
|
|
147
|
-
hash = hash * seed + c
|
|
132
|
+
hash = (hash * seed) + c
|
|
148
133
|
end
|
|
149
134
|
|
|
150
135
|
hash
|
data/lib/color_hash/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: color_hash
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1
|
|
4
|
+
version: 1.0.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Nicolas Bettenburg
|
|
8
|
-
autorequire:
|
|
9
8
|
bindir: exe
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
12
11
|
dependencies: []
|
|
13
12
|
description: Generate a color based on the given string (in the HSL color space).
|
|
14
13
|
email:
|
|
@@ -17,14 +16,8 @@ executables: []
|
|
|
17
16
|
extensions: []
|
|
18
17
|
extra_rdoc_files: []
|
|
19
18
|
files:
|
|
20
|
-
- ".rspec"
|
|
21
|
-
- ".rubocop.yml"
|
|
22
19
|
- CHANGELOG.md
|
|
23
|
-
- Gemfile
|
|
24
|
-
- Gemfile.lock
|
|
25
20
|
- README.md
|
|
26
|
-
- Rakefile
|
|
27
|
-
- color_hash.gemspec
|
|
28
21
|
- lib/color_hash.rb
|
|
29
22
|
- lib/color_hash/generator.rb
|
|
30
23
|
- lib/color_hash/version.rb
|
|
@@ -35,8 +28,8 @@ metadata:
|
|
|
35
28
|
allowed_push_host: https://rubygems.org
|
|
36
29
|
homepage_uri: https://github.com/kuyio/color_hash
|
|
37
30
|
source_code_uri: https://github.com/kuyio/color_hash
|
|
38
|
-
changelog_uri: https://github.com/kuyio/color_hash/CHANGELOG.md
|
|
39
|
-
|
|
31
|
+
changelog_uri: https://github.com/kuyio/color_hash/blob/main/CHANGELOG.md
|
|
32
|
+
rubygems_mfa_required: 'true'
|
|
40
33
|
rdoc_options: []
|
|
41
34
|
require_paths:
|
|
42
35
|
- lib
|
|
@@ -44,15 +37,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
44
37
|
requirements:
|
|
45
38
|
- - ">="
|
|
46
39
|
- !ruby/object:Gem::Version
|
|
47
|
-
version:
|
|
40
|
+
version: '3.4'
|
|
48
41
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
49
42
|
requirements:
|
|
50
43
|
- - ">="
|
|
51
44
|
- !ruby/object:Gem::Version
|
|
52
45
|
version: '0'
|
|
53
46
|
requirements: []
|
|
54
|
-
rubygems_version:
|
|
55
|
-
signing_key:
|
|
47
|
+
rubygems_version: 4.0.23
|
|
56
48
|
specification_version: 4
|
|
57
49
|
summary: Generate a color based on the given string.
|
|
58
50
|
test_files: []
|
data/.rspec
DELETED
data/.rubocop.yml
DELETED
data/Gemfile
DELETED
data/Gemfile.lock
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
PATH
|
|
2
|
-
remote: .
|
|
3
|
-
specs:
|
|
4
|
-
color_hash (0.1.0)
|
|
5
|
-
|
|
6
|
-
GEM
|
|
7
|
-
remote: https://rubygems.org/
|
|
8
|
-
specs:
|
|
9
|
-
ast (2.4.2)
|
|
10
|
-
diff-lcs (1.5.0)
|
|
11
|
-
json (2.6.2)
|
|
12
|
-
parallel (1.22.1)
|
|
13
|
-
parser (3.1.2.1)
|
|
14
|
-
ast (~> 2.4.1)
|
|
15
|
-
rainbow (3.1.1)
|
|
16
|
-
rake (13.0.6)
|
|
17
|
-
regexp_parser (2.6.1)
|
|
18
|
-
rexml (3.2.5)
|
|
19
|
-
rspec (3.11.0)
|
|
20
|
-
rspec-core (~> 3.11.0)
|
|
21
|
-
rspec-expectations (~> 3.11.0)
|
|
22
|
-
rspec-mocks (~> 3.11.0)
|
|
23
|
-
rspec-core (3.11.0)
|
|
24
|
-
rspec-support (~> 3.11.0)
|
|
25
|
-
rspec-expectations (3.11.1)
|
|
26
|
-
diff-lcs (>= 1.2.0, < 2.0)
|
|
27
|
-
rspec-support (~> 3.11.0)
|
|
28
|
-
rspec-mocks (3.11.1)
|
|
29
|
-
diff-lcs (>= 1.2.0, < 2.0)
|
|
30
|
-
rspec-support (~> 3.11.0)
|
|
31
|
-
rspec-support (3.11.1)
|
|
32
|
-
rubocop (1.36.0)
|
|
33
|
-
json (~> 2.3)
|
|
34
|
-
parallel (~> 1.10)
|
|
35
|
-
parser (>= 3.1.2.1)
|
|
36
|
-
rainbow (>= 2.2.2, < 4.0)
|
|
37
|
-
regexp_parser (>= 1.8, < 3.0)
|
|
38
|
-
rexml (>= 3.2.5, < 4.0)
|
|
39
|
-
rubocop-ast (>= 1.20.1, < 2.0)
|
|
40
|
-
ruby-progressbar (~> 1.7)
|
|
41
|
-
unicode-display_width (>= 1.4.0, < 3.0)
|
|
42
|
-
rubocop-ast (1.21.0)
|
|
43
|
-
parser (>= 3.1.1.0)
|
|
44
|
-
ruby-progressbar (1.11.0)
|
|
45
|
-
unicode-display_width (2.3.0)
|
|
46
|
-
|
|
47
|
-
PLATFORMS
|
|
48
|
-
arm64-darwin-21
|
|
49
|
-
|
|
50
|
-
DEPENDENCIES
|
|
51
|
-
color_hash!
|
|
52
|
-
rake (~> 13.0)
|
|
53
|
-
rspec (~> 3.0)
|
|
54
|
-
rubocop (~> 1.21)
|
|
55
|
-
|
|
56
|
-
BUNDLED WITH
|
|
57
|
-
2.3.17
|
data/Rakefile
DELETED
data/color_hash.gemspec
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require_relative "lib/color_hash/version"
|
|
4
|
-
|
|
5
|
-
Gem::Specification.new do |spec|
|
|
6
|
-
spec.name = "color_hash"
|
|
7
|
-
spec.version = ColorHash::VERSION
|
|
8
|
-
spec.authors = ["Nicolas Bettenburg"]
|
|
9
|
-
spec.email = ["nicbet@kuy.io"]
|
|
10
|
-
|
|
11
|
-
spec.summary = "Generate a color based on the given string."
|
|
12
|
-
spec.description = "Generate a color based on the given string (in the HSL color space)."
|
|
13
|
-
spec.homepage = "https://github.com/kuyio/color_hash"
|
|
14
|
-
spec.required_ruby_version = ">= 2.6.0"
|
|
15
|
-
|
|
16
|
-
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
|
17
|
-
|
|
18
|
-
spec.metadata["homepage_uri"] = spec.homepage
|
|
19
|
-
spec.metadata["source_code_uri"] = "https://github.com/kuyio/color_hash"
|
|
20
|
-
spec.metadata["changelog_uri"] = "https://github.com/kuyio/color_hash/CHANGELOG.md"
|
|
21
|
-
|
|
22
|
-
# Specify which files should be added to the gem when it is released.
|
|
23
|
-
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
|
24
|
-
spec.files = Dir.chdir(__dir__) do
|
|
25
|
-
`git ls-files -z`.split("\x0").reject do |f|
|
|
26
|
-
(f == __FILE__) || f.match(%r{\A(?:(?:doc|bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
|
|
27
|
-
end
|
|
28
|
-
end
|
|
29
|
-
spec.bindir = "exe"
|
|
30
|
-
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
|
31
|
-
spec.require_paths = ["lib"]
|
|
32
|
-
end
|