rgb 0.1.0 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/rgb/color.rb +127 -63
- data/lib/rgb/version.rb +3 -0
- data/lib/rgb.rb +5 -1
- metadata +55 -32
- data/README.md +0 -54
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 5220fed36087348bdf09b5d67a604d0eb64df18c8d4ce7744c8c64d19275dd82
|
4
|
+
data.tar.gz: e6fa4726da09bc76575a17d63b8ae589abc33cb2e7dfff10e363e96e74af7bc7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0ae1005d382691de01596d72ded5639a25b903eb4898f53eb79f00dc0f8b20056913f6c52ba561b03e8c4eb94cf9f09c83946c8765f64b7aa59356d482a29137
|
7
|
+
data.tar.gz: af38e0378034c398b7ef5efdc641fe4a43e812e15a57b4fafeef76f3aea497a49290f9db388ee02fde28864fcd1968f39bc57c0cccc903450e5fee395ee9061a
|
data/lib/rgb/color.rb
CHANGED
@@ -1,139 +1,182 @@
|
|
1
1
|
module RGB
|
2
2
|
class Color
|
3
|
-
attr_reader :
|
3
|
+
attr_reader :hue, :saturation, :lightness # ? HSL
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
def self.from_rgb(*rgb)
|
12
|
-
rgb.map!{|c| c / 255.0}
|
13
|
-
min_rgb = rgb.min
|
14
|
-
max_rgb = rgb.max
|
15
|
-
delta = max_rgb - min_rgb
|
16
|
-
|
17
|
-
lightness = (max_rgb + min_rgb) / 2.0
|
5
|
+
class << self
|
6
|
+
def from_rgb_hex(color)
|
7
|
+
color = '#%.6x' % color if color.is_a? Integer
|
8
|
+
rgb = color[1,7].scan(/.{2}/).map{ |component| component.to_i(16) }
|
9
|
+
from_rgb(*rgb)
|
10
|
+
end
|
18
11
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
else
|
23
|
-
saturation = if ( lightness < 0.5 )
|
24
|
-
delta / ( max_rgb + min_rgb )
|
12
|
+
def calc_saturation(max_rgb, min_rgb, delta, lightness)
|
13
|
+
if lightness < 0.5
|
14
|
+
delta / (max_rgb + min_rgb)
|
25
15
|
else
|
26
|
-
|
16
|
+
delta / (2 - max_rgb - min_rgb)
|
27
17
|
end
|
18
|
+
end
|
28
19
|
|
29
|
-
|
20
|
+
def from_rgb(*rgb)
|
21
|
+
from_fractions(*rgb_to_hsl(*rgb))
|
22
|
+
end
|
30
23
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
24
|
+
def rgb_to_hsl(*rgb)
|
25
|
+
rgb.map!{ |c| c / 255.0 }
|
26
|
+
min_rgb, max_rgb = rgb.min, rgb.max
|
27
|
+
delta = max_rgb - min_rgb
|
28
|
+
|
29
|
+
lightness = (max_rgb + min_rgb) / 2.0
|
30
|
+
|
31
|
+
if delta < 1e-5
|
32
|
+
hue = saturation = 0
|
35
33
|
else
|
36
|
-
|
34
|
+
saturation = calc_saturation(max_rgb, min_rgb, delta, lightness)
|
35
|
+
deltas = rgb.map{ |c| (((max_rgb - c) / 6.0) + (delta / 2.0)) / delta }
|
36
|
+
|
37
|
+
hue = if (rgb[0] - max_rgb).abs < 1e-5
|
38
|
+
deltas[2] - deltas[1]
|
39
|
+
elsif (rgb[1] - max_rgb).abs < 1e-5
|
40
|
+
(1.0 / 3.0) + deltas[0] - deltas[2]
|
41
|
+
else
|
42
|
+
(2.0 / 3.0) + deltas[1] - deltas[0]
|
43
|
+
end
|
44
|
+
|
45
|
+
hue += 1 if hue < 0
|
46
|
+
hue -= 1 if hue > 1
|
37
47
|
end
|
38
|
-
hue
|
39
|
-
hue -= 1 if hue > 1
|
48
|
+
[hue, saturation, lightness]
|
40
49
|
end
|
41
|
-
from_fractions(hue, saturation, lightness)
|
42
|
-
end
|
43
50
|
|
44
|
-
|
45
|
-
|
51
|
+
def from_fractions(hue, saturation, lightness)
|
52
|
+
new(360 * hue, saturation, lightness)
|
53
|
+
end
|
46
54
|
end
|
47
55
|
|
48
56
|
def initialize(*hsl)
|
49
|
-
self.
|
57
|
+
self.hue, self.saturation, self.lightness = hsl
|
50
58
|
end
|
51
59
|
|
52
60
|
def to_rgb
|
53
|
-
m2 =
|
54
|
-
|
55
|
-
|
61
|
+
m2 = if lightness <= 0.5
|
62
|
+
lightness * (saturation + 1)
|
63
|
+
else
|
64
|
+
lightness + saturation - lightness * saturation
|
65
|
+
end
|
66
|
+
|
67
|
+
m1 = lightness * 2 - m2
|
68
|
+
|
69
|
+
[
|
70
|
+
hue_to_rgb(m1, m2, hue_percentage + 1.0 / 3),
|
71
|
+
hue_to_rgb(m1, m2, hue_percentage),
|
72
|
+
hue_to_rgb(m1, m2, hue_percentage - 1.0 / 3)
|
73
|
+
].map { |c| (c * 0xff).round }
|
56
74
|
end
|
57
75
|
|
58
76
|
def to_hsl
|
59
|
-
[
|
77
|
+
[hue.to_i, saturation.to_f, lightness.to_f]
|
78
|
+
# [((hue / 255) * 360).to_i, saturation.to_f, lightness.to_f]
|
60
79
|
end
|
61
80
|
|
62
81
|
def to_rgb_hex
|
63
|
-
|
82
|
+
'#' + to_rgb.map {|c| '%02X' % c }.join
|
64
83
|
end
|
65
84
|
|
66
|
-
def
|
67
|
-
@
|
85
|
+
def hue=(value)
|
86
|
+
@hue = value % 360
|
87
|
+
check_hue()
|
68
88
|
end
|
69
89
|
|
70
|
-
def
|
71
|
-
@
|
90
|
+
def saturation=(value)
|
91
|
+
@saturation = if value < 0
|
72
92
|
0.0
|
73
|
-
elsif
|
93
|
+
elsif value > 1
|
74
94
|
1.0
|
75
95
|
else
|
76
|
-
|
96
|
+
value
|
77
97
|
end
|
78
98
|
end
|
79
99
|
|
80
|
-
def
|
81
|
-
@
|
100
|
+
def lightness=(value)
|
101
|
+
@lightness = if value < 0
|
82
102
|
0.0
|
83
|
-
elsif
|
103
|
+
elsif value > 1
|
84
104
|
1.0
|
85
105
|
else
|
86
|
-
|
106
|
+
value
|
87
107
|
end
|
88
108
|
end
|
89
109
|
|
90
110
|
def lighten!(amount)
|
91
|
-
@
|
111
|
+
@lightness += amount / 100.0
|
112
|
+
check_lightness()
|
92
113
|
end
|
93
114
|
|
94
115
|
def lighten_percent!(percentage)
|
95
|
-
@
|
116
|
+
@lightness += (1 - @lightness) * (percentage / 100.0)
|
117
|
+
check_lightness()
|
96
118
|
end
|
97
119
|
|
98
120
|
def darken!(amount)
|
99
|
-
@
|
121
|
+
@lightness -= (amount / 100.0)
|
122
|
+
@lightness = 0 if @lightness < 0
|
123
|
+
check_lightness()
|
100
124
|
end
|
101
125
|
|
102
126
|
def darken_percent!(percentage)
|
103
|
-
@
|
127
|
+
@lightness *= 1.0 - (percentage / 100.0)
|
128
|
+
check_lightness()
|
104
129
|
end
|
105
130
|
|
106
131
|
def saturate!(amount)
|
107
|
-
@
|
132
|
+
@saturation += amount / 100.0
|
133
|
+
check_saturation()
|
108
134
|
end
|
109
135
|
|
110
136
|
def saturate_percent!(percentage)
|
111
|
-
@
|
137
|
+
@saturation += (1 - @saturation) * (percentage / 100.0)
|
138
|
+
check_saturation()
|
112
139
|
end
|
113
140
|
|
114
141
|
def desaturate!(amount)
|
115
|
-
@
|
142
|
+
@saturation -= amount / 100.0
|
143
|
+
check_saturation()
|
116
144
|
end
|
117
145
|
|
118
146
|
def desaturate_percent!(percentage)
|
119
|
-
@
|
147
|
+
@saturation *= (1.0 - (percentage / 100.0))
|
148
|
+
check_saturation()
|
149
|
+
end
|
150
|
+
|
151
|
+
def invert!
|
152
|
+
@hue, @saturation, @lightness = RGB::Color.from_rgb(*self.to_rgb.map{ |c| 255 - c }).to_hsl
|
153
|
+
end
|
154
|
+
|
155
|
+
|
156
|
+
# ? Shamelessly stolen from
|
157
|
+
# ? https://github.com/chriseppstein/compass-colors/blob/master/lib/compass-colors/sass_extensions.rb#L86
|
158
|
+
# ? Though, I've inverted the coefficients, which seems more logical
|
159
|
+
def mix!(other, pct = 50.0)
|
160
|
+
coeff = pct.to_f / 100.0
|
161
|
+
new_rgb = to_rgb.zip(other.to_rgb).map{ |c1, c2| (c1 * (1 - coeff)) + (c2 * coeff) }
|
162
|
+
h, s, l = self.class.rgb_to_hsl(*new_rgb)
|
163
|
+
self.hue, self.saturation, self.lightness = 360 * h, s, l
|
120
164
|
end
|
121
165
|
|
122
|
-
#
|
166
|
+
# ? Define non-bang methods
|
123
167
|
[:darken, :darken_percent, :lighten, :lighten_percent, :saturate, :saturate_percent, :desaturate,
|
124
|
-
:desaturate_percent].each do |method_name|
|
168
|
+
:desaturate_percent, :invert, :mix].each do |method_name|
|
125
169
|
define_method method_name do |*args|
|
126
170
|
dup.tap { |color| color.send(:"#{method_name}!", *args) }
|
127
171
|
end
|
128
172
|
end
|
129
173
|
|
130
174
|
private
|
131
|
-
|
132
|
-
|
133
|
-
h / 360.0
|
175
|
+
def hue_percentage
|
176
|
+
hue / 360.0
|
134
177
|
end
|
135
178
|
|
136
|
-
#
|
179
|
+
# ? Helper for making rgb
|
137
180
|
def hue_to_rgb(m1, m2, h)
|
138
181
|
h += 1 if h < 0
|
139
182
|
h -= 1 if h > 1
|
@@ -142,5 +185,26 @@ module RGB
|
|
142
185
|
return m1 + (m2 - m1) * (2.0/3 - h) * 6 if h * 3 < 2
|
143
186
|
return m1
|
144
187
|
end
|
188
|
+
|
189
|
+
# ? Method to ensure lightness value is valid (0-1)
|
190
|
+
def check_lightness
|
191
|
+
if @lightness > 1; @lightness = 1.0; end;
|
192
|
+
if @lightness < 0; @lightness = 0.0; end;
|
193
|
+
@lightness
|
194
|
+
end
|
195
|
+
|
196
|
+
# ? Method to ensure saturation value is valid (0-1)
|
197
|
+
def check_saturation
|
198
|
+
if @saturation > 1; @saturation = 1.0; end;
|
199
|
+
if @saturation < 0; @saturation = 0.0; end;
|
200
|
+
@saturation
|
201
|
+
end
|
202
|
+
|
203
|
+
# ? Method to ensure hue value is valid (0-360)
|
204
|
+
def check_hue
|
205
|
+
if @hue > 360; @hue = 360; end;
|
206
|
+
if @hue < 0; @hue = 0; end;
|
207
|
+
@hue
|
208
|
+
end
|
145
209
|
end
|
146
210
|
end
|
data/lib/rgb/version.rb
ADDED
data/lib/rgb.rb
CHANGED
metadata
CHANGED
@@ -1,81 +1,104 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rgb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
5
|
-
prerelease:
|
4
|
+
version: 0.1.2
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
|
-
-
|
9
|
-
autorequire:
|
7
|
+
- Dzmitry Plashchynski
|
8
|
+
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2024-05-23 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 2.5.9
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 2.5.9
|
14
27
|
- !ruby/object:Gem::Dependency
|
15
28
|
name: rake
|
16
29
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
30
|
requirements:
|
19
|
-
- -
|
31
|
+
- - "~>"
|
20
32
|
- !ruby/object:Gem::Version
|
21
|
-
version:
|
33
|
+
version: 13.1.0
|
22
34
|
type: :development
|
23
35
|
prerelease: false
|
24
36
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
37
|
requirements:
|
27
|
-
- -
|
38
|
+
- - "~>"
|
28
39
|
- !ruby/object:Gem::Version
|
29
|
-
version:
|
40
|
+
version: 13.1.0
|
30
41
|
- !ruby/object:Gem::Dependency
|
31
42
|
name: rspec
|
32
43
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
44
|
requirements:
|
35
|
-
- -
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 3.13.0
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 3.13.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: simplecov
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
36
60
|
- !ruby/object:Gem::Version
|
37
|
-
version:
|
61
|
+
version: 0.22.0
|
38
62
|
type: :development
|
39
63
|
prerelease: false
|
40
64
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
65
|
requirements:
|
43
|
-
- -
|
66
|
+
- - "~>"
|
44
67
|
- !ruby/object:Gem::Version
|
45
|
-
version:
|
68
|
+
version: 0.22.0
|
46
69
|
description: A library built to handle the easy conversion, comparison and manipulation
|
47
70
|
of colors with CSS-style hex color notation.
|
48
|
-
email:
|
71
|
+
email:
|
72
|
+
- plashchynski@gmail.com
|
49
73
|
executables: []
|
50
74
|
extensions: []
|
51
75
|
extra_rdoc_files: []
|
52
76
|
files:
|
53
|
-
- README.md
|
54
77
|
- lib/rgb.rb
|
55
78
|
- lib/rgb/color.rb
|
79
|
+
- lib/rgb/version.rb
|
56
80
|
homepage: https://github.com/plashchynski/rgb
|
57
|
-
licenses:
|
58
|
-
|
81
|
+
licenses:
|
82
|
+
- Apache-2.0
|
83
|
+
metadata: {}
|
84
|
+
post_install_message:
|
59
85
|
rdoc_options: []
|
60
86
|
require_paths:
|
61
87
|
- lib
|
62
88
|
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
-
none: false
|
64
89
|
requirements:
|
65
|
-
- -
|
90
|
+
- - ">="
|
66
91
|
- !ruby/object:Gem::Version
|
67
|
-
version:
|
92
|
+
version: 2.7.0
|
68
93
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
|
-
none: false
|
70
94
|
requirements:
|
71
|
-
- -
|
95
|
+
- - ">="
|
72
96
|
- !ruby/object:Gem::Version
|
73
97
|
version: '0'
|
74
98
|
requirements: []
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
of colors with CSS-style hex color notation.
|
99
|
+
rubygems_version: 3.5.9
|
100
|
+
signing_key:
|
101
|
+
specification_version: 4
|
102
|
+
summary: A simple Ruby library built to handle easy conversion and manipulation of
|
103
|
+
colors.
|
81
104
|
test_files: []
|
data/README.md
DELETED
@@ -1,54 +0,0 @@
|
|
1
|
-
rgb
|
2
|
-
===
|
3
|
-
|
4
|
-
A simple Ruby library built to handle the easy conversion and manipulation of colors. Inspired by compass-colors https://github.com/chriseppstein/compass-colors and jColour.js https://github.com/lingo/jcolour.
|
5
|
-
|
6
|
-
Example
|
7
|
-
===
|
8
|
-
|
9
|
-
require "rgb"
|
10
|
-
|
11
|
-
# Supported input data color formas:
|
12
|
-
color = RGB::Color.from_rgb_hex("#333333")
|
13
|
-
color = RGB::Color.from_rgb_hex(0xFF0000)
|
14
|
-
color = RGB::Color.from_rgb(115, 38, 38)
|
15
|
-
color = RGB::Color.from_fractions(0, 1.0, 0.5) # HSL
|
16
|
-
|
17
|
-
# Supported color manipulations:
|
18
|
-
color.darken(20)
|
19
|
-
color.darken_percent(10)
|
20
|
-
color.darken!(20)
|
21
|
-
color.darken_percent!(10)
|
22
|
-
color.lighten(20)
|
23
|
-
color.lighten_percent(20)
|
24
|
-
color.lighten!(20)
|
25
|
-
color.lighten_percent!(20)
|
26
|
-
color.saturate(20)
|
27
|
-
color.saturate_percent(20)
|
28
|
-
color.saturate!(20)
|
29
|
-
color.saturate_percent!(20)
|
30
|
-
color.desaturate(20)
|
31
|
-
color.desaturate_percent(20)
|
32
|
-
color.desaturate!(20)
|
33
|
-
color.desaturate_percent!(20)
|
34
|
-
|
35
|
-
# Also you can adjust color hue, saturation, and lightness values manually:
|
36
|
-
color.h = 0.1
|
37
|
-
color.s = 0.2
|
38
|
-
color.l = 0.3
|
39
|
-
|
40
|
-
# Supported output formats:
|
41
|
-
color.to_rgb_hex
|
42
|
-
=> "#732626"
|
43
|
-
color.to_hsl
|
44
|
-
=> [0, 1.0, 0.5]
|
45
|
-
color.to_rgb
|
46
|
-
=> [115, 38, 38]
|
47
|
-
|
48
|
-
|
49
|
-
Resources
|
50
|
-
===
|
51
|
-
|
52
|
-
* GitHub Source: https://github.com/plashchynski/rgb
|
53
|
-
|
54
|
-
Copyright (c) 2013 Dmitry Plashchynski. Released under the MIT open source license.
|