Component : Open Light

logo

Description

This is the component provided by the open light block.

Component name: openlight.

Methods

Example

Here is a hsv script

            
light = require("component").openlight

function rgb_to_hex(r, g, b)
    -- Ensure values are within 0 to 255 range
    r = math.max(0, math.min(255, r))
    g = math.max(0, math.min(255, g))
    b = math.max(0, math.min(255, b))
    
    return r * 65536 + g * 256 + b
end
function deg_to_hsv(d)
    if d < 0 then d = d+360 end    
    if d > 360 then d = d-360 end
    
    if d > 0 and d < 60 then
        return d*4.25
    elseif d < 180 then
        return 255
    elseif d < 240 then
        return (240-d)*255
    else 
        return 0
    end
end
light.setBrightness(15)

while true do 
    for n = 0,360,1 do
        local r = deg_to_hsv(n+120)
        local g = deg_to_hsv(n)
        local b = deg_to_hsv(n-120)
        light.setColor(rgb_to_hex(r,g,b))
        require("os").sleep(0.1)
    end
end