husler 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/husler.rb +34 -32
  2. data/lib/husler/version.rb +1 -1
  3. metadata +1 -1
@@ -10,13 +10,19 @@ module Husler
10
10
  # Pass in HUSL values and get back RGB values, H ranges from 0 to 360, S and L from 0 to 100.
11
11
  # RGB values will range from 0 to 1.
12
12
  def husl_to_rgb(h, s, l)
13
+ raise ArgumentError.new("h value (#{h}) must be in the range 0..360") unless (0..360).include? h
14
+ raise ArgumentError.new("s value (#{s}) must be in the range 0..100") unless (0..100).include? s
15
+ raise ArgumentError.new("l value (#{l}) must be in the range 0..100") unless (0..100).include? l
16
+
13
17
  xyz_rgb(luv_xyz(lch_luv(husl_lch([h, s, l]))))
14
18
  end
15
19
 
16
20
  # Pass in RGB values ranging from 0 to 1 and get back HUSL values.
17
21
  # H ranges from 0 to 360, S and L from 0 to 100.
18
22
  def rgb_to_husl(r, g, b)
19
- lch_husl(luv_lch(xyz_luv(rgb_xyz([r, g, b]))))
23
+ rgb = [r, g, b]
24
+ raise ArgumentError.new("rgb values (#{r}, #{g}, #{b}) must all be in the range 0..1") if rgb.any? { |v| !(0..1).include? v }
25
+ lch_husl(luv_lch(xyz_luv(rgb_xyz(rgb))))
20
26
  end
21
27
 
22
28
  private
@@ -31,10 +37,10 @@ module Husler
31
37
  result = Float::INFINITY
32
38
 
33
39
  M.each do |m1, m2, m3|
34
- top = ((0.99915 * m1 + 1.05122 * m2 + 1.14460 * m3) * sub2);
35
- rbottom = (0.86330 * m3 - 0.17266 * m2);
36
- lbottom = (0.12949 * m3 - 0.38848 * m1);
37
- bottom = (rbottom * sin_h + lbottom * cos_h) * sub2;
40
+ top = ((0.99915 * m1 + 1.05122 * m2 + 1.14460 * m3) * sub2);
41
+ rbottom = (0.86330 * m3 - 0.17266 * m2);
42
+ lbottom = (0.12949 * m3 - 0.38848 * m1);
43
+ bottom = (rbottom * sin_h + lbottom * cos_h) * sub2;
38
44
 
39
45
  LIMITS.each do |t|
40
46
  c = (l * (top - 1.05122 * t) / (bottom + 0.17266 * sin_h * t));
@@ -46,11 +52,11 @@ module Husler
46
52
  end
47
53
 
48
54
  def dot_product l1, l2
49
- sum = 0
50
- for i in 0...l1.size
51
- sum += l1[i] * l2[i]
52
- end
53
- sum
55
+ sum = 0
56
+ for i in 0...l1.size
57
+ sum += l1[i] * l2[i]
58
+ end
59
+ sum
54
60
  end
55
61
 
56
62
  def f(t)
@@ -63,7 +69,7 @@ module Husler
63
69
  end
64
70
 
65
71
  def from_linear(c)
66
- c <= 0.0031308 ? (12.92 * c) : (1.055 * (c ** (1 / 2.4)) - 0.055)
72
+ c <= 0.0031308 ? (12.92 * c) : (1.055 * (c ** (1 / 2.4)) - 0.055)
67
73
  end
68
74
 
69
75
  def to_linear(c)
@@ -83,9 +89,10 @@ module Husler
83
89
  end
84
90
 
85
91
  def xyz_rgb(tuple)
86
- tuple.map! do |v|
87
- from_linear(dot_product(v, tuple))
92
+ (0...3).each do |i|
93
+ tuple[i] = from_linear(dot_product(M[i], tuple))
88
94
  end
95
+ tuple
89
96
  end
90
97
 
91
98
  def rgb_xyz(tuple)
@@ -100,16 +107,14 @@ module Husler
100
107
  def xyz_luv(tuple)
101
108
  x, y, z = *tuple
102
109
 
103
- var_u = (4 * x) / (x + (15.0 * y) + (3 * z));
104
- var_v = (9 * y) / (x + (15.0 * y) + (3 * z));
110
+ var_u = (4 * x) / (x + (15.0 * y) + (3 * z));
111
+ var_v = (9 * y) / (x + (15.0 * y) + (3 * z));
105
112
 
106
- l = 116 * f(y / REF_Y) - 16;
107
- u = 13 * l * (var_u - REF_U);
108
- v = 13 * l * (var_v - REF_V);
113
+ l = 116 * f(y / REF_Y) - 16;
114
+ u = 13 * l * (var_u - REF_U);
115
+ v = 13 * l * (var_v - REF_V);
109
116
 
110
- tuple[0] = l
111
- tuple[1] = u
112
- tuple[2] = v
117
+ tuple[0], tuple[1], tuple[2] = l, u, v
113
118
 
114
119
  tuple
115
120
  end
@@ -119,17 +124,15 @@ module Husler
119
124
 
120
125
  return tuple.map! { 0.0 } if l == 0
121
126
 
122
- var_y = f_inv((l + 16) / 116.0)
123
- var_u = u / (13.0 * l) + REF_U
124
- var_v = v / (13.0 * l) + REF_V
127
+ var_y = f_inv((l + 16) / 116.0)
128
+ var_u = u / (13.0 * l) + REF_U
129
+ var_v = v / (13.0 * l) + REF_V
125
130
 
126
- y = var_y * REF_Y
127
- x = 0 - (9 * y * var_u) / ((var_u - 4.0) * var_v - var_u * var_v)
128
- z = (9 * y - (15 * var_v * y) - (var_v * x)) / (3.0 * var_v)
131
+ y = var_y * REF_Y
132
+ x = 0 - (9 * y * var_u) / ((var_u - 4.0) * var_v - var_u * var_v)
133
+ z = (9 * y - (15 * var_v * y) - (var_v * x)) / (3.0 * var_v)
129
134
 
130
- tuple[0] = x
131
- tuple[1] = y
132
- tuple[2] = z
135
+ tuple[0], tuple[1], tuple[2] = x, y, z
133
136
 
134
137
  tuple
135
138
  end
@@ -142,8 +145,7 @@ module Husler
142
145
  h = h_rad * 360.0 / 2.0 / PI
143
146
  h = h + 360 if h < 0
144
147
 
145
- tuple[1] = c
146
- tuple[2] = h
148
+ tuple[1], tuple[2] = c, h
147
149
 
148
150
  tuple
149
151
  end
@@ -1,3 +1,3 @@
1
1
  module Husler
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: husler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: