Module: Husl

Extended by:
Husl
Included in:
Husl
Defined in:
lib/husl.rb,
lib/husl/version.rb

Constant Summary collapse

M =
[
  [3.240969941904521, -1.537383177570093, -0.498610760293],
  [-0.96924363628087, 1.87596750150772, 0.041555057407175],
  [0.055630079696993, -0.20397695888897, 1.056971514242878],
]
M_INV =
[
  [0.41239079926595, 0.35758433938387, 0.18048078840183],
  [0.21263900587151, 0.71516867876775, 0.072192315360733],
  [0.019330818715591, 0.11919477979462, 0.95053215224966],
]
REF_X =
0.95045592705167
REF_Y =
1.0
REF_Z =
1.089057750759878
REF_U =
0.19783000664283
REF_V =
0.46831999493879
KAPPA =
903.2962962
EPSILON =
0.0088564516
VERSION =
"1.0.1"

Instance Method Summary collapse

Instance Method Details

#degrees_to_radians(degrees) ⇒ Object



196
197
198
# File 'lib/husl.rb', line 196

def degrees_to_radians degrees
  degrees * Math::PI / 180.0
end

#distance_from_pole(point) ⇒ Object



251
252
253
# File 'lib/husl.rb', line 251

def distance_from_pole point
  Math.sqrt(point[0] ** 2 + point[1] ** 2)
end

#dot_product(a, b) ⇒ Object



271
272
273
# File 'lib/husl.rb', line 271

def dot_product a, b
  a.zip(b).map { |i, j| i * j }.inject(:+)
end

#f(t) ⇒ Object



255
256
257
# File 'lib/husl.rb', line 255

def f t
  t > EPSILON ? 116 * ((t / REF_Y) ** (1.0 / 3.0)) - 16.0 : t / REF_Y * KAPPA
end

#f_inv(t) ⇒ Object



259
260
261
# File 'lib/husl.rb', line 259

def f_inv t
  t > 8 ? REF_Y * ((t + 16.0) / 116.0) ** 3.0 : REF_Y * t / KAPPA
end

#from_linear(c) ⇒ Object



267
268
269
# File 'lib/husl.rb', line 267

def from_linear c
  c <= 0.0031308 ? 12.92 * c : (1.055 * (c ** (1.0 / 2.4)) - 0.055)
end

#get_bounds(l) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/husl.rb', line 223

def get_bounds l
  sub1 = ((l + 16.0) ** 3.0) / 1560896.0
  sub2 = sub1 > EPSILON ? sub1 : l / KAPPA
  ret = []

  M.each do |m1, m2, m3|
    [0, 1].each do |t|
      top1 = (284517.0 * m1 - 94839.0 * m3) * sub2
      top2 = (838422.0 * m3 + 769860.0 * m2 + 731718.0 * m1) * l * sub2 - 769860.0 * t * l
      bottom = (632260.0 * m3 - 126452.0 * m2) * sub2 + 126452.0 * t
      ret << [top1 / bottom, top2 / bottom]
    end
  end

  ret
end

#hex_to_husl(hex) ⇒ Object



38
39
40
# File 'lib/husl.rb', line 38

def hex_to_husl hex
  rgb_to_husl(*hex_to_rgb(hex))
end

#hex_to_huslp(hex) ⇒ Object



42
43
44
# File 'lib/husl.rb', line 42

def hex_to_huslp hex
  rgb_to_huslp(*hex_to_rgb(hex))
end

#hex_to_rgb(hex) ⇒ Object



74
75
76
77
# File 'lib/husl.rb', line 74

def hex_to_rgb hex
  hex = hex.tr("#", "")
  [].tap { |arr| hex.split('').each_slice(2) { |block| arr << block.join.to_i(16) / 255.0 } }
end

#husl_to_hex(h, s, l) ⇒ Object



30
31
32
# File 'lib/husl.rb', line 30

def husl_to_hex h, s, l
  rgb_to_hex(*husl_to_rgb(h, s, l))
end

#husl_to_lch(arr) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
# File 'lib/husl.rb', line 166

def husl_to_lch arr
  h, s, l = arr

  return [100, 0.0, h] if l > 99.9999999
  return [0.0, 0.0, h] if l < 0.00000001

  mx = max_chroma_for(l, h)
  c = mx / 100.0 * s

  [l, c, h]
end

#husl_to_rgb(h, s, l) ⇒ Object



46
47
48
# File 'lib/husl.rb', line 46

def husl_to_rgb h, s, l
  xyz_to_rgb(luv_to_xyz(lch_to_luv(husl_to_lch([h, s, l]))))
end

#huslp_to_hex(h, s, l) ⇒ Object



34
35
36
# File 'lib/husl.rb', line 34

def huslp_to_hex h, s, l
  rgb_to_hex(*huslp_to_rgb(h, s, l))
end

#huslp_to_lch(arr) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
# File 'lib/husl.rb', line 178

def huslp_to_lch arr
  h, s, l = arr

  return [100, 0.0, h] if l > 99.9999999
  return [0.0, 0.0, h] if l < 0.00000001

  mx = max_safe_chroma_for(l)
  c = mx / 100.0 * s

  [l, c, h]
end

#huslp_to_rgb(h, s, l) ⇒ Object



54
55
56
# File 'lib/husl.rb', line 54

def huslp_to_rgb h, s, l
  lch_to_rgb(*huslp_to_lch([h, s, l]))
end

#intersect_line_line(line1, line2) ⇒ Object



247
248
249
# File 'lib/husl.rb', line 247

def intersect_line_line line1, line2
  (line1[1] - line2[1]) / (line2[0] - line1[0])
end

#lch_to_husl(arr) ⇒ Object



109
110
111
112
113
114
115
116
117
118
# File 'lib/husl.rb', line 109

def lch_to_husl arr
  l, c, h = arr
  return [h, 0.0, 100.0] if l > 99.9999999
  return [h, 0.0, 0.0] if l < 0.00000001

  mx = max_chroma_for(l, h)
  s = c / mx * 100.0

  [h, s, l]
end

#lch_to_huslp(arr) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
# File 'lib/husl.rb', line 120

def lch_to_huslp arr
  l, c, h = arr

  return [h, 0.0, 100.0] if l > 99.9999999
  return [h, 0.0, 0.0] if l < 0.00000001

  mx = max_safe_chroma_for(l)
  s = c / mx * 100.0

  [h, s, l]
end

#lch_to_luv(arr) ⇒ Object



156
157
158
159
160
161
162
163
164
# File 'lib/husl.rb', line 156

def lch_to_luv arr
  l, c, h = arr

  hrad = degrees_to_radians(h)
  u = Math.cos(hrad) * c
  v = Math.sin(hrad) * c

  [l, u, v]
end

#lch_to_rgb(l, c, h) ⇒ Object



62
63
64
# File 'lib/husl.rb', line 62

def lch_to_rgb l, c, h
  xyz_to_rgb(luv_to_xyz(lch_to_luv([l, c, h])))
end

#length_of_ray_until_intersect(theta, line) ⇒ Object



240
241
242
243
244
245
# File 'lib/husl.rb', line 240

def length_of_ray_until_intersect theta, line
  m1, b1 = line
  length = b1 / (Math.sin(theta) - m1 * Math.cos(theta))
  return nil if length < 0
  length
end

#luv_to_lch(arr) ⇒ Object



100
101
102
103
104
105
106
107
# File 'lib/husl.rb', line 100

def luv_to_lch arr
  l, u, v = arr
  c = ((u ** 2) + (v ** 2)) ** (1 / 2.0)
  hrad = Math.atan2(v, u)
  h = radians_to_degrees(hrad)
  h += 360.0 if h < 0.0
  [l, c, h]
end

#luv_to_xyz(arr) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/husl.rb', line 139

def luv_to_xyz arr
  l, u, v = arr

  return [0.0, 0.0, 0.0] if l == 0

  var_y = f_inv(l)
  var_u = u / (13.0 * l) + REF_U
  var_v = v / (13.0 * l) + REF_V


  y = var_y * REF_Y
  x = 0.0 - (9.0 * y * var_u) / ((var_u - 4.0) * var_v - var_u * var_v)
  z = (9.0 * y - (15.0 * var_v * y) - (var_v * x)) / (3.0 * var_v)

  [x, y, z]
end

#max_chroma_for(l, h) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
# File 'lib/husl.rb', line 200

def max_chroma_for l, h
  hrad = h / 360.0 * Math::PI * 2.0
  lengths = []

  get_bounds(l).each do |line|
    l = length_of_ray_until_intersect(hrad, line)
    lengths << l if l
  end

  lengths.min
end

#max_safe_chroma_for(l) ⇒ Object



212
213
214
215
216
217
218
219
220
221
# File 'lib/husl.rb', line 212

def max_safe_chroma_for l
  lengths = []

  get_bounds(l).each do |m1, b1|
    x = intersect_line_line([m1, b1], [-1.0 / m1, 0.0])
    lengths << distance_from_pole([x, b1 + x * m1])
  end

  lengths.min
end

#radians_to_degrees(rad) ⇒ Object



192
193
194
# File 'lib/husl.rb', line 192

def radians_to_degrees rad
  rad * 180.0 / Math::PI
end

#rgb_prepare(arr) ⇒ Object



275
276
277
# File 'lib/husl.rb', line 275

def rgb_prepare arr
  arr.map! { |ch| ch = ch.round(3); ch = [0, ch].max; ch = [1, ch].min; (ch * 255).round }
end

#rgb_to_hex(r, g, b) ⇒ Object



70
71
72
# File 'lib/husl.rb', line 70

def rgb_to_hex r, g, b
  "#%02x%02x%02x" % rgb_prepare([r, g, b])
end

#rgb_to_husl(r, g, b) ⇒ Object



50
51
52
# File 'lib/husl.rb', line 50

def rgb_to_husl r, g, b
  lch_to_husl(rgb_to_lch(r, g, b))
end

#rgb_to_huslp(r, g, b) ⇒ Object



58
59
60
# File 'lib/husl.rb', line 58

def rgb_to_huslp r, g, b
  lch_to_huslp(rgb_to_lch(r, g, b))
end

#rgb_to_lch(r, g, b) ⇒ Object



66
67
68
# File 'lib/husl.rb', line 66

def rgb_to_lch r, g, b
  luv_to_lch(xyz_to_luv(rgb_to_xyz([r, g, b])))
end

#rgb_to_xyz(arr) ⇒ Object



81
82
83
84
# File 'lib/husl.rb', line 81

def rgb_to_xyz arr
  rgbl = arr.map { |val| to_linear(val) }
  M_INV.map { |i| dot_product(i, rgbl) }
end

#to_linear(c) ⇒ Object



263
264
265
# File 'lib/husl.rb', line 263

def to_linear c
  c > 0.04045 ? ((c + 0.055) / 1.055) ** 2.4 : c / 12.92
end

#xyz_to_luv(arr) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/husl.rb', line 86

def xyz_to_luv arr
  x, y, z = arr
  l = f(y)

  return [0.0, 0.0, 0.0] if [x, y, z, 0.0].uniq.length == 1 || l == 0.0

  var_u = (4.0 * x) / (x + (15.0 * y) + (3.0 * z))
  var_v = (9.0 * y) / (x + (15.0 * y) + (3.0 * z))
  u = 13.0 * l * (var_u - REF_U)
  v = 13.0 * l * (var_v - REF_V)

  [l, u, v]
end

#xyz_to_rgb(arr) ⇒ Object



134
135
136
137
# File 'lib/husl.rb', line 134

def xyz_to_rgb arr
  xyz = M.map { |i| dot_product(i, arr) }
  xyz.map { |i| from_linear(i) }
end