Class: Led

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

Overview

Led type with full RGB hexadecimals output The Led class accepts any value and transform it to an hexadecimals RGB output.

Author:

Instance Method Summary collapse

Constructor Details

#initializeLed

Returns a new instance of Led.



5
6
7
8
# File 'lib/led.rb', line 5

def initialize
    @color = "000000"
    @zero = "00"
end

Instance Method Details

#get_colorNumber

Returns RGB value in hexadecimals.

Returns:

  • (Number)

    RGB value in hexadecimals



34
35
36
# File 'lib/led.rb', line 34

def get_color
    @color
end

#set_color(value) ⇒ Object

Set color in RGB hexadecimals

Parameters:

  • value (Number)

    color in decimals



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/led.rb', line 12

def set_color(value)
    if value < 0
        calc = value.abs.to_s(16).rjust(2,'0')
        calc2 = (255-value.abs).abs.to_s(16).rjust(2,'0')
        @color = @zero+calc2+calc

    elsif value > 0
        calc = value.abs.to_s(16).rjust(2,'0')
        calc2 = (255-value.abs).abs.to_s(16).rjust(2,'0')
        @color = calc + calc2 + @zero

    else
        @color = "00FF00"
    end
end

#stringString

Return color in RGB hexadecimals in string format

Returns:

  • (String)

    RGB value in hexadecimals



29
30
31
# File 'lib/led.rb', line 29

def string
    "The LED is #{@color}"
end