decolmor 1.1.0 → 1.1.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 +7 -1
- data/NEWS.md +5 -0
- data/README.md +23 -13
- data/decolmor.gemspec +3 -3
- data/lib/decolmor/main.rb +245 -233
- data/lib/decolmor/version.rb +1 -1
- data/lib/decolmor.rb +0 -8
- data/spec/decolmor_spec.rb +62 -51
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 73c5b6a307982579fd26496c5398ce26bfe4ca900ce1ea64500249d0cfe2ca60
|
4
|
+
data.tar.gz: bb00a9c25c480ad47152e2f463cc216c317f3706fbe1a3f313a7aa35b0825577
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f28a363a685048fc28d39cca59c63fafde302b17b1ae8c1c3dbe615842a54bbd4c8228ca59c21afa0a0b14e8ee694c8b50d307bd66aff0780214e7363aac4a30
|
7
|
+
data.tar.gz: c8651a6e4151162cea6c55d6f61f9c45ee171f0f6bd0384ba059dede341562ea1086c2e70ce953fef9c2ef2c67c7f19e8a7bb6e8d1fecd9a1bf01275e4e8ed1b
|
data/CHANGELOG.md
CHANGED
@@ -9,7 +9,13 @@
|
|
9
9
|
* ::hex_to_rgb
|
10
10
|
* change default rounding 5 => 3 for Alpha channel
|
11
11
|
*reason: 3 digits is enough for a lossless conversion `0..255` -> `0..1` -> `0..255`*
|
12
|
-
* for the Alpha channel you can
|
12
|
+
* for the Alpha channel now you can set rounding as the second argument:
|
13
13
|
`Decolmor::hex_to_rgb(hex, 2)`
|
14
14
|
* support short version of HEX
|
15
15
|
e.g: `#CF3`, `0F9`, `#0F9F`
|
16
|
+
|
17
|
+
## 1.1.1 (September 16, 2021)
|
18
|
+
|
19
|
+
* Now you can `include` the module into your class
|
20
|
+
* gem methods will be available as class methods
|
21
|
+
* Fixed default branch in .gemspec metadata paths
|
data/NEWS.md
CHANGED
data/README.md
CHANGED
@@ -32,30 +32,40 @@ gem install decolmor
|
|
32
32
|
require 'decolmor'
|
33
33
|
|
34
34
|
rgb = [29, 128, 86]
|
35
|
-
Decolmor
|
35
|
+
Decolmor.rgb_to_hsb(rgb)
|
36
36
|
=> [154.5, 77.3, 50.2]
|
37
37
|
```
|
38
|
+
or `include` into class:
|
39
|
+
```ruby
|
40
|
+
class SomeClass
|
41
|
+
include Decolmor
|
42
|
+
end
|
43
|
+
SomeClass.rgb_to_hsb(rgb)
|
44
|
+
=> [154.5, 77.3, 50.2]
|
45
|
+
```
|
46
|
+
Gem methods will be available as class methods.
|
47
|
+
|
38
48
|
## Rounding for HSL/HSV/HSB/CMYK
|
39
49
|
By default, rounding 1 is used to convert to HSL/HSV/HSB/CMYK.
|
40
50
|
This is enough to loselessly convert RGB -> HSL/HSV/HSB/CMYK -> RGB:
|
41
51
|
```ruby
|
42
52
|
rgb = [224, 23, 131]
|
43
|
-
hsl = Decolmor
|
44
|
-
hsv = Decolmor
|
45
|
-
Decolmor
|
46
|
-
Decolmor
|
53
|
+
hsl = Decolmor.rgb_to_hsl(rgb) # => [327.8, 81.4, 48.4]
|
54
|
+
hsv = Decolmor.rgb_to_hsv(rgb) # => [327.8, 89.7, 87.8]
|
55
|
+
Decolmor.hsv_to_rgb(hsv) # => [224, 23, 131]
|
56
|
+
Decolmor.hsl_to_rgb(hsl) # => [224, 23, 131]
|
47
57
|
```
|
48
58
|
If you convert between HSL <==> HSV (HSB) with a rounding of 2, you can get more accurate results.
|
49
59
|
This can also be useful if you use HSL/HSB for intermediate changes and then go back to RGB.
|
50
60
|
You can change rounding globally:
|
51
61
|
```ruby
|
52
|
-
Decolmor
|
53
|
-
Decolmor
|
54
|
-
Decolmor
|
62
|
+
Decolmor.hsx_round = 2
|
63
|
+
Decolmor.rgb_to_hsl(rgb) # => [154.55, 63.06, 30.78]
|
64
|
+
Decolmor.hsx_round # => 2
|
55
65
|
```
|
56
66
|
You can also specify rounding as a second argument when calling the method:
|
57
67
|
```ruby
|
58
|
-
Decolmor
|
68
|
+
Decolmor.rgb_to_hsl(rgb, 3) # => [154.545, 63.057, 30.784]
|
59
69
|
```
|
60
70
|
In this case, the global rounding will be ignored.
|
61
71
|
If you need to get integers, use 0.
|
@@ -69,12 +79,12 @@ If you need to get integers, use 0.
|
|
69
79
|
When converting from HEX to RGBA Alpha channel is converted to a value from the range `0..1` with rounding 3:
|
70
80
|
- 3 digits is enough for a lossless conversion `0..255` -> `0..1` -> `0..255`
|
71
81
|
```ruby
|
72
|
-
Decolmor
|
82
|
+
Decolmor.hex_to_rgb('#19988BB8') # => [25, 152, 139, 0.722]
|
73
83
|
```
|
74
84
|
Consequently, when converting to HEX from RGBA, Alpha from the range `0..1` is assumed.
|
75
85
|
You can also set rounding for Alpha channel as a second argument:
|
76
86
|
```ruby
|
77
|
-
Decolmor
|
87
|
+
Decolmor.hex_to_rgb('#19988BB8', 2) # => [25, 152, 139, 0.72]
|
78
88
|
```
|
79
89
|
This only works for converting HEX to RGBA.
|
80
90
|
In other cases (conversions between RGB/HSL/HSV/HSB/CMYK) Alpha channel remains unchanged.
|
@@ -84,8 +94,8 @@ HSB is an alternative name for HSV, it is the same thing.
|
|
84
94
|
However, for convenience, aliasing methods are made for HSB from HSV.
|
85
95
|
```ruby
|
86
96
|
rgb = [255, 109, 55]
|
87
|
-
Decolmor
|
88
|
-
Decolmor
|
97
|
+
Decolmor.rgb_to_hsv(rgb) # => [16.2, 78.4, 100.0]
|
98
|
+
Decolmor.rgb_to_hsb(rgb) # => [16.2, 78.4, 100.0]
|
89
99
|
```
|
90
100
|
## HSL/HSV/HSB to RGB conversion
|
91
101
|
HSL/HSV/HSB to RGB conversion has two implementations, the gem includes both:
|
data/decolmor.gemspec
CHANGED
@@ -24,9 +24,9 @@ Gem::Specification.new do |spec|
|
|
24
24
|
if spec.respond_to?(:metadata)
|
25
25
|
spec.metadata = {
|
26
26
|
"homepage_uri" => spec.homepage.to_s,
|
27
|
-
"news_uri" => "#{spec.homepage}/blob/
|
28
|
-
"changelog_uri" => "#{spec.homepage}/blob/
|
29
|
-
"documentation_uri" => "#{spec.homepage}/blob/
|
27
|
+
"news_uri" => "#{spec.homepage}/blob/main/NEWS.md",
|
28
|
+
"changelog_uri" => "#{spec.homepage}/blob/main/CHANGELOG.md",
|
29
|
+
"documentation_uri" => "#{spec.homepage}/blob/main/README.md",
|
30
30
|
"bug_tracker_uri" => "#{spec.homepage}/issues",
|
31
31
|
"source_code_uri" => spec.homepage.to_s
|
32
32
|
}
|
data/lib/decolmor/main.rb
CHANGED
@@ -1,282 +1,294 @@
|
|
1
1
|
module Decolmor
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
def self.hex_to_rgb(hex, alpha_round = 3)
|
6
|
-
hex = hex.gsub('#','')
|
7
|
-
hex = if [3, 4].include? hex.length
|
8
|
-
hex.chars.map{ |char| char * 2 }
|
9
|
-
else
|
10
|
-
hex.scan(/../)
|
11
|
-
end
|
12
|
-
rgb = hex.map(&:hex)
|
13
|
-
rgb.size == 4 ? rgb + [(rgb.delete_at(3) / 255.to_f).round(alpha_round)] : rgb
|
3
|
+
def self.included(base)
|
4
|
+
base.extend ClassMethods
|
14
5
|
end
|
15
6
|
|
16
|
-
|
17
|
-
template = rgb.size == 3 ? "#%02X%02X%02X" : "#%02X%02X%02X%02X"
|
18
|
-
rgb = rgb[0..2] + [(rgb[3] * 255).round] if rgb.size == 4
|
19
|
-
template % rgb
|
20
|
-
end
|
21
|
-
|
22
|
-
#=======================================================================
|
7
|
+
module ClassMethods
|
23
8
|
|
24
|
-
|
25
|
-
def self.new_rgb(red: nil, green: nil, blue: nil, alpha: nil)
|
26
|
-
range = 0..255
|
27
|
-
rgb = [red, green, blue].map { |channel| channel || rand(range) }
|
28
|
-
alpha.nil? ? rgb : rgb + [alpha]
|
29
|
-
end
|
9
|
+
attr_writer :hsx_round
|
30
10
|
|
31
|
-
|
11
|
+
def hsx_round
|
12
|
+
@hsx_round ||= HSX_ROUND
|
13
|
+
end
|
32
14
|
|
33
|
-
|
34
|
-
|
35
|
-
|
15
|
+
#========= HEX <==> RGB(A) =============================================
|
16
|
+
|
17
|
+
def hex_to_rgb(hex, alpha_round = 3)
|
18
|
+
hex = hex.gsub('#','')
|
19
|
+
hex = if [3, 4].include? hex.length
|
20
|
+
hex.chars.map{ |char| char * 2 }
|
21
|
+
else
|
22
|
+
hex.scan(/../)
|
23
|
+
end
|
24
|
+
rgb = hex.map(&:hex)
|
25
|
+
rgb.size == 4 ? rgb + [(rgb.delete_at(3) / 255.to_f).round(alpha_round)] : rgb
|
26
|
+
end
|
36
27
|
|
37
|
-
|
38
|
-
|
28
|
+
def rgb_to_hex(rgb)
|
29
|
+
template = rgb.size == 3 ? "#%02X%02X%02X" : "#%02X%02X%02X%02X"
|
30
|
+
rgb = rgb[0..2] + [(rgb[3] * 255).round] if rgb.size == 4
|
31
|
+
template % rgb
|
32
|
+
end
|
39
33
|
|
40
|
-
|
41
|
-
hue = get_hue(red, green, blue)
|
42
|
-
lightness = (cmax + cmin) / 2
|
43
|
-
saturation = chroma == 0 ? 0 : chroma / (1 - (2 * lightness - 1).abs)
|
34
|
+
#=======================================================================
|
44
35
|
|
45
|
-
#
|
46
|
-
|
47
|
-
|
36
|
+
# simple generator RGB, you can set any channel(s)
|
37
|
+
def new_rgb(red: nil, green: nil, blue: nil, alpha: nil)
|
38
|
+
range = 0..255
|
39
|
+
rgb = [red, green, blue].map { |channel| channel || rand(range) }
|
40
|
+
alpha.nil? ? rgb : rgb + [alpha]
|
41
|
+
end
|
48
42
|
|
49
|
-
|
50
|
-
hsl = [hue, saturation, lightness].map { |x| x.round(rounding) }
|
51
|
-
alpha.nil? ? hsl : hsl + [alpha * 255]
|
52
|
-
end
|
43
|
+
#========= RGB(A) to HSL/HSV/HSB =======================================
|
53
44
|
|
54
|
-
|
55
|
-
|
56
|
-
|
45
|
+
def rgb_to_hsl(rgb_arr, rounding = hsx_round)
|
46
|
+
# scaling RGB values into range 0..1
|
47
|
+
red, green, blue, alpha = rgb_arr.map { |color| color / 255.to_f }
|
57
48
|
|
58
|
-
|
59
|
-
|
49
|
+
# calculation intermediate values
|
50
|
+
cmin, cmax, chroma = get_min_max_chroma(red, green, blue)
|
60
51
|
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
52
|
+
# calculation HSL values
|
53
|
+
hue = get_hue(red, green, blue)
|
54
|
+
lightness = (cmax + cmin) / 2
|
55
|
+
saturation = chroma == 0 ? 0 : chroma / (1 - (2 * lightness - 1).abs)
|
65
56
|
|
66
|
-
|
67
|
-
|
68
|
-
|
57
|
+
# scaling values to fill 0..100 interval
|
58
|
+
saturation *= 100
|
59
|
+
lightness *= 100
|
69
60
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
61
|
+
# rounding, drop Alpha if not set (nil)
|
62
|
+
hsl = [hue, saturation, lightness].map { |x| x.round(rounding) }
|
63
|
+
alpha.nil? ? hsl : hsl + [alpha * 255]
|
64
|
+
end
|
74
65
|
|
66
|
+
def rgb_to_hsv(rgb_arr, rounding = hsx_round)
|
67
|
+
# scaling RGB values into range 0..1
|
68
|
+
red, green, blue, alpha = rgb_arr.map { |color| color / 255.to_f }
|
75
69
|
|
76
|
-
|
70
|
+
# calculation intermediate values
|
71
|
+
_cmin, cmax, chroma = get_min_max_chroma(red, green, blue)
|
77
72
|
|
78
|
-
|
73
|
+
# calculation HSV values
|
74
|
+
hue = get_hue(red, green, blue)
|
75
|
+
saturation = chroma == 0 ? 0 : chroma / cmax
|
76
|
+
value = cmax
|
79
77
|
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
saturation /= 100
|
84
|
-
lightness /= 100
|
78
|
+
# scaling values into range 0..100
|
79
|
+
saturation *= 100
|
80
|
+
value *= 100
|
85
81
|
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
k = (n + hue / 30) % 12
|
90
|
-
lightness - a * [-1, [k - 3, 9 - k, 1].min].max
|
82
|
+
# rounding
|
83
|
+
hsv = [hue, saturation, value].map { |x| x.round(rounding) }
|
84
|
+
alpha.nil? ? hsv : hsv + [alpha * 255]
|
91
85
|
end
|
92
86
|
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
alpha.nil? ? rgb : rgb + [alpha]
|
97
|
-
end
|
87
|
+
alias_method :rgb_to_hsb, :rgb_to_hsv
|
88
|
+
|
89
|
+
#========= HSL/HSV/HSB to RGB(A) =======================================
|
98
90
|
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
91
|
+
def hsl_to_rgb(hsl_arr)
|
92
|
+
hue, saturation, lightness, alpha = hsl_arr.map(&:to_f)
|
93
|
+
# scaling values into range 0..1
|
94
|
+
saturation /= 100
|
95
|
+
lightness /= 100
|
104
96
|
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
97
|
+
# calculation intermediate values
|
98
|
+
a = saturation * [lightness, 1 - lightness].min
|
99
|
+
converter = proc do |n|
|
100
|
+
k = (n + hue / 30) % 12
|
101
|
+
lightness - a * [-1, [k - 3, 9 - k, 1].min].max
|
102
|
+
end
|
103
|
+
|
104
|
+
# calculation rgb & scaling into range 0..255
|
105
|
+
rgb = [0, 8, 4]
|
106
|
+
rgb.map! { |channel| (converter.call(channel) * 255).round }
|
107
|
+
alpha.nil? ? rgb : rgb + [alpha]
|
109
108
|
end
|
110
109
|
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
110
|
+
def hsv_to_rgb(hsv_arr)
|
111
|
+
hue, saturation, value, alpha = hsv_arr.map(&:to_f)
|
112
|
+
# scaling values into range 0..1
|
113
|
+
saturation /= 100
|
114
|
+
value /= 100
|
115
|
+
|
116
|
+
# calculation intermediate values
|
117
|
+
converter = proc do |n|
|
118
|
+
k = (n + hue / 60) % 6
|
119
|
+
value - value * saturation * [0, [k, 4 - k, 1].min].max
|
120
|
+
end
|
121
|
+
|
122
|
+
# calculation rgb & scaling into range 0..255
|
123
|
+
rgb = [5, 3, 1]
|
124
|
+
rgb.map! { |channel| (converter.call(channel) * 255).round }
|
125
|
+
alpha.nil? ? rgb : rgb + [alpha]
|
126
|
+
end
|
116
127
|
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
128
|
+
alias_method :hsb_to_rgb, :hsv_to_rgb
|
129
|
+
|
130
|
+
#========= Alternative implementation HSL/HSV/HSB to RGB(A) ============
|
131
|
+
|
132
|
+
def hsl_to_rgb_alt(hsl_arr)
|
133
|
+
hue, saturation, lightness, alpha = hsl_arr.map(&:to_f)
|
134
|
+
# scaling values into range 0..1
|
135
|
+
saturation /= 100
|
136
|
+
lightness /= 100
|
137
|
+
|
138
|
+
# calculation chroma & intermediate values
|
139
|
+
chroma = (1 - (2 * lightness - 1).abs) * saturation
|
140
|
+
hue /= 60
|
141
|
+
x = chroma * (1 - (hue % 2 - 1).abs)
|
142
|
+
|
143
|
+
# possible RGB points
|
144
|
+
points = [[chroma, x, 0],
|
145
|
+
[x, chroma, 0],
|
146
|
+
[0, chroma, x],
|
147
|
+
[0, x, chroma],
|
148
|
+
[x, 0, chroma],
|
149
|
+
[chroma, 0, x]]
|
150
|
+
# point selection based on entering HUE input in range
|
151
|
+
point = points.each_with_index.detect { |rgb_, n| (n..n + 1).include? hue }&.first
|
152
|
+
# if point == nil (hue undefined)
|
153
|
+
rgb = point || [0, 0, 0]
|
154
|
+
|
155
|
+
# calculation rgb & scaling into range 0..255
|
156
|
+
m = lightness - chroma / 2
|
157
|
+
rgb.map! { |channel| ((channel + m) * 255).round }
|
158
|
+
alpha.nil? ? rgb : rgb + [alpha]
|
159
|
+
end
|
149
160
|
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
161
|
+
def hsv_to_rgb_alt(hsv_arr)
|
162
|
+
hue, saturation, value, alpha = hsv_arr.map(&:to_f)
|
163
|
+
# scaling values into range 0..1
|
164
|
+
saturation /= 100
|
165
|
+
value /= 100
|
166
|
+
|
167
|
+
# calculation chroma & intermediate values
|
168
|
+
chroma = value * saturation
|
169
|
+
hue /= 60
|
170
|
+
x = chroma * (1 - (hue % 2 - 1).abs)
|
171
|
+
|
172
|
+
# possible RGB points
|
173
|
+
points = [[chroma, x, 0],
|
174
|
+
[x, chroma, 0],
|
175
|
+
[0, chroma, x],
|
176
|
+
[0, x, chroma],
|
177
|
+
[x, 0, chroma],
|
178
|
+
[chroma, 0, x]]
|
179
|
+
# point selection based on entering HUE input in range
|
180
|
+
point = points.each_with_index.detect { |rgb_, n| (n * (1 / 100.000)...n + 1).include? hue }&.first
|
181
|
+
# if point == nil (hue undefined)
|
182
|
+
rgb = point || [0, 0, 0]
|
183
|
+
|
184
|
+
# calculation rgb & scaling into range 0..255
|
185
|
+
m = value - chroma
|
186
|
+
rgb.map! { |channel| ((channel + m) * 255).round }
|
187
|
+
alpha.nil? ? rgb : rgb + [alpha]
|
188
|
+
end
|
178
189
|
|
179
|
-
|
190
|
+
alias_method :hsb_to_rgb_alt, :hsv_to_rgb_alt
|
180
191
|
|
181
|
-
|
192
|
+
#========= HSL <==> HSV (HSB) ==========================================
|
182
193
|
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
194
|
+
def hsl_to_hsv(hsl_arr, rounding = hsx_round)
|
195
|
+
hue, saturation, lightness, alpha = hsl_arr.map(&:to_f)
|
196
|
+
# scaling values into range 0..1
|
197
|
+
saturation /= 100
|
198
|
+
lightness /= 100
|
188
199
|
|
189
|
-
|
190
|
-
|
191
|
-
|
200
|
+
# calculation value & saturation HSV
|
201
|
+
value = lightness + saturation * [lightness, 1 - lightness].min
|
202
|
+
saturation_hsv = lightness == 0 ? 0 : 2 * (1 - lightness / value)
|
192
203
|
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
204
|
+
# scaling HSV values & rounding
|
205
|
+
hsv = [hue, saturation_hsv * 100, value * 100].map { |x| x.round(rounding) }
|
206
|
+
alpha.nil? ? hsv : hsv + [alpha]
|
207
|
+
end
|
197
208
|
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
209
|
+
alias_method :hsl_to_hsb, :hsl_to_hsv
|
210
|
+
|
211
|
+
def hsv_to_hsl(hsv_arr, rounding = hsx_round)
|
212
|
+
hue, saturation, value, alpha = hsv_arr.map(&:to_f)
|
213
|
+
# scaling values into range 0..1
|
214
|
+
saturation /= 100
|
215
|
+
value /= 100
|
216
|
+
|
217
|
+
# calculation lightness & saturation HSL
|
218
|
+
lightness = value * (1 - saturation / 2)
|
219
|
+
saturation_hsl = if [0, 1].any? { |v| v == lightness }
|
220
|
+
0
|
221
|
+
else
|
222
|
+
(value - lightness) / [lightness, 1 - lightness].min
|
223
|
+
end
|
224
|
+
|
225
|
+
# scaling HSL values & rounding
|
226
|
+
hsl = [hue, saturation_hsl * 100, lightness * 100].map { |x| x.round(rounding) }
|
227
|
+
alpha.nil? ? hsl : hsl + [alpha]
|
228
|
+
end
|
218
229
|
|
219
|
-
|
230
|
+
alias_method :hsb_to_hsl, :hsv_to_hsl
|
220
231
|
|
221
|
-
|
232
|
+
#========= RGB(A) <==> CMYK ============================================
|
222
233
|
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
234
|
+
def rgb_to_cmyk(rgb_arr, rounding = hsx_round)
|
235
|
+
# scaling RGB values into range 0..1
|
236
|
+
rgb = rgb_arr[0..2].map { |color| color / 255.to_f }
|
237
|
+
k = 1 - rgb.max
|
238
|
+
converter = proc do |color|
|
239
|
+
(1 - k) == 0 ? 0 : (1 - color - k) / (1 - k)
|
240
|
+
end
|
241
|
+
|
242
|
+
# calculation CMYK & scaling into percentages & rounding
|
243
|
+
c, m, y = rgb.map { |color| converter.call(color) || 0 }
|
244
|
+
cmyk = [c, m, y, k].map { |x| (x * 100).round(rounding) }
|
245
|
+
rgb_arr.size == 4 ? cmyk + [rgb_arr.last] : cmyk
|
229
246
|
end
|
230
247
|
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
248
|
+
def cmyk_to_rgb(cmyk_arr)
|
249
|
+
c, m, y, k = cmyk_arr[0..3].map { |color| color / 100.to_f }
|
250
|
+
converter = proc do |channel|
|
251
|
+
255 * (1 - channel) * (1 - k)
|
252
|
+
end
|
236
253
|
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
255 * (1 - channel) * (1 - k)
|
254
|
+
# calculation RGB & rounding
|
255
|
+
rgb = [c, m, y].map { |channel| converter.call(channel).round }
|
256
|
+
cmyk_arr.size == 5 ? rgb + [cmyk_arr.last] : rgb
|
241
257
|
end
|
242
258
|
|
243
|
-
|
244
|
-
rgb = [c, m, y].map { |channel| converter.call(channel).round }
|
245
|
-
cmyk_arr.size == 5 ? rgb + [cmyk_arr.last] : rgb
|
246
|
-
end
|
247
|
-
|
248
|
-
private
|
259
|
+
#========= helper methods for RGB to HSL/HSB/HSV =======================
|
249
260
|
|
250
|
-
|
261
|
+
# find greatest and smallest channel values and chroma from RGB
|
262
|
+
def get_min_max_chroma(red, green, blue)
|
263
|
+
cmin = [red, green, blue].min
|
264
|
+
cmax = [red, green, blue].max
|
265
|
+
# calculation chroma
|
266
|
+
chroma = cmax - cmin
|
251
267
|
|
252
|
-
|
253
|
-
|
254
|
-
cmin = [red, green, blue].min
|
255
|
-
cmax = [red, green, blue].max
|
256
|
-
# calculation chroma
|
257
|
-
chroma = cmax - cmin
|
268
|
+
[cmin, cmax, chroma]
|
269
|
+
end
|
258
270
|
|
259
|
-
|
271
|
+
# calculation HUE from RGB
|
272
|
+
def get_hue(red, green, blue)
|
273
|
+
_cmin, cmax, chroma = get_min_max_chroma(red, green, blue)
|
274
|
+
|
275
|
+
hue = if chroma == 0
|
276
|
+
0
|
277
|
+
elsif cmax == red
|
278
|
+
# red is max
|
279
|
+
((green - blue) / chroma) % 6
|
280
|
+
elsif cmax == green
|
281
|
+
# green is max
|
282
|
+
(blue - red) / chroma + 2
|
283
|
+
else
|
284
|
+
# blue is max
|
285
|
+
(red - green) / chroma + 4
|
286
|
+
end
|
287
|
+
hue *= 60
|
288
|
+
# make negative HUEs positive behind 360°
|
289
|
+
0 <= hue ? hue : hue + 360
|
290
|
+
end
|
260
291
|
end
|
261
292
|
|
262
|
-
|
263
|
-
def self.get_hue(red, green, blue)
|
264
|
-
_cmin, cmax, chroma = get_min_max_chroma(red, green, blue)
|
265
|
-
|
266
|
-
hue = if chroma == 0
|
267
|
-
0
|
268
|
-
elsif cmax == red
|
269
|
-
# red is max
|
270
|
-
((green - blue) / chroma) % 6
|
271
|
-
elsif cmax == green
|
272
|
-
# green is max
|
273
|
-
(blue - red) / chroma + 2
|
274
|
-
else
|
275
|
-
# blue is max
|
276
|
-
(red - green) / chroma + 4
|
277
|
-
end
|
278
|
-
hue *= 60
|
279
|
-
# make negative HUEs positive behind 360°
|
280
|
-
0 <= hue ? hue : hue + 360
|
281
|
-
end
|
293
|
+
extend ClassMethods
|
282
294
|
end
|
data/lib/decolmor/version.rb
CHANGED
data/lib/decolmor.rb
CHANGED
data/spec/decolmor_spec.rb
CHANGED
@@ -1,7 +1,20 @@
|
|
1
1
|
load_class(__FILE__)
|
2
2
|
|
3
3
|
RSpec.describe Decolmor do
|
4
|
-
|
4
|
+
|
5
|
+
describe 'we can include the module into the class' do
|
6
|
+
let(:dummy_class) { Class.new { include Decolmor } }
|
7
|
+
|
8
|
+
it "class contains the module Decolmor" do
|
9
|
+
expect( dummy_class.include?(Decolmor) ).to eq true
|
10
|
+
end
|
11
|
+
|
12
|
+
it "methods will be available into our class as class methods" do
|
13
|
+
expect { dummy_class.hsx_round = 3 }.to change { dummy_class.hsx_round }.from(1).to(3)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe 'we can set rounding globally for convert to HSL/HSV/HSB/CMYK' do
|
5
18
|
after(:each) do
|
6
19
|
Decolmor.hsx_round = 1
|
7
20
|
end
|
@@ -9,15 +22,14 @@ RSpec.describe Decolmor do
|
|
9
22
|
let(:colors) { FactoryBot.build(:colors_map, round: 2) }
|
10
23
|
|
11
24
|
it ".hsx_round by default 1" do
|
12
|
-
expect( Decolmor
|
25
|
+
expect( Decolmor.hsx_round ).to eq 1
|
13
26
|
end
|
14
27
|
|
15
28
|
it ".hsx_round= changes hsx_round" do
|
16
|
-
expect { Decolmor
|
29
|
+
expect { Decolmor.hsx_round = 2 }.to change { Decolmor.hsx_round }.from(1).to(2)
|
17
30
|
end
|
18
31
|
end
|
19
32
|
|
20
|
-
|
21
33
|
context 'HEX <==> RGB(A)' do
|
22
34
|
describe ".hex_to_rgb" do
|
23
35
|
let(:colors) { FactoryBot.build(:colors_map) }
|
@@ -25,14 +37,14 @@ RSpec.describe Decolmor do
|
|
25
37
|
|
26
38
|
it "HEX w prefix # to RGB" do
|
27
39
|
colors.each_pair do |hex, values|
|
28
|
-
expect( Decolmor
|
40
|
+
expect( Decolmor.hex_to_rgb(hex) ).to eq values[:rgb]
|
29
41
|
end
|
30
42
|
end
|
31
43
|
|
32
44
|
it "HEX w/o prefix # to RGB" do
|
33
45
|
colors.each_pair do |hex, values|
|
34
46
|
hex = hex.delete('#')
|
35
|
-
expect( Decolmor
|
47
|
+
expect( Decolmor.hex_to_rgb(hex) ).to eq values[:rgb]
|
36
48
|
end
|
37
49
|
end
|
38
50
|
|
@@ -42,7 +54,7 @@ RSpec.describe Decolmor do
|
|
42
54
|
alphas.each_pair do |hex_alpha, alpha|
|
43
55
|
hex = format('%s%s', color, hex_alpha)
|
44
56
|
rgba = colors[color][:rgb] + [alpha[:rgb]]
|
45
|
-
expect( Decolmor
|
57
|
+
expect( Decolmor.hex_to_rgb(hex) ).to eq rgba
|
46
58
|
end
|
47
59
|
end
|
48
60
|
|
@@ -52,7 +64,7 @@ RSpec.describe Decolmor do
|
|
52
64
|
rounding = 2
|
53
65
|
hex = format('%s%s', color, hex_alpha)
|
54
66
|
rgba = colors[color][:rgb] + [alpha[:rgb].round(rounding)]
|
55
|
-
expect( Decolmor
|
67
|
+
expect( Decolmor.hex_to_rgb(hex, rounding) ).to eq rgba
|
56
68
|
end
|
57
69
|
end
|
58
70
|
|
@@ -62,7 +74,7 @@ RSpec.describe Decolmor do
|
|
62
74
|
alphas.each_pair do |hex_alpha, alpha|
|
63
75
|
hex = format('%s%s', color, hex_alpha).delete('#')
|
64
76
|
rgba = colors[color][:rgb] + [alpha[:rgb]]
|
65
|
-
expect( Decolmor
|
77
|
+
expect( Decolmor.hex_to_rgb(hex) ).to eq rgba
|
66
78
|
end
|
67
79
|
end
|
68
80
|
|
@@ -70,7 +82,7 @@ RSpec.describe Decolmor do
|
|
70
82
|
colors = {'6FC' => [102, 255, 204], '#9C3' => [153, 204, 51], '36FF' => [51, 102, 255, 1]}
|
71
83
|
|
72
84
|
colors.each_pair do |hex_short, rgb|
|
73
|
-
expect( Decolmor
|
85
|
+
expect( Decolmor.hex_to_rgb(hex_short) ).to eq rgb
|
74
86
|
end
|
75
87
|
end
|
76
88
|
end
|
@@ -81,7 +93,7 @@ RSpec.describe Decolmor do
|
|
81
93
|
|
82
94
|
it "RGB converts to HEX" do
|
83
95
|
colors.each_pair do |hex, values|
|
84
|
-
expect( Decolmor
|
96
|
+
expect( Decolmor.rgb_to_hex(values[:rgb]) ).to eq hex
|
85
97
|
end
|
86
98
|
end
|
87
99
|
|
@@ -91,7 +103,7 @@ RSpec.describe Decolmor do
|
|
91
103
|
alphas.each_pair do |hex, alpha|
|
92
104
|
hex = format('%s%s', color, hex)
|
93
105
|
rgba = colors[color][:rgb] + [alpha[:rgb]]
|
94
|
-
expect( Decolmor
|
106
|
+
expect( Decolmor.rgb_to_hex(rgba) ).to eq hex
|
95
107
|
end
|
96
108
|
end
|
97
109
|
end
|
@@ -102,7 +114,7 @@ RSpec.describe Decolmor do
|
|
102
114
|
describe ".new_rgb" do
|
103
115
|
it "generate RGB with values into range 0..255" do
|
104
116
|
100.times do
|
105
|
-
expect( Decolmor
|
117
|
+
expect( Decolmor.new_rgb ).to all( be_between(0, 255) )
|
106
118
|
end
|
107
119
|
end
|
108
120
|
|
@@ -111,10 +123,10 @@ RSpec.describe Decolmor do
|
|
111
123
|
color = {red: 72, green: 209, blue: 204, alpha: 244}
|
112
124
|
# set and check each channel separately
|
113
125
|
color.each_with_index do |(key, value), index|
|
114
|
-
expect( Decolmor
|
126
|
+
expect( Decolmor.new_rgb(**{key => value})[index] ).to eq value
|
115
127
|
end
|
116
128
|
# set all channels
|
117
|
-
expect( Decolmor
|
129
|
+
expect( Decolmor.new_rgb(**color) ).to eq color.values
|
118
130
|
end
|
119
131
|
end
|
120
132
|
end
|
@@ -127,7 +139,7 @@ RSpec.describe Decolmor do
|
|
127
139
|
|
128
140
|
it "RGB converts to HSL" do
|
129
141
|
colors.each_pair do |hex, values|
|
130
|
-
expect( Decolmor
|
142
|
+
expect( Decolmor.rgb_to_hsl(values[:rgb]) ).to eq values[:hsl]
|
131
143
|
end
|
132
144
|
end
|
133
145
|
|
@@ -135,14 +147,14 @@ RSpec.describe Decolmor do
|
|
135
147
|
color = colors.keys.sample
|
136
148
|
alphas.each_pair do |hex_, alpha|
|
137
149
|
rgba = colors[color][:rgb] + [alpha[:rgb]]
|
138
|
-
expect( Decolmor
|
150
|
+
expect( Decolmor.rgb_to_hsl(rgba).last ).to eq alpha[:rgb]
|
139
151
|
end
|
140
152
|
end
|
141
153
|
|
142
154
|
it "you can set rounding for resulting HSL values (default = 1)" do
|
143
155
|
docs "round 1 enough for a lossless conversion RGB -> HSL/HSV/HSB -> RGB"
|
144
156
|
colors.each_pair do |hex, values|
|
145
|
-
expect( Decolmor
|
157
|
+
expect( Decolmor.rgb_to_hsl(values[:rgb], 0) ).to eq values[:hsl].map(&:round)
|
146
158
|
end
|
147
159
|
end
|
148
160
|
|
@@ -152,7 +164,7 @@ RSpec.describe Decolmor do
|
|
152
164
|
rgba = colors[color][:rgb] + [alpha[:rgb]]
|
153
165
|
# alpha shouldn't be rounded because its range is 0..1
|
154
166
|
# if that did happen, we would get 0 or 1 instead of the normal value
|
155
|
-
expect( Decolmor
|
167
|
+
expect( Decolmor.rgb_to_hsl(rgba, 0).last ).to eq alpha[:rgb]
|
156
168
|
end
|
157
169
|
end
|
158
170
|
end
|
@@ -164,7 +176,7 @@ RSpec.describe Decolmor do
|
|
164
176
|
|
165
177
|
it "RGB converts to HSV" do
|
166
178
|
colors.each_pair do |hex, values|
|
167
|
-
expect( Decolmor
|
179
|
+
expect( Decolmor.rgb_to_hsv(values[:rgb]) ).to eq values[:hsv]
|
168
180
|
end
|
169
181
|
end
|
170
182
|
|
@@ -172,13 +184,13 @@ RSpec.describe Decolmor do
|
|
172
184
|
color = colors.keys.sample
|
173
185
|
alphas.each_pair do |hex_, alpha|
|
174
186
|
rgba = colors[color][:rgb] + [alpha[:rgb]]
|
175
|
-
expect( Decolmor
|
187
|
+
expect( Decolmor.rgb_to_hsv(rgba).last ).to eq alpha[:rgb]
|
176
188
|
end
|
177
189
|
end
|
178
190
|
|
179
191
|
it "you can set rounding for resulting HSV values (default = 1)" do
|
180
192
|
colors_round_2.each_pair do |hex, values|
|
181
|
-
expect( Decolmor
|
193
|
+
expect( Decolmor.rgb_to_hsv(values[:rgb], 0) ).to eq values[:hsv].map(&:round)
|
182
194
|
end
|
183
195
|
end
|
184
196
|
|
@@ -186,14 +198,14 @@ RSpec.describe Decolmor do
|
|
186
198
|
color = colors.keys.sample
|
187
199
|
alphas.each_pair do |hex_, alpha|
|
188
200
|
rgba = colors[color][:rgb] + [alpha[:rgb]]
|
189
|
-
expect( Decolmor
|
201
|
+
expect( Decolmor.rgb_to_hsv(rgba, 0).last ).to eq alpha[:rgb]
|
190
202
|
end
|
191
203
|
end
|
192
204
|
end
|
193
205
|
|
194
206
|
describe ".rgb_to_hsb" do
|
195
207
|
it "alias .rgb_to_hsv" do
|
196
|
-
expect(
|
208
|
+
expect( Decolmor.method(:rgb_to_hsb ).original_name).to eq(:rgb_to_hsv)
|
197
209
|
end
|
198
210
|
end
|
199
211
|
end
|
@@ -206,7 +218,7 @@ RSpec.describe Decolmor do
|
|
206
218
|
|
207
219
|
it "HSL converts to RGB" do
|
208
220
|
colors.each_pair do |hex, values|
|
209
|
-
expect( Decolmor
|
221
|
+
expect( Decolmor.hsl_to_rgb(values[:hsl]) ).to eq values[:rgb]
|
210
222
|
end
|
211
223
|
end
|
212
224
|
|
@@ -214,7 +226,7 @@ RSpec.describe Decolmor do
|
|
214
226
|
color = colors.keys.sample
|
215
227
|
alphas.each_pair do |hex_, values|
|
216
228
|
hsla = colors[color][:hsl] + [values[:rgb]]
|
217
|
-
expect( Decolmor
|
229
|
+
expect( Decolmor.hsl_to_rgb(hsla).last ).to eq values[:rgb]
|
218
230
|
end
|
219
231
|
end
|
220
232
|
end
|
@@ -225,7 +237,7 @@ RSpec.describe Decolmor do
|
|
225
237
|
|
226
238
|
it "HSV converts to RGB" do
|
227
239
|
colors.each_pair do |hex, values|
|
228
|
-
expect( Decolmor
|
240
|
+
expect( Decolmor.hsv_to_rgb(values[:hsv]) ).to eq values[:rgb]
|
229
241
|
end
|
230
242
|
end
|
231
243
|
|
@@ -233,14 +245,14 @@ RSpec.describe Decolmor do
|
|
233
245
|
color = colors.keys.sample
|
234
246
|
alphas.each_pair do |hex_, values|
|
235
247
|
hsva = colors[color][:hsv] + [values[:rgb]]
|
236
|
-
expect( Decolmor
|
248
|
+
expect( Decolmor.hsv_to_rgb(hsva).last ).to eq values[:rgb]
|
237
249
|
end
|
238
250
|
end
|
239
251
|
end
|
240
252
|
|
241
253
|
describe ".hsb_to_rgb" do
|
242
254
|
it "alias .hsv_to_rgb" do
|
243
|
-
expect(
|
255
|
+
expect( Decolmor.method(:hsb_to_rgb ).original_name).to eq(:hsv_to_rgb)
|
244
256
|
end
|
245
257
|
end
|
246
258
|
end
|
@@ -253,7 +265,7 @@ RSpec.describe Decolmor do
|
|
253
265
|
|
254
266
|
it "HSL converts to RGB" do
|
255
267
|
colors.each_pair do |hex, values|
|
256
|
-
expect( Decolmor
|
268
|
+
expect( Decolmor.hsl_to_rgb_alt(values[:hsl]) ).to eq values[:rgb]
|
257
269
|
end
|
258
270
|
end
|
259
271
|
|
@@ -261,7 +273,7 @@ RSpec.describe Decolmor do
|
|
261
273
|
color = colors.keys.sample
|
262
274
|
alphas.each_pair do |hex_, values|
|
263
275
|
hsla = colors[color][:hsl] + [values[:rgb]]
|
264
|
-
expect( Decolmor
|
276
|
+
expect( Decolmor.hsl_to_rgb_alt(hsla).last ).to eq values[:rgb]
|
265
277
|
end
|
266
278
|
end
|
267
279
|
end
|
@@ -272,7 +284,7 @@ RSpec.describe Decolmor do
|
|
272
284
|
|
273
285
|
it "HSV converts to RGB" do
|
274
286
|
colors.each_pair do |hex, values|
|
275
|
-
expect( Decolmor
|
287
|
+
expect( Decolmor.hsv_to_rgb_alt(values[:hsv]) ).to eq values[:rgb]
|
276
288
|
end
|
277
289
|
end
|
278
290
|
|
@@ -280,14 +292,14 @@ RSpec.describe Decolmor do
|
|
280
292
|
color = colors.keys.sample
|
281
293
|
alphas.each_pair do |hex_, values|
|
282
294
|
hsva = colors[color][:hsv] + [values[:rgb]]
|
283
|
-
expect( Decolmor
|
295
|
+
expect( Decolmor.hsv_to_rgb_alt(hsva).last ).to eq values[:rgb]
|
284
296
|
end
|
285
297
|
end
|
286
298
|
end
|
287
299
|
|
288
300
|
describe ".hsb_to_rgb_alt" do
|
289
301
|
it "alias .hsv_to_rgb_alt" do
|
290
|
-
expect(
|
302
|
+
expect( Decolmor.method(:hsb_to_rgb_alt ).original_name).to eq(:hsv_to_rgb_alt)
|
291
303
|
end
|
292
304
|
end
|
293
305
|
end
|
@@ -302,7 +314,7 @@ RSpec.describe Decolmor do
|
|
302
314
|
it "HSL converts to HSV" do
|
303
315
|
colors.each_pair do |hex_, values|
|
304
316
|
hsv = values[:hsv].map { |value| value.round(1) }
|
305
|
-
expect( Decolmor
|
317
|
+
expect( Decolmor.hsl_to_hsv(values[:hsl]) ).to eq hsv
|
306
318
|
end
|
307
319
|
end
|
308
320
|
|
@@ -310,13 +322,13 @@ RSpec.describe Decolmor do
|
|
310
322
|
color = colors.keys.sample
|
311
323
|
alphas.each_pair do |hex_, alpha|
|
312
324
|
hsla = colors[color][:hsl] + [alpha[:rgb]]
|
313
|
-
expect( Decolmor
|
325
|
+
expect( Decolmor.hsl_to_hsv(hsla).last ).to eq alpha[:rgb]
|
314
326
|
end
|
315
327
|
end
|
316
328
|
|
317
329
|
it "you can set rounding for resulting HSV values (default = 1)" do
|
318
330
|
colors.each_pair do |hex, values|
|
319
|
-
expect( Decolmor
|
331
|
+
expect( Decolmor.hsl_to_hsv(values[:hsl], 0) ).to eq values[:hsv].map(&:round)
|
320
332
|
end
|
321
333
|
end
|
322
334
|
|
@@ -324,14 +336,14 @@ RSpec.describe Decolmor do
|
|
324
336
|
color = colors.keys.sample
|
325
337
|
alphas.each_pair do |hex_, alpha|
|
326
338
|
hsla = colors[color][:hsl] + [alpha[:rgb]]
|
327
|
-
expect( Decolmor
|
339
|
+
expect( Decolmor.hsl_to_hsv(hsla, 0).last ).to eq alpha[:rgb]
|
328
340
|
end
|
329
341
|
end
|
330
342
|
end
|
331
343
|
|
332
344
|
describe ".hsl_to_hsb" do
|
333
345
|
it "alias .hsl_to_hsv" do
|
334
|
-
expect(
|
346
|
+
expect( Decolmor.method(:hsl_to_hsb).original_name ).to eq(:hsl_to_hsv)
|
335
347
|
end
|
336
348
|
end
|
337
349
|
|
@@ -343,7 +355,7 @@ RSpec.describe Decolmor do
|
|
343
355
|
it "HSV converts to HSL" do
|
344
356
|
colors.each_pair do |hex_, values|
|
345
357
|
hsl = values[:hsl].map { |value| value.round(1) }
|
346
|
-
expect( Decolmor
|
358
|
+
expect( Decolmor.hsv_to_hsl(values[:hsv]) ).to eq hsl
|
347
359
|
end
|
348
360
|
end
|
349
361
|
|
@@ -351,13 +363,13 @@ RSpec.describe Decolmor do
|
|
351
363
|
color = colors.keys.sample
|
352
364
|
alphas.each_pair do |hex_, alpha|
|
353
365
|
hsva = colors[color][:hsv] + [alpha[:rgb]]
|
354
|
-
expect( Decolmor
|
366
|
+
expect( Decolmor.hsv_to_hsl(hsva).last ).to eq alpha[:rgb]
|
355
367
|
end
|
356
368
|
end
|
357
369
|
|
358
370
|
it "you can set rounding for resulting HSL values (default = 1)" do
|
359
371
|
colors.each_pair do |hex, values|
|
360
|
-
expect( Decolmor
|
372
|
+
expect( Decolmor.hsv_to_hsl(values[:hsv], 0) ).to eq values[:hsl].map(&:round)
|
361
373
|
end
|
362
374
|
end
|
363
375
|
|
@@ -365,14 +377,14 @@ RSpec.describe Decolmor do
|
|
365
377
|
color = colors.keys.sample
|
366
378
|
alphas.each_pair do |hex_, alpha|
|
367
379
|
hsva = colors[color][:hsv] + [alpha[:rgb]]
|
368
|
-
expect( Decolmor
|
380
|
+
expect( Decolmor.hsv_to_hsl(hsva, 0).last ).to eq alpha[:rgb]
|
369
381
|
end
|
370
382
|
end
|
371
383
|
end
|
372
384
|
|
373
385
|
describe ".hsb_to_hsl" do
|
374
386
|
it "alias .hsv_to_hsl" do
|
375
|
-
expect(
|
387
|
+
expect( Decolmor.method(:hsb_to_hsl).original_name ).to eq(:hsv_to_hsl)
|
376
388
|
end
|
377
389
|
end
|
378
390
|
end
|
@@ -387,7 +399,7 @@ RSpec.describe Decolmor do
|
|
387
399
|
colors.each_pair do |hex_, values|
|
388
400
|
|
389
401
|
cmyk = values[:cmyk].map {|arr| arr.round(1) }
|
390
|
-
expect( Decolmor
|
402
|
+
expect( Decolmor.rgb_to_cmyk(values[:rgb]) ).to eq cmyk
|
391
403
|
end
|
392
404
|
end
|
393
405
|
|
@@ -395,13 +407,13 @@ RSpec.describe Decolmor do
|
|
395
407
|
color = colors.keys.sample
|
396
408
|
alphas.each_pair do |hex_, alpha|
|
397
409
|
rgba = colors[color][:rgb] + [alpha[:rgb]]
|
398
|
-
expect( Decolmor
|
410
|
+
expect( Decolmor.rgb_to_cmyk(rgba).last ).to eq alpha[:rgb]
|
399
411
|
end
|
400
412
|
end
|
401
413
|
|
402
414
|
it "you can set rounding for resulting CMYK values (default = 1)" do
|
403
415
|
colors.each_pair do |hex, values|
|
404
|
-
expect( Decolmor
|
416
|
+
expect( Decolmor.hsv_to_hsl(values[:hsv], 0) ).to eq values[:hsl].map(&:round)
|
405
417
|
end
|
406
418
|
end
|
407
419
|
|
@@ -409,7 +421,7 @@ RSpec.describe Decolmor do
|
|
409
421
|
color = colors.keys.sample
|
410
422
|
alphas.each_pair do |hex_, alpha|
|
411
423
|
rgba = colors[color][:rgb] + [alpha[:rgb]]
|
412
|
-
expect( Decolmor
|
424
|
+
expect( Decolmor.rgb_to_cmyk(rgba, 0).last ).to eq alpha[:rgb]
|
413
425
|
end
|
414
426
|
end
|
415
427
|
end
|
@@ -420,7 +432,7 @@ RSpec.describe Decolmor do
|
|
420
432
|
|
421
433
|
it "CMYK converts to RGB" do
|
422
434
|
colors.each_pair do |hex_, values|
|
423
|
-
expect( Decolmor
|
435
|
+
expect( Decolmor.cmyk_to_rgb(values[:cmyk]) ).to eq values[:rgb]
|
424
436
|
end
|
425
437
|
end
|
426
438
|
|
@@ -428,11 +440,10 @@ RSpec.describe Decolmor do
|
|
428
440
|
color = colors.keys.sample
|
429
441
|
alphas.each_pair do |hex_, values|
|
430
442
|
cmyka = colors[color][:cmyk] + [values[:rgb]]
|
431
|
-
expect( Decolmor
|
443
|
+
expect( Decolmor.cmyk_to_rgb(cmyka).last ).to eq values[:rgb]
|
432
444
|
end
|
433
445
|
end
|
434
446
|
end
|
435
447
|
end
|
436
448
|
|
437
449
|
end
|
438
|
-
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: decolmor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ChildrenofkoRn
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-09-
|
11
|
+
date: 2021-09-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -123,9 +123,9 @@ licenses:
|
|
123
123
|
- MIT
|
124
124
|
metadata:
|
125
125
|
homepage_uri: https://github.com/ChildrenofkoRn/decolmor
|
126
|
-
news_uri: https://github.com/ChildrenofkoRn/decolmor/blob/
|
127
|
-
changelog_uri: https://github.com/ChildrenofkoRn/decolmor/blob/
|
128
|
-
documentation_uri: https://github.com/ChildrenofkoRn/decolmor/blob/
|
126
|
+
news_uri: https://github.com/ChildrenofkoRn/decolmor/blob/main/NEWS.md
|
127
|
+
changelog_uri: https://github.com/ChildrenofkoRn/decolmor/blob/main/CHANGELOG.md
|
128
|
+
documentation_uri: https://github.com/ChildrenofkoRn/decolmor/blob/main/README.md
|
129
129
|
bug_tracker_uri: https://github.com/ChildrenofkoRn/decolmor/issues
|
130
130
|
source_code_uri: https://github.com/ChildrenofkoRn/decolmor
|
131
131
|
post_install_message:
|
@@ -143,7 +143,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
143
143
|
- !ruby/object:Gem::Version
|
144
144
|
version: '0'
|
145
145
|
requirements: []
|
146
|
-
rubygems_version: 3.
|
146
|
+
rubygems_version: 3.2.27
|
147
147
|
signing_key:
|
148
148
|
specification_version: 4
|
149
149
|
summary: 'Converter color spaces from/to: HEX/RGB/HSL/HSV/HSB/CMYK'
|