Method: Graphics::AbstractSimulation#from_hsl

Defined in:
lib/graphics/simulation.rb

#from_hsl(h, s, l) ⇒ Object

Raises:

  • (ArgumentError)


263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/graphics/simulation.rb', line 263

def from_hsl h, s, l # 0..360, 0..1, 0..1
  raise ArgumentError, "%f, %f, %f out of range" % [h, s, v] unless
    h.between?(0, 360) && s.between?(0, 1) && l.between?(0, 1)

  c  = (1 - (2*l - 1).abs) * s
  h2 = h / 60.0
  x  = c * (1 - (h2 % 2 - 1).abs)
  m  = l - c/2

  r, g, b = case
            when 0 <= h2 && h2 < 1 then [c+m, x+m, 0+m]
            when 1 <= h2 && h2 < 2 then [x+m, c+m, 0+m]
            when 2 <= h2 && h2 < 3 then [0+m, c+m, x+m]
            when 3 <= h2 && h2 < 4 then [0+m, x+m, c+m]
            when 4 <= h2 && h2 < 5 then [x+m, 0+m, c+m]
            when 5 <= h2 && h2 < 6 then [c+m, 0+m, x+m]
            else
              raise [h, s, v, h2, x, m].inspect
            end

  [(r*255).round, (g*255).round, (b*255).round]
end