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.



376
377
378
379
380
381
382
383
# File 'lib/rubyvis/color/color.rb', line 376

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].



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

def a
  @a
end

#hObject

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



368
369
370
# File 'lib/rubyvis/color/color.rb', line 368

def h
  @h
end

#lObject

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



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

def l
  @l
end

#sObject

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



370
371
372
# File 'lib/rubyvis/color/color.rb', line 370

def s
  @s
end

Instance Method Details

#==(v) ⇒ Object



384
385
386
# File 'lib/rubyvis/color/color.rb', line 384

def ==(v)
  self.class==v.class and @h==v.h and @s==v.s and @l==v.l and @a==v.a
end

#rgbObject

Returns the RGB color equivalent to this HSL color.



389
390
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
# File 'lib/rubyvis/color/color.rb', line 389

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.0 if (h1 < 60.0)
    return m2 if (h1 < 180.0) 
    return m1 + (m2 - m1) * (240.0 - h1) / 60.0 if (h1 < 240.0)
    return m1
  }
  
  vv=lambda {|h1| (v.call(h1) * 255).round}
  Rubyvis.rgb(vv.call(h + 120), vv.call(h), vv.call(h - 120), a)
 
end