rgb 0.1.0 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. checksums.yaml +7 -0
  2. data/lib/rgb/color.rb +127 -63
  3. data/lib/rgb/version.rb +3 -0
  4. data/lib/rgb.rb +5 -1
  5. metadata +55 -32
  6. 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 :h, :s, :l
3
+ attr_reader :hue, :saturation, :lightness # ? HSL
4
4
 
5
- def self.from_rgb_hex(color)
6
- color = "#%.6x" % color if color.is_a? Integer
7
- rgb = color[1,7].scan(/.{2}/).map{|component| component.to_i(16)}
8
- from_rgb(*rgb)
9
- end
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
- if delta < 1e-5
20
- hue = 0
21
- saturation = 0
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
- delta / ( 2 - max_rgb - min_rgb )
16
+ delta / (2 - max_rgb - min_rgb)
27
17
  end
18
+ end
28
19
 
29
- deltas = rgb.map{|c| (((max_rgb - c) / 6.0) + (delta / 2.0)) / delta}
20
+ def from_rgb(*rgb)
21
+ from_fractions(*rgb_to_hsl(*rgb))
22
+ end
30
23
 
31
- hue = if (rgb[0] - max_rgb).abs < 1e-5
32
- deltas[2] - deltas[1]
33
- elsif (rgb[1] - max_rgb).abs < 1e-5
34
- ( 1.0 / 3.0 ) + deltas[0] - deltas[2]
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
- ( 2.0 / 3.0 ) + deltas[1] - deltas[0]
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 += 1 if hue < 0
39
- hue -= 1 if hue > 1
48
+ [hue, saturation, lightness]
40
49
  end
41
- from_fractions(hue, saturation, lightness)
42
- end
43
50
 
44
- def self.from_fractions(hue, saturation, lightness)
45
- new(360 * hue, saturation, lightness)
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.h, self.s, self.l = hsl
57
+ self.hue, self.saturation, self.lightness = hsl
50
58
  end
51
59
 
52
60
  def to_rgb
53
- m2 = l <= 0.5 ? l * (s + 1) : l + s - l * s
54
- m1 = l * 2 - m2
55
- [hue_to_rgb(m1, m2, hp + 1.0/3), hue_to_rgb(m1, m2, hp), hue_to_rgb(m1, m2, hp - 1.0/3)].map { |c| (c * 0xff).round }
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
- [h,s,l]
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
- "#" + to_rgb.map {|c| "%02X" % c }.join
82
+ '#' + to_rgb.map {|c| '%02X' % c }.join
64
83
  end
65
84
 
66
- def h=(hue)
67
- @h = hue % 360
85
+ def hue=(value)
86
+ @hue = value % 360
87
+ check_hue()
68
88
  end
69
89
 
70
- def s=(saturation)
71
- @s = if saturation < 0
90
+ def saturation=(value)
91
+ @saturation = if value < 0
72
92
  0.0
73
- elsif saturation > 1
93
+ elsif value > 1
74
94
  1.0
75
95
  else
76
- saturation
96
+ value
77
97
  end
78
98
  end
79
99
 
80
- def l=(lightness)
81
- @l = if lightness < 0
100
+ def lightness=(value)
101
+ @lightness = if value < 0
82
102
  0.0
83
- elsif lightness > 1
103
+ elsif value > 1
84
104
  1.0
85
105
  else
86
- lightness
106
+ value
87
107
  end
88
108
  end
89
109
 
90
110
  def lighten!(amount)
91
- @l += amount / 100.0
111
+ @lightness += amount / 100.0
112
+ check_lightness()
92
113
  end
93
114
 
94
115
  def lighten_percent!(percentage)
95
- @l += (1 - @l) * (percentage / 100.0)
116
+ @lightness += (1 - @lightness) * (percentage / 100.0)
117
+ check_lightness()
96
118
  end
97
119
 
98
120
  def darken!(amount)
99
- @l -= (amount / 100.0)
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
- @l *= 1.0 - (percentage / 100.0)
127
+ @lightness *= 1.0 - (percentage / 100.0)
128
+ check_lightness()
104
129
  end
105
130
 
106
131
  def saturate!(amount)
107
- @s += amount / 100.0
132
+ @saturation += amount / 100.0
133
+ check_saturation()
108
134
  end
109
135
 
110
136
  def saturate_percent!(percentage)
111
- @s += (1 - @s) * (percentage / 100.0)
137
+ @saturation += (1 - @saturation) * (percentage / 100.0)
138
+ check_saturation()
112
139
  end
113
140
 
114
141
  def desaturate!(amount)
115
- @s -= amount / 100.0
142
+ @saturation -= amount / 100.0
143
+ check_saturation()
116
144
  end
117
145
 
118
146
  def desaturate_percent!(percentage)
119
- @s *= (1.0 - (percentage / 100.0))
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
- # define non-bang methods
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
- #hue as a percentage
132
- def hp
133
- h / 360.0
175
+ def hue_percentage
176
+ hue / 360.0
134
177
  end
135
178
 
136
- # helper for making rgb
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
@@ -0,0 +1,3 @@
1
+ module RGB
2
+ VERSION = '0.1.2'
3
+ end
data/lib/rgb.rb CHANGED
@@ -1 +1,5 @@
1
- require "rgb/color"
1
+ module RGB
2
+ end
3
+
4
+ require 'rgb/color'
5
+ require 'rgb/version'
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.0
5
- prerelease:
4
+ version: 0.1.2
6
5
  platform: ruby
7
6
  authors:
8
- - Dmitry Plashchynski
9
- autorequire:
7
+ - Dzmitry Plashchynski
8
+ autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-02-12 00:00:00.000000000 Z
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: '0'
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: '0'
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: '0'
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: '0'
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: plashchynski@gmail.com
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
- post_install_message:
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: '0'
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
- rubyforge_project:
76
- rubygems_version: 1.8.25
77
- signing_key:
78
- specification_version: 3
79
- summary: A library built to handle the easy conversion, comparison and manipulation
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.