husl 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 802b38dbbe059a518dd54868775ecb5947c307e9
4
+ data.tar.gz: bb97a320ddc75100acc0f8d3f0a67fb6101e37f9
5
+ SHA512:
6
+ metadata.gz: b1cb618f1f8ad0e631078919245b3e842e32234fd33f5c3daf8a2c64e716db28a5e3cdc1accf3254f54de2a949230bcbe0b5f60601fa849d7be0e67fa620ba76
7
+ data.tar.gz: 47054b907542db4ee93704f76375746b53c4c20edbfa3fd39b5e3c90da258f657dd74ed4bcdd17038234bb4ee8bbbeed2f738df2307ef264df43b1c1d1bfc9c5
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1 @@
1
+ 2.2.1
@@ -0,0 +1,13 @@
1
+ language: ruby
2
+ cache: bundler
3
+ rvm:
4
+ - 2.2.1
5
+
6
+ script: 'bundle exec rspec'
7
+
8
+ notifications:
9
+ email:
10
+ recipients:
11
+ - croitoruradubogdan@gmail.com
12
+ on_failure: change
13
+ on_success: never
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in husl.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Radu-Bogdan Croitoru
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,43 @@
1
+ # Husl
2
+
3
+ A Ruby implementation of [HUSL](http://www.husl-colors.org).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'husl'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install husl
20
+
21
+ ## Usage
22
+
23
+ **husl_to_hex(hue, saturation, lightness)**
24
+
25
+ `hue` is a float between 0 and 360, `saturation` and `lightness` are floats between 0 and 100. This function returns the resulting color as a hex string.
26
+
27
+ **husl_to_rgb(hue, saturation, lightness)**
28
+
29
+ Like above, but returns a list of 3 floats between 0 and 1, for each RGB channel.
30
+
31
+ **hex_to_husl(hex)**
32
+
33
+ Takes a hex string and returns the HUSL color as a list of floats as defined above.
34
+
35
+ **rgb_to_husl(red, green, blue)**
36
+
37
+ Like above, but `red`, `green` and `blue` are passed as floats between 0 and 1.
38
+
39
+ For HUSLp (the pastel variant), use `huslp_to_hex`, `huslp_to_rgb`, `hex_to_huslp` and `rgb_to_huslp`.
40
+
41
+ ## Testing
42
+
43
+ Run `rspec spec/`.
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "husl"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ require "pry"
11
+ Pry.start
12
+
13
+ # require "irb"
14
+ # IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'husl/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "husl"
8
+ spec.version = Husl::VERSION
9
+ spec.authors = ["Radu-Bogdan Croitoru"]
10
+ spec.email = ["croitoruradubogdan@gmail.com"]
11
+
12
+ spec.summary = %q{Human friendly alternative to HSL.}
13
+ spec.description = %q{HUSL is implemented as a set of functions to convert colors between RGB, HUSL and HUSLp. }
14
+ spec.homepage = "https://github.com/radubogdan/husl"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.8"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "pry"
25
+ spec.add_development_dependency "rspec"
26
+ end
@@ -0,0 +1,276 @@
1
+ require "husl/version"
2
+
3
+ module Husl
4
+ extend self
5
+
6
+ M = [
7
+ [3.240969941904521, -1.537383177570093, -0.498610760293],
8
+ [-0.96924363628087, 1.87596750150772, 0.041555057407175],
9
+ [0.055630079696993, -0.20397695888897, 1.056971514242878],
10
+ ]
11
+
12
+ M_INV = [
13
+ [0.41239079926595, 0.35758433938387, 0.18048078840183],
14
+ [0.21263900587151, 0.71516867876775, 0.072192315360733],
15
+ [0.019330818715591, 0.11919477979462, 0.95053215224966],
16
+ ]
17
+
18
+ REF_X = 0.95045592705167
19
+ REF_Y = 1.0
20
+ REF_Z = 1.089057750759878
21
+ REF_U = 0.19783000664283
22
+ REF_V = 0.46831999493879
23
+ KAPPA = 903.2962962
24
+ EPSILON = 0.0088564516
25
+
26
+ ###
27
+
28
+ def husl_to_hex h, s, l
29
+ rgb_to_hex(*husl_to_rgb(h, s, l))
30
+ end
31
+
32
+ def huslp_to_hex h, s, l
33
+ rgb_to_hex(*huslp_to_rgb(h, s, l))
34
+ end
35
+
36
+ def hex_to_husl hex
37
+ rgb_to_husl(*hex_to_rgb(hex))
38
+ end
39
+
40
+ def hex_to_huslp hex
41
+ rgb_to_huslp(*hex_to_rgb(hex))
42
+ end
43
+
44
+ def husl_to_rgb h, s, l
45
+ xyz_to_rgb(luv_to_xyz(lch_to_luv(husl_to_lch([h, s, l]))))
46
+ end
47
+
48
+ def rgb_to_husl r, g, b
49
+ lch_to_husl(rgb_to_lch(r, g, b))
50
+ end
51
+
52
+ def huslp_to_rgb h, s, l
53
+ lch_to_rgb(*huslp_to_lch([h, s, l]))
54
+ end
55
+
56
+ def rgb_to_huslp r, g, b
57
+ lch_to_huslp(rgb_to_lch(r, g, b))
58
+ end
59
+
60
+ def lch_to_rgb l, c, h
61
+ xyz_to_rgb(luv_to_xyz(lch_to_luv([l, c, h])))
62
+ end
63
+
64
+ def rgb_to_lch r, g, b
65
+ luv_to_lch(xyz_to_luv(rgb_to_xyz([r, g, b])))
66
+ end
67
+
68
+ def rgb_to_hex r, g, b
69
+ "#%02x%02x%02x" % rgb_prepare([r, g, b])
70
+ end
71
+
72
+ def hex_to_rgb hex
73
+ hex = hex.tr("#", "")
74
+ [].tap { |arr| hex.split('').each_slice(2) { |block| arr << block.join.to_i(16) / 255.0 } }
75
+ end
76
+
77
+ ###
78
+
79
+ def rgb_to_xyz arr
80
+ rgbl = arr.map { |val| to_linear(val) }
81
+ M_INV.map { |i| dot_product(i, rgbl) }
82
+ end
83
+
84
+ def xyz_to_luv arr
85
+ x, y, z = arr
86
+ l = f(y)
87
+
88
+ return [0.0, 0.0, 0.0] if [x, y, z, 0.0].uniq.length == 1 || l == 0.0
89
+
90
+ var_u = (4.0 * x) / (x + (15.0 * y) + (3.0 * z))
91
+ var_v = (9.0 * y) / (x + (15.0 * y) + (3.0 * z))
92
+ u = 13.0 * l * (var_u - REF_U)
93
+ v = 13.0 * l * (var_v - REF_V)
94
+
95
+ [l, u, v]
96
+ end
97
+
98
+ def luv_to_lch arr
99
+ l, u, v = arr
100
+ c = ((u ** 2) + (v ** 2)) ** (1 / 2.0)
101
+ hrad = Math.atan2(v, u)
102
+ h = radians_to_degrees(hrad)
103
+ h += 360.0 if h < 0.0
104
+ [l, c, h]
105
+ end
106
+
107
+ def lch_to_husl arr
108
+ l, c, h = arr
109
+ return [h, 0.0, 100.0] if l > 99.9999999
110
+ return [h, 0.0, 0.0] if l < 0.00000001
111
+
112
+ mx = max_chroma_for(l, h)
113
+ s = c / mx * 100.0
114
+
115
+ [h, s, l]
116
+ end
117
+
118
+ def lch_to_huslp arr
119
+ l, c, h = arr
120
+
121
+ return [h, 0.0, 100.0] if l > 99.9999999
122
+ return [h, 0.0, 0.0] if l < 0.00000001
123
+
124
+ mx = max_safe_chroma_for(l)
125
+ s = c / mx * 100.0
126
+
127
+ [h, s, l]
128
+ end
129
+
130
+ ###
131
+
132
+ def xyz_to_rgb arr
133
+ xyz = M.map { |i| dot_product(i, arr) }
134
+ xyz.map { |i| from_linear(i) }
135
+ end
136
+
137
+ def luv_to_xyz arr
138
+ l, u, v = arr
139
+
140
+ return [0.0, 0.0, 0.0] if l == 0
141
+
142
+ var_y = f_inv(l)
143
+ var_u = u / (13.0 * l) + REF_U
144
+ var_v = v / (13.0 * l) + REF_V
145
+
146
+
147
+ y = var_y * REF_Y
148
+ x = 0.0 - (9.0 * y * var_u) / ((var_u - 4.0) * var_v - var_u * var_v)
149
+ z = (9.0 * y - (15.0 * var_v * y) - (var_v * x)) / (3.0 * var_v)
150
+
151
+ [x, y, z]
152
+ end
153
+
154
+ def lch_to_luv arr
155
+ l, c, h = arr
156
+
157
+ hrad = degrees_to_radians(h)
158
+ u = Math.cos(hrad) * c
159
+ v = Math.sin(hrad) * c
160
+
161
+ [l, u, v]
162
+ end
163
+
164
+ def husl_to_lch arr
165
+ h, s, l = arr
166
+
167
+ return [100, 0.0, h] if l > 99.9999999
168
+ return [0.0, 0.0, h] if l < 0.00000001
169
+
170
+ mx = max_chroma_for(l, h)
171
+ c = mx / 100.0 * s
172
+
173
+ [l, c, h]
174
+ end
175
+
176
+ def huslp_to_lch arr
177
+ h, s, l = arr
178
+
179
+ return [100, 0.0, h] if l > 99.9999999
180
+ return [0.0, 0.0, h] if l < 0.00000001
181
+
182
+ mx = max_safe_chroma_for(l)
183
+ c = mx / 100.0 * s
184
+
185
+ [l, c, h]
186
+ end
187
+
188
+ ###
189
+
190
+ def radians_to_degrees rad
191
+ rad * 180.0 / Math::PI
192
+ end
193
+
194
+ def degrees_to_radians degrees
195
+ degrees * Math::PI / 180.0
196
+ end
197
+
198
+ def max_chroma_for l, h
199
+ hrad = h / 360.0 * Math::PI * 2.0
200
+ lengths = []
201
+
202
+ get_bounds(l).each do |line|
203
+ l = length_of_ray_until_intersect(hrad, line)
204
+ lengths << l if l
205
+ end
206
+
207
+ lengths.min
208
+ end
209
+
210
+ def max_safe_chroma_for l
211
+ lengths = []
212
+
213
+ get_bounds(l).each do |m1, b1|
214
+ x = intersect_line_line([m1, b1], [-1.0 / m1, 0.0])
215
+ lengths << distance_from_pole([x, b1 + x * m1])
216
+ end
217
+
218
+ lengths.min
219
+ end
220
+
221
+ def get_bounds l
222
+ sub1 = ((l + 16.0) ** 3.0) / 1560896.0
223
+ sub2 = sub1 > EPSILON ? sub1 : l / KAPPA
224
+ ret = []
225
+
226
+ M.each do |m1, m2, m3|
227
+ [0, 1].each do |t|
228
+ top1 = (284517.0 * m1 - 94839.0 * m3) * sub2
229
+ top2 = (838422.0 * m3 + 769860.0 * m2 + 731718.0 * m1) * l * sub2 - 769860.0 * t * l
230
+ bottom = (632260.0 * m3 - 126452.0 * m2) * sub2 + 126452.0 * t
231
+ ret << [top1 / bottom, top2 / bottom]
232
+ end
233
+ end
234
+
235
+ ret
236
+ end
237
+
238
+ def length_of_ray_until_intersect theta, line
239
+ m1, b1 = line
240
+ length = b1 / (Math.sin(theta) - m1 * Math.cos(theta))
241
+ return nil if length < 0
242
+ length
243
+ end
244
+
245
+ def intersect_line_line line1, line2
246
+ (line1[1] - line2[1]) / (line2[0] - line1[0])
247
+ end
248
+
249
+ def distance_from_pole point
250
+ Math.sqrt(point[0] ** 2 + point[1] ** 2)
251
+ end
252
+
253
+ def f t
254
+ t > EPSILON ? 116 * ((t / REF_Y) ** (1.0 / 3.0)) - 16.0 : t / REF_Y * KAPPA
255
+ end
256
+
257
+ def f_inv t
258
+ t > 8 ? REF_Y * ((t + 16.0) / 116.0) ** 3.0 : REF_Y * t / KAPPA
259
+ end
260
+
261
+ def to_linear c
262
+ c > 0.04045 ? ((c + 0.055) / 1.055) ** 2.4 : c / 12.92
263
+ end
264
+
265
+ def from_linear c
266
+ c <= 0.0031308 ? 12.92 * c : (1.055 * (c ** (1.0 / 2.4)) - 0.055)
267
+ end
268
+
269
+ def dot_product a, b
270
+ a.zip(b).map { |i, j| i * j }.inject(:+)
271
+ end
272
+
273
+ def rgb_prepare arr
274
+ arr.map! { |ch| ch = ch.round(3); ch = [0, ch].max; ch = [1, ch].min; (ch * 255).round }
275
+ end
276
+ end
@@ -0,0 +1,3 @@
1
+ module Husl
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: husl
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Radu-Bogdan Croitoru
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-07-09 00:00:00.000000000 Z
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: '1.8'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: 'HUSL is implemented as a set of functions to convert colors between
70
+ RGB, HUSL and HUSLp. '
71
+ email:
72
+ - croitoruradubogdan@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".ruby-version"
80
+ - ".travis.yml"
81
+ - Gemfile
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - bin/console
86
+ - bin/setup
87
+ - husl.gemspec
88
+ - lib/husl.rb
89
+ - lib/husl/version.rb
90
+ homepage: https://github.com/radubogdan/husl
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.4.5
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: Human friendly alternative to HSL.
114
+ test_files: []