Class: Rubyvis::Color::Hsl

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

Overview

Represents a color in HSL space.

Constant Summary

Constants inherited from Rubyvis::Color

NAMES

Instance Attribute Summary collapse

Attributes inherited from Rubyvis::Color

#color, #opacity

Instance Method Summary collapse

Methods inherited from Rubyvis::Color

#brighter, #darker, transparent

Constructor Details

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

Returns a new instance of Hsl.



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

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



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

def a
  @a
end

#hObject

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



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

def h
  @h
end

#lObject

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



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

def l
  @l
end

#sObject

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



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

def s
  @s
end

Instance Method Details

#==(v) ⇒ Object



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

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.



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

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