Class: ColorCode::HSL

Inherits:
Object
  • Object
show all
Defined in:
lib/color_code/hsl.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code = nil, h: 0, s: 0, l: 0) ⇒ HSL

Returns a new instance of HSL.



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/color_code/hsl.rb', line 5

def initialize(code=nil, h: 0, s: 0, l: 0)
  if code
    hsl = ColorCode::RGB.new(code).to_hsl
    h, s, l = hsl.to_a
  end

  @h = h
  @s = s
  @l = l
  raise if @h > 360 || @s > 100 || @l > 100
rescue
  raise ArgumentError.new('invalid value')
end

Instance Attribute Details

#hObject

Returns the value of attribute h.



3
4
5
# File 'lib/color_code/hsl.rb', line 3

def h
  @h
end

#lObject

Returns the value of attribute l.



3
4
5
# File 'lib/color_code/hsl.rb', line 3

def l
  @l
end

#sObject

Returns the value of attribute s.



3
4
5
# File 'lib/color_code/hsl.rb', line 3

def s
  @s
end

Instance Method Details

#to_aObject



24
25
26
# File 'lib/color_code/hsl.rb', line 24

def to_a
  [@h, @s, @l]
end

#to_hashObject



28
29
30
# File 'lib/color_code/hsl.rb', line 28

def to_hash
  { h: @h, s: @s, l: @l }
end

#to_rgbObject



32
33
34
35
# File 'lib/color_code/hsl.rb', line 32

def to_rgb
  r, g, b = convert_rgb
  ColorCode::RGB.new(r: r, g: g, b: b)
end

#to_sObject



19
20
21
22
# File 'lib/color_code/hsl.rb', line 19

def to_s
  rgb = convert_rgb.map { |hue| '%02x' % hue }.join
  "##{rgb}"
end