Class: Rubyvis::Color::Hsl

Inherits:
Rubyvis::Color show all
Defined in:
lib/rubyvis/color/color.rb

Overview

Represents a color in HSL space.

Instance Attribute Summary collapse

Attributes inherited from Rubyvis::Color

#color, #opacity

Instance Method Summary collapse

Methods inherited from Rubyvis::Color

#brighter, #darker, names, transparent

Constructor Details

#initialize(h, s, l, a) ⇒ Hsl

Returns a new instance of Hsl.



381
382
383
384
385
386
387
388
# File 'lib/rubyvis/color/color.rb', line 381

def initialize(h,s,l,a)
  c="hsl(#{h},#{s * 100}%,#{l * 100}%)"
  super(c,a)
  @h=h
  @s=s
  @l=l
  @a=a
end

Instance Attribute Details

#aObject

The opacity, a float in [0, 1].



379
380
381
# File 'lib/rubyvis/color/color.rb', line 379

def a
  @a
end

#hObject

The hue, an integer in [0, 360].



373
374
375
# File 'lib/rubyvis/color/color.rb', line 373

def h
  @h
end

#lObject

The lightness, a float in [0, 1].



377
378
379
# File 'lib/rubyvis/color/color.rb', line 377

def l
  @l
end

#sObject

The saturation, a float in [0, 1].



375
376
377
# File 'lib/rubyvis/color/color.rb', line 375

def s
  @s
end

Instance Method Details

#rgbObject

Returns the RGB color equivalent to this HSL color.



391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# File 'lib/rubyvis/color/color.rb', line 391

def rgb
  h = self.h
  s = self.s
  l = self.l
  
  # Some simple corrections for h, s and l. */
  h = h % 360
  h += 360 if (h < 0)
  s = [0, [s, 1].min].max
  l = [0, [l, 1].min].max
  
  # From FvD 13.37, CSS Color Module Level 3 
  m2 = (l <= 0.5) ? (l * (1 + s)) : (l + s - l * s)
  m1 = 2 * l - m2
  v=lambda {|h1|
    if (h1 > 360)
      h1 -= 360;
    elsif (h1 < 0)
       h1 += 360
    end
    
    
    return m1 + (m2 - m1) * h1 / 60 if (h1 < 60)
    return m2 if (h1 < 180) 
    return m1 + (m2 - m1) * (240 - h1) / 60 if (h1 < 240)
    return m1
  }
  vv=lambda {|h1|
    (v(h1) * 255).round
  }
  
  Rubyvis.rgb(vv.call(h + 120), vv.call(h), vv.call(h - 120), a)
end